diff options
author | Anna Henningsen <anna@addaleax.net> | 2016-04-18 13:19:12 +0200 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2016-04-18 15:31:51 -0700 |
commit | ebc8c37f70a84a64851b440493f3441eb9f70fdb (patch) | |
tree | afc968a43eabbeb55a87baa08c1d8cbcaefd2e6f /lib | |
parent | 39d905e2937ab10040f45b0ab0707f8bd9bf86b9 (diff) | |
download | android-node-v8-ebc8c37f70a84a64851b440493f3441eb9f70fdb.tar.gz android-node-v8-ebc8c37f70a84a64851b440493f3441eb9f70fdb.tar.bz2 android-node-v8-ebc8c37f70a84a64851b440493f3441eb9f70fdb.zip |
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: https://github.com/nodejs/node/pull/6207
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/module.js | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/lib/internal/module.js b/lib/internal/module.js index aa1ffc0142..a12af12f3e 100644 --- a/lib/internal/module.js +++ b/lib/internal/module.js @@ -59,21 +59,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 }); |