#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>
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);
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 */
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 };
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);
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());
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);
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;
// 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();
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();
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());
#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;
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 *>();
extenderClasses->clear();
extenderFunctions->clear();
+#if defined(V8_MAJOR_VERSION) && V8_MAJOR_VERSION >=5
+ if (isolate) {
+#else
if (!Isolate::GetCurrent()) {
+#endif
enteredIsolate = true;
isolate->Enter();
}
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());
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);
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