From 66349261a5d61106578375ab3c68e8629deadf37 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Thu, 25 Oct 2018 20:59:13 +0530 Subject: [PATCH] fix: update to per context v8::Script::Compile api --- atom/common/api/atom_api_asar.cc | 27 ++++++++++--------- atom/renderer/atom_renderer_client.cc | 11 +++++--- .../atom_sandboxed_renderer_client.cc | 20 +++++--------- atom/renderer/renderer_client_base.cc | 9 +++++++ atom/renderer/renderer_client_base.h | 3 +++ 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/atom/common/api/atom_api_asar.cc b/atom/common/api/atom_api_asar.cc index 4ccf799a2203..42693461dc7e 100644 --- a/atom/common/api/atom_api_asar.cc +++ b/atom/common/api/atom_api_asar.cc @@ -122,20 +122,23 @@ void InitAsarSupport(v8::Isolate* isolate, v8::Local process, v8::Local require) { // Evaluate asar_init.js. - auto context = isolate->GetCurrentContext(); - auto source = node::asar_init_value.ToStringChecked(isolate); - auto asar_init = v8::Script::Compile(context, source).ToLocalChecked(); - auto result = asar_init->Run(context).ToLocalChecked(); + v8::Local context(isolate->GetCurrentContext()); + auto maybe_asar_init = v8::Script::Compile( + context, node::asar_init_value.ToStringChecked(isolate)); + v8::Local asar_init; + v8::Local result; + if (maybe_asar_init.ToLocal(&asar_init)) + result = asar_init->Run(context).ToLocalChecked(); // Initialize asar support. - if (result->IsFunction()) { - v8::Local args[] = { - process, - require, - node::asar_value.ToStringChecked(isolate), - }; - result.As()->Call(result, 3, args); - } + CHECK(result->IsFunction()); + + v8::Local args[] = { + process, + require, + node::asar_value.ToStringChecked(isolate), + }; + result.As()->Call(result, 3, args); } void Initialize(v8::Local exports, diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 836ae1b112fc..e886f771509c 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -191,6 +191,8 @@ void AtomRendererClient::SetupMainWorldOverrides( v8::Handle context) { // Setup window overrides in the main world context v8::Isolate* isolate = context->GetIsolate(); + v8::HandleScope handle_scope(isolate); + v8::Context::Scope context_scope(context); // Wrap the bundle into a function that receives the binding object as // an argument. @@ -200,9 +202,9 @@ void AtomRendererClient::SetupMainWorldOverrides( mate::ConvertToV8(isolate, left)->ToString(), v8::String::Concat(node::isolated_bundle_value.ToStringChecked(isolate), mate::ConvertToV8(isolate, right)->ToString())); - auto script = v8::Script::Compile(context, source).ToLocalChecked(); - auto func = - v8::Handle::Cast(script->Run(context).ToLocalChecked()); + auto result = RunScript(context, source); + + CHECK(result->IsFunction()); auto binding = v8::Object::New(isolate); api::Initialize(binding, v8::Null(isolate), context, nullptr); @@ -221,7 +223,8 @@ void AtomRendererClient::SetupMainWorldOverrides( command_line->HasSwitch(switches::kNativeWindowOpen)); v8::Local args[] = {binding}; - ignore_result(func->Call(context, v8::Null(isolate), 1, args)); + ignore_result( + result.As()->Call(context, v8::Null(isolate), 1, args)); } node::Environment* AtomRendererClient::GetEnvironment( diff --git a/atom/renderer/atom_sandboxed_renderer_client.cc b/atom/renderer/atom_sandboxed_renderer_client.cc index a024f4f05c77..ac3cf4101dc6 100644 --- a/atom/renderer/atom_sandboxed_renderer_client.cc +++ b/atom/renderer/atom_sandboxed_renderer_client.cc @@ -85,14 +85,6 @@ v8::Local GetBinding(v8::Isolate* isolate, return exports; } -v8::Local CreatePreloadScript(v8::Isolate* isolate, - v8::Local preloadSrc) { - auto context = isolate->GetCurrentContext(); - auto script = v8::Script::Compile(context, preloadSrc).ToLocalChecked(); - auto func = script->Run(context).ToLocalChecked(); - return func; -} - class AtomSandboxedRenderFrameObserver : public AtomRenderFrameObserver { public: AtomSandboxedRenderFrameObserver(content::RenderFrame* render_frame, @@ -142,7 +134,7 @@ void AtomSandboxedRendererClient::InitializeBindings( auto* isolate = context->GetIsolate(); mate::Dictionary b(isolate, binding); b.SetMethod("get", GetBinding); - b.SetMethod("createPreloadScript", CreatePreloadScript); + b.SetMethod("createPreloadScript", RunScript); mate::Dictionary process = mate::Dictionary::CreateEmpty(isolate); b.Set("process", process); @@ -195,17 +187,17 @@ void AtomSandboxedRendererClient::DidCreateScriptContext( mate::ConvertToV8(isolate, left)->ToString(), v8::String::Concat(node::preload_bundle_value.ToStringChecked(isolate), mate::ConvertToV8(isolate, right)->ToString())); - auto script = v8::Script::Compile(context, source).ToLocalChecked(); - auto func = - v8::Handle::Cast(script->Run(context).ToLocalChecked()); + auto result = RunScript(context, source); + + CHECK(result->IsFunction()); // Create and initialize the binding object auto binding = v8::Object::New(isolate); InitializeBindings(binding, context); AddRenderBindings(isolate, binding); v8::Local args[] = {binding}; // Execute the function with proper arguments - ignore_result( - func->Call(context, v8::Null(isolate), node::arraysize(args), args)); + ignore_result(result.As()->Call(context, v8::Null(isolate), + node::arraysize(args), args)); } void AtomSandboxedRendererClient::WillReleaseScriptContext( diff --git a/atom/renderer/renderer_client_base.cc b/atom/renderer/renderer_client_base.cc index 1e581703fd30..9b26cc7dca49 100644 --- a/atom/renderer/renderer_client_base.cc +++ b/atom/renderer/renderer_client_base.cc @@ -278,4 +278,13 @@ v8::Local RendererClientBase::GetContext( return frame->MainWorldScriptContext(); } +v8::Local RunScript(v8::Local context, + v8::Local source) { + auto maybe_script = v8::Script::Compile(context, source); + v8::Local script; + if (!maybe_script.ToLocal(&script)) + return v8::Local(); + return script->Run(context).ToLocalChecked(); +} + } // namespace atom diff --git a/atom/renderer/renderer_client_base.h b/atom/renderer/renderer_client_base.h index 40991de2c5c2..6ff87d9e7081 100644 --- a/atom/renderer/renderer_client_base.h +++ b/atom/renderer/renderer_client_base.h @@ -39,6 +39,9 @@ class RendererClientBase : public content::ContentRendererClient { // Get the context that the Electron API is running in. v8::Local GetContext(blink::WebLocalFrame* frame, v8::Isolate* isolate) const; + // Executes a given v8 Script + inline v8::Local RunScript(v8::Local context, + v8::Local source); protected: void AddRenderBindings(v8::Isolate* isolate,