axmol/tools/bindings-generator/targets/spidermonkey/templates/constructor.c

55 lines
2.0 KiB
C

## ===== constructor function implementation template
bool ${signature_name}(JSContext *cx, uint32_t argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
bool ok = true;
#if len($arguments) >= $min_args
#set arg_count = len($arguments)
#set arg_idx = $min_args
#set $count = 0
#while $count < $arg_idx
#set $arg = $arguments[$count]
#if $arg.is_numeric
${arg.to_string($generator)} arg${count} = 0;
#elif $arg.is_pointer
${arg.to_string($generator)} arg${count} = nullptr;
#else
${arg.to_string($generator)} arg${count};
#end if
#set $count = $count + 1
#end while
#set $count = 0
#set arg_list = ""
#set arg_array = []
#while $count < $arg_idx
#set $arg = $arguments[$count]
${arg.to_native({"generator": $generator,
"in_value": "args.get(" + str(count) + ")",
"out_value": "arg" + str(count),
"class_name": $class_name,
"level": 2,
"ntype": str($arg)})};
#set $arg_array += ["arg"+str(count)]
#set $count = $count + 1
#end while
#if $arg_idx > 0
JSB_PRECONDITION2(ok, cx, false, "${signature_name} : Error processing arguments");
#end if
#set $arg_list = ", ".join($arg_array)
${namespaced_class_name}* cobj = new (std::nothrow) ${namespaced_class_name}($arg_list);
js_type_class_t *typeClass = js_get_type_from_native<${namespaced_class_name}>(cobj);
// link the native object with the javascript object
#if $is_ref_class
JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, cobj, typeClass, "${namespaced_class_name}"));
#else
JS::RootedObject jsobj(cx, jsb_create_weak_jsobject(cx, cobj, typeClass, "${namespaced_class_name}"));
#end if
args.rval().set(OBJECT_TO_JSVAL(jsobj));
if (JS_HasProperty(cx, jsobj, "_ctor", &ok) && ok)
ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(jsobj), "_ctor", args);
return true;
#end if
}