| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6528ce6 commit 0e2fbe4
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,18 +6,16 @@ | |||
| 6 | 6 | // cannot be tampered with even with --expose-internals. | |
| 7 | 7 | ||
| 8 | 8 | const { NativeModule } = require('internal/bootstrap/loaders'); | |
| 9 | - const { getSource, compileCodeCache } = internalBinding('native_module'); | ||
| 9 | + const { source, compileCodeCache } = internalBinding('native_module'); | ||
| 10 | 10 | const { hasTracing } = process.binding('config'); | |
| 11 | 11 | ||
| 12 | - const source = getSource(); | ||
| 13 | 12 | const depsModule = Object.keys(source).filter( | |
| 14 | 13 | (key) => NativeModule.isDepsModule(key) || key.startsWith('internal/deps') | |
| 15 | 14 | ); | |
| 16 | 15 | ||
| 17 | 16 | // Modules with source code compiled in js2c that | |
| 18 | 17 | // cannot be compiled with the code cache. | |
| 19 | 18 | const cannotUseCache = [ | |
| 20 | - 'config', | ||
| 21 | 19 | 'sys', // Deprecated. | |
| 22 | 20 | 'internal/v8_prof_polyfill', | |
| 23 | 21 | 'internal/v8_prof_processor', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -169,10 +169,15 @@ function NativeModule(id) { | |||
| 169 | 169 | this.loading = false; | |
| 170 | 170 | } | |
| 171 | 171 | ||
| 172 | - NativeModule._source = getInternalBinding('natives'); | ||
| 172 | + const { | ||
| 173 | + source, | ||
| 174 | + compileFunction | ||
| 175 | + } = internalBinding('native_module'); | ||
| 176 | + | ||
| 177 | + NativeModule._source = source; | ||
| 173 | 178 | NativeModule._cache = {}; | |
| 174 | 179 | ||
| 175 | - const config = getInternalBinding('config'); | ||
| 180 | + const config = internalBinding('config'); | ||
| 176 | 181 | ||
| 177 | 182 | // Think of this as module.exports in this file even though it is not | |
| 178 | 183 | // written in CommonJS style. | |
@@ -323,7 +328,6 @@ NativeModule.prototype.proxifyExports = function() { | |||
| 323 | 328 | this.exports = new Proxy(this.exports, handler); | |
| 324 | 329 | }; | |
| 325 | 330 | ||
| 326 | - const { compileFunction } = getInternalBinding('native_module'); | ||
| 327 | 331 | NativeModule.prototype.compile = function() { | |
| 328 | 332 | const id = this.id; | |
| 329 | 333 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,7 +59,7 @@ function startup() { | |||
| 59 | 59 | } | |
| 60 | 60 | ||
| 61 | 61 | perThreadSetup.setupAssert(); | |
| 62 | - perThreadSetup.setupConfig(NativeModule._source); | ||
| 62 | + perThreadSetup.setupConfig(); | ||
| 63 | 63 | ||
| 64 | 64 | if (isMainThread) { | |
| 65 | 65 | mainThreadSetup.setupSignalHandlers(internalBinding); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -143,17 +143,9 @@ function setupMemoryUsage(_memoryUsage) { | |||
| 143 | 143 | }; | |
| 144 | 144 | } | |
| 145 | 145 | ||
| 146 | - function setupConfig(_source) { | ||
| 147 | - // NativeModule._source | ||
| 148 | - // used for `process.config`, but not a real module | ||
| 149 | - const config = _source.config; | ||
| 150 | - delete _source.config; | ||
| 151 | - | ||
| 152 | - process.config = JSON.parse(config, function(key, value) { | ||
| 153 | - if (value === 'true') return true; | ||
| 154 | - if (value === 'false') return false; | ||
| 155 | - return value; | ||
| 156 | - }); | ||
| 146 | + function setupConfig() { | ||
| 147 | + // Serialized config.gypi | ||
| 148 | + process.config = JSON.parse(internalBinding('native_module').config); | ||
| 157 | 149 | } | |
| 158 | 150 | ||
| 159 | 151 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -140,6 +140,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2; | |||
| 140 | 140 | V(channel_string, "channel") \ | |
| 141 | 141 | V(chunks_sent_since_last_write_string, "chunksSentSinceLastWrite") \ | |
| 142 | 142 | V(code_string, "code") \ | |
| 143 | + V(config_string, "config") \ | ||
| 143 | 144 | V(constants_string, "constants") \ | |
| 144 | 145 | V(crypto_dsa_string, "dsa") \ | |
| 145 | 146 | V(crypto_ec_string, "ec") \ | |
@@ -312,7 +313,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2; | |||
| 312 | 313 | V(write_host_object_string, "_writeHostObject") \ | |
| 313 | 314 | V(write_queue_size_string, "writeQueueSize") \ | |
| 314 | 315 | V(x_forwarded_string, "x-forwarded-for") \ | |
| 315 | - V(zero_return_string, "ZERO_RETURN") \ | ||
| 316 | + V(zero_return_string, "ZERO_RETURN") | ||
| 316 | 317 | ||
| 317 | 318 | #define ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V) \ | |
| 318 | 319 | V(as_external, v8::External) \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -390,6 +390,13 @@ void GetInternalBinding(const FunctionCallbackInfo<Value>& args) { | |||
| 390 | 390 | DefineConstants(env->isolate(), exports); | |
| 391 | 391 | } else if (!strcmp(*module_v, "natives")) { | |
| 392 | 392 | exports = per_process_loader.GetSourceObject(env->context()); | |
| 393 | + // Legacy feature: process.binding('natives').config contains stringified | ||
| 394 | + // config.gypi | ||
| 395 | + CHECK(exports | ||
| 396 | + ->Set(env->context(), | ||
| 397 | + env->config_string(), | ||
| 398 | + per_process_loader.GetConfigString(env->isolate())) | ||
| 399 | + .FromJust()); | ||
| 393 | 400 | } else { | |
| 394 | 401 | return ThrowIfNoSuchModule(env, *module_v); | |
| 395 | 402 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ using v8::Array; | |||
| 9 | 9 | using v8::ArrayBuffer; | |
| 10 | 10 | using v8::ArrayBufferCreationMode; | |
| 11 | 11 | using v8::Context; | |
| 12 | + using v8::DEFAULT; | ||
| 12 | 13 | using v8::EscapableHandleScope; | |
| 13 | 14 | using v8::Function; | |
| 14 | 15 | using v8::FunctionCallbackInfo; | |
@@ -19,11 +20,15 @@ using v8::Isolate; | |||
| 19 | 20 | using v8::Local; | |
| 20 | 21 | using v8::Maybe; | |
| 21 | 22 | using v8::MaybeLocal; | |
| 23 | + using v8::Name; | ||
| 24 | + using v8::None; | ||
| 22 | 25 | using v8::Object; | |
| 26 | + using v8::PropertyCallbackInfo; | ||
| 23 | 27 | using v8::Script; | |
| 24 | 28 | using v8::ScriptCompiler; | |
| 25 | 29 | using v8::ScriptOrigin; | |
| 26 | 30 | using v8::Set; | |
| 31 | + using v8::SideEffectType; | ||
| 27 | 32 | using v8::String; | |
| 28 | 33 | using v8::Uint8Array; | |
| 29 | 34 | using v8::Value; | |
@@ -70,25 +75,35 @@ void NativeModuleLoader::GetCacheUsage( | |||
| 70 | 75 | args.GetReturnValue().Set(result); | |
| 71 | 76 | } | |
| 72 | 77 | ||
| 73 | - void NativeModuleLoader::GetSourceObject( | ||
| 74 | - const FunctionCallbackInfo<Value>& args) { | ||
| 75 | - Environment* env = Environment::GetCurrent(args); | ||
| 76 | - args.GetReturnValue().Set(per_process_loader.GetSourceObject(env->context())); | ||
| 78 | + void NativeModuleLoader::SourceObjectGetter( | ||
| 79 | + Local<Name> property, const PropertyCallbackInfo<Value>& info) { | ||
| 80 | + Local<Context> context = info.GetIsolate()->GetCurrentContext(); | ||
| 81 | + info.GetReturnValue().Set(per_process_loader.GetSourceObject(context)); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + void NativeModuleLoader::ConfigStringGetter( | ||
| 85 | + Local<Name> property, const PropertyCallbackInfo<Value>& info) { | ||
| 86 | + info.GetReturnValue().Set( | ||
| 87 | + per_process_loader.GetConfigString(info.GetIsolate())); | ||
| 77 | 88 | } | |
| 78 | 89 | ||
| 79 | 90 | Local<Object> NativeModuleLoader::GetSourceObject( | |
| 80 | 91 | Local<Context> context) const { | |
| 81 | 92 | return MapToObject(context, source_); | |
| 82 | 93 | } | |
| 83 | 94 | ||
| 95 | + Local<String> NativeModuleLoader::GetConfigString(Isolate* isolate) const { | ||
| 96 | + return config_.ToStringChecked(isolate); | ||
| 97 | + } | ||
| 98 | + | ||
| 84 | 99 | Local<String> NativeModuleLoader::GetSource(Isolate* isolate, | |
| 85 | 100 | const char* id) const { | |
| 86 | 101 | const auto it = source_.find(id); | |
| 87 | 102 | CHECK_NE(it, source_.end()); | |
| 88 | 103 | return it->second.ToStringChecked(isolate); | |
| 89 | 104 | } | |
| 90 | 105 | ||
| 91 | - NativeModuleLoader::NativeModuleLoader() { | ||
| 106 | + NativeModuleLoader::NativeModuleLoader() : config_(GetConfig()) { | ||
| 92 | 107 | LoadJavaScriptSource(); | |
| 93 | 108 | LoadJavaScriptHash(); | |
| 94 | 109 | LoadCodeCache(); | |
@@ -321,8 +336,27 @@ void NativeModuleLoader::Initialize(Local<Object> target, | |||
| 321 | 336 | void* priv) { | |
| 322 | 337 | Environment* env = Environment::GetCurrent(context); | |
| 323 | 338 | ||
| 324 | - env->SetMethod( | ||
| 325 | - target, "getSource", NativeModuleLoader::GetSourceObject); | ||
| 339 | + CHECK(target | ||
| 340 | + ->SetAccessor(env->context(), | ||
| 341 | + env->config_string(), | ||
| 342 | + ConfigStringGetter, | ||
| 343 | + nullptr, | ||
| 344 | + MaybeLocal<Value>(), | ||
| 345 | + DEFAULT, | ||
| 346 | + None, | ||
| 347 | + SideEffectType::kHasNoSideEffect) | ||
| 348 | + .FromJust()); | ||
| 349 | + CHECK(target | ||
| 350 | + ->SetAccessor(env->context(), | ||
| 351 | + env->source_string(), | ||
| 352 | + SourceObjectGetter, | ||
| 353 | + nullptr, | ||
| 354 | + MaybeLocal<Value>(), | ||
| 355 | + DEFAULT, | ||
| 356 | + None, | ||
| 357 | + SideEffectType::kHasNoSideEffect) | ||
| 358 | + .FromJust()); | ||
| 359 | + | ||
| 326 | 360 | env->SetMethod( | |
| 327 | 361 | target, "getCacheUsage", NativeModuleLoader::GetCacheUsage); | |
| 328 | 362 | env->SetMethod( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,6 +40,9 @@ class NativeModuleLoader { | |||
| 40 | 40 | v8::Local<v8::Context> context, | |
| 41 | 41 | void* priv); | |
| 42 | 42 | v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context) const; | |
| 43 | + // Returns config.gypi as a JSON string | ||
| 44 | + v8::Local<v8::String> GetConfigString(v8::Isolate* isolate) const; | ||
| 45 | + | ||
| 43 | 46 | v8::Local<v8::String> GetSource(v8::Isolate* isolate, const char* id) const; | |
| 44 | 47 | ||
| 45 | 48 | // Run a script with JS source bundled inside the binary as if it's wrapped | |
@@ -56,9 +59,15 @@ class NativeModuleLoader { | |||
| 56 | 59 | ||
| 57 | 60 | private: | |
| 58 | 61 | static void GetCacheUsage(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 59 | - // For legacy process.binding('natives') which is mutable, and for | ||
| 60 | - // internalBinding('native_module').source for internal use | ||
| 61 | - static void GetSourceObject(const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| 62 | + // Passing map of builtin module source code into JS land as | ||
| 63 | + // internalBinding('native_module').source | ||
| 64 | + static void SourceObjectGetter( | ||
| 65 | + v8::Local<v8::Name> property, | ||
| 66 | + const v8::PropertyCallbackInfo<v8::Value>& info); | ||
| 67 | + // Passing config.gypi into JS land as internalBinding('native_module').config | ||
| 68 | + static void ConfigStringGetter( | ||
| 69 | + v8::Local<v8::Name> property, | ||
| 70 | + const v8::PropertyCallbackInfo<v8::Value>& info); | ||
| 62 | 71 | // Compile code cache for a specific native module | |
| 63 | 72 | static void CompileCodeCache(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 64 | 73 | // Compile a specific native module as a function | |
@@ -67,6 +76,7 @@ class NativeModuleLoader { | |||
| 67 | 76 | // Generated by tools/js2c.py as node_javascript.cc | |
| 68 | 77 | void LoadJavaScriptSource(); // Loads data into source_ | |
| 69 | 78 | void LoadJavaScriptHash(); // Loads data into source_hash_ | |
| 79 | + UnionBytes GetConfig(); // Return data for config.gypi | ||
| 70 | 80 | ||
| 71 | 81 | // Generated by tools/generate_code_cache.js as node_code_cache.cc when | |
| 72 | 82 | // the build is configured with --code-cache-path=.... They are noops | |
@@ -94,6 +104,8 @@ class NativeModuleLoader { | |||
| 94 | 104 | bool has_code_cache_ = false; | |
| 95 | 105 | NativeModuleRecordMap source_; | |
| 96 | 106 | NativeModuleRecordMap code_cache_; | |
| 107 | + UnionBytes config_; | ||
| 108 | + | ||
| 97 | 109 | NativeModuleHashMap source_hash_; | |
| 98 | 110 | NativeModuleHashMap code_cache_hash_; | |
| 99 | 111 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,21 +55,31 @@ class UnionBytes { | |||
| 55 | 55 | : is_one_byte_(false), two_bytes_(data), length_(length) {} | |
| 56 | 56 | UnionBytes(const uint8_t* data, size_t length) | |
| 57 | 57 | : is_one_byte_(true), one_bytes_(data), length_(length) {} | |
| 58 | + | ||
| 59 | + UnionBytes(const UnionBytes&) = default; | ||
| 60 | + UnionBytes& operator=(const UnionBytes&) = default; | ||
| 61 | + UnionBytes(UnionBytes&&) = default; | ||
| 62 | + UnionBytes& operator=(UnionBytes&&) = default; | ||
| 63 | + | ||
| 58 | 64 | bool is_one_byte() const { return is_one_byte_; } | |
| 59 | 65 | const uint16_t* two_bytes_data() const { | |
| 60 | 66 | CHECK(!is_one_byte_); | |
| 67 | + CHECK_NE(two_bytes_, nullptr); | ||
| 61 | 68 | return two_bytes_; | |
| 62 | 69 | } | |
| 63 | 70 | const uint8_t* one_bytes_data() const { | |
| 64 | 71 | CHECK(is_one_byte_); | |
| 72 | + CHECK_NE(one_bytes_, nullptr); | ||
| 65 | 73 | return one_bytes_; | |
| 66 | 74 | } | |
| 67 | 75 | v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const { | |
| 68 | 76 | if (is_one_byte_) { | |
| 77 | + CHECK_NE(one_bytes_, nullptr); | ||
| 69 | 78 | NonOwningExternalOneByteResource* source = | |
| 70 | 79 | new NonOwningExternalOneByteResource(one_bytes_, length_); | |
| 71 | 80 | return v8::String::NewExternalOneByte(isolate, source).ToLocalChecked(); | |
| 72 | 81 | } else { | |
| 82 | + CHECK_NE(two_bytes_, nullptr); | ||
| 73 | 83 | NonOwningExternalTwoByteResource* source = | |
| 74 | 84 | new NonOwningExternalTwoByteResource(two_bytes_, length_); | |
| 75 | 85 | return v8::String::NewExternalTwoByte(isolate, source).ToLocalChecked(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,7 +9,7 @@ const common = require('../common'); | |||
| 9 | 9 | const assert = require('assert'); | |
| 10 | 10 | ||
| 11 | 11 | const isMainThread = common.isMainThread; | |
| 12 | - const kMaxModuleCount = isMainThread ? 59 : 80; | ||
| 12 | + const kMaxModuleCount = isMainThread ? 61 : 82; | ||
| 13 | 13 | ||
| 14 | 14 | assert(list.length <= kMaxModuleCount, | |
| 15 | 15 | `Total length: ${list.length}\n` + list.join('\n') | |
| Back | FazBrowse Home | New Git URL |
0 commit comments