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

Commit

Permalink
chore: re-add CompileAndCall, this should be added as a helper in ele…
Browse files Browse the repository at this point in the history
…ctron
  • Loading branch information
MarshallOfSound authored and codebytere committed Jul 3, 2019
1 parent a530320 commit 79187b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/node_native_module_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ MaybeLocal<Function> NativeModuleEnv::LookupAndCompile(
return maybe;
}

MaybeLocal<Value> NativeModuleEnv::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 = LookupAndCompile(context, id, parameters, optional_env);
if (compiled.IsEmpty()) {
return MaybeLocal<Value>();
}
Local<Function> fn = compiled.ToLocalChecked().As<Function>();
return fn->Call(
context, v8::Null(isolate), arguments->size(), arguments->data());
}

// TODO(joyeecheung): It is somewhat confusing that Class::Initialize
// is used to initialize to the binding, but it is the current convention.
// Rename this across the code base to something that makes more sense.
Expand Down
11 changes: 11 additions & 0 deletions src/node_native_module_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ class NativeModuleEnv {
const char* id,
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.
static 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);

static v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context);
// Returns config.gypi as a JSON string
Expand Down

0 comments on commit 79187b3

Please sign in to comment.