Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 40945e0

Browse filesBrowse files
addaleaxMylesBorins
authored andcommitted
repl: keep the built-in modules non-enumerable
Make sure that the built-in modules in the repl stay non-enumerable. Previously, they would pop up as enumerable properties of the global object after having been accessed for the first time. PR-URL: #6207 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 87e0734 commit 40945e0
Copy full SHA for 40945e0

File tree

Expand file treeCollapse file tree

1 file changed

+25
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+25
-10
lines changed
Open diff view settings
Collapse file

‎lib/internal/module.js‎

Copy file name to clipboardExpand all lines: lib/internal/module.js
+25-10Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,36 @@ exports.builtinLibs = ['assert', 'buffer', 'child_process', 'cluster',
5757
function addBuiltinLibsToObject(object) {
5858
// Make built-in modules available directly (loaded lazily).
5959
exports.builtinLibs.forEach((name) => {
60+
// Goals of this mechanism are:
61+
// - Lazy loading of built-in modules
62+
// - Having all built-in modules available as non-enumerable properties
63+
// - Allowing the user to re-assign these variables as if there were no
64+
// pre-existing globals with the same name.
65+
66+
const setReal = (val) => {
67+
// Deleting the property before re-assigning it disables the
68+
// getter/setter mechanism.
69+
delete object[name];
70+
object[name] = val;
71+
};
72+
6073
Object.defineProperty(object, name, {
6174
get: () => {
6275
const lib = require(name);
63-
// This implicitly invokes the setter, so that this getter is only
64-
// invoked at most once and does not overwrite anything.
65-
object[name] = lib;
66-
return lib;
67-
},
68-
// Allow the creation of other globals with this name.
69-
set: (val) => {
70-
// Deleting the property before re-assigning it disables the
71-
// getter/setter mechanism.
76+
77+
// Disable the current getter/setter and set up a new
78+
// non-enumerable property.
7279
delete object[name];
73-
object[name] = val;
80+
Object.defineProperty(object, name, {
81+
get: () => lib,
82+
set: setReal,
83+
configurable: true,
84+
enumerable: false
85+
});
86+
87+
return lib;
7488
},
89+
set: setReal,
7590
configurable: true,
7691
enumerable: false
7792
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.