process: handle --expose-internals during pre-execution

Instead of relying on the value of the CLI option when
executing bootstrap/loaders.js.

PR-URL: https://github.com/nodejs/node/pull/26759
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Joyee Cheung 2019-03-19 06:11:44 +08:00
parent 8209caec39
commit 83972ff6ac
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
3 changed files with 16 additions and 13 deletions

View File

@ -41,8 +41,7 @@
// This file is compiled as if it's wrapped in a function with arguments // This file is compiled as if it's wrapped in a function with arguments
// passed by node::RunBootstrapping() // passed by node::RunBootstrapping()
/* global process, getLinkedBinding, getInternalBinding */ /* global process, getLinkedBinding, getInternalBinding, primordials */
/* global exposeInternals, primordials */
const { const {
Reflect, Reflect,
@ -157,16 +156,20 @@ function NativeModule(id) {
this.exportKeys = undefined; this.exportKeys = undefined;
this.loaded = false; this.loaded = false;
this.loading = false; this.loading = false;
if (id === loaderId) { this.canBeRequiredByUsers = !id.startsWith('internal/');
// Do not expose this to user land even with --expose-internals.
this.canBeRequiredByUsers = false;
} else if (id.startsWith('internal/')) {
this.canBeRequiredByUsers = exposeInternals;
} else {
this.canBeRequiredByUsers = true;
}
} }
// To be called during pre-execution when --expose-internals is on.
// Enables the user-land module loader to access internal modules.
NativeModule.exposeInternals = function() {
for (const [id, mod] of NativeModule.map) {
// Do not expose this to user land even with --expose-internals.
if (id !== loaderId) {
mod.canBeRequiredByUsers = true;
}
}
};
const { const {
moduleIds, moduleIds,
compileFunction compileFunction

View File

@ -133,6 +133,9 @@ function initializeReport() {
function setupDebugEnv() { function setupDebugEnv() {
require('internal/util/debuglog').initializeDebugEnv(process.env.NODE_DEBUG); require('internal/util/debuglog').initializeDebugEnv(process.env.NODE_DEBUG);
if (getOptionValue('--expose-internals')) {
require('internal/bootstrap/loaders').NativeModule.exposeInternals();
}
} }
function setupSignalHandlers() { function setupSignalHandlers() {

View File

@ -296,8 +296,6 @@ MaybeLocal<Value> RunBootstrapping(Environment* env) {
env->process_string(), env->process_string(),
FIXED_ONE_BYTE_STRING(isolate, "getLinkedBinding"), FIXED_ONE_BYTE_STRING(isolate, "getLinkedBinding"),
FIXED_ONE_BYTE_STRING(isolate, "getInternalBinding"), FIXED_ONE_BYTE_STRING(isolate, "getInternalBinding"),
// --expose-internals
FIXED_ONE_BYTE_STRING(isolate, "exposeInternals"),
env->primordials_string()}; env->primordials_string()};
std::vector<Local<Value>> loaders_args = { std::vector<Local<Value>> loaders_args = {
process, process,
@ -307,7 +305,6 @@ MaybeLocal<Value> RunBootstrapping(Environment* env) {
env->NewFunctionTemplate(binding::GetInternalBinding) env->NewFunctionTemplate(binding::GetInternalBinding)
->GetFunction(context) ->GetFunction(context)
.ToLocalChecked(), .ToLocalChecked(),
Boolean::New(isolate, env->options()->expose_internals),
env->primordials()}; env->primordials()};
// Bootstrap internal loaders // Bootstrap internal loaders