GeezerVison is showing github.com — node: make builtin libs available for `--eval` · nodejs/node@87e0734
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/cli.markdown +2-1Lines changed: 2 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -36,7 +36,8 @@ The output of this option is less detailed than this document. | 36 | 36 |
| 37 | 37 | ### `-e`, `--eval "script"` | 38 | 38 |
| 39 | | -Evaluate the following argument as JavaScript. | | 39 | +Evaluate the following argument as JavaScript. The modules which are | | 40 | +predefined in the REPL can also be used in `script`. | 40 | 41 |
| 41 | 42 |
| 42 | 43 | ### `-p`, `--print "script"` | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap_node.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 94 | 94 | // User passed '-e' or '--eval' arguments to Node without '-i' or | 95 | 95 | // '--interactive' | 96 | 96 | preloadModules(); | | 97 | + | | 98 | + const internalModule = NativeModule.require('internal/module'); | | 99 | + internalModule.addBuiltinLibsToObject(global); | 97 | 100 | evalScript('[eval]'); | 98 | 101 | } else if (process.argv[1]) { | 99 | 102 | // make process.argv[1] into a full path | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/module.js +34-1Lines changed: 34 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -exports = module.exports = { makeRequireFunction, stripBOM }; | | 3 | +exports = module.exports = { | | 4 | + makeRequireFunction, | | 5 | + stripBOM, | | 6 | + addBuiltinLibsToObject | | 7 | +}; | 4 | 8 |
| 5 | 9 | exports.requireDepth = 0; | 6 | 10 |
| @@ -44,3 +48,32 @@ function stripBOM(content) { | 44 | 48 | } | 45 | 49 | return content; | 46 | 50 | } | | 51 | + | | 52 | +exports.builtinLibs = ['assert', 'buffer', 'child_process', 'cluster', | | 53 | + 'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net', | | 54 | + 'os', 'path', 'punycode', 'querystring', 'readline', 'repl', 'stream', | | 55 | + 'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib']; | | 56 | + | | 57 | +function addBuiltinLibsToObject(object) { | | 58 | + // Make built-in modules available directly (loaded lazily). | | 59 | + exports.builtinLibs.forEach((name) => { | | 60 | + Object.defineProperty(object, name, { | | 61 | + get: () => { | | 62 | + 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. | | 72 | + delete object[name]; | | 73 | + object[name] = val; | | 74 | + }, | | 75 | + configurable: true, | | 76 | + enumerable: false | | 77 | + }); | | 78 | + }); | | 79 | +} |
Collapse file Copy file name to clipboardExpand all lines: lib/repl.js +2-21Lines changed: 2 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,10 +64,7 @@ function hasOwnProperty(obj, prop) { | 64 | 64 | // This is the default "writer" value if none is passed in the REPL options. | 65 | 65 | exports.writer = util.inspect; | 66 | 66 |
| 67 | | -exports._builtinLibs = ['assert', 'buffer', 'child_process', 'cluster', | 68 | | - 'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net', | 69 | | - 'os', 'path', 'punycode', 'querystring', 'readline', 'stream', | 70 | | - 'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib']; | | 67 | +exports._builtinLibs = internalModule.builtinLibs; | 71 | 68 |
| 72 | 69 |
| 73 | 70 | const BLOCK_SCOPED_ERROR = 'Block-scoped declarations (let, const, function, ' + | @@ -559,23 +556,7 @@ REPLServer.prototype.createContext = function() { | 559 | 556 | this.lines = []; | 560 | 557 | this.lines.level = []; | 561 | 558 |
| 562 | | - // make built-in modules available directly | 563 | | - // (loaded lazily) | 564 | | - exports._builtinLibs.forEach(function(name) { | 565 | | - Object.defineProperty(context, name, { | 566 | | - get: function() { | 567 | | - var lib = require(name); | 568 | | - context._ = context[name] = lib; | 569 | | - return lib; | 570 | | - }, | 571 | | - // allow the creation of other globals with this name | 572 | | - set: function(val) { | 573 | | - delete context[name]; | 574 | | - context[name] = val; | 575 | | - }, | 576 | | - configurable: true | 577 | | - }); | 578 | | - }); | | 559 | + internalModule.addBuiltinLibsToObject(context); | 579 | 560 |
| 580 | 561 | return context; | 581 | 562 | }; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-cli-eval.js +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,13 @@ child.exec(nodejs + ' --eval "require(\'' + filename + '\')"', | 53 | 53 | assert.equal(status.code, 42); | 54 | 54 | }); | 55 | 55 |
| | 56 | +// Check that builtin modules are pre-defined. | | 57 | +child.exec(nodejs + ' --print "os.platform()"', | | 58 | + function(status, stdout, stderr) { | | 59 | + assert.strictEqual(stderr, ''); | | 60 | + assert.strictEqual(stdout.trim(), require('os').platform()); | | 61 | + }); | | 62 | + | 56 | 63 | // module path resolve bug, regression test | 57 | 64 | child.exec(nodejs + ' --eval "require(\'./test/parallel/test-cli-eval.js\')"', | 58 | 65 | function(status, stdout, stderr) { | |
|