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

Commit

Permalink
fixme: Add back NativeModuleLoader CompileAndCall
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nitsakh committed Apr 11, 2019
1 parent 6c65aab commit 6190d02
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/node_native_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,24 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
return scope.Escape(fun);
}

MaybeLocal<Value> NativeModuleLoader::CompileAndCall(
Local<Context> context,
const char* id,
std::vector<Local<String>>* parameters,
std::vector<Local<Value>>* arguments,
Environment* optional_env) {
Isolate* isolate = context->GetIsolate();
MaybeLocal<Function> compiled =
per_process::native_module_loader.LookupAndCompile(
context, id, parameters, nullptr);
if (compiled.IsEmpty()) {
return MaybeLocal<Value>();
}
Local<Function> fn = compiled.ToLocalChecked().As<Function>();
return fn->Call(
context, v8::Null(isolate), arguments->size(), arguments->data());
}

void NativeModuleLoader::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand Down
12 changes: 12 additions & 0 deletions src/node_native_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ class NODE_EXTERN NativeModuleLoader {
std::vector<v8::Local<v8::String>>* parameters,
Environment* optional_env);

// 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,
Environment* optional_env);

private:
static void GetCacheUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
// Passing ids of builtin module source code into JS land as
Expand Down

0 comments on commit 6190d02

Please sign in to comment.