Skip to content

Commit

Permalink
repl: keep the built-in modules non-enumerable
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
addaleax authored and Myles Borins committed Apr 20, 2016
1 parent 457f24f commit 07c9f98
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions lib/internal/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,36 @@ exports.builtinLibs = ['assert', 'buffer', 'child_process', 'cluster',
function addBuiltinLibsToObject(object) {
// Make built-in modules available directly (loaded lazily).
exports.builtinLibs.forEach((name) => {
// Goals of this mechanism are:
// - Lazy loading of built-in modules
// - Having all built-in modules available as non-enumerable properties
// - Allowing the user to re-assign these variables as if there were no
// pre-existing globals with the same name.

const setReal = (val) => {
// Deleting the property before re-assigning it disables the
// getter/setter mechanism.
delete object[name];
object[name] = val;
};

Object.defineProperty(object, name, {
get: () => {
const lib = require(name);
// This implicitly invokes the setter, so that this getter is only
// invoked at most once and does not overwrite anything.
object[name] = lib;
return lib;
},
// Allow the creation of other globals with this name.
set: (val) => {
// Deleting the property before re-assigning it disables the
// getter/setter mechanism.

// Disable the current getter/setter and set up a new
// non-enumerable property.
delete object[name];
object[name] = val;
Object.defineProperty(object, name, {
get: () => lib,
set: setReal,
configurable: true,
enumerable: false
});

return lib;
},
set: setReal,
configurable: true,
enumerable: false
});
Expand Down

0 comments on commit 07c9f98

Please sign in to comment.