diff --git a/src/node_native_module.cc b/src/node_native_module.cc index 47d3d94f127..fec7dcd940f 100644 --- a/src/node_native_module.cc +++ b/src/node_native_module.cc @@ -272,6 +272,24 @@ MaybeLocal NativeModuleLoader::LookupAndCompile( return scope.Escape(fun); } +MaybeLocal NativeModuleLoader::CompileAndCall( + Local context, + const char* id, + std::vector>* parameters, + std::vector>* arguments, + Environment* optional_env) { + Isolate* isolate = context->GetIsolate(); + MaybeLocal compiled = + per_process::native_module_loader.LookupAndCompile( + context, id, parameters, nullptr); + if (compiled.IsEmpty()) { + return MaybeLocal(); + } + Local fn = compiled.ToLocalChecked().As(); + return fn->Call( + context, v8::Null(isolate), arguments->size(), arguments->data()); +} + void NativeModuleLoader::Initialize(Local target, Local unused, Local context, diff --git a/src/node_native_module.h b/src/node_native_module.h index cbf625fc4df..b873145602c 100644 --- a/src/node_native_module.h +++ b/src/node_native_module.h @@ -53,6 +53,18 @@ class NODE_EXTERN NativeModuleLoader { std::vector>* 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 CompileAndCall( + v8::Local context, + const char* id, + std::vector>* parameters, + std::vector>* arguments, + Environment* optional_env); + private: static void GetCacheUsage(const v8::FunctionCallbackInfo& args); // Passing ids of builtin module source code into JS land as