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

This commit is contained in:
deepak1556 2018-10-25 20:59:13 +05:30
parent c1f32afeeb
commit 66349261a5
5 changed files with 40 additions and 30 deletions

View file

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

View file

@ -191,6 +191,8 @@ void AtomRendererClient::SetupMainWorldOverrides(
v8::Handle<v8::Context> context) { v8::Handle<v8::Context> context) {
// Setup window overrides in the main world context // Setup window overrides in the main world context
v8::Isolate* isolate = context->GetIsolate(); 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 // Wrap the bundle into a function that receives the binding object as
// an argument. // an argument.
@ -200,9 +202,9 @@ void AtomRendererClient::SetupMainWorldOverrides(
mate::ConvertToV8(isolate, left)->ToString(), mate::ConvertToV8(isolate, left)->ToString(),
v8::String::Concat(node::isolated_bundle_value.ToStringChecked(isolate), v8::String::Concat(node::isolated_bundle_value.ToStringChecked(isolate),
mate::ConvertToV8(isolate, right)->ToString())); mate::ConvertToV8(isolate, right)->ToString()));
auto script = v8::Script::Compile(context, source).ToLocalChecked(); auto result = RunScript(context, source);
auto func =
v8::Handle<v8::Function>::Cast(script->Run(context).ToLocalChecked()); CHECK(result->IsFunction());
auto binding = v8::Object::New(isolate); auto binding = v8::Object::New(isolate);
api::Initialize(binding, v8::Null(isolate), context, nullptr); api::Initialize(binding, v8::Null(isolate), context, nullptr);
@ -221,7 +223,8 @@ void AtomRendererClient::SetupMainWorldOverrides(
command_line->HasSwitch(switches::kNativeWindowOpen)); command_line->HasSwitch(switches::kNativeWindowOpen));
v8::Local<v8::Value> args[] = {binding}; 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( node::Environment* AtomRendererClient::GetEnvironment(

View file

@ -85,14 +85,6 @@ v8::Local<v8::Value> GetBinding(v8::Isolate* isolate,
return exports; 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 { class AtomSandboxedRenderFrameObserver : public AtomRenderFrameObserver {
public: public:
AtomSandboxedRenderFrameObserver(content::RenderFrame* render_frame, AtomSandboxedRenderFrameObserver(content::RenderFrame* render_frame,
@ -142,7 +134,7 @@ void AtomSandboxedRendererClient::InitializeBindings(
auto* isolate = context->GetIsolate(); auto* isolate = context->GetIsolate();
mate::Dictionary b(isolate, binding); mate::Dictionary b(isolate, binding);
b.SetMethod("get", GetBinding); b.SetMethod("get", GetBinding);
b.SetMethod("createPreloadScript", CreatePreloadScript); b.SetMethod("createPreloadScript", RunScript);
mate::Dictionary process = mate::Dictionary::CreateEmpty(isolate); mate::Dictionary process = mate::Dictionary::CreateEmpty(isolate);
b.Set("process", process); b.Set("process", process);
@ -195,17 +187,17 @@ void AtomSandboxedRendererClient::DidCreateScriptContext(
mate::ConvertToV8(isolate, left)->ToString(), mate::ConvertToV8(isolate, left)->ToString(),
v8::String::Concat(node::preload_bundle_value.ToStringChecked(isolate), v8::String::Concat(node::preload_bundle_value.ToStringChecked(isolate),
mate::ConvertToV8(isolate, right)->ToString())); mate::ConvertToV8(isolate, right)->ToString()));
auto script = v8::Script::Compile(context, source).ToLocalChecked(); auto result = RunScript(context, source);
auto func =
v8::Handle<v8::Function>::Cast(script->Run(context).ToLocalChecked()); CHECK(result->IsFunction());
// Create and initialize the binding object // Create and initialize the binding object
auto binding = v8::Object::New(isolate); auto binding = v8::Object::New(isolate);
InitializeBindings(binding, context); InitializeBindings(binding, context);
AddRenderBindings(isolate, binding); AddRenderBindings(isolate, binding);
v8::Local<v8::Value> args[] = {binding}; v8::Local<v8::Value> args[] = {binding};
// Execute the function with proper arguments // Execute the function with proper arguments
ignore_result( ignore_result(result.As<v8::Function>()->Call(context, v8::Null(isolate),
func->Call(context, v8::Null(isolate), node::arraysize(args), args)); node::arraysize(args), args));
} }
void AtomSandboxedRendererClient::WillReleaseScriptContext( void AtomSandboxedRendererClient::WillReleaseScriptContext(

View file

@ -278,4 +278,13 @@ v8::Local<v8::Context> RendererClientBase::GetContext(
return frame->MainWorldScriptContext(); 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 } // namespace atom

View file

@ -39,6 +39,9 @@ class RendererClientBase : public content::ContentRendererClient {
// Get the context that the Electron API is running in. // Get the context that the Electron API is running in.
v8::Local<v8::Context> GetContext(blink::WebLocalFrame* frame, v8::Local<v8::Context> GetContext(blink::WebLocalFrame* frame,
v8::Isolate* isolate) const; 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: protected:
void AddRenderBindings(v8::Isolate* isolate, void AddRenderBindings(v8::Isolate* isolate,