From 3d63f9113bf6bd805f763a2efbe879e7ce08f992 Mon Sep 17 00:00:00 2001 From: Richard Date: Tue, 12 Aug 2014 22:25:32 -0500 Subject: [PATCH] Fixes for v8 3.19.18 (0x031918) --- Lib/javascript/v8/javascriptrun.swg | 12 +++- Tools/javascript/v8_shell.cxx | 104 ++++++++++++++-------------- 2 files changed, 63 insertions(+), 53 deletions(-) diff --git a/Lib/javascript/v8/javascriptrun.swg b/Lib/javascript/v8/javascriptrun.swg index dd5c9784e..50e4c5b87 100644 --- a/Lib/javascript/v8/javascriptrun.swg +++ b/Lib/javascript/v8/javascriptrun.swg @@ -214,12 +214,18 @@ void SWIGV8_SetPrivateData(v8::Handle obj, void* ptr, swig_type_info } else { cdata->handle.MakeWeak(cdata, SWIGV8_Proxy_DefaultDtor); } -#else +#elif (SWIG_V8_VERSION < 0x031918) if(cdata->swigCMemOwn && (SWIGV8_ClientData*)info->clientdata) { cdata->handle.MakeWeak(v8::Isolate::GetCurrent(), cdata, ((SWIGV8_ClientData*)info->clientdata)->dtor); } else { cdata->handle.MakeWeak(v8::Isolate::GetCurrent(), cdata, SWIGV8_Proxy_DefaultDtor); } +#else + if(cdata->swigCMemOwn && (SWIGV8_ClientData*)info->clientdata) { + cdata->handle.MakeWeak(cdata, ((SWIGV8_ClientData*)info->clientdata)->dtor); + } else { + cdata->handle.MakeWeak(cdata, SWIGV8_Proxy_DefaultDtor); + } #endif #if (SWIG_V8_VERSION < 0x031710) @@ -463,8 +469,10 @@ v8::Handle SWIGV8_NewPackedObj(void *data, size_t size, swig_type_inf #if (SWIG_V8_VERSION < 0x031710) cdata->handle.MakeWeak(cdata, _wrap_SwigV8PackedData_delete); -#else +#elif (SWIG_V8_VERSION < 0x031918) cdata->handle.MakeWeak(v8::Isolate::GetCurrent(), cdata, _wrap_SwigV8PackedData_delete); +#else + cdata->handle.MakeWeak(cdata, _wrap_SwigV8PackedData_delete); #endif #if (SWIG_V8_VERSION < 0x031710) diff --git a/Tools/javascript/v8_shell.cxx b/Tools/javascript/v8_shell.cxx index 5148d32d8..cf3836369 100644 --- a/Tools/javascript/v8_shell.cxx +++ b/Tools/javascript/v8_shell.cxx @@ -35,7 +35,11 @@ private: v8::Handle Import(const std::string& moduleName); +#if (V8_VERSION < 0x031900) v8::Persistent CreateShellContext(); +#else + v8::Local CreateShellContext(); +#endif void ReportException(v8::TryCatch* handler); @@ -49,11 +53,6 @@ private: static const char* ToCString(const v8::String::Utf8Value& value); - virtual bool _ExecuteScript(const std::string& source, const std::string& scriptPath); - -protected: - - v8::Persistent context; }; #ifdef __GNUC__ @@ -68,47 +67,25 @@ V8Shell::V8Shell(){} V8Shell::~V8Shell() {} bool V8Shell::RunScript(const std::string& scriptPath) { + std::string source = ReadFile(scriptPath); - if (!context.IsEmpty()) { -#if (V8_VERSION < 0x031710) - context.Dispose(); -#elif (V8_VERSION < 0x031900) - context.Dispose(v8::Isolate::GetCurrent()); + v8::HandleScope scope; + +#if (V8_VERSION < 0x031900) + v8::Persistent context = CreateShellContext(); #else - context.Dispose(); + v8::Local context = CreateShellContext(); #endif - } - std::string source = ReadFile(scriptPath); - - context = CreateShellContext(); if (context.IsEmpty()) { printf("Could not create context.\n"); return false; } - context->Enter(); - bool success = _ExecuteScript(source, scriptPath); - - context->Exit(); - -#if (V8_VERSION < 0x031710) - context.Dispose(); -#elif (V8_VERSION < 0x031900) - context.Dispose(v8::Isolate::GetCurrent()); -#else - context.Dispose(); -#endif - - v8::V8::Dispose(); - - return true; -} - -bool V8Shell::_ExecuteScript(const std::string& source, const std::string& scriptPath) { - v8::HandleScope scope; + context->Enter(); // Store a pointer to this shell for later use + v8::Handle global = context->Global(); v8::Local __shell__ = v8::External::New((void*) (long) this); global->SetHiddenValue(v8::String::New("__shell__"), __shell__); @@ -116,26 +93,34 @@ bool V8Shell::_ExecuteScript(const std::string& source, const std::string& scrip // Node.js compatibility: make `print` available as `console.log()` ExecuteScript("var console = {}; console.log = print;", ""); - if(!ExecuteScript(source, scriptPath)) { - return false; - } + bool success = ExecuteScript(source, scriptPath); - return true; -} + // Cleanup -bool V8Shell::RunShell() { + context->Exit(); - if (!context.IsEmpty()) { #if (V8_VERSION < 0x031710) context.Dispose(); #elif (V8_VERSION < 0x031900) context.Dispose(v8::Isolate::GetCurrent()); #else - context.Dispose(); +// context.Dispose(); +#endif + +// v8::V8::Dispose(); + + return success; +} + +bool V8Shell::RunShell() { + v8::HandleScope scope; + +#if (V8_VERSION < 0x031900) + v8::Persistent context = CreateShellContext(); +#else + v8::Local context = CreateShellContext(); #endif - } - context = CreateShellContext(); if (context.IsEmpty()) { printf("Could not create context.\n"); return false; @@ -158,15 +143,19 @@ bool V8Shell::RunShell() { } printf("\n"); + // Cleanup + context->Exit(); + #if (V8_VERSION < 0x031710) - context.Dispose(); + context.Dispose(); #elif (V8_VERSION < 0x031900) - context.Dispose(v8::Isolate::GetCurrent()); + context.Dispose(v8::Isolate::GetCurrent()); #else - context.Dispose(); +// context.Dispose(); #endif - v8::V8::Dispose(); + +// v8::V8::Dispose(); return true; } @@ -203,7 +192,11 @@ bool V8Shell::DisposeEngine() { return true; } +#if (V8_VERSION < 0x031900) v8::Persistent V8Shell::CreateShellContext() { +#else +v8::Local V8Shell::CreateShellContext() { +#endif v8::HandleScope scope; // Create a template for the global object. @@ -215,9 +208,18 @@ v8::Persistent V8Shell::CreateShellContext() { global->Set(v8::String::New("require"), v8::FunctionTemplate::New(V8Shell::Require)); global->Set(v8::String::New("version"), v8::FunctionTemplate::New(V8Shell::Version)); - v8::Persistent _context = v8::Context::New(NULL, global); - return _context; +#if (V8_VERSION < 0x031900) + v8::Persistent context = v8::Context::New(NULL, global); + return context; +#else +// v8::Local context_ = v8::Context::New(v8::Isolate::GetCurrent(), NULL, global); +// v8::Persistent context; +// context.Reset(v8::Isolate::GetCurrent(), context_); + v8::Local context = v8::Context::New(v8::Isolate::GetCurrent(), NULL, global); + return scope.Close(context); +#endif + } v8::Handle V8Shell::Import(const std::string& module_path)