diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-04-10 00:08:48 +0300 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2019-04-13 12:24:51 +0300 |
commit | dfd7e994258a36f3941c74295a8c037cb4850418 (patch) | |
tree | 03eaa022a99159912c97773a1b41952f3ee404b1 /src/node_native_module.h | |
parent | 9b6b567bc4dd8f40bad12528eebf12dac8a8027f (diff) |
src: make a Environment-independent proxy class for NativeModuleLoader
This patch splits `NativeModuleLoader` into two parts - a singleton
that only relies on v8 and `node::Mutex` and a proxy class for
the singleton (`NativeModuleEnv`) that provides limited access to
the singleton as well as C++ bindings for the Node.js binary.
`NativeModuleLoader` is then no longer aware of `Environment`.
PR-URL: https://github.com/nodejs/node/pull/27160
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Diffstat (limited to 'src/node_native_module.h')
-rw-r--r-- | src/node_native_module.h | 97 |
1 files changed, 35 insertions, 62 deletions
diff --git a/src/node_native_module.h b/src/node_native_module.h index 587c59022af..bbd4d89c400 100644 --- a/src/node_native_module.h +++ b/src/node_native_module.h @@ -4,9 +4,9 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #include <map> +#include <memory> #include <set> #include <string> -#include "env.h" #include "node_mutex.h" #include "node_union_bytes.h" #include "v8.h" @@ -23,79 +23,57 @@ using NativeModuleCacheMap = // handles compilation and caching of builtin modules (NativeModule) // and bootstrappers, whose source are bundled into the binary // as static data. -// This class should not depend on a particular isolate, context, or -// environment. Rather it should take them as arguments when necessary. -// The instances of this class are per-process. +// This class should not depend on any Environment, or depend on access to +// the its own singleton - that should be encapsulated in NativeModuleEnv +// instead. class NativeModuleLoader { - public: + private: + // Only allow access from friends. + friend class NativeModuleEnv; + friend class CodeCacheBuilder; + NativeModuleLoader(); - // TODO(joyeecheung): maybe we should make this a singleton, instead of - // putting it in per_process. NativeModuleLoader(const NativeModuleLoader&) = delete; NativeModuleLoader& operator=(const NativeModuleLoader&) = delete; - - static void Initialize(v8::Local<v8::Object> target, - v8::Local<v8::Value> unused, - v8::Local<v8::Context> context, - void* priv); - v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context) const; - // Returns config.gypi as a JSON string - v8::Local<v8::String> GetConfigString(v8::Isolate* isolate) const; - - bool Exists(const char* id); - - // For bootstrappers optional_env may be a nullptr. - // If an exception is encountered (e.g. source code contains - // syntax error), the returned value is empty. - v8::MaybeLocal<v8::Function> LookupAndCompile( - v8::Local<v8::Context> context, - const char* id, - std::vector<v8::Local<v8::String>>* parameters, - Environment* optional_env); - - private: - static void GetModuleCategories( - v8::Local<v8::Name> property, - const v8::PropertyCallbackInfo<v8::Value>& info); - static void GetCacheUsage(const v8::FunctionCallbackInfo<v8::Value>& args); - // Passing ids of builtin module source code into JS land as - // internalBinding('native_module').moduleIds - static void ModuleIdsGetter(v8::Local<v8::Name> property, - const v8::PropertyCallbackInfo<v8::Value>& info); - // Passing config.gypi into JS land as internalBinding('native_module').config - static void ConfigStringGetter( - v8::Local<v8::Name> property, - const v8::PropertyCallbackInfo<v8::Value>& info); - // Get code cache for a specific native module - static void GetCodeCache(const v8::FunctionCallbackInfo<v8::Value>& args); - v8::MaybeLocal<v8::Uint8Array> GetCodeCache(v8::Isolate* isolate, - const char* id) const; - // Compile a specific native module as a function - static void CompileFunction(const v8::FunctionCallbackInfo<v8::Value>& args); + static NativeModuleLoader* GetInstance(); // Generated by tools/js2c.py as node_javascript.cc void LoadJavaScriptSource(); // Loads data into source_ UnionBytes GetConfig(); // Return data for config.gypi - // Generated by tools/generate_code_cache.js as node_code_cache.cc when - // the build is configured with --code-cache-path=.... They are noops - // in node_code_cache_stub.cc - void LoadCodeCache(); // Loads data into code_cache_ - - // Compile a script as a NativeModule that can be loaded via - // NativeModule.p.require in JS land. - static v8::MaybeLocal<v8::Function> CompileAsModule(Environment* env, - const char* id); + bool Exists(const char* id); + v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context); + v8::Local<v8::String> GetConfigString(v8::Isolate* isolate); + std::vector<std::string> GetModuleIds(); - void InitializeModuleCategories(); struct ModuleCategories { bool is_initialized = false; std::set<std::string> can_be_required; std::set<std::string> cannot_be_required; }; + void InitializeModuleCategories(); + const std::set<std::string>& GetCannotBeRequired(); + const std::set<std::string>& GetCanBeRequired(); - ModuleCategories module_categories_; + bool CanBeRequired(const char* id); + bool CannotBeRequired(const char* id); + + NativeModuleCacheMap* code_cache(); + v8::ScriptCompiler::CachedData* GetCodeCache(const char* id) const; + enum class Result { kWithCache, kWithoutCache }; + // If an exception is encountered (e.g. source code contains + // syntax error), the returned value is empty. + v8::MaybeLocal<v8::Function> LookupAndCompile( + v8::Local<v8::Context> context, + const char* id, + std::vector<v8::Local<v8::String>>* parameters, + Result* result); + v8::MaybeLocal<v8::Function> CompileAsModule(v8::Local<v8::Context> context, + const char* id, + Result* result); + static NativeModuleLoader instance_; + ModuleCategories module_categories_; NativeModuleRecordMap source_; NativeModuleCacheMap code_cache_; UnionBytes config_; @@ -103,13 +81,8 @@ class NativeModuleLoader { // Used to synchronize access to the code cache map Mutex code_cache_mutex_; }; - } // namespace native_module -namespace per_process { -extern native_module::NativeModuleLoader native_module_loader; -} // namespace per_process - } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |