From 0866718518820e49d6554bbc3c06b558ec5038f1 Mon Sep 17 00:00:00 2001 From: Nitish Sakhawalkar Date: Wed, 27 Mar 2019 00:28:29 -0700 Subject: [PATCH] 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 https://github.com/nodejs/node/pull/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. --- src/node_native_module.cc | 18 ++++++++++++++++++ src/node_native_module.h | 12 ++++++++++++ 2 files changed, 30 insertions(+) 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