Permalink
Browse files

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
  • Loading branch information...
1 parent 38153ea commit c5e90a68e764052e74d66aaa82e9341a3a418d6a @TooTallNate committed Apr 24, 2012
Showing with 15 additions and 10 deletions.
  1. +15 −10 src/node.js
View
@@ -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

0 comments on commit c5e90a6

Please sign in to comment.