# HG changeset patch # User Thinker K.F. Li # Date 1280380045 -28800 # Node ID 123e7eeff10c19b2fb79963320494469fc16ee03 # Parent 2030e5d9f7f730f430846e678aa525647717c445 Generate JS binding for a function diff -r 2030e5d9f7f7 -r 123e7eeff10c nodejs/observer.m4 --- a/nodejs/observer.m4 Thu Jul 29 01:37:15 2010 +0800 +++ b/nodejs/observer.m4 Thu Jul 29 13:07:25 2010 +0800 @@ -11,3 +11,5 @@ [OBJ([observer], [observer_t])]), METHOD([remove_observer], [subject_remove_observer], (OBJ([observer], [observer_t])), 1, [])]) + +FUNCTION([load], [my_load], [INT(sz), STR(name)], 2, [OBJ([test],[test_t])]) diff -r 2030e5d9f7f7 -r 123e7eeff10c tools/gen_v8_binding.m4 --- a/tools/gen_v8_binding.m4 Thu Jul 29 01:37:15 2010 +0800 +++ b/tools/gen_v8_binding.m4 Thu Jul 29 13:07:25 2010 +0800 @@ -96,13 +96,13 @@ define([START_METHOD_ARG_VAR], [dnl define([INT], [dnl - int $][1];)dnl + int arg_$][1];)dnl define([OBJ], [dnl - $][2 *$][1];)dnl + $][2 *arg_$][1];)dnl define([STR], [dnl - char *$][1];)dnl + char *arg_$][1];)dnl define([FUNC], [ - Handle $][1];)dnl + Handle arg_$][1];)dnl ]) define([START_METHOD_ARG_TYPE_CHK], [dnl @@ -118,20 +118,20 @@ define([START_METHOD_ARG_ASSIGN], [dnl define([INT], [ - $][1 = args[[i++]]->Int32Value();])dnl + arg_$][1 = args[[i++]]->Int32Value();])dnl define([OBJ], [ - $][1 = ($][2 *)UNWRAP(args[[i++]]->ToObject());])dnl + arg_$][1 = ($][2 *)UNWRAP(args[[i++]]->ToObject());])dnl define([STR], [ - $][1 = strdup(*String::Utf8Value(args[[i++]]->ToString()));])dnl + arg_$][1 = strdup(*String::Utf8Value(args[[i++]]->ToString()));])dnl define([FUNC], [ - $][1 = Function::Cast(args[[i++]]);])dnl + arg_$][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([INT], [arg_$][1])dnl +define([OBJ], [arg_$][1])dnl +define([STR], [arg_$][1])dnl +define([FUNC], [arg_$][1])dnl ]) define([START_METHOD_RET_VAL], [dnl @@ -161,7 +161,7 @@ return Integer::New(_ret); ])dnl define([OBJ], [ - return PROJ_PREFIX[]STRUCT_NAME[]_$][1[]_new(_ret); + return PROJ_PREFIX[]$][1[]_new(_ret); ])dnl define([STR], [ return String::New(_ret); @@ -224,7 +224,7 @@ define([STRUCT_TYPE], [$2])dnl [ /* ************************************************** - * STRUCT: $1 + * [STRUCT]: $1 * Generated by gen_v8_binding.m4 */ static Handle @@ -267,4 +267,44 @@ PROJ_PREFIX[$1][_temp = Persistent::New(func_temp); }]dnl ]) + +define([FUNCTION], [dnl +/* ************************************************** + * [FUNCTION]: $1 + * Generated by gen_v8_binding.m4 + */ +static Handle +PROJ_PREFIX[]$1(const Arguments &args) { + int argc = args.Length(); +foreach([ITER], ($3), [START_METHOD_ARG_VAR[]ITER[]STOP_METHOD_ARG])dnl +START_METHOD_RET_VAL[]$5[]STOP_METHOD_ARG[]dnl + + 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])) + +define([SEP], [])dnl + START_METHOD_RET_ASSIGN[]$5[]STOP_METHOD_ARG[]$2(foreach([ITER], ($3), [START_METHOD_ARG_PASS[]SEP[]ITER[]STOP_METHOD_ARG[]define([SEP], [, ])]));[]undefine([SEP]) +START_METHOD_RET[]$][5[]STOP_METHOD_ARG[]dnl +ifelse($][5, [], [ + return Null(); +])dnl +} +static Persistent PROJ_PREFIX[]$1[]_temp; + +static void +PROJ_PREFIX[]$1[]_init(void) { + Handle func_temp; + + func_temp = FunctionTemplate::New(PROJ_PREFIX[]$1); + PROJ_PREFIX[]$1[]_temp = Persistent::New(func_temp); +} +]) + divert([])dnl