Skip to content

Commit 640353a

Browse files
XadillaXBethGriggs
authored andcommittedSep 21, 2021
lib,repl: ignore non-canBeRequiredByUsers built-in
e.g. `wasi` under no `--experimental-wasi-unstable-preview1` flag shouldn't be pre-required. PR-URL: #39942 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
1 parent 29104f5 commit 640353a

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed
 

‎lib/internal/main/eval_string.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
1616
const { getOptionValue } = require('internal/options');
1717

1818
prepareMainThreadExecution();
19-
addBuiltinLibsToObject(globalThis);
19+
addBuiltinLibsToObject(globalThis, '<eval>');
2020
markBootstrapComplete();
2121

2222
const source = getOptionValue('--eval');

‎lib/internal/modules/cjs/helpers.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,16 @@ function stripBOM(content) {
131131
return content;
132132
}
133133

134-
function addBuiltinLibsToObject(object) {
134+
function addBuiltinLibsToObject(object, dummyModuleName) {
135135
// Make built-in modules available directly (loaded lazily).
136-
const { builtinModules } = require('internal/modules/cjs/loader').Module;
136+
const Module = require('internal/modules/cjs/loader').Module;
137+
const { builtinModules } = Module;
138+
139+
// To require built-in modules in user-land and ignore modules whose
140+
// `canBeRequiredByUsers` is false. So we create a dummy module object and not
141+
// use `require()` directly.
142+
const dummyModule = new Module(dummyModuleName);
143+
137144
ArrayPrototypeForEach(builtinModules, (name) => {
138145
// Neither add underscored modules, nor ones that contain slashes (e.g.,
139146
// 'fs/promises') or ones that are already defined.
@@ -157,7 +164,7 @@ function addBuiltinLibsToObject(object) {
157164

158165
ObjectDefineProperty(object, name, {
159166
get: () => {
160-
const lib = require(name);
167+
const lib = dummyModule.require(name);
161168

162169
// Disable the current getter/setter and set up a new
163170
// non-enumerable property.

‎lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ REPLServer.prototype.createContext = function() {
10981098
value: makeRequireFunction(replModule)
10991099
});
11001100

1101-
addBuiltinLibsToObject(context);
1101+
addBuiltinLibsToObject(context, '<REPL>');
11021102

11031103
return context;
11041104
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const cp = require('child_process');
6+
7+
function runREPLWithAdditionalFlags(flags) {
8+
// Use -i to force node into interactive mode, despite stdout not being a TTY
9+
const args = ['-i'].concat(flags);
10+
const ret = cp.execFileSync(process.execPath, args, {
11+
input: 'require(\'events\');\nrequire(\'wasi\');',
12+
encoding: 'utf8',
13+
});
14+
return ret;
15+
}
16+
17+
// Run REPL in normal mode.
18+
let stdout = runREPLWithAdditionalFlags([]);
19+
assert.match(stdout, /\[Function: EventEmitter\] {/);
20+
assert.match(
21+
stdout,
22+
/Uncaught Error: Cannot find module 'wasi'[\w\W]+- <repl>\n/);
23+
24+
// Run REPL with '--experimental-wasi-unstable-preview1'
25+
stdout = runREPLWithAdditionalFlags([
26+
'--experimental-wasi-unstable-preview1',
27+
]);
28+
assert.match(stdout, /\[Function: EventEmitter\] {/);
29+
assert.doesNotMatch(
30+
stdout,
31+
/Uncaught Error: Cannot find module 'wasi'[\w\W]+- <repl>\n/);
32+
assert.match(stdout, /{ WASI: \[class WASI\] }/);

0 commit comments

Comments
 (0)
Please sign in to comment.