FS-10435 [mod_v8] Update mod_v8 to support future v8 engine version change
This commit is contained in:
parent
3fef65b7c2
commit
ba32e0e23d
|
@ -33,6 +33,10 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <v8.h>
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
#include <libplatform/libplatform.h>
|
||||
#include <v8-util.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
@ -202,7 +206,11 @@ private:
|
|||
JSMain *js; /* The "owner" of this instance */
|
||||
|
||||
/* The callback that happens when the V8 GC cleans up object instances */
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
static void WeakCallback(const v8::WeakCallbackInfo<JSBase>& data);
|
||||
#else
|
||||
static void WeakCallback(const v8::WeakCallbackData<v8::Object, JSBase>& data);
|
||||
#endif
|
||||
|
||||
/* Internal basic constructor when creating a new instance from JS. It will call the actual user code inside */
|
||||
static void CreateInstance(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
@ -305,7 +313,12 @@ public:
|
|||
const std::string ExecuteScript(const std::string& filename, bool *resultIsError);
|
||||
const std::string ExecuteString(const std::string& scriptData, const std::string& fileName, bool *resultIsError);
|
||||
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
static void Initialize(v8::Platform **platform); /* Initialize the V8 engine */
|
||||
#else
|
||||
static void Initialize(); /* Initialize the V8 engine */
|
||||
#endif
|
||||
|
||||
static void Dispose(); /* Deinitialize the V8 engine */
|
||||
|
||||
static void Include(const v8::FunctionCallbackInfo<v8::Value>& args); /* Adds functionality to include another JavaScript from the running script */
|
||||
|
|
|
@ -120,6 +120,9 @@ typedef struct {
|
|||
switch_event_node_t *event_node;
|
||||
set<FSEventHandler *> *event_handlers;
|
||||
char *xml_handler;
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
v8::Platform *v8platform;
|
||||
#endif
|
||||
} mod_v8_global_t;
|
||||
|
||||
static mod_v8_global_t globals = { 0 };
|
||||
|
@ -651,12 +654,23 @@ static int v8_parse_and_execute(switch_core_session_t *session, const char *inpu
|
|||
free(path);
|
||||
}
|
||||
// Create a string containing the JavaScript source code.
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
ScriptCompiler::Source *source = new ScriptCompiler::Source(String::NewFromUtf8(isolate, script_data));
|
||||
#else
|
||||
Handle<String> source = String::NewFromUtf8(isolate, script_data);
|
||||
#endif
|
||||
|
||||
TryCatch try_catch;
|
||||
|
||||
// Compile the source code.
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
v8::ScriptCompiler::CompileOptions options = v8::ScriptCompiler::kNoCompileOptions;
|
||||
Handle<v8::Script> v8_script = v8::ScriptCompiler::Compile(context, source, options).ToLocalChecked();
|
||||
//Handle<v8::Script> v8_script = v8::ScriptCompiler::Compile(context, source,/* String::NewFromUtf8(isolate, script_file),*/ v8::ScriptCompiler::kProduceCodeCache).ToLocalChecked();
|
||||
//source->GetCachedData();
|
||||
#else
|
||||
Handle<Script> v8_script = Script::Compile(source, Local<Value>::New(isolate, String::NewFromUtf8(isolate, script_file)));
|
||||
#endif
|
||||
|
||||
if (try_catch.HasCaught()) {
|
||||
v8_error(isolate, &try_catch);
|
||||
|
@ -1007,7 +1021,12 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_v8_load)
|
|||
return SWITCH_STATUS_FALSE;
|
||||
}
|
||||
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
globals.v8platform = NULL;
|
||||
JSMain::Initialize(&globals.v8platform);
|
||||
#else
|
||||
JSMain::Initialize();
|
||||
#endif
|
||||
|
||||
/* Make all "built in" modules available to load on demand */
|
||||
v8_mod_init_built_in(FSCoreDB::GetModuleInterface());
|
||||
|
@ -1045,6 +1064,10 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_v8_shutdown)
|
|||
delete globals.event_handlers;
|
||||
switch_mutex_destroy(globals.event_mutex);
|
||||
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
delete globals.v8platform;
|
||||
#endif
|
||||
|
||||
switch_core_hash_destroy(&module_manager.load_hash);
|
||||
switch_core_destroy_memory_pool(&module_manager.pool);
|
||||
|
||||
|
|
|
@ -478,7 +478,11 @@ JS_GLOBAL_FUNCTION_IMPL_STATIC(Include)
|
|||
|
||||
if (js_file.length() > 0) {
|
||||
Handle<String> source = String::NewFromUtf8(info.GetIsolate(), js_file.c_str());
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
Handle<Script> script = Script::Compile(source, info[i]->ToString());
|
||||
#else
|
||||
Handle<Script> script = Script::Compile(source, info[i]);
|
||||
#endif
|
||||
info.GetReturnValue().Set(script->Run());
|
||||
switch_safe_free(path);
|
||||
return;
|
||||
|
|
|
@ -102,18 +102,33 @@ void JSBase::AddInstance(Isolate *isolate, const Handle<Object>& handle, const H
|
|||
|
||||
// Make the handle weak
|
||||
obj->persistentHandle->Reset(isolate, handle);
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
obj->persistentHandle->SetWeak<JSBase>(obj, WeakCallback, WeakCallbackType::kParameter);
|
||||
#else
|
||||
obj->persistentHandle->SetWeak<JSBase>(obj, WeakCallback);
|
||||
#endif
|
||||
obj->persistentHandle->MarkIndependent();
|
||||
}
|
||||
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
void JSBase::WeakCallback(const WeakCallbackInfo<JSBase>& data)
|
||||
#else
|
||||
void JSBase::WeakCallback(const WeakCallbackData<Object, JSBase>& data)
|
||||
#endif
|
||||
{
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
JSBase *wrap = (JSBase*)data.GetParameter();
|
||||
#else
|
||||
JSBase *wrap = data.GetParameter();
|
||||
Local<Object> pobj = data.GetValue();
|
||||
#endif
|
||||
|
||||
if (wrap->autoDestroy) {
|
||||
HandleScope scope(data.GetIsolate());
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
#else
|
||||
assert(pobj == *wrap->persistentHandle);
|
||||
#endif
|
||||
delete wrap;
|
||||
} else if (!wrap->persistentHandle->IsEmpty()) {
|
||||
wrap->persistentHandle->ClearWeak();
|
||||
|
@ -138,7 +153,11 @@ void JSBase::CreateInstance(const v8::FunctionCallbackInfo<Value>& args)
|
|||
autoDestroy = args[1]->BooleanValue();
|
||||
} else {
|
||||
// Create a new C++ instance
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
Handle<External> ex = Handle<External>::Cast(args.Callee()->GetPrivate(args.GetIsolate()->GetCurrentContext(), Private::New(args.GetIsolate(), String::NewFromUtf8(args.GetIsolate(), "constructor_method"))).ToLocalChecked());
|
||||
#else
|
||||
Handle<External> ex = Handle<External>::Cast(args.Callee()->GetHiddenValue(String::NewFromUtf8(args.GetIsolate(), "constructor_method")));
|
||||
#endif
|
||||
|
||||
if (ex->Value()) {
|
||||
ConstructorCallback cb = (ConstructorCallback)ex->Value();
|
||||
|
@ -189,7 +208,11 @@ void JSBase::Register(Isolate *isolate, const js_class_definition_t *desc)
|
|||
function->InstanceTemplate()->SetAccessor(String::NewFromUtf8(isolate, desc->properties[i].name), desc->properties[i].get, desc->properties[i].set);
|
||||
}
|
||||
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
function->GetFunction()->SetPrivate(isolate->GetCurrentContext(), Private::New(isolate, String::NewFromUtf8(isolate, "constructor_method")), External::New(isolate, (void *)desc->constructor));
|
||||
#else
|
||||
function->GetFunction()->SetHiddenValue(String::NewFromUtf8(isolate, "constructor_method"), External::New(isolate, (void *)desc->constructor));
|
||||
#endif
|
||||
|
||||
// Set the function in the global scope, to make it available
|
||||
global->Set(v8::String::NewFromUtf8(isolate, desc->name), function->GetFunction());
|
||||
|
|
|
@ -41,6 +41,9 @@
|
|||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
#include <switch.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
using namespace v8;
|
||||
|
@ -97,7 +100,14 @@ const string JSMain::LoadFileToString(const string& filename)
|
|||
|
||||
JSMain::JSMain(void)
|
||||
{
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
Isolate::CreateParams params;
|
||||
params.array_buffer_allocator =
|
||||
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
||||
isolate = Isolate::New(params);
|
||||
#else
|
||||
isolate = Isolate::New();
|
||||
#endif
|
||||
|
||||
extenderClasses = new vector<const js_class_definition_t *>();
|
||||
extenderFunctions = new vector<js_function_t *>();
|
||||
|
@ -136,7 +146,11 @@ JSMain::~JSMain(void)
|
|||
extenderClasses->clear();
|
||||
extenderFunctions->clear();
|
||||
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
if (isolate) {
|
||||
#else
|
||||
if (!Isolate::GetCurrent()) {
|
||||
#endif
|
||||
enteredIsolate = true;
|
||||
isolate->Enter();
|
||||
}
|
||||
|
@ -216,7 +230,11 @@ void JSMain::Include(const v8::FunctionCallbackInfo<Value>& args)
|
|||
if (js_file.length() > 0) {
|
||||
Handle<String> source = String::NewFromUtf8(args.GetIsolate(), js_file.c_str());
|
||||
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
Handle<Script> script = Script::Compile(source, args[i]->ToString());
|
||||
#else
|
||||
Handle<Script> script = Script::Compile(source, args[i]);
|
||||
#endif
|
||||
|
||||
args.GetReturnValue().Set(script->Run());
|
||||
|
||||
|
@ -302,7 +320,11 @@ const string JSMain::ExecuteString(const string& scriptData, const string& fileN
|
|||
TryCatch try_catch;
|
||||
|
||||
// Compile the source code.
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
Handle<Script> script = Script::Compile(source, String::NewFromUtf8(isolate, fileName.c_str()));
|
||||
#else
|
||||
Handle<Script> script = Script::Compile(source, Local<Value>::New(isolate, String::NewFromUtf8(isolate, fileName.c_str())));
|
||||
#endif
|
||||
|
||||
if (try_catch.HasCaught()) {
|
||||
res = JSMain::GetExceptionInfo(isolate, &try_catch);
|
||||
|
@ -404,18 +426,37 @@ Isolate *JSMain::GetIsolate()
|
|||
return isolate;
|
||||
}
|
||||
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
void JSMain::Initialize(v8::Platform **platform)
|
||||
{
|
||||
bool res = V8::InitializeICUDefaultLocation(SWITCH_GLOBAL_dirs.mod_dir);
|
||||
V8::InitializeExternalStartupData(SWITCH_GLOBAL_dirs.mod_dir);
|
||||
|
||||
*platform = v8::platform::CreateDefaultPlatform();
|
||||
V8::InitializePlatform(*platform);
|
||||
V8::Initialize();
|
||||
}
|
||||
#else
|
||||
void JSMain::Initialize()
|
||||
{
|
||||
V8::InitializeICU(); // Initialize();
|
||||
}
|
||||
#endif
|
||||
|
||||
void JSMain::Dispose()
|
||||
{
|
||||
// Make sure to cleanup properly!
|
||||
#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
|
||||
v8::Isolate::GetCurrent()->LowMemoryNotification();
|
||||
while (!v8::Isolate::GetCurrent()->IdleNotificationDeadline(0.500)) {}
|
||||
V8::Dispose();
|
||||
V8::ShutdownPlatform();
|
||||
#else
|
||||
V8::LowMemoryNotification();
|
||||
while (!V8::IdleNotification()) {}
|
||||
|
||||
V8::Dispose();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
const vector<const js_class_definition_t *>& JSMain::GetExtenderClasses() const
|
||||
|
|
Loading…
Reference in New Issue