changeset 648:e13dbfd37eab

JS binding generator and observer definition with the generator
author Thinker K.F. Li <thinker@branda.to>
date Thu, 29 Jul 2010 01:02:29 +0800
parents c9f8c7e6f57e
children 2030e5d9f7f7
files nodejs/observer.m4 tools/gen_v8_binding.m4
diffstat 2 files changed, 240 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nodejs/observer.m4	Thu Jul 29 01:02:29 2010 +0800
@@ -0,0 +1,12 @@
+STRUCT([event], [event_t],
+       [INT([event]), OBJ([tgt], [subject]),
+        OBJ([cur_tgt], [subject]), INT([flags])], [])
+
+STRUCT([observer], [observer_t],
+       [INT([type])], [])
+
+STRUCT([subject], [subject_t], [],
+       [METHOD([add_event_observer], [subject_add_event_observer],
+       	       (INT([type]), FUNC([handler])), 2),
+        METHOD([remove_observer], [subject_remove_observer],
+	       (OBJ([observer], [observer_t])), 1)])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/gen_v8_binding.m4	Thu Jul 29 01:02:29 2010 +0800
@@ -0,0 +1,228 @@
+changequote(`[', `]')dnl
+include([foreach.m4])dnl
+divert([-1])dnl
+
+define([UNQUOTE], [$*])
+
+define([QUOTE], [[[$*]]])
+
+define([COUNT],[ifelse([$*],[],0,[$#])])
+
+define([IMPORT],[define([$1],[$2$1(]$[]@[)])])
+
+define([PROJ_PREFIX], [xnjsmb_])
+
+define([START_ACCESSOR], [dnl
+divert([-1])dnl
+  define([INT], [
+static Handle<Value>
+]PROJ_PREFIX[]STRUCT_NAME[_get_$][1(Local<String> property, const AccessorInfo &info) {
+    return Integer::New(data->$][1);
+}
+
+static void
+]PROJ_PREFIX[]STRUCT_NAME[_set_$][1(Local<String> property,
+		      Local<Value> value,
+		      const AccessorInfo &info) {
+    Handle<Object> self = info.This();
+    STRUCT_TYPE *data;
+
+    data = (STRUCT_TYPE *)UNWRAP(self);
+    data->$][1 = value->Int32Value();
+}
+])
+  define([OBJ], [
+static Handle<Value>
+]PROJ_PREFIX[]STRUCT_NAME[_get_$][1(Local<String> property, const AccessorInfo &info) {
+    return ]PROJ_PREFIX[$][2_new(data->$][1);
+}
+
+static void
+]PROJ_PREFIX[]STRUCT_NAME[_set_$][1(Local<String> property,
+		      Local<Value> value,
+		      const AccessorInfo &info) {
+    Handle<Object> self = info.This();
+    Handle<Object> obj;
+    $][2 *v;
+    STRUCT_TYPE *data;
+
+    data = UNWRAP(self);
+    obj = value->ToObject();
+    v = ($][2 *)UNWRAP(obj);
+    data->$][1 = v;
+}
+])
+  define([STR], [
+static Handle<Value>
+]PROJ_PREFIX[]STRUCT_NAME[_get_$][1(Local<String> property, const AccessorInfo &info) {
+    return String::New(data->$][1);
+}
+
+static void
+]PROJ_PREFIX[]STRUCT_NAME[_set_$][1(Local<String> property,
+		      Local<Value> value,
+		      const AccessorInfo &info) {
+    Handle<Object> self = info.This();
+    STRUCT_TYPE *data;
+
+    data = (STRUCT_TYPE *)UNWRAP(self);
+    String::Utf8Value utf8(value->ToString());
+    free(data->$][1);
+    data->$][1 = strdup(*utf8);
+}
+])
+divert([])dnl
+])
+
+define([STOP_ACCESSOR], [dnl
+divert([-1])dnl
+undefine([INT])
+undefine([OBJ])
+undefine([STR])
+divert([])dnl
+])
+
+define([SET_ACCESSSOR], [dnl
+define([INT], [$][1])dnl
+define([OBJ], [$][1])dnl
+define([STR], [$][1])dnl
+    inst_temp->SetAccessor(String::New("$1"),
+			   PROJ_PREFIX[]STRUCT_NAME[]_get_[]$1,
+			   PROJ_PREFIX[]STRUCT_NAME[]_set_[]$1);
+undefine([INT])dnl
+undefine([OBJ])dnl
+undefine([STR])dnl
+])
+
+define([START_METHOD_ARG_VAR], [dnl
+define([INT], [dnl
+    int $][1];)dnl
+define([OBJ], [dnl
+    $][2 *$][1];)dnl
+define([STR], [dnl
+    char *$][1];)dnl
+define([FUNC], [
+    Handle<Function> $][1];)dnl
+])
+
+define([START_METHOD_ARG_TYPE_CHK], [dnl
+define([INT], [ ||
+       !args[[i++]]->IsInt32()])dnl
+define([OBJ], [ ||
+       !args[[i++]]->IsObject()])dnl
+define([STR], [ ||
+       !args[[i++]]->IsString()])dnl
+define([FUNC], [ ||
+       !args[[i++]]->IsFunction()])dnl
+])
+
+define([START_METHOD_ARG_ASSIGN], [dnl
+define([INT], [
+    $][1 = args[[i++]]->Int32Value();])dnl
+define([OBJ], [
+    $][1 = ($][2 *)UNWRAP(args[[i++]]->ToObject());])dnl
+define([STR], [
+    $][1 = strdup(*String::Utf8Value(args[[i++]]->ToString()));])dnl
+define([FUNC], [
+    $][1 = Function::Cast(args[[i++]]);])dnl
+])
+
+define([START_METHOD_ARG_PASS], [dnl
+define([INT], [$][1])dnl
+define([OBJ], [$][1])dnl
+define([STR], [$][1])dnl
+define([FUNC], [$][1])dnl
+])
+
+define([STOP_METHOD_ARG], [dnl
+undefine([INT])dnl
+undefine([OBJ])dnl
+undefine([STR])dnl
+undefine([FUNC])dnl
+])
+
+define([START_METHOD], [dnl
+define([METHOD], [
+static Handle<Value>
+PROJ_PREFIX[]STRUCT_NAME[]_$][1(const Arguments &args) {
+    int i;
+    int argc = args.Length();
+    Handle<Object> self = args.This();
+    STRUCT_TYPE *_self = (STRUCT_TYPE *)UNWRAP(self);
+foreach([ITER], $][3, [START_METHOD_ARG_VAR[]ITER[]STOP_METHOD_ARG])
+
+    if(argc != $][4)
+        THROW("Invalid number of arguments (!=$][4)");
+    i = 0;
+    if(0]dnl
+[foreach([ITER], $][3, [START_METHOD_ARG_TYPE_CHK[]ITER[]STOP_METHOD_ARG]))
+        THROW("Invalid argument type");
+
+    i = 0;
+foreach([ITER], $][3, [START_METHOD_ARG_ASSIGN[]ITER[]STOP_METHOD_ARG]))
+
+    $][2(_self[]foreach([ITER], $][3, [START_METHOD_ARG_PASS[], ITER[]STOP_METHOD_ARG]));
+}
+])dnl
+])
+
+define([STOP_METHOD], [undefine([METHOD])])
+
+define([SET_METHOD], [dnl
+define([METHOD], [dnl
+    SET(proto_temp, "$][1",
+        FunctionTemplate::New(PROJ_PREFIX[]STRUCT_NAME[]_$][1));
+])dnl
+$1[]dnl
+undefine([METHOD])dnl
+])
+
+define([STRUCT], [dnl
+define([STRUCT_NAME], [$1])dnl
+define([STRUCT_TYPE], [$2])dnl
+[
+/* **************************************************
+ * STRUCT: $1
+ * Generated by gen_v8_binding.m4
+ */
+static Handle<Value>
+]PROJ_PREFIX[$1(const Arguments &args) {
+}
+
+static Persistent<FunctionTemplate> ]PROJ_PREFIX[$1][_temp;
+
+static Handle<Value>
+]PROJ_PREFIX[$1][_new($2 *data) {
+    Handle<Object> obj;
+    Handle<Function> func;
+
+    func = ]PROJ_PREFIX[$1][_temp->GetFunction();
+    obj = func->NewInstance();
+    WRAP(obj, data);
+
+    return obj;
+}
+]dnl
+foreach([ITER], ($3), [START_ACCESSOR ITER STOP_ACCESSOR])dnl
+foreach([ITER], ($4), [START_METHOD ITER STOP_METHOD])dnl
+[
+static void
+]PROJ_PREFIX[$1][_init(void) {
+    Handle<FunctionTemplate> func_temp;
+    Handle<ObjectTemplate> inst_temp;
+    Handle<ObjectTemplate> proto_temp;
+
+    func_temp = FunctionTemplate::New(]PROJ_PREFIX[$1);
+    func_temp->SetClassName("]STRUCT_NAME[");
+    inst_temp = func_temp->InstanceTemplate();
+    inst_temp->SetInternalFieldCount(1);
+    ]
+foreach([ITER], ($3), [SET_ACCESSSOR(ITER)])dnl
+    inst_temp = func_temp->InstanceTemplate();
+
+foreach([ITER], ($4), [SET_METHOD(ITER)])dnl
+
+    PROJ_PREFIX[$1][_temp = Persistent<FunctionTemplate>::New(func_temp);
+}]dnl
+])
+divert([])dnl