refactor: move CompileAndCall to a helper (#20675)

This commit is contained in:
Shelley Vohr
2019-10-23 09:26:32 -07:00
committed by GitHub
parent 5abce7ec08
commit db4d01c517
8 changed files with 86 additions and 76 deletions

View File

@@ -16,8 +16,7 @@
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/native_mate_converters/file_path_converter.h"
#include "shell/common/node_includes.h"
#include "third_party/electron_node/src/node_native_module_env.h"
#include "shell/common/node_util.h"
namespace {
class Archive : public mate::Wrappable<Archive> {
@@ -124,9 +123,9 @@ void InitAsarSupport(v8::Isolate* isolate, v8::Local<v8::Value> require) {
std::vector<v8::Local<v8::String>> asar_init_params = {
node::FIXED_ONE_BYTE_STRING(isolate, "require")};
std::vector<v8::Local<v8::Value>> asar_init_args = {require};
node::native_module::NativeModuleEnv::CompileAndCall(
isolate->GetCurrentContext(), "electron/js2c/asar_init",
&asar_init_params, &asar_init_args, nullptr);
electron::util::CompileAndCall(isolate->GetCurrentContext(),
"electron/js2c/asar_init", &asar_init_params,
&asar_init_args, nullptr);
}
v8::Local<v8::Value> SplitPath(v8::Isolate* isolate,

33
shell/common/node_util.cc Normal file
View File

@@ -0,0 +1,33 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/node_util.h"
#include "shell/common/node_includes.h"
#include "third_party/electron_node/src/node_native_module_env.h"
namespace electron {
namespace util {
v8::MaybeLocal<v8::Value> CompileAndCall(
v8::Local<v8::Context> context,
const char* id,
std::vector<v8::Local<v8::String>>* parameters,
std::vector<v8::Local<v8::Value>>* arguments,
node::Environment* optional_env) {
v8::Isolate* isolate = context->GetIsolate();
v8::MaybeLocal<v8::Function> compiled =
node::native_module::NativeModuleEnv::LookupAndCompile(
context, id, parameters, optional_env);
if (compiled.IsEmpty()) {
return v8::MaybeLocal<v8::Value>();
}
v8::Local<v8::Function> fn = compiled.ToLocalChecked().As<v8::Function>();
return fn->Call(context, v8::Null(isolate), arguments->size(),
arguments->data());
}
} // namespace util
} // namespace electron

36
shell/common/node_util.h Normal file
View File

@@ -0,0 +1,36 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_COMMON_NODE_UTIL_H_
#define SHELL_COMMON_NODE_UTIL_H_
#include <vector>
#include "v8/include/v8.h"
namespace node {
class Environment;
} // namespace node
namespace electron {
namespace util {
// Run a script with JS source bundled inside the binary as if it's wrapped
// in a function called with a null receiver and arguments specified in C++.
// The returned value is empty if an exception is encountered.
// JS code run with this method can assume that their top-level
// declarations won't affect the global scope.
v8::MaybeLocal<v8::Value> CompileAndCall(
v8::Local<v8::Context> context,
const char* id,
std::vector<v8::Local<v8::String>>* parameters,
std::vector<v8::Local<v8::Value>>* arguments,
node::Environment* optional_env);
} // namespace util
} // namespace electron
#endif // SHELL_COMMON_NODE_UTIL_H_

View File

@@ -16,12 +16,12 @@
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/node_bindings.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
#include "shell/common/options_switches.h"
#include "shell/renderer/atom_render_frame_observer.h"
#include "shell/renderer/web_worker_observer.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/electron_node/src/node_native_module_env.h"
namespace electron {
@@ -227,9 +227,8 @@ void AtomRendererClient::SetupMainWorldOverrides(
env->process_object(),
GetContext(render_frame->GetWebFrame(), isolate)->Global()};
node::native_module::NativeModuleEnv::CompileAndCall(
context, "electron/js2c/isolated_bundle", &isolated_bundle_params,
&isolated_bundle_args, nullptr);
util::CompileAndCall(context, "electron/js2c/isolated_bundle",
&isolated_bundle_params, &isolated_bundle_args, nullptr);
}
void AtomRendererClient::SetupExtensionWorldOverrides(
@@ -255,9 +254,8 @@ void AtomRendererClient::SetupExtensionWorldOverrides(
GetContext(render_frame->GetWebFrame(), isolate)->Global(),
v8::Integer::New(isolate, world_id)};
node::native_module::NativeModuleEnv::CompileAndCall(
context, "electron/js2c/content_script_bundle", &isolated_bundle_params,
&isolated_bundle_args, nullptr);
util::CompileAndCall(context, "electron/js2c/content_script_bundle",
&isolated_bundle_params, &isolated_bundle_args, nullptr);
#endif
}

View File

@@ -20,12 +20,12 @@
#include "shell/common/native_mate_converters/value_converter.h"
#include "shell/common/node_bindings.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
#include "shell/common/options_switches.h"
#include "shell/renderer/atom_render_frame_observer.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/electron_node/src/node_binding.h"
#include "third_party/electron_node/src/node_native_module_env.h"
namespace electron {
@@ -231,7 +231,7 @@ void AtomSandboxedRendererClient::DidCreateScriptContext(
std::vector<v8::Local<v8::Value>> sandbox_preload_bundle_args = {binding};
node::native_module::NativeModuleEnv::CompileAndCall(
util::CompileAndCall(
isolate->GetCurrentContext(), "electron/js2c/sandbox_bundle",
&sandbox_preload_bundle_params, &sandbox_preload_bundle_args, nullptr);
@@ -259,9 +259,8 @@ void AtomSandboxedRendererClient::SetupMainWorldOverrides(
process.GetHandle(),
GetContext(render_frame->GetWebFrame(), isolate)->Global()};
node::native_module::NativeModuleEnv::CompileAndCall(
context, "electron/js2c/isolated_bundle", &isolated_bundle_params,
&isolated_bundle_args, nullptr);
util::CompileAndCall(context, "electron/js2c/isolated_bundle",
&isolated_bundle_params, &isolated_bundle_args, nullptr);
}
void AtomSandboxedRendererClient::SetupExtensionWorldOverrides(
@@ -286,9 +285,8 @@ void AtomSandboxedRendererClient::SetupExtensionWorldOverrides(
GetContext(render_frame->GetWebFrame(), isolate)->Global(),
v8::Integer::New(isolate, world_id)};
node::native_module::NativeModuleEnv::CompileAndCall(
context, "electron/js2c/content_script_bundle", &isolated_bundle_params,
&isolated_bundle_args, nullptr);
util::CompileAndCall(context, "electron/js2c/content_script_bundle",
&isolated_bundle_params, &isolated_bundle_args, nullptr);
#endif
}