From 29ccb270afa761d57d6a6728ca49d92fe9d35bc0 Mon Sep 17 00:00:00 2001 From: Oliver Buchtala Date: Fri, 6 Sep 2013 00:40:29 +0300 Subject: [PATCH] Renamed object provided to JS initializers. JSC initializer create a new module object. V8 initializer fill a provided 'exports' object. --- Lib/javascript/jsc/javascriptcode.swg | 6 ++-- Lib/javascript/v8/javascriptcode.swg | 13 +++++--- Source/Modules/javascript.cxx | 45 ++++++++++++++------------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/Lib/javascript/jsc/javascriptcode.swg b/Lib/javascript/jsc/javascriptcode.swg index 0325a7a35..a55f24d33 100644 --- a/Lib/javascript/jsc/javascriptcode.swg +++ b/Lib/javascript/jsc/javascriptcode.swg @@ -279,11 +279,9 @@ int $jswrapper(JSContextRef context, JSObjectRef function, JSObjectRef thisObjec extern "C" { #endif -bool SWIGJSC_INIT (JSGlobalContextRef context) { +bool SWIGJSC_INIT (JSGlobalContextRef context, JSObjectRef *exports) { SWIG_InitializeModule(0); - JSObjectRef global_object = JSContextGetGlobalObject(context); - /* Initialize the base swig type object */ _SwigObject_objectDefinition.staticFunctions = _SwigObject_functions; _SwigObject_objectDefinition.staticValues = _SwigObject_values; @@ -298,6 +296,8 @@ bool SWIGJSC_INIT (JSGlobalContextRef context) { /* Register namespaces */ $jsregisternamespaces + *exports = exports_object; + return true; } diff --git a/Lib/javascript/v8/javascriptcode.swg b/Lib/javascript/v8/javascriptcode.swg index 4277e8b0c..dcaf2a707 100644 --- a/Lib/javascript/v8/javascriptcode.swg +++ b/Lib/javascript/v8/javascriptcode.swg @@ -477,15 +477,19 @@ fail: %fragment("js_initializer", "templates") %{ -#if defined(BUILDING_NODE_EXTENSION) -void $jsname_initialize(v8::Handle global_obj, v8::Handle /*module*/) +// 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 global_obj) +void $jsname_initialize(v8::Handle exports, v8::Handle /*module*/) #endif { - SWIG_InitializeModule(static_cast(&global_obj)); + SWIG_InitializeModule(static_cast(&exports)); v8::HandleScope scope; + v8::Handle exports_obj = exports; // a class template for creating proxies of undefined types @@ -495,7 +499,6 @@ void $jsname_initialize(v8::Handle global_obj) SWIGV8_SWIGTYPE_Proxy_class_templ.Reset(v8::Isolate::GetCurrent(), SWIGV8_CreateClassTemplate("SwigProxy")); #endif - /* create objects for namespaces */ $jsv8nspaces diff --git a/Source/Modules/javascript.cxx b/Source/Modules/javascript.cxx index cec65338a..360fb1aec 100644 --- a/Source/Modules/javascript.cxx +++ b/Source/Modules/javascript.cxx @@ -521,7 +521,10 @@ void JAVASCRIPT::main(int argc, char *argv[]) { int mode = -1; - bool createModuleObject = true; + // Note: creating a module object is not supported anymore. + // instead the initializer is called with an externally created object + // This makes it obsolete to handle node extensions differently + bool createModuleObject = false; for (int i = 1; i < argc; i++) { if (argv[i]) { @@ -533,7 +536,6 @@ void JAVASCRIPT::main(int argc, char *argv[]) { Swig_mark_arg(i); mode = JSEmitter::V8; SWIG_library_directory("javascript/v8"); - createModuleObject = false; Preprocessor_define("BUILDING_NODE_EXTENSION 1", 0); } else if (strcmp(argv[i], "-jsc") == 0) { Swig_mark_arg(i); @@ -544,7 +546,6 @@ void JAVASCRIPT::main(int argc, char *argv[]) { js_template_enable_debug = true; } else if (strcmp(argv[i], "-no-moduleobject") == 0) { Swig_mark_arg(i); - createModuleObject = false; } } } @@ -655,18 +656,18 @@ JSEmitterState &JSEmitter::getState() { return state; } -int JSEmitter::initialize(Node *n) { +int JSEmitter::initialize(Node * /*n*/) { if(namespaces != NULL) { Delete(namespaces); } namespaces = NewHash(); Hash *global_namespace; - if(State::IsSet(state.global(FLAG_NO_MODULE_OBJECT))) { - global_namespace = createNamespaceEntry("global", 0); - } else { - global_namespace = createNamespaceEntry(Char(Getattr(n, "name")), "global"); - } +// if(State::IsSet(state.global(FLAG_NO_MODULE_OBJECT))) { + global_namespace = createNamespaceEntry("exports", 0); +// } else { +// global_namespace = createNamespaceEntry(Char(Getattr(n, "name")), "global"); +// } Setattr(namespaces, "::", global_namespace); current_namespace = global_namespace; @@ -1407,8 +1408,6 @@ private: String *NULL_STR; String *VETO_SET; - const char *GLOBAL_STR; - // output file and major code parts File *f_wrap_cpp; @@ -1433,7 +1432,6 @@ JSCEmitter::JSCEmitter() : JSEmitter(), NULL_STR(NewString("NULL")), VETO_SET(NewString("JS_veto_set_variable")), - GLOBAL_STR(NULL), f_wrap_cpp(NULL), f_runtime(NULL), f_header(NULL), @@ -1735,6 +1733,8 @@ int JSCEmitter::emitNamespaces() { String *functions = Getattr(entry, "functions"); String *variables = Getattr(entry, "values"); + // skip the global namespace which is given by the application + Template namespace_definition(getTemplate("jsc_nspace_declaration")); namespace_definition.replace("$jsglobalvariables", variables) .replace("$jsglobalfunctions", functions) @@ -1745,11 +1745,15 @@ int JSCEmitter::emitNamespaces() { t_createNamespace.replace(T_NAME_MANGLED, name_mangled); Append(state.global(CREATE_NAMESPACES), t_createNamespace.str()); - Template t_registerNamespace(getTemplate("jsc_nspace_registration")); - t_registerNamespace.replace(T_NAME_MANGLED, name_mangled) - .replace(T_NAME, name) - .replace(T_PARENT, parent_mangled); - Append(state.global(REGISTER_NAMESPACES), t_registerNamespace.str()); + // Don't register 'exports' as namespace. It is return to the application. + if (!Equal("exports", name)) { + Template t_registerNamespace(getTemplate("jsc_nspace_registration")); + t_registerNamespace.replace(T_NAME_MANGLED, name_mangled) + .replace(T_NAME, name) + .replace(T_PARENT, parent_mangled); + Append(state.global(REGISTER_NAMESPACES), t_registerNamespace.str()); + } + } return SWIG_OK; @@ -1809,7 +1813,6 @@ private: // the output cpp file File *f_wrap_cpp; - String* GLOBAL; String* NULL_STR; String *VETO_SET; String *moduleName; @@ -1818,7 +1821,6 @@ private: V8Emitter::V8Emitter() : JSEmitter(), - GLOBAL(NewString("global")), NULL_STR(NewString("0")), VETO_SET(NewString("JS_veto_set_variable")) { @@ -1826,7 +1828,6 @@ V8Emitter::V8Emitter() V8Emitter::~V8Emitter() { - Delete(GLOBAL); Delete(NULL_STR); Delete(VETO_SET); } @@ -2161,7 +2162,9 @@ int V8Emitter::emitNamespaces() { do_register = false; } - if (Equal(name, "global")) { + // Note: 'exports' is by convention the name of the object where + // globals are stored into + if (Equal(name, "exports")) { do_create = false; }