From 0b5235e68c7f62116a27ea27d4aeb4fc6b211cf4 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 24 Apr 2012 01:34:17 -0700 Subject: [PATCH] process: make --eval and reading scripts from stdin act the same Reusing the same logic for both places for the behavior is consistent. For example: $ ./node -p -e "'Hello World'" Hello World $ echo "'Hello World'" | ./node -p Hello World --- src/node.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/node.js b/src/node.js index cc5e7b65d..ab20dc4ea 100644 --- a/src/node.js +++ b/src/node.js @@ -67,15 +67,7 @@ } else if (process._eval != null) { // User passed '-e' or '--eval' arguments to Node. - var Module = NativeModule.require('module'); - var path = NativeModule.require('path'); - var cwd = process.cwd(); - - var module = new Module('eval'); - module.filename = path.join(cwd, 'eval'); - module.paths = Module._nodeModulePaths(cwd); - var result = module._compile('return eval(process._eval)', 'eval'); - if (process._print_eval) console.log(result); + evalScript('eval'); } else if (process.argv[1]) { // make process.argv[1] into a full path var path = NativeModule.require('path'); @@ -147,7 +139,8 @@ }); process.stdin.on('end', function() { - new Module()._compile(code, '[stdin]'); + process._eval = code; + evalScript('[stdin]'); }); } } @@ -265,6 +258,18 @@ }; }; + function evalScript(name) { + var Module = NativeModule.require('module'); + var path = NativeModule.require('path'); + var cwd = process.cwd(); + + var module = new Module(name); + module.filename = path.join(cwd, name); + module.paths = Module._nodeModulePaths(cwd); + var result = module._compile('return eval(process._eval)', name); + if (process._print_eval) console.log(result); + } + function errnoException(errorno, syscall) { // TODO make this more compatible with ErrnoException from src/node.cc // Once all of Node is using this function the ErrnoException from