Browse Source

fix: update to per context v8::Script::Compile api

deepak1556 6 years ago
parent
commit
66349261a5

+ 15 - 12
atom/common/api/atom_api_asar.cc

@@ -122,20 +122,23 @@ void InitAsarSupport(v8::Isolate* isolate,
                      v8::Local<v8::Value> process,
                      v8::Local<v8::Value> 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<v8::Context> context(isolate->GetCurrentContext());
+  auto maybe_asar_init = v8::Script::Compile(
+      context, node::asar_init_value.ToStringChecked(isolate));
+  v8::Local<v8::Script> asar_init;
+  v8::Local<v8::Value> result;
+  if (maybe_asar_init.ToLocal(&asar_init))
+    result = asar_init->Run(context).ToLocalChecked();
 
   // Initialize asar support.
-  if (result->IsFunction()) {
-    v8::Local<v8::Value> args[] = {
-        process,
-        require,
-        node::asar_value.ToStringChecked(isolate),
-    };
-    result.As<v8::Function>()->Call(result, 3, args);
-  }
+  CHECK(result->IsFunction());
+
+  v8::Local<v8::Value> args[] = {
+      process,
+      require,
+      node::asar_value.ToStringChecked(isolate),
+  };
+  result.As<v8::Function>()->Call(result, 3, args);
 }
 
 void Initialize(v8::Local<v8::Object> exports,

+ 7 - 4
atom/renderer/atom_renderer_client.cc

@@ -191,6 +191,8 @@ void AtomRendererClient::SetupMainWorldOverrides(
     v8::Handle<v8::Context> 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<v8::Function>::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<v8::Value> args[] = {binding};
-  ignore_result(func->Call(context, v8::Null(isolate), 1, args));
+  ignore_result(
+      result.As<v8::Function>()->Call(context, v8::Null(isolate), 1, args));
 }
 
 node::Environment* AtomRendererClient::GetEnvironment(

+ 6 - 14
atom/renderer/atom_sandboxed_renderer_client.cc

@@ -85,14 +85,6 @@ v8::Local<v8::Value> GetBinding(v8::Isolate* isolate,
   return exports;
 }
 
-v8::Local<v8::Value> CreatePreloadScript(v8::Isolate* isolate,
-                                         v8::Local<v8::String> 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<v8::Function>::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<v8::Value> args[] = {binding};
   // Execute the function with proper arguments
-  ignore_result(
-      func->Call(context, v8::Null(isolate), node::arraysize(args), args));
+  ignore_result(result.As<v8::Function>()->Call(context, v8::Null(isolate),
+                                                node::arraysize(args), args));
 }
 
 void AtomSandboxedRendererClient::WillReleaseScriptContext(

+ 9 - 0
atom/renderer/renderer_client_base.cc

@@ -278,4 +278,13 @@ v8::Local<v8::Context> RendererClientBase::GetContext(
     return frame->MainWorldScriptContext();
 }
 
+v8::Local<v8::Value> RunScript(v8::Local<v8::Context> context,
+                               v8::Local<v8::String> source) {
+  auto maybe_script = v8::Script::Compile(context, source);
+  v8::Local<v8::Script> script;
+  if (!maybe_script.ToLocal(&script))
+    return v8::Local<v8::Value>();
+  return script->Run(context).ToLocalChecked();
+}
+
 }  // namespace atom

+ 3 - 0
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<v8::Context> GetContext(blink::WebLocalFrame* frame,
                                     v8::Isolate* isolate) const;
+  // Executes a given v8 Script
+  inline v8::Local<v8::Value> RunScript(v8::Local<v8::Context> context,
+                                        v8::Local<v8::String> source);
 
  protected:
   void AddRenderBindings(v8::Isolate* isolate,