From 248921b58ca5fdff439f418f8adc8f07a9cb154c Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 4 Dec 2019 01:11:29 +0800 Subject: [PATCH] lib: delay access to CLI option to pre-execution CLI options should not be added through the config binding, instead they are already available in the JS land via `require('internal/options')`. Also CLI options in principle should be processed in pre-execution instead of bootstrap scripts. PR-URL: https://github.com/nodejs/node/pull/30778 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Yongsheng Zhang Reviewed-By: Rich Trott --- lib/internal/bootstrap/loaders.js | 3 --- lib/internal/bootstrap/pre_execution.js | 12 +++++++++++- lib/internal/main/worker_thread.js | 2 ++ src/node_config.cc | 8 ++++---- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index dbf1609dbc410a..1a0db5f882fc6f 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -160,9 +160,6 @@ function NativeModule(id) { this.loaded = false; this.loading = false; this.canBeRequiredByUsers = !id.startsWith('internal/'); - - if (id === 'wasi') - this.canBeRequiredByUsers = !!internalBinding('config').experimentalWasi; } // To be called during pre-execution when --expose-internals is on. diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index e20fd10fd4d053..81002a3e3c49f6 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -62,6 +62,7 @@ function prepareMainThreadExecution(expandArgv1 = false) { initializeClusterIPC(); initializeDeprecations(); + initializeWASI(); initializeCJSLoader(); initializeESMLoader(); @@ -400,6 +401,14 @@ function initializePolicy() { } } +function initializeWASI() { + if (getOptionValue('--experimental-wasi-unstable-preview0')) { + const { NativeModule } = require('internal/bootstrap/loaders'); + const mod = NativeModule.map.get('wasi'); + mod.canBeRequiredByUsers = true; + } +} + function initializeCJSLoader() { const CJSLoader = require('internal/modules/cjs/loader'); CJSLoader.Module._initPaths(); @@ -466,5 +475,6 @@ module.exports = { setupTraceCategoryState, setupInspectorHooks, initializeReport, - initializeCJSLoader + initializeCJSLoader, + initializeWASI }; diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js index 49fad1b5c30513..56ccd9df5e9b53 100644 --- a/lib/internal/main/worker_thread.js +++ b/lib/internal/main/worker_thread.js @@ -14,6 +14,7 @@ const { setupWarningHandler, setupDebugEnv, initializeDeprecations, + initializeWASI, initializeCJSLoader, initializeESMLoader, initializeFrozenIntrinsics, @@ -109,6 +110,7 @@ port.on('message', (message) => { require('internal/process/policy').setup(manifestSrc, manifestURL); } initializeDeprecations(); + initializeWASI(); initializeCJSLoader(); initializeESMLoader(); diff --git a/src/node_config.cc b/src/node_config.cc index 16050bdd5b967d..6ee3164a134fe8 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -15,11 +15,14 @@ using v8::Number; using v8::Object; using v8::Value; -// The config binding is used to provide an internal view of compile or runtime +// The config binding is used to provide an internal view of compile time // config options that are required internally by lib/*.js code. This is an // alternative to dropping additional properties onto the process object as // has been the practice previously in node.cc. +// Command line arguments are already accessible in the JS land via +// require('internal/options').getOptionValue('--some-option'). Do not add them +// here. static void Initialize(Local target, Local unused, Local context, @@ -84,9 +87,6 @@ static void Initialize(Local target, READONLY_PROPERTY(target, "hasCachedBuiltins", v8::Boolean::New(isolate, native_module::has_code_cache)); - - if (env->options()->experimental_wasi) - READONLY_TRUE_PROPERTY(target, "experimentalWasi"); } // InitConfig } // namespace node