From b0c01ea85117ba50754aeeaa18875f8bcdae6279 Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Sun, 28 Feb 2021 17:08:14 +0100 Subject: [PATCH] Lib/javascript/v8: use context-aware initialization. Context-aware initialization allows to instantiate add-ons multiple times, most importantly in multiple Workers' contexts. Workers made first appearance in v10.5. Context-aware initialization was option earlier than that, even before supported minimum v6.x, yet condition is chosen more conservatively as NODE_MODULE_VERSION >= 64, a.k.a. v10.0. --- Lib/javascript/v8/javascriptcode.swg | 10 +++---- Lib/javascript/v8/javascripthelpers.swg | 11 ++++---- Lib/javascript/v8/javascriptinit.swg | 35 ++++++++++++++----------- Lib/swiginit.swg | 5 +++- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/Lib/javascript/v8/javascriptcode.swg b/Lib/javascript/v8/javascriptcode.swg index 0b0f1304e..60b1aba3a 100644 --- a/Lib/javascript/v8/javascriptcode.swg +++ b/Lib/javascript/v8/javascriptcode.swg @@ -432,7 +432,7 @@ fail: $jsmangledname_class_0->SetHiddenPrototype(true); v8::Local $jsmangledname_obj = $jsmangledname_class_0->GetFunction(); #else - v8::Local $jsmangledname_obj = $jsmangledname_class_0->GetFunction(SWIGV8_CURRENT_CONTEXT()).ToLocalChecked(); + v8::Local $jsmangledname_obj = $jsmangledname_class_0->GetFunction(context).ToLocalChecked(); #endif %} @@ -447,7 +447,7 @@ fail: #if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903) $jsparent_obj->Set(SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj); #else - SWIGV8_MAYBE_CHECK($jsparent_obj->Set(SWIGV8_CURRENT_CONTEXT(), SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj)); + SWIGV8_MAYBE_CHECK($jsparent_obj->Set(context, SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj)); #endif %} @@ -472,7 +472,7 @@ fail: #if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903) $jsparent_obj->Set(SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj); #else - SWIGV8_MAYBE_CHECK($jsparent_obj->Set(SWIGV8_CURRENT_CONTEXT(), SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj)); + SWIGV8_MAYBE_CHECK($jsparent_obj->Set(context, SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj)); #endif %} @@ -509,7 +509,7 @@ fail: * ----------------------------------------------------------------------------- */ %fragment("jsv8_register_static_function", "templates") %{ - SWIGV8_AddStaticFunction($jsparent_obj, "$jsname", $jswrapper); + SWIGV8_AddStaticFunction($jsparent_obj, "$jsname", $jswrapper, context); %} /* ----------------------------------------------------------------------------- @@ -523,5 +523,5 @@ fail: * ----------------------------------------------------------------------------- */ %fragment("jsv8_register_static_variable", "templates") %{ - SWIGV8_AddStaticVariable($jsparent_obj, "$jsname", $jsgetter, $jssetter); + SWIGV8_AddStaticVariable($jsparent_obj, "$jsname", $jsgetter, $jssetter, context); %} diff --git a/Lib/javascript/v8/javascripthelpers.swg b/Lib/javascript/v8/javascripthelpers.swg index 9da3ad639..9c3296d82 100644 --- a/Lib/javascript/v8/javascripthelpers.swg +++ b/Lib/javascript/v8/javascripthelpers.swg @@ -61,11 +61,11 @@ SWIGRUNTIME void SWIGV8_AddMemberVariable(SWIGV8_FUNCTION_TEMPLATE class_templ, * Registers a class method with given name for a given object. */ SWIGRUNTIME void SWIGV8_AddStaticFunction(SWIGV8_OBJECT obj, const char* symbol, - const SwigV8FunctionCallback& _func) { -#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903) || (SWIG_V8_VERSION < 0x0704) + const SwigV8FunctionCallback& _func, v8::Local context) { +#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903) obj->Set(SWIGV8_SYMBOL_NEW(symbol), SWIGV8_FUNCTEMPLATE_NEW(_func)->GetFunction()); #else - SWIGV8_MAYBE_CHECK(obj->Set(SWIGV8_CURRENT_CONTEXT(), SWIGV8_SYMBOL_NEW(symbol), SWIGV8_FUNCTEMPLATE_NEW(_func)->GetFunction(SWIGV8_CURRENT_CONTEXT()).ToLocalChecked())); + SWIGV8_MAYBE_CHECK(obj->Set(context, SWIGV8_SYMBOL_NEW(symbol), SWIGV8_FUNCTEMPLATE_NEW(_func)->GetFunction(context).ToLocalChecked())); #endif } @@ -73,11 +73,12 @@ SWIGRUNTIME void SWIGV8_AddStaticFunction(SWIGV8_OBJECT obj, const char* symbol, * Registers a class method with given name for a given object. */ SWIGRUNTIME void SWIGV8_AddStaticVariable(SWIGV8_OBJECT obj, const char* symbol, - SwigV8AccessorGetterCallback getter, SwigV8AccessorSetterCallback setter) { + SwigV8AccessorGetterCallback getter, SwigV8AccessorSetterCallback setter, + v8::Local context) { #if (V8_MAJOR_VERSION-0) < 5 obj->SetAccessor(SWIGV8_SYMBOL_NEW(symbol), getter, setter); #else - SWIGV8_MAYBE_CHECK(obj->SetAccessor(SWIGV8_CURRENT_CONTEXT(), SWIGV8_SYMBOL_NEW(symbol), getter, setter)); + SWIGV8_MAYBE_CHECK(obj->SetAccessor(context, SWIGV8_SYMBOL_NEW(symbol), getter, setter)); #endif } diff --git a/Lib/javascript/v8/javascriptinit.swg b/Lib/javascript/v8/javascriptinit.swg index 401f9ae1f..772189787 100644 --- a/Lib/javascript/v8/javascriptinit.swg +++ b/Lib/javascript/v8/javascriptinit.swg @@ -5,27 +5,27 @@ %insert(init) %{ SWIGRUNTIME void -SWIG_V8_SetModule(void *, swig_module_info *swig_module) { - v8::Local global_obj = SWIGV8_CURRENT_CONTEXT()->Global(); +SWIG_V8_SetModule(v8::Local context, swig_module_info *swig_module) { + v8::Local global_obj = context->Global(); v8::Local mod = SWIGV8_EXTERNAL_NEW(swig_module); assert(!mod.IsEmpty()); #if (V8_MAJOR_VERSION-0) < 5 global_obj->SetHiddenValue(SWIGV8_STRING_NEW("swig_module_info_data"), mod); #else v8::Local privateKey = v8::Private::ForApi(v8::Isolate::GetCurrent(), SWIGV8_STRING_NEW("swig_module_info_data")); - global_obj->SetPrivate(SWIGV8_CURRENT_CONTEXT(), privateKey, mod); + global_obj->SetPrivate(context, privateKey, mod); #endif } SWIGRUNTIME swig_module_info * -SWIG_V8_GetModule(void *) { - v8::Local global_obj = SWIGV8_CURRENT_CONTEXT()->Global(); +SWIG_V8_GetModule(v8::Local context) { + v8::Local global_obj = context->Global(); #if (V8_MAJOR_VERSION-0) < 5 v8::Local moduleinfo = global_obj->GetHiddenValue(SWIGV8_STRING_NEW("swig_module_info_data")); #else v8::Local privateKey = v8::Private::ForApi(v8::Isolate::GetCurrent(), SWIGV8_STRING_NEW("swig_module_info_data")); v8::Local moduleinfo; - if (!global_obj->GetPrivate(SWIGV8_CURRENT_CONTEXT(), privateKey).ToLocal(&moduleinfo)) + if (!global_obj->GetPrivate(context, privateKey).ToLocal(&moduleinfo)) return 0; #endif @@ -52,6 +52,7 @@ SWIG_V8_GetModule(void *) { #define SWIG_GetModule(clientdata) SWIG_V8_GetModule(clientdata) #define SWIG_SetModule(clientdata, pointer) SWIG_V8_SetModule(clientdata, pointer) +#define SWIG_INIT_CLIENT_DATA_TYPE v8::Local %} @@ -64,20 +65,20 @@ SWIG_V8_GetModule(void *) { %} %insert(init) %{ +#if !defined(NODE_MODULE_VERSION) || (NODE_MODULE_VERSION < 12) // 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 (SWIGV8_OBJECT exports) +extern "C" void SWIGV8_INIT (SWIGV8_OBJECT exports_obj) +#elif (NODE_MODULE_VERSION < 64) +void SWIGV8_INIT (SWIGV8_OBJECT exports_obj, SWIGV8_VALUE /*module*/, void*) #else -void SWIGV8_INIT (SWIGV8_OBJECT exports, SWIGV8_OBJECT /*module*/) +void SWIGV8_INIT (SWIGV8_OBJECT exports_obj, SWIGV8_VALUE /*module*/, v8::Local context, void*) #endif { - SWIG_InitializeModule(static_cast(&exports)); +#if !defined(NODE_MODULE_VERSION) || NODE_MODULE_VERSION < 64 + v8::Local context = SWIGV8_CURRENT_CONTEXT(); +#endif - SWIGV8_HANDLESCOPE(); - - SWIGV8_OBJECT exports_obj = exports; + SWIG_InitializeModule(context); %} @@ -124,6 +125,10 @@ void SWIGV8_INIT (SWIGV8_OBJECT exports, SWIGV8_OBJECT /*module*/) } #if defined(BUILDING_NODE_EXTENSION) +#if (NODE_MODULE_VERSION < 64) NODE_MODULE($jsname, $jsname_initialize) +#else +NODE_MODULE_CONTEXT_AWARE($jsname, $jsname_initialize) +#endif #endif %} diff --git a/Lib/swiginit.swg b/Lib/swiginit.swg index 33926b10f..e50b1b46d 100644 --- a/Lib/swiginit.swg +++ b/Lib/swiginit.swg @@ -50,9 +50,12 @@ extern "C" { #define SWIGRUNTIME_DEBUG #endif +#ifndef SWIG_INIT_CLIENT_DATA_TYPE +#define SWIG_INIT_CLIENT_DATA_TYPE void * +#endif SWIGRUNTIME void -SWIG_InitializeModule(void *clientdata) { +SWIG_InitializeModule(SWIG_INIT_CLIENT_DATA_TYPE clientdata) { size_t i; swig_module_info *module_head, *iter; int init;