From b56814ce08ff27cc4bd195b427dbe43c7b6e354e Mon Sep 17 00:00:00 2001 From: Andy Polyakov Date: Tue, 9 Mar 2021 15:34:31 +0100 Subject: [PATCH] Lib/javascript/v8/javascriptrun.swg: clean up pre-processor conditions. Harmonize javascriptcode.swg javascripthelpers.swg and clarify documentation. --- Doc/Manual/Javascript.html | 15 ++++++---- Lib/javascript/v8/javascriptcode.swg | 4 +-- Lib/javascript/v8/javascripthelpers.swg | 2 +- Lib/javascript/v8/javascriptrun.swg | 39 +++++++++---------------- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/Doc/Manual/Javascript.html b/Doc/Manual/Javascript.html index cce5b5e2e..c031b0561 100644 --- a/Doc/Manual/Javascript.html +++ b/Doc/Manual/Javascript.html @@ -89,19 +89,24 @@ $ swig -javascript -jsc example.i
 $ swig -c++ -javascript -jsc example.i
-

The V8 code that SWIG generates should work with most versions from 3.11.10 up to 3.29.14 and later.

-

The API headers for V8 >= 4.3.0 define constants which SWIG can use to -determine the V8 version it is compiling for. For versions < 4.3.0, you +

The V8 code that SWIG generates should work with most versions from 3.11.10. +However, the only early version that receives some testing is 3.14.5, which is +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...

+

The API headers for V8 >= 4.3.10 define constants which SWIG can use to +determine the V8 version it is compiling for. For versions < 4.3.10, you 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 3.25.30 use constant 0x032530. This scheme can't represent components > 99, -but this constant is only useful for V8 < 4.3.0, and no V8 versions from +but this constant is only useful for V8 < 4.3.10, and no V8 versions from that era had a component > 99. For example:

 $ swig -c++ -javascript -v8 -DV8_VERSION=0x032530 example.i
-

If you're targeting V8 >= 4.3.0, you would just run swig like so:

+

If you're targeting V8 >= 4.3.10, you would just run swig like so:

 $ swig -c++ -javascript -v8 example.i
diff --git a/Lib/javascript/v8/javascriptcode.swg b/Lib/javascript/v8/javascriptcode.swg index 60b1aba3a..2abed9488 100644 --- a/Lib/javascript/v8/javascriptcode.swg +++ b/Lib/javascript/v8/javascriptcode.swg @@ -444,7 +444,7 @@ fail: * ----------------------------------------------------------------------------- */ %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); #else SWIGV8_MAYBE_CHECK($jsparent_obj->Set(context, SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj)); @@ -469,7 +469,7 @@ fail: * ----------------------------------------------------------------------------- */ %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); #else SWIGV8_MAYBE_CHECK($jsparent_obj->Set(context, SWIGV8_SYMBOL_NEW("$jsname"), $jsmangledname_obj)); diff --git a/Lib/javascript/v8/javascripthelpers.swg b/Lib/javascript/v8/javascripthelpers.swg index 9c3296d82..fdbff000e 100644 --- a/Lib/javascript/v8/javascripthelpers.swg +++ b/Lib/javascript/v8/javascripthelpers.swg @@ -62,7 +62,7 @@ SWIGRUNTIME void SWIGV8_AddMemberVariable(SWIGV8_FUNCTION_TEMPLATE class_templ, */ SWIGRUNTIME void SWIGV8_AddStaticFunction(SWIGV8_OBJECT obj, const char* symbol, const SwigV8FunctionCallback& _func, v8::Local 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()); #else SWIGV8_MAYBE_CHECK(obj->Set(context, SWIGV8_SYMBOL_NEW(symbol), SWIGV8_FUNCTEMPLATE_NEW(_func)->GetFunction(context).ToLocalChecked())); diff --git a/Lib/javascript/v8/javascriptrun.swg b/Lib/javascript/v8/javascriptrun.swg index 642908a37..c6e31ff9d 100644 --- a/Lib/javascript/v8/javascriptrun.swg +++ b/Lib/javascript/v8/javascriptrun.swg @@ -50,21 +50,22 @@ typedef v8::PropertyCallbackInfo SwigV8PropertyCallbackInfo; #define SWIGV8_THROW_EXCEPTION(err) v8::ThrowException(err) #define SWIGV8_STRING_NEW(str) v8::String::New(str) #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 #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) +#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_SYMBOL_NEW(sym) (v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), sym, v8::NewStringType::kNormal)).ToLocalChecked() #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() #else #define SWIGV8_MAYBE_CHECK(maybe) maybe.Check() @@ -120,15 +121,7 @@ typedef v8::PropertyCallbackInfo SwigV8PropertyCallbackInfo; #define SWIGV8_SET_CLASS_TEMPL(class_templ, class) class_templ.Reset(v8::Isolate::GetCurrent(), class); #endif -#ifdef NODE_VERSION -#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) +#if (V8_MAJOR_VERSION-0) < 6 || (SWIG_V8_VERSION < 0x0608) #define SWIGV8_TO_OBJECT(handle) (handle)->ToObject() #define SWIGV8_TO_STRING(handle) (handle)->ToString() #define SWIGV8_NUMBER_VALUE(handle) (handle)->NumberValue() @@ -136,22 +129,18 @@ typedef v8::PropertyCallbackInfo SwigV8PropertyCallbackInfo; #define SWIGV8_BOOLEAN_VALUE(handle) (handle)->BooleanValue() #define SWIGV8_WRITE_UTF8(handle, buffer, len) (handle)->WriteUtf8(buffer, len) #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 #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(v8::Isolate::GetCurrent()) #define SWIGV8_WRITE_UTF8(handle, buffer, len) (handle)->WriteUtf8(v8::Isolate::GetCurrent(), buffer, len) #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 /* ---------------------------------------------------------------------------