Merge branch 'v8-context-aware-race-free-preparation'

* v8-context-aware-race-free-preparation:
  Lib/javascript/v8: use ::Cast instead of To* when possible.
  Lib/javascript/v8/javascriptrun.swg: clean up pre-processor conditions.
  Lib/javascript/jsc/javascriptinit.swg: shortcut JSGlobalContextRef casts.
  Examples/test-suite/grouping.i: resolve compiler warning.
This commit is contained in:
William S Fulton 2021-03-11 22:48:38 +00:00
commit 3f391478c4
7 changed files with 39 additions and 50 deletions

View file

@ -89,19 +89,24 @@ $ swig -javascript -jsc example.i</pre>
<pre> <pre>
$ swig -c++ -javascript -jsc example.i</pre> $ swig -c++ -javascript -jsc example.i</pre>
</div> </div>
<p>The V8 code that SWIG generates should work with most versions from 3.11.10 up to 3.29.14 and later.</p> <p>The V8 code that SWIG generates should work with most versions from 3.11.10.
<p>The API headers for V8 &gt;= 4.3.0 define constants which SWIG can use to However, the only early version that receives some testing is 3.14.5, which is
determine the V8 version it is compiling for. For versions &lt; 4.3.0, you still shipped with Ubuntu for some reason. Other than that it's probably safer
to assume that versions earlier than 5.0 are no longer supported. Keep in mind
that these are V8 versions, not Node.js. To give some perspective, Node.js v6.0
uses V8 5.0, v12.0 - 7.4, v14.0 - 8.1...</p>
<p>The API headers for V8 &gt;= 4.3.10 define constants which SWIG can use to
determine the V8 version it is compiling for. For versions &lt; 4.3.10, you
need to specify the V8 version when running SWIG. This is specified as a hex need to specify the V8 version when running SWIG. This is specified as a hex
constant, but the constant is read as pairs of decimal digits, so for V8 constant, but the constant is read as pairs of decimal digits, so for V8
3.25.30 use constant 0x032530. This scheme can't represent components &gt; 99, 3.25.30 use constant 0x032530. This scheme can't represent components &gt; 99,
but this constant is only useful for V8 &lt; 4.3.0, and no V8 versions from but this constant is only useful for V8 &lt; 4.3.10, and no V8 versions from
that era had a component &gt; 99. For example:</p> that era had a component &gt; 99. For example:</p>
<div class="shell"> <div class="shell">
<pre> <pre>
$ swig -c++ -javascript -v8 -DV8_VERSION=0x032530 example.i</pre> $ swig -c++ -javascript -v8 -DV8_VERSION=0x032530 example.i</pre>
</div> </div>
<p>If you're targeting V8 &gt;= 4.3.0, you would just run swig like so:</p> <p>If you're targeting V8 &gt;= 4.3.10, you would just run swig like so:</p>
<div class="shell"> <div class="shell">
<pre> <pre>
$ swig -c++ -javascript -v8 example.i</pre> $ swig -c++ -javascript -v8 example.i</pre>

View file

@ -14,7 +14,7 @@ int *(test2)(int x) {
return &y; return &y;
} }
int (test3) = 37; int test3 = 37;
typedef Integer (UnaryOp)(Integer); typedef Integer (UnaryOp)(Integer);

View file

@ -1,19 +1,16 @@
%insert(init) %{ %insert(init) %{
SWIGRUNTIME void SWIGRUNTIME void
SWIG_JSC_SetModule(void *clientdata, swig_module_info *swig_module) { SWIG_JSC_SetModule(JSGlobalContextRef context, swig_module_info *swig_module) {
JSGlobalContextRef context;
JSObjectRef globalObject; JSObjectRef globalObject;
JSStringRef moduleName; JSStringRef moduleName;
JSClassDefinition classDef; JSClassDefinition classDef;
JSClassRef classRef; JSClassRef classRef;
JSObjectRef object; JSObjectRef object;
if(clientdata == 0){ if(context == 0){
return; return;
} }
context = (JSGlobalContextRef)clientdata;
globalObject = JSContextGetGlobalObject(context); globalObject = JSContextGetGlobalObject(context);
moduleName = JSStringCreateWithUTF8CString("swig_module_info_data"); moduleName = JSStringCreateWithUTF8CString("swig_module_info_data");
@ -29,19 +26,16 @@ SWIG_JSC_SetModule(void *clientdata, swig_module_info *swig_module) {
JSStringRelease(moduleName); JSStringRelease(moduleName);
} }
SWIGRUNTIME swig_module_info * SWIGRUNTIME swig_module_info *
SWIG_JSC_GetModule(void *clientdata) { SWIG_JSC_GetModule(JSGlobalContextRef context) {
JSGlobalContextRef context;
JSObjectRef globalObject; JSObjectRef globalObject;
JSStringRef moduleName; JSStringRef moduleName;
JSValueRef value; JSValueRef value;
JSObjectRef object; JSObjectRef object;
if(clientdata == 0){ if(context == 0){
return 0; return 0;
} }
context = (JSGlobalContextRef)clientdata;
globalObject = JSContextGetGlobalObject(context); globalObject = JSContextGetGlobalObject(context);
moduleName = JSStringCreateWithUTF8CString("swig_module_info_data"); moduleName = JSStringCreateWithUTF8CString("swig_module_info_data");
@ -59,6 +53,7 @@ SWIG_JSC_GetModule(void *clientdata) {
#define SWIG_GetModule(clientdata) SWIG_JSC_GetModule(clientdata) #define SWIG_GetModule(clientdata) SWIG_JSC_GetModule(clientdata)
#define SWIG_SetModule(clientdata, pointer) SWIG_JSC_SetModule(clientdata, pointer) #define SWIG_SetModule(clientdata, pointer) SWIG_JSC_SetModule(clientdata, pointer)
#define SWIG_INIT_CLIENT_DATA_TYPE JSGlobalContextRef
%} %}
%insert(init) "swiginit.swg" %insert(init) "swiginit.swg"
@ -76,7 +71,7 @@ extern "C" {
#endif #endif
bool SWIGJSC_INIT (JSGlobalContextRef context, JSObjectRef *exports) { bool SWIGJSC_INIT (JSGlobalContextRef context, JSObjectRef *exports) {
SWIG_InitializeModule((void*)context); SWIG_InitializeModule(context);
%} %}
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------

View file

@ -444,7 +444,7 @@ fail:
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%fragment("jsv8_register_class", "templates") %fragment("jsv8_register_class", "templates")
%{ %{
#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903) #if (V8_MAJOR_VERSION-0) < 5
$jsparent_obj->Set(SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj); $jsparent_obj->Set(SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj);
#else #else
SWIGV8_MAYBE_CHECK($jsparent_obj->Set(context, SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj)); SWIGV8_MAYBE_CHECK($jsparent_obj->Set(context, SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj));
@ -469,7 +469,7 @@ fail:
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%fragment("jsv8_register_namespace", "templates") %fragment("jsv8_register_namespace", "templates")
%{ %{
#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903) #if (V8_MAJOR_VERSION-0) < 5
$jsparent_obj->Set(SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj); $jsparent_obj->Set(SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj);
#else #else
SWIGV8_MAYBE_CHECK($jsparent_obj->Set(context, SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj)); SWIGV8_MAYBE_CHECK($jsparent_obj->Set(context, SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj));

View file

@ -62,7 +62,7 @@ SWIGRUNTIME void SWIGV8_AddMemberVariable(SWIGV8_FUNCTION_TEMPLATE class_templ,
*/ */
SWIGRUNTIME void SWIGV8_AddStaticFunction(SWIGV8_OBJECT obj, const char* symbol, SWIGRUNTIME void SWIGV8_AddStaticFunction(SWIGV8_OBJECT obj, const char* symbol,
const SwigV8FunctionCallback& _func, v8::Local<v8::Context> context) { const SwigV8FunctionCallback& _func, v8::Local<v8::Context> context) {
#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903) #if (V8_MAJOR_VERSION-0) < 5
obj->Set(SWIGV8_SYMBOL_NEW(symbol), SWIGV8_FUNCTEMPLATE_NEW(_func)->GetFunction()); obj->Set(SWIGV8_SYMBOL_NEW(symbol), SWIGV8_FUNCTEMPLATE_NEW(_func)->GetFunction());
#else #else
SWIGV8_MAYBE_CHECK(obj->Set(context, SWIGV8_SYMBOL_NEW(symbol), SWIGV8_FUNCTEMPLATE_NEW(_func)->GetFunction(context).ToLocalChecked())); SWIGV8_MAYBE_CHECK(obj->Set(context, SWIGV8_SYMBOL_NEW(symbol), SWIGV8_FUNCTEMPLATE_NEW(_func)->GetFunction(context).ToLocalChecked()));

View file

@ -50,21 +50,22 @@ typedef v8::PropertyCallbackInfo<v8::Value> SwigV8PropertyCallbackInfo;
#define SWIGV8_THROW_EXCEPTION(err) v8::ThrowException(err) #define SWIGV8_THROW_EXCEPTION(err) v8::ThrowException(err)
#define SWIGV8_STRING_NEW(str) v8::String::New(str) #define SWIGV8_STRING_NEW(str) v8::String::New(str)
#define SWIGV8_SYMBOL_NEW(sym) v8::String::NewSymbol(sym) #define SWIGV8_SYMBOL_NEW(sym) v8::String::NewSymbol(sym)
#elif (SWIG_V8_VERSION < 0x0704)
#define SWIGV8_ADJUST_MEMORY(size) v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(size)
#define SWIGV8_CURRENT_CONTEXT() v8::Isolate::GetCurrent()->GetCurrentContext()
#define SWIGV8_THROW_EXCEPTION(err) v8::Isolate::GetCurrent()->ThrowException(err)
#define SWIGV8_STRING_NEW(str) v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), str, v8::String::kNormalString)
#define SWIGV8_SYMBOL_NEW(sym) v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), sym, v8::String::kNormalString)
#else #else
#define SWIGV8_ADJUST_MEMORY(size) v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(size) #define SWIGV8_ADJUST_MEMORY(size) v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(size)
#define SWIGV8_CURRENT_CONTEXT() v8::Isolate::GetCurrent()->GetCurrentContext() #define SWIGV8_CURRENT_CONTEXT() v8::Isolate::GetCurrent()->GetCurrentContext()
#define SWIGV8_THROW_EXCEPTION(err) v8::Isolate::GetCurrent()->ThrowException(err) #define SWIGV8_THROW_EXCEPTION(err) v8::Isolate::GetCurrent()->ThrowException(err)
#if (SWIG_V8_VERSION < 0x0704)
#define SWIGV8_STRING_NEW(str) v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), str, v8::String::kNormalString)
#define SWIGV8_SYMBOL_NEW(sym) v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), sym, v8::String::kNormalString)
#else
#define SWIGV8_STRING_NEW(str) (v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), str, v8::NewStringType::kNormal)).ToLocalChecked() #define SWIGV8_STRING_NEW(str) (v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), str, v8::NewStringType::kNormal)).ToLocalChecked()
#define SWIGV8_SYMBOL_NEW(sym) (v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), sym, v8::NewStringType::kNormal)).ToLocalChecked() #define SWIGV8_SYMBOL_NEW(sym) (v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), sym, v8::NewStringType::kNormal)).ToLocalChecked()
#endif #endif
#endif
#if (SWIG_V8_VERSION < 0x0704) #if (V8_MAJOR_VERSION-0) < 5
#define SWIGV8_MAYBE_CHECK(maybe) maybe
#elif (SWIG_V8_VERSION < 0x0704)
#define SWIGV8_MAYBE_CHECK(maybe) maybe.FromJust() #define SWIGV8_MAYBE_CHECK(maybe) maybe.FromJust()
#else #else
#define SWIGV8_MAYBE_CHECK(maybe) maybe.Check() #define SWIGV8_MAYBE_CHECK(maybe) maybe.Check()
@ -120,15 +121,7 @@ typedef v8::PropertyCallbackInfo<v8::Value> SwigV8PropertyCallbackInfo;
#define SWIGV8_SET_CLASS_TEMPL(class_templ, class) class_templ.Reset(v8::Isolate::GetCurrent(), class); #define SWIGV8_SET_CLASS_TEMPL(class_templ, class) class_templ.Reset(v8::Isolate::GetCurrent(), class);
#endif #endif
#ifdef NODE_VERSION #if (V8_MAJOR_VERSION-0) < 6 || (SWIG_V8_VERSION < 0x0608)
#if NODE_VERSION_AT_LEAST(10, 12, 0)
#define SWIG_NODE_AT_LEAST_1012
#endif
#endif
//Necessary to check Node.js version because V8 API changes are backported in Node.js
#if (defined(NODE_VERSION) && !defined(SWIG_NODE_AT_LEAST_1012)) || \
(!defined(NODE_VERSION) && (V8_MAJOR_VERSION-0) < 7)
#define SWIGV8_TO_OBJECT(handle) (handle)->ToObject() #define SWIGV8_TO_OBJECT(handle) (handle)->ToObject()
#define SWIGV8_TO_STRING(handle) (handle)->ToString() #define SWIGV8_TO_STRING(handle) (handle)->ToString()
#define SWIGV8_NUMBER_VALUE(handle) (handle)->NumberValue() #define SWIGV8_NUMBER_VALUE(handle) (handle)->NumberValue()
@ -136,22 +129,18 @@ typedef v8::PropertyCallbackInfo<v8::Value> SwigV8PropertyCallbackInfo;
#define SWIGV8_BOOLEAN_VALUE(handle) (handle)->BooleanValue() #define SWIGV8_BOOLEAN_VALUE(handle) (handle)->BooleanValue()
#define SWIGV8_WRITE_UTF8(handle, buffer, len) (handle)->WriteUtf8(buffer, len) #define SWIGV8_WRITE_UTF8(handle, buffer, len) (handle)->WriteUtf8(buffer, len)
#define SWIGV8_UTF8_LENGTH(handle) (handle)->Utf8Length() #define SWIGV8_UTF8_LENGTH(handle) (handle)->Utf8Length()
#elif (SWIG_V8_VERSION < 0x0704)
#define SWIGV8_TO_OBJECT(handle) (handle)->ToObject(SWIGV8_CURRENT_CONTEXT()).ToLocalChecked()
#define SWIGV8_TO_STRING(handle) (handle)->ToString(SWIGV8_CURRENT_CONTEXT()).ToLocalChecked()
#define SWIGV8_NUMBER_VALUE(handle) (handle)->NumberValue(SWIGV8_CURRENT_CONTEXT()).ToChecked()
#define SWIGV8_INTEGER_VALUE(handle) (handle)->IntegerValue(SWIGV8_CURRENT_CONTEXT()).ToChecked()
#define SWIGV8_BOOLEAN_VALUE(handle) (handle)->BooleanValue(SWIGV8_CURRENT_CONTEXT()).ToChecked()
#define SWIGV8_WRITE_UTF8(handle, buffer, len) (handle)->WriteUtf8(v8::Isolate::GetCurrent(), buffer, len)
#define SWIGV8_UTF8_LENGTH(handle) (handle)->Utf8Length(v8::Isolate::GetCurrent())
#else #else
#define SWIGV8_TO_OBJECT(handle) (handle)->ToObject(SWIGV8_CURRENT_CONTEXT()).ToLocalChecked() #define SWIGV8_TO_OBJECT(handle) (handle)->ToObject(SWIGV8_CURRENT_CONTEXT()).ToLocalChecked()
#define SWIGV8_TO_STRING(handle) (handle)->ToString(SWIGV8_CURRENT_CONTEXT()).ToLocalChecked() #define SWIGV8_TO_STRING(handle) (handle)->ToString(SWIGV8_CURRENT_CONTEXT()).ToLocalChecked()
#define SWIGV8_NUMBER_VALUE(handle) (handle)->NumberValue(SWIGV8_CURRENT_CONTEXT()).ToChecked() #define SWIGV8_NUMBER_VALUE(handle) (handle)->NumberValue(SWIGV8_CURRENT_CONTEXT()).ToChecked()
#define SWIGV8_INTEGER_VALUE(handle) (handle)->IntegerValue(SWIGV8_CURRENT_CONTEXT()).ToChecked() #define SWIGV8_INTEGER_VALUE(handle) (handle)->IntegerValue(SWIGV8_CURRENT_CONTEXT()).ToChecked()
#define SWIGV8_BOOLEAN_VALUE(handle) (handle)->BooleanValue(v8::Isolate::GetCurrent())
#define SWIGV8_WRITE_UTF8(handle, buffer, len) (handle)->WriteUtf8(v8::Isolate::GetCurrent(), buffer, len) #define SWIGV8_WRITE_UTF8(handle, buffer, len) (handle)->WriteUtf8(v8::Isolate::GetCurrent(), buffer, len)
#define SWIGV8_UTF8_LENGTH(handle) (handle)->Utf8Length(v8::Isolate::GetCurrent()) #define SWIGV8_UTF8_LENGTH(handle) (handle)->Utf8Length(v8::Isolate::GetCurrent())
#if (SWIG_V8_VERSION < 0x0704)
#define SWIGV8_BOOLEAN_VALUE(handle) (handle)->BooleanValue(SWIGV8_CURRENT_CONTEXT()).ToChecked()
#else
#define SWIGV8_BOOLEAN_VALUE(handle) (handle)->BooleanValue(v8::Isolate::GetCurrent())
#endif
#endif #endif
/* --------------------------------------------------------------------------- /* ---------------------------------------------------------------------------
@ -326,7 +315,7 @@ SWIGRUNTIME int SWIG_V8_GetInstancePtr(SWIGV8_VALUE valRef, void **ptr) {
if(!valRef->IsObject()) { if(!valRef->IsObject()) {
return SWIG_TypeError; return SWIG_TypeError;
} }
SWIGV8_OBJECT objRef = SWIGV8_TO_OBJECT(valRef); SWIGV8_OBJECT objRef = SWIGV8_OBJECT::Cast(valRef);
if(objRef->InternalFieldCount() < 1) return SWIG_ERROR; if(objRef->InternalFieldCount() < 1) return SWIG_ERROR;
@ -421,7 +410,7 @@ SWIGRUNTIME int SWIG_V8_ConvertPtr(SWIGV8_VALUE valRef, void **ptr, swig_type_in
if(!valRef->IsObject()) { if(!valRef->IsObject()) {
return SWIG_TypeError; return SWIG_TypeError;
} }
SWIGV8_OBJECT objRef = SWIGV8_TO_OBJECT(valRef); SWIGV8_OBJECT objRef = SWIGV8_OBJECT::Cast(valRef);
return SWIG_V8_ConvertInstancePtr(objRef, ptr, info, flags); return SWIG_V8_ConvertInstancePtr(objRef, ptr, info, flags);
} }

View file

@ -8,9 +8,9 @@ SWIG_AsCharPtrAndSize(SWIGV8_VALUE valRef, char** cptr, size_t* psize, int *allo
{ {
if(valRef->IsString()) { if(valRef->IsString()) {
%#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903) %#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903)
v8::Handle<v8::String> js_str = SWIGV8_TO_STRING(valRef); v8::Handle<v8::String> js_str = v8::Handle<v8::String>::Cast(valRef);
%#else %#else
v8::Local<v8::String> js_str = SWIGV8_TO_STRING(valRef); v8::Local<v8::String> js_str = v8::Local<v8::String>::Cast(valRef);
%#endif %#endif
size_t len = SWIGV8_UTF8_LENGTH(js_str) + 1; size_t len = SWIGV8_UTF8_LENGTH(js_str) + 1;
@ -24,7 +24,7 @@ SWIG_AsCharPtrAndSize(SWIGV8_VALUE valRef, char** cptr, size_t* psize, int *allo
return SWIG_OK; return SWIG_OK;
} else { } else {
if(valRef->IsObject()) { if(valRef->IsObject()) {
SWIGV8_OBJECT obj = SWIGV8_TO_OBJECT(valRef); SWIGV8_OBJECT obj = SWIGV8_OBJECT::Cast(valRef);
// try if the object is a wrapped char[] // try if the object is a wrapped char[]
swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); swig_type_info* pchar_descriptor = SWIG_pchar_descriptor();
if (pchar_descriptor) { if (pchar_descriptor) {