Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 1d69d8b

Browse files
nitsakhzcbenz
authored andcommitted
fixme: Add back NativeModuleLoader CompileAndCall
Node removed the NativeModuleLoader CompileAndCall function and exposed the LookupAndCompile function, with the intention of giving the ability to the caller to handle errors. The node PR made the changes nodejs/node#25667. I'm adding this back for now, but this can easily be moved to electron as a helper or can probably be upstreamed somehow.
1 parent d0b2089 commit 1d69d8b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/node_native_module.cc

+18
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,24 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
272272
return scope.Escape(fun);
273273
}
274274

275+
MaybeLocal<Value> NativeModuleLoader::CompileAndCall(
276+
Local<Context> context,
277+
const char* id,
278+
std::vector<Local<String>>* parameters,
279+
std::vector<Local<Value>>* arguments,
280+
Environment* optional_env) {
281+
Isolate* isolate = context->GetIsolate();
282+
MaybeLocal<Function> compiled =
283+
per_process::native_module_loader.LookupAndCompile(
284+
context, id, parameters, nullptr);
285+
if (compiled.IsEmpty()) {
286+
return MaybeLocal<Value>();
287+
}
288+
Local<Function> fn = compiled.ToLocalChecked().As<Function>();
289+
return fn->Call(
290+
context, v8::Null(isolate), arguments->size(), arguments->data());
291+
}
292+
275293
void NativeModuleLoader::Initialize(Local<Object> target,
276294
Local<Value> unused,
277295
Local<Context> context,

src/node_native_module.h

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ class NODE_EXTERN NativeModuleLoader {
5353
std::vector<v8::Local<v8::String>>* parameters,
5454
Environment* optional_env);
5555

56+
// Run a script with JS source bundled inside the binary as if it's wrapped
57+
// in a function called with a null receiver and arguments specified in C++.
58+
// The returned value is empty if an exception is encountered.
59+
// JS code run with this method can assume that their top-level
60+
// declarations won't affect the global scope.
61+
v8::MaybeLocal<v8::Value> CompileAndCall(
62+
v8::Local<v8::Context> context,
63+
const char* id,
64+
std::vector<v8::Local<v8::String>>* parameters,
65+
std::vector<v8::Local<v8::Value>>* arguments,
66+
Environment* optional_env);
67+
5668
private:
5769
static void GetCacheUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
5870
// Passing ids of builtin module source code into JS land as

0 commit comments

Comments
 (0)