diff options
author | Anna Henningsen <anna@addaleax.net> | 2016-04-15 02:03:12 +0200 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2016-04-18 15:31:32 -0700 |
commit | 39d905e2937ab10040f45b0ab0707f8bd9bf86b9 (patch) | |
tree | e38cb49a02520613e24764d0e3f17559417704f4 /lib | |
parent | 15d970d65c8da7970e6bacb9feccb4b830376881 (diff) | |
download | android-node-v8-39d905e2937ab10040f45b0ab0707f8bd9bf86b9.tar.gz android-node-v8-39d905e2937ab10040f45b0ab0707f8bd9bf86b9.tar.bz2 android-node-v8-39d905e2937ab10040f45b0ab0707f8bd9bf86b9.zip |
node: make builtin libs available for `--eval`
Make the builtin libraries available for the `--eval` and
`--print` CLI options, using the same mechanism that the
REPL uses.
This renders workarounds like `node -e 'require("fs").doStuff()'`
unnecessary.
As part of this, the list of builtin modules and the code for
adding the corresponding properties to the target context is moved
to `internal/module.js`, and the previously missing `repl` entry
is added.
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/bootstrap_node.js | 3 | ||||
-rw-r--r-- | lib/internal/module.js | 35 | ||||
-rw-r--r-- | lib/repl.js | 23 |
3 files changed, 39 insertions, 22 deletions
diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 2960234fa0..195dfbc0fb 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -105,6 +105,9 @@ // User passed '-e' or '--eval' arguments to Node without '-i' or // '--interactive' preloadModules(); + + const internalModule = NativeModule.require('internal/module'); + internalModule.addBuiltinLibsToObject(global); evalScript('[eval]'); } else if (process.argv[1]) { // make process.argv[1] into a full path diff --git a/lib/internal/module.js b/lib/internal/module.js index 9918c73626..aa1ffc0142 100644 --- a/lib/internal/module.js +++ b/lib/internal/module.js @@ -1,6 +1,10 @@ 'use strict'; -exports = module.exports = { makeRequireFunction, stripBOM }; +exports = module.exports = { + makeRequireFunction, + stripBOM, + addBuiltinLibsToObject +}; exports.requireDepth = 0; @@ -46,3 +50,32 @@ function stripBOM(content) { } return content; } + +exports.builtinLibs = ['assert', 'buffer', 'child_process', 'cluster', + 'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net', + 'os', 'path', 'punycode', 'querystring', 'readline', 'repl', 'stream', + 'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib']; + +function addBuiltinLibsToObject(object) { + // Make built-in modules available directly (loaded lazily). + exports.builtinLibs.forEach((name) => { + 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. + delete object[name]; + object[name] = val; + }, + configurable: true, + enumerable: false + }); + }); +} diff --git a/lib/repl.js b/lib/repl.js index 82555497eb..d802ff304c 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -64,10 +64,7 @@ function hasOwnProperty(obj, prop) { // This is the default "writer" value if none is passed in the REPL options. exports.writer = util.inspect; -exports._builtinLibs = ['assert', 'buffer', 'child_process', 'cluster', - 'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net', - 'os', 'path', 'punycode', 'querystring', 'readline', 'stream', - 'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib']; +exports._builtinLibs = internalModule.builtinLibs; const BLOCK_SCOPED_ERROR = 'Block-scoped declarations (let, ' + @@ -565,23 +562,7 @@ REPLServer.prototype.createContext = function() { this.lines = []; this.lines.level = []; - // make built-in modules available directly - // (loaded lazily) - exports._builtinLibs.forEach((name) => { - Object.defineProperty(context, name, { - get: () => { - var lib = require(name); - this.last = context[name] = lib; - return lib; - }, - // allow the creation of other globals with this name - set: (val) => { - delete context[name]; - context[name] = val; - }, - configurable: true - }); - }); + internalModule.addBuiltinLibsToObject(context); Object.defineProperty(context, '_', { configurable: true, |