diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2021-10-07 07:03:10 +0300 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2021-10-16 05:03:50 +0300 |
commit | 38aa7cc7c7ea644c8d60859880f69a98f7159ffa (patch) | |
tree | 8c3a575b5dc10afc3e5833bdae39ccfa10fd1abe /lib | |
parent | 6baea14506a4ebd2f29bc9bcd5fb3beccba5a301 (diff) |
src: get embedder options on-demand
Only query embedder options when they are needed so that the bootstrap
remains as stateless as possible so that the bootstrap snapshot is
controlled solely by configure options, and subsequent runtime changes
should be done in pre-execution.
PR-URL: https://github.com/nodejs/node/pull/40357
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Shelley Vohr <shelley.vohr@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/bootstrap/pre_execution.js | 7 | ||||
-rw-r--r-- | lib/internal/options.js | 28 |
2 files changed, 20 insertions, 15 deletions
diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index a4c73cba548..f2a10641906 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -11,8 +11,7 @@ const { const { getOptionValue, - noGlobalSearchPaths, - shouldNotRegisterESMLoader, + getEmbedderOptions, } = require('internal/options'); const { reconnectZeroFillToggle } = require('internal/buffer'); @@ -421,7 +420,7 @@ function initializeWASI() { function initializeCJSLoader() { const CJSLoader = require('internal/modules/cjs/loader'); - if (!noGlobalSearchPaths) { + if (!getEmbedderOptions().noGlobalSearchPaths) { CJSLoader.Module._initPaths(); } // TODO(joyeecheung): deprecate this in favor of a proper hook? @@ -433,7 +432,7 @@ function initializeESMLoader() { // Create this WeakMap in js-land because V8 has no C++ API for WeakMap. internalBinding('module_wrap').callbackMap = new SafeWeakMap(); - if (shouldNotRegisterESMLoader) return; + if (getEmbedderOptions().shouldNotRegisterESMLoader) return; const { setImportModuleDynamicallyCallback, diff --git a/lib/internal/options.js b/lib/internal/options.js index cb694c7dfdd..01b334d4ec5 100644 --- a/lib/internal/options.js +++ b/lib/internal/options.js @@ -1,36 +1,43 @@ 'use strict'; const { - getOptions, - noGlobalSearchPaths, - shouldNotRegisterESMLoader, + getCLIOptions, + getEmbedderOptions: getEmbedderOptionsFromBinding, } = internalBinding('options'); let warnOnAllowUnauthorized = true; let optionsMap; let aliasesMap; +let embedderOptions; -// getOptions() would serialize the option values from C++ land. +// getCLIOptions() would serialize the option values from C++ land. // It would error if the values are queried before bootstrap is // complete so that we don't accidentally include runtime-dependent // states into a runtime-independent snapshot. -function getOptionsFromBinding() { +function getCLIOptionsFromBinding() { if (!optionsMap) { - ({ options: optionsMap } = getOptions()); + ({ options: optionsMap } = getCLIOptions()); } return optionsMap; } function getAliasesFromBinding() { if (!aliasesMap) { - ({ aliases: aliasesMap } = getOptions()); + ({ aliases: aliasesMap } = getCLIOptions()); } return aliasesMap; } +function getEmbedderOptions() { + if (!embedderOptions) { + embedderOptions = getEmbedderOptionsFromBinding(); + } + return embedderOptions; +} + function getOptionValue(optionName) { - const options = getOptionsFromBinding(); + const options = getCLIOptionsFromBinding(); if (optionName.startsWith('--no-')) { const option = options.get('--' + optionName.slice(5)); return option && !option.value; @@ -54,13 +61,12 @@ function getAllowUnauthorized() { module.exports = { get options() { - return getOptionsFromBinding(); + return getCLIOptionsFromBinding(); }, get aliases() { return getAliasesFromBinding(); }, getOptionValue, getAllowUnauthorized, - noGlobalSearchPaths, - shouldNotRegisterESMLoader, + getEmbedderOptions }; |