Skip to content

Commit db4d01c

Browse files
authored
refactor: move CompileAndCall to a helper (electron#20675)
1 parent 5abce7e commit db4d01c

File tree

8 files changed

+86
-76
lines changed

8 files changed

+86
-76
lines changed

filenames.gni

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ filenames = {
545545
"shell/common/node_bindings_win.cc",
546546
"shell/common/node_bindings_win.h",
547547
"shell/common/node_includes.h",
548+
"shell/common/node_util.h",
549+
"shell/common/node_util.cc",
548550
"shell/common/options_switches.cc",
549551
"shell/common/options_switches.h",
550552
"shell/common/platform_util.h",

patches/node/.patches

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ chore_prevent_warn_non_context-aware_native_modules_being_loaded.patch
2828
chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch
2929
inherit_electron_crashpad_pipe_name_in_child_process.patch
3030
fixme_revert_crypto_add_support_for_rsa-pss_keys.patch
31-
chore_re-add_compileandcall_this_should_be_added_as_a_helper_in.patch
3231
fix_extern_the_nativemoduleenv_and_options_parser_for_debug_builds.patch
3332
chore_read_nobrowserglobals_from_global_not_process.patch
3433
chore_split_createenvironment_into_createenvironment_and.patch

patches/node/chore_re-add_compileandcall_this_should_be_added_as_a_helper_in.patch

Lines changed: 0 additions & 55 deletions
This file was deleted.

shell/common/api/atom_api_asar.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
#include "shell/common/gin_helper/dictionary.h"
1717
#include "shell/common/native_mate_converters/file_path_converter.h"
1818
#include "shell/common/node_includes.h"
19-
#include "third_party/electron_node/src/node_native_module_env.h"
20-
19+
#include "shell/common/node_util.h"
2120
namespace {
2221

2322
class Archive : public mate::Wrappable<Archive> {
@@ -124,9 +123,9 @@ void InitAsarSupport(v8::Isolate* isolate, v8::Local<v8::Value> require) {
124123
std::vector<v8::Local<v8::String>> asar_init_params = {
125124
node::FIXED_ONE_BYTE_STRING(isolate, "require")};
126125
std::vector<v8::Local<v8::Value>> asar_init_args = {require};
127-
node::native_module::NativeModuleEnv::CompileAndCall(
128-
isolate->GetCurrentContext(), "electron/js2c/asar_init",
129-
&asar_init_params, &asar_init_args, nullptr);
126+
electron::util::CompileAndCall(isolate->GetCurrentContext(),
127+
"electron/js2c/asar_init", &asar_init_params,
128+
&asar_init_args, nullptr);
130129
}
131130

132131
v8::Local<v8::Value> SplitPath(v8::Isolate* isolate,

shell/common/node_util.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2019 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#include "shell/common/node_util.h"
6+
#include "shell/common/node_includes.h"
7+
#include "third_party/electron_node/src/node_native_module_env.h"
8+
9+
namespace electron {
10+
11+
namespace util {
12+
13+
v8::MaybeLocal<v8::Value> CompileAndCall(
14+
v8::Local<v8::Context> context,
15+
const char* id,
16+
std::vector<v8::Local<v8::String>>* parameters,
17+
std::vector<v8::Local<v8::Value>>* arguments,
18+
node::Environment* optional_env) {
19+
v8::Isolate* isolate = context->GetIsolate();
20+
v8::MaybeLocal<v8::Function> compiled =
21+
node::native_module::NativeModuleEnv::LookupAndCompile(
22+
context, id, parameters, optional_env);
23+
if (compiled.IsEmpty()) {
24+
return v8::MaybeLocal<v8::Value>();
25+
}
26+
v8::Local<v8::Function> fn = compiled.ToLocalChecked().As<v8::Function>();
27+
return fn->Call(context, v8::Null(isolate), arguments->size(),
28+
arguments->data());
29+
}
30+
31+
} // namespace util
32+
33+
} // namespace electron

shell/common/node_util.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2019 GitHub, Inc.
2+
// Use of this source code is governed by the MIT license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef SHELL_COMMON_NODE_UTIL_H_
6+
#define SHELL_COMMON_NODE_UTIL_H_
7+
8+
#include <vector>
9+
10+
#include "v8/include/v8.h"
11+
12+
namespace node {
13+
class Environment;
14+
} // namespace node
15+
16+
namespace electron {
17+
18+
namespace util {
19+
20+
// Run a script with JS source bundled inside the binary as if it's wrapped
21+
// in a function called with a null receiver and arguments specified in C++.
22+
// The returned value is empty if an exception is encountered.
23+
// JS code run with this method can assume that their top-level
24+
// declarations won't affect the global scope.
25+
v8::MaybeLocal<v8::Value> CompileAndCall(
26+
v8::Local<v8::Context> context,
27+
const char* id,
28+
std::vector<v8::Local<v8::String>>* parameters,
29+
std::vector<v8::Local<v8::Value>>* arguments,
30+
node::Environment* optional_env);
31+
32+
} // namespace util
33+
34+
} // namespace electron
35+
36+
#endif // SHELL_COMMON_NODE_UTIL_H_

shell/renderer/atom_renderer_client.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
#include "shell/common/gin_helper/event_emitter_caller.h"
1717
#include "shell/common/node_bindings.h"
1818
#include "shell/common/node_includes.h"
19+
#include "shell/common/node_util.h"
1920
#include "shell/common/options_switches.h"
2021
#include "shell/renderer/atom_render_frame_observer.h"
2122
#include "shell/renderer/web_worker_observer.h"
2223
#include "third_party/blink/public/web/web_document.h"
2324
#include "third_party/blink/public/web/web_local_frame.h"
24-
#include "third_party/electron_node/src/node_native_module_env.h"
2525

2626
namespace electron {
2727

@@ -227,9 +227,8 @@ void AtomRendererClient::SetupMainWorldOverrides(
227227
env->process_object(),
228228
GetContext(render_frame->GetWebFrame(), isolate)->Global()};
229229

230-
node::native_module::NativeModuleEnv::CompileAndCall(
231-
context, "electron/js2c/isolated_bundle", &isolated_bundle_params,
232-
&isolated_bundle_args, nullptr);
230+
util::CompileAndCall(context, "electron/js2c/isolated_bundle",
231+
&isolated_bundle_params, &isolated_bundle_args, nullptr);
233232
}
234233

235234
void AtomRendererClient::SetupExtensionWorldOverrides(
@@ -255,9 +254,8 @@ void AtomRendererClient::SetupExtensionWorldOverrides(
255254
GetContext(render_frame->GetWebFrame(), isolate)->Global(),
256255
v8::Integer::New(isolate, world_id)};
257256

258-
node::native_module::NativeModuleEnv::CompileAndCall(
259-
context, "electron/js2c/content_script_bundle", &isolated_bundle_params,
260-
&isolated_bundle_args, nullptr);
257+
util::CompileAndCall(context, "electron/js2c/content_script_bundle",
258+
&isolated_bundle_params, &isolated_bundle_args, nullptr);
261259
#endif
262260
}
263261

shell/renderer/atom_sandboxed_renderer_client.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
#include "shell/common/native_mate_converters/value_converter.h"
2121
#include "shell/common/node_bindings.h"
2222
#include "shell/common/node_includes.h"
23+
#include "shell/common/node_util.h"
2324
#include "shell/common/options_switches.h"
2425
#include "shell/renderer/atom_render_frame_observer.h"
2526
#include "third_party/blink/public/web/blink.h"
2627
#include "third_party/blink/public/web/web_document.h"
2728
#include "third_party/electron_node/src/node_binding.h"
28-
#include "third_party/electron_node/src/node_native_module_env.h"
2929

3030
namespace electron {
3131

@@ -231,7 +231,7 @@ void AtomSandboxedRendererClient::DidCreateScriptContext(
231231

232232
std::vector<v8::Local<v8::Value>> sandbox_preload_bundle_args = {binding};
233233

234-
node::native_module::NativeModuleEnv::CompileAndCall(
234+
util::CompileAndCall(
235235
isolate->GetCurrentContext(), "electron/js2c/sandbox_bundle",
236236
&sandbox_preload_bundle_params, &sandbox_preload_bundle_args, nullptr);
237237

@@ -259,9 +259,8 @@ void AtomSandboxedRendererClient::SetupMainWorldOverrides(
259259
process.GetHandle(),
260260
GetContext(render_frame->GetWebFrame(), isolate)->Global()};
261261

262-
node::native_module::NativeModuleEnv::CompileAndCall(
263-
context, "electron/js2c/isolated_bundle", &isolated_bundle_params,
264-
&isolated_bundle_args, nullptr);
262+
util::CompileAndCall(context, "electron/js2c/isolated_bundle",
263+
&isolated_bundle_params, &isolated_bundle_args, nullptr);
265264
}
266265

267266
void AtomSandboxedRendererClient::SetupExtensionWorldOverrides(
@@ -286,9 +285,8 @@ void AtomSandboxedRendererClient::SetupExtensionWorldOverrides(
286285
GetContext(render_frame->GetWebFrame(), isolate)->Global(),
287286
v8::Integer::New(isolate, world_id)};
288287

289-
node::native_module::NativeModuleEnv::CompileAndCall(
290-
context, "electron/js2c/content_script_bundle", &isolated_bundle_params,
291-
&isolated_bundle_args, nullptr);
288+
util::CompileAndCall(context, "electron/js2c/content_script_bundle",
289+
&isolated_bundle_params, &isolated_bundle_args, nullptr);
292290
#endif
293291
}
294292

0 commit comments

Comments
 (0)