From 1f07195812f79a20bb1f37178e596dd0bab8ef2e Mon Sep 17 00:00:00 2001 From: Oliver Buchtala Date: Mon, 16 Sep 2013 03:53:00 +0200 Subject: [PATCH] Rearrange generation of init block to have custom init code within the initializer body. --- Lib/javascript/jsc/javascriptcode.swg | 50 ----------------- Lib/javascript/jsc/javascriptinit.swg | 54 ++++++++++++++++++- Lib/javascript/v8/javascriptcode.swg | 68 ----------------------- Lib/javascript/v8/javascriptinit.swg | 78 +++++++++++++++++++++++++-- Source/Modules/javascript.cxx | 12 +++-- 5 files changed, 137 insertions(+), 125 deletions(-) diff --git a/Lib/javascript/jsc/javascriptcode.swg b/Lib/javascript/jsc/javascriptcode.swg index 42837f7f4..60c52b2b3 100644 --- a/Lib/javascript/jsc/javascriptcode.swg +++ b/Lib/javascript/jsc/javascriptcode.swg @@ -263,56 +263,6 @@ int $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObjec } %} -%fragment ("js_initializer_define", "templates") %{ -#define SWIGJSC_INIT $jsname_initialize -%} - -/* ----------------------------------------------------------------------------- - * 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 - -bool SWIGJSC_INIT (JSGlobalContextRef context, JSObjectRef *exports) { - SWIG_InitializeModule(0); - - /* Initialize the base swig type object */ - _SwigObject_objectDefinition.staticFunctions = _SwigObject_functions; - _SwigObject_objectDefinition.staticValues = _SwigObject_values; - _SwigObject_classRef = JSClassCreate(&_SwigObject_objectDefinition); - - /* Initialize the PackedData class */ - _SwigPackedData_objectDefinition.staticFunctions = _SwigPackedData_functions; - _SwigPackedData_objectDefinition.staticValues = _SwigPackedData_values; - _SwigPackedData_objectDefinition.finalize = _wrap_SwigPackedData_delete; - _SwigPackedData_classRef = JSClassCreate(&_SwigPackedData_objectDefinition); - - /* Create objects for namespaces */ - $jscreatenamespaces - - /* Register classes */ - $jsregisterclasses - - /* Register namespaces */ - $jsregisternamespaces - - *exports = exports_object; - - return true; -} - -#ifdef __cplusplus -} -#endif - -%} - /* ----------------------------------------------------------------------------- * jsc_variable_declaration: template for a variable table entry * - $jsname: name of the variable diff --git a/Lib/javascript/jsc/javascriptinit.swg b/Lib/javascript/jsc/javascriptinit.swg index 69bc3a9b1..a32ba336c 100644 --- a/Lib/javascript/jsc/javascriptinit.swg +++ b/Lib/javascript/jsc/javascriptinit.swg @@ -9,7 +9,59 @@ SWIG_JSC_GetModule(void) { #define SWIG_GetModule(clientdata) SWIG_JSC_GetModule() #define SWIG_SetModule(clientdata, pointer) SWIG_JSC_SetModule(pointer) - %} %insert(init) "swiginit.swg" + +%fragment ("js_initializer_define", "templates") %{ +#define SWIGJSC_INIT $jsname_initialize +%} + +// Open the initializer function +%insert(init) +%{ + +#ifdef __cplusplus +extern "C" { +#endif + +bool SWIGJSC_INIT (JSGlobalContextRef context, JSObjectRef *exports) { + SWIG_InitializeModule(0); +%} + +/* ----------------------------------------------------------------------------- + * 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") %{ + /* Initialize the base swig type object */ + _SwigObject_objectDefinition.staticFunctions = _SwigObject_functions; + _SwigObject_objectDefinition.staticValues = _SwigObject_values; + _SwigObject_classRef = JSClassCreate(&_SwigObject_objectDefinition); + + /* Initialize the PackedData class */ + _SwigPackedData_objectDefinition.staticFunctions = _SwigPackedData_functions; + _SwigPackedData_objectDefinition.staticValues = _SwigPackedData_values; + _SwigPackedData_objectDefinition.finalize = _wrap_SwigPackedData_delete; + _SwigPackedData_classRef = JSClassCreate(&_SwigPackedData_objectDefinition); + + /* Create objects for namespaces */ + $jscreatenamespaces + + /* Register classes */ + $jsregisterclasses + + /* Register namespaces */ + $jsregisternamespaces + + *exports = exports_object; + + return true; +} +#ifdef __cplusplus +} +#endif +%} diff --git a/Lib/javascript/v8/javascriptcode.swg b/Lib/javascript/v8/javascriptcode.swg index 1b06dbdc8..c05c6daf3 100644 --- a/Lib/javascript/v8/javascriptcode.swg +++ b/Lib/javascript/v8/javascriptcode.swg @@ -464,71 +464,3 @@ fail: %{ SWIGV8_AddStaticVariable($jsparent_obj, "$jsname", $jsgetter, $jssetter); %} - -/* ----------------------------------------------------------------------------- - * js_initializer: template for the module initializer function - * - $jsname: module name - * - $jsv8nspaces: part with code creating namespace objects - * - $jsv8classtemplates: part with code creating class templates - * - $jsv8wrappers: part with code that registers wrapper functions - * - $jsv8inheritance: part with inherit statements - * - $jsv8classinstances: part with code creating class objects - * - $jsv8staticwrappers: part with code adding static functions to class objects - * - $jsv8registerclasses: part with code that registers class objects in namespaces - * - $jsv8registernspaces: part with code that registers namespaces in parent namespaces - * ----------------------------------------------------------------------------- */ -%fragment("js_initializer", "templates") -%{ - -// Note: 'extern "C"'' disables name mangling which makes it easier to load the symbol manually -// TODO: is it ok to do that? -extern "C" -#if (NODE_MODULE_VERSION < 0x000C) -void $jsname_initialize(v8::Handle exports) -#else -void $jsname_initialize(v8::Handle exports, v8::Handle /*module*/) -#endif -{ - SWIG_InitializeModule(static_cast(&exports)); - - v8::HandleScope scope; - v8::Handle exports_obj = exports; - - // a class template for creating proxies of undefined types - -#if (SWIG_V8_VERSION < 0x031900) - SWIGV8_SWIGTYPE_Proxy_class_templ = v8::Persistent::New(SWIGV8_CreateClassTemplate("SwigProxy")); -#else - SWIGV8_SWIGTYPE_Proxy_class_templ.Reset(v8::Isolate::GetCurrent(), SWIGV8_CreateClassTemplate("SwigProxy")); -#endif - - /* create objects for namespaces */ - $jsv8nspaces - - /* create class templates */ - $jsv8classtemplates - - /* register wrapper functions */ - $jsv8wrappers - - /* setup inheritances */ - $jsv8inheritance - - /* class instances */ - $jsv8classinstances - - /* add static class functions and variables */ - $jsv8staticwrappers - - /* register classes */ - $jsv8registerclasses - - /* create and register namespace objects */ - $jsv8registernspaces -} - -#if defined(BUILDING_NODE_EXTENSION) -NODE_MODULE($jsname, $jsname_initialize); -#endif - -%} diff --git a/Lib/javascript/v8/javascriptinit.swg b/Lib/javascript/v8/javascriptinit.swg index 13ec1aff8..de1fe91f4 100644 --- a/Lib/javascript/v8/javascriptinit.swg +++ b/Lib/javascript/v8/javascriptinit.swg @@ -22,7 +22,7 @@ SWIG_V8_GetModule(void *) { } v8::Local moduleinfo_extern = v8::Local::Cast(moduleinfo); - + if (moduleinfo_extern.IsEmpty()) { // Something's not right @@ -41,6 +41,78 @@ SWIG_V8_GetModule(void *) { %} -%insert(init) %{/************ BEGIN: "swiginit.swg" *******************/ %} %insert(init) "swiginit.swg" -%insert(init) %{/************ END: "swiginit.swg" *******************/ %} + +// Open the initializer function definition here + +%fragment ("js_initializer_define", "templates") %{ +#define SWIGV8_INIT $jsname_initialize +%} + +%insert(init) %{ +// Note: 'extern "C"'' disables name mangling which makes it easier to load the symbol manually +// TODO: is it ok to do that? +extern "C" +#if (NODE_MODULE_VERSION < 0x000C) +void SWIGV8_INIT (v8::Handle exports) +#else +void SWIGV8_INIT (v8::Handle exports, v8::Handle /*module*/) +#endif +{ + SWIG_InitializeModule(static_cast(&exports)); + + v8::HandleScope scope; + v8::Handle exports_obj = exports; +%} + + +/* ----------------------------------------------------------------------------- + * js_initializer: template for the module initializer function + * - $jsname: module name + * - $jsv8nspaces: part with code creating namespace objects + * - $jsv8classtemplates: part with code creating class templates + * - $jsv8wrappers: part with code that registers wrapper functions + * - $jsv8inheritance: part with inherit statements + * - $jsv8classinstances: part with code creating class objects + * - $jsv8staticwrappers: part with code adding static functions to class objects + * - $jsv8registerclasses: part with code that registers class objects in namespaces + * - $jsv8registernspaces: part with code that registers namespaces in parent namespaces + * ----------------------------------------------------------------------------- */ +%fragment("js_initializer", "templates") +%{ + // a class template for creating proxies of undefined types +#if (SWIG_V8_VERSION < 0x031900) + SWIGV8_SWIGTYPE_Proxy_class_templ = v8::Persistent::New(SWIGV8_CreateClassTemplate("SwigProxy")); +#else + SWIGV8_SWIGTYPE_Proxy_class_templ.Reset(v8::Isolate::GetCurrent(), SWIGV8_CreateClassTemplate("SwigProxy")); +#endif + + /* create objects for namespaces */ + $jsv8nspaces + + /* create class templates */ + $jsv8classtemplates + + /* register wrapper functions */ + $jsv8wrappers + + /* setup inheritances */ + $jsv8inheritance + + /* class instances */ + $jsv8classinstances + + /* add static class functions and variables */ + $jsv8staticwrappers + + /* register classes */ + $jsv8registerclasses + + /* create and register namespace objects */ + $jsv8registernspaces +} + +#if defined(BUILDING_NODE_EXTENSION) +NODE_MODULE($jsname, $jsname_initialize); +#endif +%} diff --git a/Source/Modules/javascript.cxx b/Source/Modules/javascript.cxx index 038094a12..d9ec51247 100644 --- a/Source/Modules/javascript.cxx +++ b/Source/Modules/javascript.cxx @@ -1828,11 +1828,17 @@ int V8Emitter::initialize(Node *n) return SWIG_OK; } -int V8Emitter::dump(Node *) +int V8Emitter::dump(Node *n) { + /* Get the module name */ + String *module = Getattr(n, "name"); + // write the swig banner Swig_banner(f_wrap_cpp); + Template initializer_define(getTemplate("js_initializer_define")); + initializer_define.replace("$jsname", module).pretty_print(f_header); + SwigType_emit_type_table(f_runtime, f_wrappers); Printv(f_wrap_cpp, f_runtime, "\n", 0); @@ -1853,8 +1859,8 @@ int V8Emitter::dump(Node *) .replace("$jsv8classinstances", f_init_class_instances) .replace("$jsv8staticwrappers", f_init_static_wrappers) .replace("$jsv8registerclasses", f_init_register_classes) - .replace("$jsv8registernspaces", f_init_register_namespaces) - .pretty_print(f_init); + .replace("$jsv8registernspaces", f_init_register_namespaces); + Printv(f_init, initializer.str(), 0); Printv(f_wrap_cpp, f_init, 0);