Skip to content

Commit 07c9f98

Browse files
addaleaxMyles Borins
authored and
Myles Borins
committedApr 20, 2016
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 457f24f commit 07c9f98

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed
 

‎lib/internal/module.js

+25-10
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)
Please sign in to comment.