diff --git a/Lib/javascript/jsc/javascriptcode.swg b/Lib/javascript/jsc/javascriptcode.swg index 4a83b87d2..9a91c874a 100644 --- a/Lib/javascript/jsc/javascriptcode.swg +++ b/Lib/javascript/jsc/javascriptcode.swg @@ -1,12 +1,108 @@ -/********************************************************************* - *getproperty: This template gives name to generated wrapper for the getproperty - *{LOCALS}: declarations for input arguments - *{CODE}: contains input marshalling, and the action -*********************************************************************/ - -%fragment ("JS_getproperty", "templates") +/* ----------------------------------------------------------------------------- + * js_ctor: template for wrapping a ctor. + * - $jswrapper: wrapper of called ctor + * - $jslocals: locals part of wrapper + * - $jscode: code part of wrapper + * - $jsargcount: number of arguments + * - $jsmangledtype: mangled type of class + * ----------------------------------------------------------------------------- */ +%fragment ("js_ctor", "templates") %{ -JSValueRef $jsgetter(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception) +JSObjectRef $jswrapper(JSContextRef context, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception) +{ + $jslocals + + if(argc != $jsargcount) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for $jswrapper."); + + $jscode + + return SWIG_JSC_NewPointerObj(context, result, SWIGTYPE_$jsmangledtype, SWIG_POINTER_OWN); + + goto fail; + fail: + return NULL; +} +%} + +/* ----------------------------------------------------------------------------- + * js_veto_ctor: a vetoing ctor for abstract classes + * - $jswrapper: name of wrapper + * - $jsname: class name + * ----------------------------------------------------------------------------- */ +%fragment ("js_veto_ctor", "templates") +%{ +JSObjectRef $jswrapper(JSContextRef context, JSObjectRef ctorObject, + size_t argc, const JSValueRef argv[], JSValueRef* exception) +{ + SWIG_exception(SWIG_ERROR, "Class $jsname can not be instantiated"); + return 0; +} +%} + +/* ----------------------------------------------------------------------------- + * js_ctor_dispatcher: dispatcher for overloaded constructors + * - $jswrapper: name of wrapper + * - $jsname: class name + * - $jsdispatchcases: part containing code for dispatching + * ----------------------------------------------------------------------------- */ +%fragment ("js_ctor_dispatcher", "templates") +%{ +JSObjectRef $jswrapper(JSContextRef context, JSObjectRef ctorObject, + size_t argc, const JSValueRef argv[], JSValueRef* exception) +{ + JSObjectRef thisObject = NULL; + + // switch all cases by means of series of if-returns. + $jsdispatchcases + + // default: + SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for contruction of $jsname"); + + fail: + return thisObject; +} +%} + +/* ----------------------------------------------------------------------------- + * js_ctor_dispatch_case: template for a dispatch case for calling an overloaded ctor. + * - $jsargcount: number of arguments of called ctor + * - $jswrapper: wrapper of called ctor + * + * Note: a try-catch-like mechanism is used to switch cases + * ----------------------------------------------------------------------------- */ +%fragment ("js_ctor_dispatch_case", "templates") +%{ + if(argc == $jsargcount) { + thisObject = $jswrapper(context, NULL, argc, argv, exception); + if(thisObject != NULL) { *exception=0; return thisObject; } /* reset exception and return */ + } +%} + + +/* ----------------------------------------------------------------------------- + * js_dtor: template for a destructor wrapper + * - $jsmangledname: mangled class name + * - $jstype: class type + * ----------------------------------------------------------------------------- */ +%fragment ("js_dtor", "templates") +%{ +void $jswrapper(JSObjectRef thisObject) +{ + SWIG_PRV_DATA* t = (SWIG_PRV_DATA*) JSObjectGetPrivate(thisObject); + if(t && t->swigCMemOwn) free (($jstype*)t->swigCObject); + if(t) free(t); +} +%} + +/* ----------------------------------------------------------------------------- + * js_getter: template for getter function wrappers + * - $jswrapper: wrapper function name + * - $jslocals: locals part of wrapper + * - $jscode: code part of wrapper + * ----------------------------------------------------------------------------- */ +%fragment ("js_getter", "templates") +%{ +JSValueRef $jswrapper(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception) { $jslocals JSValueRef jsresult; @@ -20,15 +116,15 @@ JSValueRef $jsgetter(JSContextRef context, JSObjectRef thisObject, JSStringRef p } %} -/********************************************************************** - *setproperty: This template gives name to generated wrapper for the setproperty - *{LOCALS}: declarations for input arguments - *{CODE}: contains input marshalling, and the action -**********************************************************************/ - -%fragment ("JS_setproperty", "templates") +/* ----------------------------------------------------------------------------- + * js_setter: template for setter function wrappers + * - $jswrapper: wrapper function name + * - $jslocals: locals part of wrapper + * - $jscode: code part of wrapper + * ----------------------------------------------------------------------------- */ +%fragment ("js_setter", "templates") %{ -bool $jssetter(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception) +bool $jswrapper(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef value, JSValueRef* exception) { $jslocals $jscode @@ -41,12 +137,13 @@ bool $jssetter(JSContextRef context, JSObjectRef thisObject, JSStringRef propert } %} -/************************************************************************************ - *functionwrapper: This template gives name to generated wrapper for the function - *{LOCALS}: declarations for input arguments - *{CODE} contains input marshalling, and the action -************************************************************************************/ -%fragment ("JS_functionwrapper", "templates") +/* ----------------------------------------------------------------------------- + * js_function: template for function wrappers + * - $jswrapper: wrapper function name + * - $jslocals: locals part of wrapper + * - $jscode: code part of wrapper + * ----------------------------------------------------------------------------- */ +%fragment ("js_function", "templates") %{ JSValueRef $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception) { @@ -64,7 +161,14 @@ JSValueRef $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef th } %} -%fragment ("JS_function_dispatcher", "templates") +/* ----------------------------------------------------------------------------- + * js_function_dispatcher: template for a function dispatcher for overloaded functions + * - $jswrapper: wrapper function name + * - $jsname: name of the wrapped function + * - $jslocals: locals part of wrapper + * - $jscode: code part of wrapper + * ----------------------------------------------------------------------------- */ +%fragment ("js_function_dispatcher", "templates") %{ JSValueRef $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception) { @@ -82,7 +186,13 @@ JSValueRef $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef th } %} -%fragment ("JS_functionwrapper_overload", "templates") +/* ----------------------------------------------------------------------------- + * js_overloaded_function: template for a overloaded function + * - $jswrapper: wrapper function name + * - $jslocals: locals part of wrapper + * - $jscode: code part of wrapper + * ----------------------------------------------------------------------------- */ +%fragment ("js_overloaded_function", "templates") %{ int $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception, JSValueRef* result) { @@ -101,51 +211,87 @@ int $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObjec } %} -/*********************************************************************** - * JS_function_dispatch_case: - * This template is used to create a branch for dispatching - * to an overloaded function. - ***********************************************************************/ - -%fragment ("JS_function_dispatch_case", "templates") -%{if(argc == $jsargcount) { +/* ----------------------------------------------------------------------------- + * js_function_dispatch_case: template for a case used in the function dispatcher + * - $jswrapper: wrapper function name + * - $jsargcount: number of arguments of overloaded function + * - $jscode: code part of wrapper + * ----------------------------------------------------------------------------- */ +%fragment ("js_function_dispatch_case", "templates") +%{ + if(argc == $jsargcount) { res = $jswrapper(context, function, thisObject, argc, argv, exception, &jsresult); if(res == SWIG_OK) { *exception = 0; return jsresult; } } %} -/* Added template for function declaration */ +/* ----------------------------------------------------------------------------- + * js_initializer: template for the module initializer function + * - $jsname: module name + * - $jscreatenamespaces: part with code for creating namespace objects + * - $jscreateclasses: part with code for creating classes + * - $jsregisternamespaces: part with code for registration of namespaces + * ----------------------------------------------------------------------------- */ +%fragment ("js_initializer", "templates") %{ +#ifdef __cplusplus +extern "C" { +#endif -%fragment ("JS_variabledecl", "templates") -%{{"$jsname", $jsgetter, $jssetter, kJSPropertyAttributeNone},%} +bool $jsname_initialize(JSGlobalContextRef context) { + SWIG_InitializeModule(0); + JSObjectRef global_object = JSContextGetGlobalObject(context); + + /* Initialize the base swig type object */ + _SwigObject_objectDefinition.staticFunctions = _SwigObject_functions; + _SwigObject_objectDefinition.staticValues = _SwigObject_values; + _SwigObject_classRef = JSClassCreate(&_SwigObject_objectDefinition); + + /* Create objects for namespaces */ + $jscreatenamespaces -/* Added template for function declaration */ + /* Register classes */ + $jsregisterclasses -%fragment ("JS_functiondecl", "templates") -%{{"$jsname",$jswrapper, kJSPropertyAttributeNone},%} + /* Register namespaces */ + $jsregisternamespaces -%fragment ("JS_globaldefn", "templates") -%{ -JSStaticValue $jsnspace_values[] = { - $jsglobalvariables - { 0, 0, 0, 0 } -}; + return true; +} -JSStaticFunction $jsnspace_functions[] = { - $jsglobalfunctions - { 0, 0, 0 } -}; +#ifdef __cplusplus +} +#endif -JSClassDefinition $jsnspace_classDefinition; %} -/*********************************************************************** - * class_definition: - * declarations of javascript class definition objects. - ***********************************************************************/ +/* ----------------------------------------------------------------------------- + * jsc_variable_declaration: template for a variable table entry + * - $jsname: name of the variable + * - $jsgetter: wrapper of getter function + * - $jssetter: wrapper of setter function + * ----------------------------------------------------------------------------- */ +%fragment ("jsc_variable_declaration", "templates") +%{ + {"$jsname", $jsgetter, $jssetter, kJSPropertyAttributeNone}, +%} -%fragment ("JS_class_definition", "templates") + +/* ----------------------------------------------------------------------------- + * jsc_function_declaration: template for a function table entry + * - $jsname: name of the variable + * - $jswrapper: wrapper function + * ----------------------------------------------------------------------------- */ +%fragment ("jsc_function_declaration", "templates") +%{ + {"$jsname", $jswrapper, kJSPropertyAttributeNone}, +%} + +/* ----------------------------------------------------------------------------- + * jsc_classtemplate_declaration: template for a namespace declaration + * - $jsmangledname: mangled class name + * ----------------------------------------------------------------------------- */ +%fragment ("jsc_class_declaration", "templates") %{ JSClassDefinition $jsmangledname_classDefinition; @@ -154,12 +300,15 @@ JSClassDefinition $jsmangledname_objectDefinition; JSClassRef $jsmangledname_classRef; %} -/*********************************************************************** - * class_table: - * function and variable tables for class and object creation. -***********************************************************************/ - -%fragment ("JS_class_tables", "templates") +/* ----------------------------------------------------------------------------- + * jsc_class_tables: template for a namespace declaration + * - $jsmangledname: mangled class name + * - $jsstaticclassvariables: list of static variable entries + * - $jsstaticclassfunctions: list of static function entries + * - $jsclassvariables: list of member variable entries + * - $jsclassfunctions: list of member function entries + * ----------------------------------------------------------------------------- */ +%fragment ("jsc_class_tables", "templates") %{ JSStaticValue $jsmangledname_staticValues[] = { $jsstaticclassvariables @@ -182,154 +331,76 @@ JSStaticFunction $jsmangledname_functions[] = { }; %} -/********************************************************************* - * destructordefn: - * This code template is used to adds the destructor wrapper function -***********************************************************************/ +/* ----------------------------------------------------------------------------- + * jsc_define_class_template: template for defining a class template + * - $jsmangledname: mangled class name + * - $jsmangledtype: mangled class type + * - $jsctor: wrapper of ctor + * - $jsbaseclass: mangled name of base class + * ----------------------------------------------------------------------------- */ +%fragment ("jsc_class_definition", "templates") +%{ + $jsmangledname_classDefinition.staticFunctions = $jsmangledname_staticFunctions; + $jsmangledname_classDefinition.staticValues = $jsmangledname_staticValues; + $jsmangledname_classDefinition.callAsConstructor = $jsctor; + $jsmangledname_objectDefinition.staticValues = $jsmangledname_values; + $jsmangledname_objectDefinition.staticFunctions = $jsmangledname_functions; + $jsmangledname_objectDefinition.parentClass = $jsbaseclass_classRef; + JSClassRef $jsmangledname_classRef = JSClassCreate(&$jsmangledname_objectDefinition); + SWIGTYPE_$jsmangledtype->clientdata = $jsmangledname_classRef; +%} -%fragment ("JS_destructordefn", "templates") +/* ----------------------------------------------------------------------------- + * jsc_register_class: template for registration of a class + * - $jsname: class name + * - $jsmangledname: mangled class name + * - $jsnspace: mangled name of namespace + * ----------------------------------------------------------------------------- */ +%fragment ("jsc_class_registration", "templates") %{ -void _wrap_$jsmangledname_finalize(JSObjectRef thisObject) -{ - SWIG_PRV_DATA* t = (SWIG_PRV_DATA*)JSObjectGetPrivate(thisObject); - if(t && t->swigCMemOwn) free (($jstype*)t->swigCObject); - if(t) free(t); -} + JS_registerClass(context, $jsnspace_object, "$jsname", &$jsmangledname_classDefinition); %} -/********************************************************************* - * constructor_definition: - * This code template is used to adds the main constructor wrapper function -***********************************************************************/ - -%fragment ("JS_mainctordefn", "templates") +/* ----------------------------------------------------------------------------- + * jsc_nspace_declaration: template for a namespace declaration + * - $jsnspace: mangled name of the namespace + * - $jsglobalvariables: list of variable entries + * - $jsglobalfunctions: list if fuction entries + * ----------------------------------------------------------------------------- */ +%fragment ("jsc_nspace_declaration", "templates") %{ -JSObjectRef $jswrapper(JSContextRef context, JSObjectRef ctorObject, - size_t argc, const JSValueRef argv[], JSValueRef* exception) -{ - JSObjectRef thisObject = NULL; - - // switch all cases by means of series of if-returns. - $jsdispatchcases +JSStaticValue $jsnspace_values[] = { + $jsglobalvariables + { 0, 0, 0, 0 } +}; - // default: - SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for contruction of $jsmangledname"); - - fail: - return thisObject; -} +JSStaticFunction $jsnspace_functions[] = { + $jsglobalfunctions + { 0, 0, 0 } +}; + +JSClassDefinition $jsnspace_classDefinition; %} -%fragment ("JS_veto_ctor", "templates") +/* ----------------------------------------------------------------------------- + * jsc_nspace_definition: template for definition of a namespace object + * - $jsmangledname: mangled name of namespace + * ----------------------------------------------------------------------------- */ +%fragment ("jsc_nspace_definition", "templates") +%{ + $jsmangledname_classDefinition.staticFunctions = $jsmangledname_functions; + $jsmangledname_classDefinition.staticValues = $jsmangledname_values; + JSObjectRef $jsmangledname_object = JSObjectMake(context, JSClassCreate(&$jsmangledname_classDefinition), NULL); +%} + +/* ----------------------------------------------------------------------------- + * jsc_nspace_registration: template for registration of a namespace object + * - $jsname: name of namespace + * - $jsmangledname: mangled name of namespace + * - $jsparent: mangled name of parent namespace + * ----------------------------------------------------------------------------- */ +%fragment ("jsc_nspace_registration", "templates") %{ -JSObjectRef $jsctor(JSContextRef context, JSObjectRef ctorObject, - size_t argc, const JSValueRef argv[], JSValueRef* exception) -{ - SWIG_exception(SWIG_ERROR, "Class $jsname can not be instantiated"); - return 0; -} + JS_registerNamespace(context, $jsmangledname_object, $jsparent_object, "$jsname"); %} - -/************************************************************************************** -ctor_dispatch_case: This template is used for the constructor which is overloaded -***************************************************************************************/ - -%fragment ("JS_ctor_dispatch_case", "templates") -%{if(argc == $jsargcount) { - thisObject = $jswrapper(context, NULL, argc, argv, exception); - if(thisObject != NULL) { *exception=0; return thisObject; } /* reset exception and return */ - } -%} - - -%fragment ("JS_ctordefn", "templates") -%{ -JSObjectRef $jswrapper(JSContextRef context, JSObjectRef thisObject, size_t argc, const JSValueRef argv[], JSValueRef* exception) -{ - $jslocals - - if(argc != $jsargcount) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for $jswrapper."); - - $jscode - - return SWIG_JSC_NewPointerObj(context, result, SWIGTYPE_$jsmangledtype, SWIG_POINTER_OWN); - - goto fail; - fail: - return NULL; -} -%} - -/********************************************************************** -initializer:This template is dynamic growing and aggregates everything -**********************************************************************/ - -%fragment ("JS_initializer", "templates") %{ -#ifdef __cplusplus -extern "C" { -#endif - -bool $jsname_initialize(JSGlobalContextRef context) { - SWIG_InitializeModule(0); - - JSObjectRef global_object = JSContextGetGlobalObject(context); - - /* Initialize the base swig type object */ - _SwigObject_objectDefinition.staticFunctions = _SwigObject_functions; - _SwigObject_objectDefinition.staticValues = _SwigObject_values; - _SwigObject_classRef = JSClassCreate(&_SwigObject_objectDefinition); - - /* Create objects for namespaces */ - $jscreatenamespaces - - /* Create classes */ - $jsinitializercode - - /* Register namespaces */ - $jsregisternamespaces - - return true; -} - -#ifdef __cplusplus -} -#endif - -%} - -/***************************************************************************************** - *create_class_template: - *This template is used to add a Static references to class templates. -*****************************************************************************************/ - -%fragment ("JS_create_class_template", "templates") -%{ $jsmangledname_classDefinition.staticFunctions = $jsmangledname_staticFunctions; - $jsmangledname_classDefinition.staticValues = $jsmangledname_staticValues; - $jsmangledname_classDefinition.callAsConstructor = $jsctor; - $jsmangledname_objectDefinition.staticValues = $jsmangledname_values; - $jsmangledname_objectDefinition.staticFunctions = $jsmangledname_functions; - $jsmangledname_objectDefinition.parentClass = $jsbaseclass_classRef; - JSClassRef $jsmangledname_classRef = JSClassCreate(&$jsmangledname_objectDefinition); - SWIGTYPE_$jsmangledtype->clientdata = $jsmangledname_classRef;%} - -/***************************************************************************************** - *register_class: - * This template is used to adds a class registration statement to initializer function -*****************************************************************************************/ - -%fragment ("JS_register_class", "templates") -%{JS_registerClass(context, $jsnspace_object, "$jsname", &$jsmangledname_classDefinition);%} - - -/* create and register namespaces */ - -%fragment ("JS_create_namespace", "templates") -%{ $jsmangledname_classDefinition.staticFunctions = $jsmangledname_functions; - $jsmangledname_classDefinition.staticValues = $jsmangledname_values; - JSObjectRef $jsmangledname_object = JSObjectMake(context, JSClassCreate(&$jsmangledname_classDefinition), NULL); -%} - -%fragment ("JS_register_namespace", "templates") -%{ -JS_registerNamespace(context, $jsmangledname_object, $jsparent_object, "$jsname"); %} diff --git a/Lib/javascript/v8/javascriptcode.swg b/Lib/javascript/v8/javascriptcode.swg index b46bacfa9..5e092528b 100644 --- a/Lib/javascript/v8/javascriptcode.swg +++ b/Lib/javascript/v8/javascriptcode.swg @@ -1,4 +1,4 @@ -%fragment("JS_ctordefn", "templates") %{ +%fragment("js_ctor", "templates") %{ v8::Handle $jswrapper(const v8::Arguments& args) { v8::HandleScope scope; v8::Handle self = args.Holder(); @@ -11,18 +11,36 @@ v8::Handle $jswrapper(const v8::Arguments& args) { goto fail; fail: return scope.Close(v8::Undefined()); -}%} +} +%} -%fragment ("JS_veto_ctor", "templates") +%fragment ("js_veto_ctor", "templates") %{ -v8::Handle $jsctor(const v8::Arguments& args) { +v8::Handle $jswrapper(const v8::Arguments& args) { v8::HandleScope scope; SWIG_exception(SWIG_ERROR, "Class $jsname can not be instantiated"); return scope.Close(v8::Undefined()); } %} -%fragment ("JS_mainctordefn", "templates") %{ +%fragment("js_overloaded_ctor", "templates") %{ +v8::Handle $jswrapper(const v8::Arguments& args) { + v8::HandleScope scope; + v8::Handle self = args.Holder(); + $jslocals + $jscode + + SWIGV8_SetPrivateData(self, result, SWIGTYPE_$jsmangledtype, SWIG_POINTER_OWN); + return scope.Close(self); + + goto fail; +fail: + return scope.Close(v8::Undefined()); +} +%} + +%fragment ("js_ctor_dispatcher", "templates") +%{ v8::Handle $jswrapper(const v8::Arguments& args) { v8::HandleScope scope; @@ -37,8 +55,9 @@ fail: } %} -%fragment ("JS_ctor_dispatch_case", "templates") -%{if(args.Length() == $jsargcount) { +%fragment ("js_ctor_dispatch_case", "templates") +%{ + if(args.Length() == $jsargcount) { v8::Handle self = $jswrapper(args); if(!self->IsUndefined()) { return scope.Close(self); @@ -46,7 +65,7 @@ fail: } %} -%fragment ("JS_destructordefn", "templates") +%fragment ("js_dtor", "templates") %{ void $jswrapper(v8::Persistent< v8::Value > object, void *parameter) { SWIGV8_Proxy* proxy = (SWIGV8_Proxy*) parameter; @@ -59,8 +78,9 @@ void $jswrapper(v8::Persistent< v8::Value > object, void *parameter) { %} -%fragment("JS_getproperty", "templates") %{ -v8::Handle $jsgetter(v8::Local property, const v8::AccessorInfo& info) { +%fragment("js_getter", "templates") +%{ +v8::Handle $jswrapper(v8::Local property, const v8::AccessorInfo& info) { v8::HandleScope scope; v8::Handle jsresult; $jslocals @@ -69,19 +89,23 @@ v8::Handle $jsgetter(v8::Local property, const v8::Access goto fail; fail: return scope.Close(v8::Undefined()); -}%} +} +%} -%fragment("JS_setproperty", "templates") %{ -void $jssetter(v8::Local property, v8::Local value, const v8::AccessorInfo& info) { +%fragment("js_setter", "templates") +%{ +void $jswrapper(v8::Local property, v8::Local value, const v8::AccessorInfo& info) { v8::HandleScope scope; $jslocals $jscode goto fail; fail: return; -}%} +} +%} -%fragment("JS_functionwrapper", "templates") %{ +%fragment("js_function", "templates") +%{ v8::Handle $jswrapper(const v8::Arguments &args) { v8::HandleScope scope; v8::Handle jsresult; @@ -94,7 +118,8 @@ fail: } %} -%fragment("JS_function_dispatcher", "templates") %{ +%fragment("js_function_dispatcher", "templates") +%{ v8::Handle $jswrapper(const v8::Arguments &args) { v8::HandleScope scope; v8::Handle jsresult; @@ -109,7 +134,7 @@ fail: } %} -%fragment ("JS_functionwrapper_overload", "templates") +%fragment ("js_overloaded_function", "templates") %{ v8::Handle $jswrapper(const v8::Arguments &args, V8ErrorHandler& SWIGV8_ErrorHandler) { @@ -126,7 +151,7 @@ fail: } %} -%fragment ("JS_function_dispatch_case", "templates") +%fragment ("js_function_dispatch_case", "templates") %{ if(args.Length() == $jsargcount) { errorHandler.err.Clear(); @@ -137,53 +162,69 @@ fail: } %} -%fragment ("JS_function_dispatch_case_default", "templates") -%{ - SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for function $jsname."); -%} - %fragment("jsv8_declare_class_template", "templates") -%{SWIGV8_ClientData $jsmangledname_clientData; +%{ + SWIGV8_ClientData $jsmangledname_clientData; %} %fragment("jsv8_define_class_template", "templates") -%{v8::Handle $jsmangledname_class = SWIGV8_CreateClassTemplate("$jsmangledname"); +%{ + v8::Handle $jsmangledname_class = SWIGV8_CreateClassTemplate("$jsmangledname"); $jsmangledname_clientData.class_templ = $jsmangledname_class; $jsmangledname_clientData.dtor = $jsdtor; - SWIGTYPE$jsmangledtype->clientdata = &$jsmangledname_clientData;%} + SWIGTYPE$jsmangledtype->clientdata = &$jsmangledname_clientData; +%} %fragment("jsv8_inherit", "templates") -%{$jsmangledname_class->Inherit($jsbaseclass_class);%} +%{ + $jsmangledname_class->Inherit($jsbaseclass_class); +%} %fragment("jsv8_create_class_instance", "templates") -%{v8::Handle $jsmangledname_class_0 = SWIGV8_CreateClassTemplate("$jsname"); +%{ + v8::Handle $jsmangledname_class_0 = SWIGV8_CreateClassTemplate("$jsname"); $jsmangledname_class_0->SetCallHandler($jsctor); $jsmangledname_class_0->Inherit($jsmangledname_class); $jsmangledname_class_0->SetHiddenPrototype(true); - v8::Handle $jsmangledname_obj = $jsmangledname_class_0->GetFunction();%} + v8::Handle $jsmangledname_obj = $jsmangledname_class_0->GetFunction(); +%} %fragment("jsv8_register_class", "templates") -%{$jsparent_obj->Set(v8::String::NewSymbol("$jsname"), $jsmangledname_obj);%} +%{ + $jsparent_obj->Set(v8::String::NewSymbol("$jsname"), $jsmangledname_obj); +%} %fragment("jsv8_create_namespace", "templates") -%{v8::Handle $jsmangledname_obj = v8::Object::New();%} +%{ + v8::Handle $jsmangledname_obj = v8::Object::New(); +%} %fragment("jsv8_register_member_function", "templates") -%{SWIGV8_AddMemberFunction($jsmangledname_class, "$jsname", $jswrapper);%} +%{ + SWIGV8_AddMemberFunction($jsmangledname_class, "$jsname", $jswrapper); +%} %fragment("jsv8_register_member_variable", "templates") -%{SWIGV8_AddMemberVariable($jsmangledname_class, "$jsname", $jsgetter, $jssetter);%} +%{ + SWIGV8_AddMemberVariable($jsmangledname_class, "$jsname", $jsgetter, $jssetter); +%} %fragment("jsv8_register_static_function", "templates") -%{SWIGV8_AddStaticFunction($jsparent_obj, "$jsname", $jswrapper);%} +%{ + SWIGV8_AddStaticFunction($jsparent_obj, "$jsname", $jswrapper); +%} %fragment("jsv8_register_static_variable", "templates") -%{SWIGV8_AddStaticVariable($jsparent_obj, "$jsname", $jsgetter, $jssetter);%} +%{ + SWIGV8_AddStaticVariable($jsparent_obj, "$jsname", $jsgetter, $jssetter); +%} %fragment("jsv8_register_namespace", "templates") -%{$jsparent_obj->Set(v8::String::NewSymbol("$jsname"), $jsmangledname_obj);%} +%{ + $jsparent_obj->Set(v8::String::NewSymbol("$jsname"), $jsmangledname_obj); +%} -%fragment("JS_initializer", "templates") +%fragment("js_initializer", "templates") %{ extern "C" { @@ -194,6 +235,7 @@ void $jsname_initialize(v8::Handle context) v8::HandleScope scope; v8::Local global_obj = context->Global(); + // a class template for creating proxies of undefined types SWIGV8_SWIGTYPE_Proxy_class_templ = SWIGV8_CreateClassTemplate("SwigProxy"); /* create objects for namespaces */ @@ -219,6 +261,8 @@ void $jsname_initialize(v8::Handle context) /* create and register namespace objects */ $jsv8registernspaces + } -}%} +} // extern "C" +%} diff --git a/Source/Modules/javascript.cxx b/Source/Modules/javascript.cxx index db4972310..76d8b389d 100644 --- a/Source/Modules/javascript.cxx +++ b/Source/Modules/javascript.cxx @@ -114,6 +114,10 @@ public: void operator=(const Template& t); +protected: + + void trim(); + private: String *code; @@ -518,7 +522,6 @@ void JAVASCRIPT::main(int argc, char *argv[]) { int mode = -1; - bool debug_templates = false; for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-v8") == 0) { @@ -535,7 +538,7 @@ void JAVASCRIPT::main(int argc, char *argv[]) { SWIG_library_directory("javascript/qt"); } else if (strcmp(argv[i], "-debug-codetemplates") == 0) { Swig_mark_arg(i); - debug_templates = true; + js_template_enable_debug = true; } } } @@ -565,10 +568,6 @@ void JAVASCRIPT::main(int argc, char *argv[]) { } } - if (debug_templates) { - js_template_enable_debug = true; - } - // Add a symbol to the parser for conditional compilation Preprocessor_define("SWIGJAVASCRIPT 1", 0); @@ -811,7 +810,7 @@ int JSEmitter::emitCtor(Node *n) { bool is_overloaded = GetFlag(n, "sym:overloaded"); - Template t_ctor(getTemplate("JS_ctordefn")); + Template t_ctor(getTemplate("js_ctor")); //String *mangled_name = SwigType_manglestr(Getattr(n, "name")); String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); @@ -840,7 +839,7 @@ int JSEmitter::emitCtor(Node *n) { .replace(T_ARGCOUNT, Getattr(n, ARGCOUNT)) .pretty_print(f_wrappers); - Template t_ctor_case(getTemplate("JS_ctor_dispatch_case")); + Template t_ctor_case(getTemplate("js_ctor_dispatch_case")); t_ctor_case.replace(T_WRAPPER, wrap_name) .replace(T_ARGCOUNT, Getattr(n, ARGCOUNT)); Append(state.clazz(CTOR_DISPATCHERS), t_ctor_case.str()); @@ -851,7 +850,7 @@ int JSEmitter::emitCtor(Node *n) { if(is_overloaded) { if (!Getattr(n, "sym:nextSibling")) { String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); - Template t_mainctor(getTemplate("JS_mainctordefn")); + Template t_mainctor(getTemplate("js_ctor_dispatcher")); t_mainctor.replace(T_WRAPPER, wrap_name) .replace(T_NAME_MANGLED, state.clazz(NAME_MANGLED)) .replace(T_DISPATCH_CASES, state.clazz(CTOR_DISPATCHERS)) @@ -867,13 +866,16 @@ int JSEmitter::emitCtor(Node *n) { int JSEmitter::emitDtor(Node *n) { - Template t_dtor = getTemplate("JS_destructordefn"); + Template t_dtor = getTemplate("js_dtor"); String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); SwigType *type = state.clazz(TYPE); String *p_classtype = SwigType_add_pointer(state.clazz(TYPE)); String *ctype = SwigType_lstr(p_classtype, ""); String *free = NewString(""); + + // HACK: this is only for the v8 emitter. maybe set an attribute wrap:action of node + // TODO: generate dtors more similar to other wrappers if(SwigType_isarray(type)) { Printf(free, "delete [] (%s)", ctype); } else { @@ -884,6 +886,7 @@ int JSEmitter::emitDtor(Node *n) { t_dtor.replace(T_NAME_MANGLED, state.clazz(NAME_MANGLED)) .replace(T_WRAPPER, wrap_name) .replace(T_FREE, free) + .replace(T_TYPE, ctype) .pretty_print(f_wrappers); Delete(p_classtype); @@ -895,7 +898,7 @@ int JSEmitter::emitDtor(Node *n) { int JSEmitter::emitGetter(Node *n, bool is_member, bool is_static) { Wrapper *wrapper = NewWrapper(); - Template t_getter(getTemplate("JS_getproperty")); + Template t_getter(getTemplate("js_getter")); // prepare wrapper name String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); @@ -912,7 +915,7 @@ int JSEmitter::emitGetter(Node *n, bool is_member, bool is_static) { marshalInputArgs(n, params, wrapper, Getter, is_member, is_static); marshalOutput(n, wrapper, action); - t_getter.replace(T_GETTER, wrap_name) + t_getter.replace(T_WRAPPER, wrap_name) .replace(T_LOCALS, wrapper->locals) .replace(T_CODE, wrapper->code) .pretty_print(f_wrappers); @@ -931,7 +934,7 @@ int JSEmitter::emitSetter(Node *n, bool is_member, bool is_static) { Wrapper *wrapper = NewWrapper(); - Template t_setter(getTemplate("JS_setproperty")); + Template t_setter(getTemplate("js_setter")); // prepare wrapper name String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); @@ -948,7 +951,7 @@ int JSEmitter::emitSetter(Node *n, bool is_member, bool is_static) { marshalInputArgs(n, params, wrapper, Setter, is_member, is_static); Append(wrapper->code, action); - t_setter.replace(T_SETTER, wrap_name) + t_setter.replace(T_WRAPPER, wrap_name) .replace(T_LOCALS, wrapper->locals) .replace(T_CODE, wrapper->code) .pretty_print(f_wrappers); @@ -966,7 +969,7 @@ int JSEmitter::emitConstant(Node *n) { Wrapper *wrapper = NewWrapper(); - Template t_getter(getTemplate("JS_getproperty")); + Template t_getter(getTemplate("js_getter")); // call the variable methods as a constants are // registred in same way @@ -987,7 +990,7 @@ int JSEmitter::emitConstant(Node *n) { assert(value != NULL); marshalOutput(n, wrapper, action, value, false); - t_getter.replace(T_GETTER, wrap_name) + t_getter.replace(T_WRAPPER, wrap_name) .replace(T_LOCALS, wrapper->locals) .replace(T_CODE, wrapper->code) .pretty_print(f_wrappers); @@ -1002,14 +1005,14 @@ int JSEmitter::emitConstant(Node *n) { int JSEmitter::emitFunction(Node *n, bool is_member, bool is_static) { Wrapper *wrapper = NewWrapper(); - Template t_function(getTemplate("JS_functionwrapper")); + Template t_function(getTemplate("js_function")); bool is_overloaded = GetFlag(n, "sym:overloaded"); // prepare the function wrapper name String *wrap_name = Swig_name_wrapper(Getattr(n, "sym:name")); if (is_overloaded) { - t_function = getTemplate("JS_functionwrapper_overload"); + t_function = getTemplate("js_overloaded_function"); Append(wrap_name, Getattr(n, "sym:overname")); } Setattr(n, "wrap:name", wrap_name); @@ -1033,7 +1036,7 @@ int JSEmitter::emitFunction(Node *n, bool is_member, bool is_static) { // handle function overloading if (is_overloaded) { - Template t_dispatch_case = getTemplate("JS_function_dispatch_case"); + Template t_dispatch_case = getTemplate("js_function_dispatch_case"); t_dispatch_case.replace(T_WRAPPER, wrap_name) .replace(T_ARGCOUNT, Getattr(n, ARGCOUNT)); Append(state.global(FUNCTION_DISPATCHERS), t_dispatch_case.str()); @@ -1046,7 +1049,7 @@ int JSEmitter::emitFunction(Node *n, bool is_member, bool is_static) { int JSEmitter::emitFunctionDispatcher(Node *n, bool /*is_member */ ) { - Template t_function(getTemplate("JS_function_dispatcher")); + Template t_function(getTemplate("js_function_dispatcher")); Wrapper *wrapper = NewWrapper(); String *wrap_name = Swig_name_wrapper(Getattr(n, "name")); @@ -1378,9 +1381,9 @@ int JSCEmitter::dump(Node *n) { emitNamespaces(); // compose the initializer function using a template - Template initializer(getTemplate("JS_initializer")); + Template initializer(getTemplate("js_initializer")); initializer.replace(T_NAME, module) - .replace("$jsinitializercode", state.global(INITIALIZER)) + .replace("$jsregisterclasses", state.global(INITIALIZER)) .replace("$jscreatenamespaces", state.global(CREATE_NAMESPACES)) .replace("$jsregisternamespaces", state.global(REGISTER_NAMESPACES)) .pretty_print(f_init); @@ -1414,7 +1417,7 @@ int JSCEmitter::enterFunction(Node *n) { } int JSCEmitter::exitFunction(Node *n) { - Template t_function = getTemplate("JS_functiondecl"); + Template t_function = getTemplate("jsc_function_declaration"); bool is_member = GetFlag(n, "ismember"); bool is_overloaded = GetFlag(n, "sym:overloaded"); @@ -1456,7 +1459,7 @@ int JSCEmitter::enterVariable(Node *n) { int JSCEmitter::exitVariable(Node *n) { - Template t_variable(getTemplate("JS_variabledecl")); + Template t_variable(getTemplate("jsc_variable_declaration")); t_variable.replace(T_NAME, state.variable(NAME)) .replace(T_GETTER, state.variable(GETTER)) .replace(T_SETTER, state.variable(SETTER)); @@ -1482,15 +1485,15 @@ int JSCEmitter::enterClass(Node *n) { state.clazz(STATIC_VARIABLES, NewString("")); state.clazz(STATIC_FUNCTIONS, NewString("")); - Template t_class_defn = getTemplate("JS_class_definition"); - t_class_defn.replace(T_NAME_MANGLED, state.clazz(NAME_MANGLED)) + Template t_class_decl = getTemplate("jsc_class_declaration"); + t_class_decl.replace(T_NAME_MANGLED, state.clazz(NAME_MANGLED)) .pretty_print(f_wrappers); return SWIG_OK; } int JSCEmitter::exitClass(Node *n) { - Template t_class_tables(getTemplate("JS_class_tables")); + Template t_class_tables(getTemplate("jsc_class_tables")); t_class_tables.replace(T_NAME_MANGLED, state.clazz(NAME_MANGLED)) .replace("$jsclassvariables", state.clazz(MEMBER_VARIABLES)) .replace("$jsclassfunctions", state.clazz(MEMBER_FUNCTIONS)) @@ -1504,14 +1507,14 @@ int JSCEmitter::exitClass(Node *n) { // for abstract classes add a vetoing ctor if(GetFlag(state.clazz(), IS_ABSTRACT)) { - Template t_veto_ctor(getTemplate("JS_veto_ctor")); - t_veto_ctor.replace(T_CTOR, state.clazz(CTOR)) + Template t_veto_ctor(getTemplate("js_veto_ctor")); + t_veto_ctor.replace(T_WRAPPER, state.clazz(CTOR)) .replace(T_NAME, state.clazz(NAME)) .pretty_print(f_wrappers); } /* adds a class template statement to initializer function */ - Template t_classtemplate(getTemplate("JS_create_class_template")); + Template t_classtemplate(getTemplate("jsc_class_definition")); /* prepare registration of base class */ String *base_name_mangled = NewString("_SwigObject"); @@ -1531,7 +1534,7 @@ int JSCEmitter::exitClass(Node *n) { SwigType_remember_clientdata(state.clazz(TYPE_MANGLED), NewString("0")); /* adds a class registration statement to initializer function */ - Template t_registerclass(getTemplate("JS_register_class")); + Template t_registerclass(getTemplate("jsc_class_registration")); t_registerclass.replace(T_NAME, state.clazz(NAME)) .replace(T_NAME_MANGLED, state.clazz(NAME_MANGLED)) .replace(T_NAMESPACE, Getattr(current_namespace, NAME_MANGLED)) @@ -1558,17 +1561,17 @@ int JSCEmitter::emitNamespaces() { String *functions = Getattr(entry, "functions"); String *variables = Getattr(entry, "values"); - Template namespace_definition(getTemplate("JS_globaldefn")); + Template namespace_definition(getTemplate("jsc_nspace_declaration")); namespace_definition.replace("$jsglobalvariables", variables) .replace("$jsglobalfunctions", functions) .replace(T_NAMESPACE, name_mangled) .pretty_print(f_wrap_cpp); - Template t_createNamespace(getTemplate("JS_create_namespace")); + Template t_createNamespace(getTemplate("jsc_nspace_definition")); t_createNamespace.replace(T_NAME_MANGLED, name_mangled); Append(state.global(CREATE_NAMESPACES), t_createNamespace.str()); - Template t_registerNamespace(getTemplate("JS_register_namespace")); + Template t_registerNamespace(getTemplate("jsc_nspace_registration")); t_registerNamespace.replace(T_NAME_MANGLED, name_mangled) .replace(T_NAME, name) .replace(T_PARENT, parent_mangled); @@ -1708,7 +1711,7 @@ int V8Emitter::dump(Node *n) // compose the initializer function using a template // filled with sub-parts - Template initializer(getTemplate("JS_initializer")); + Template initializer(getTemplate("js_initializer")); initializer.replace(T_NAME, module) .replace(V8_NAME_SPACES, f_init_namespaces) .replace(V8_CLASS_TEMPLATES, f_init_class_templates) @@ -1762,8 +1765,8 @@ int V8Emitter::enterClass(Node *n) int V8Emitter::exitClass(Node *n) { if(GetFlag(state.clazz(), IS_ABSTRACT)) { - Template t_veto_ctor(getTemplate("JS_veto_ctor")); - t_veto_ctor.replace(T_CTOR, state.clazz(CTOR)) + Template t_veto_ctor(getTemplate("js_veto_ctor")); + t_veto_ctor.replace(T_WRAPPER, state.clazz(CTOR)) .replace(T_NAME, state.clazz(NAME)) .pretty_print(f_wrappers); } @@ -2001,7 +2004,7 @@ void V8Emitter::emitUndefined() { .pretty_print(f_class_templates); // emit an extra dtor for unknown types - Template t_dtor = getTemplate("JS_destructordefn"); + Template t_dtor = getTemplate("js_dtor"); t_dtor.replace(T_NAME_MANGLED, mangled_name) .replace(T_WRAPPER, dtor) .replace(T_FREE, free) @@ -2134,6 +2137,8 @@ Template::Template(const String *code_) { } code = NewString(code_); templateName = NewString(""); + + trim(); } Template::Template(const String *code_, const String *templateName_) { @@ -2145,6 +2150,8 @@ Template::Template(const String *code_, const String *templateName_) { code = NewString(code_); templateName = NewString(templateName_); + + trim(); } @@ -2179,6 +2186,36 @@ String *Template::str() { return code; } +void Template::trim() { + const char* str = Char(code); + if (str == 0) return; + + int length = Len(code); + if (length == 0) return; + + int idx; + for(idx=0; idx < length; ++idx) { + if (str[idx] == ' ' || str[idx] == '\t' || str[idx] == '\r' || str[idx] == '\n') + break; + } + int start_pos = idx; + + for(idx=length-1; idx >= start_pos; --idx) { + if (str[idx] == ' ' || str[idx] == '\t' || str[idx] == '\r' || str[idx] == '\n') + break; + } + int end_pos = idx; + + int new_length = end_pos-start_pos+1; + char* newstr = new char[new_length+1]; + memcpy(newstr, str+start_pos, new_length); + newstr[new_length] = 0; + + Delete(code); + code = NewString(newstr); + delete[] newstr; +} + /* ----------------------------------------------------------------------------- * Template& Template::replace(const String* pattern, const String* repl) : *