Browse Source

require() should work with `node --eval`.

with tests

Fix module path resolve bug.  Normally the module path is taken from
realpath(__filename) but with eval there is (of course) no filename
v0.7.4-release
Ben Noordhuis 14 years ago
committed by Ryan Dahl
parent
commit
f0b8cc6a94
  1. 25
      src/node.js
  2. 28
      test/simple/test-cli-eval.js

25
src/node.js

@ -111,6 +111,7 @@ var module = (function () {
this.children = []; this.children = [];
}; };
// Modules // Modules
var debugLevel = parseInt(process.env["NODE_DEBUG"], 16); var debugLevel = parseInt(process.env["NODE_DEBUG"], 16);
@ -186,6 +187,13 @@ var module = (function () {
return [request, modulePaths]; return [request, modulePaths];
} }
// with --eval, parent.id is not set and parent.filename is null
if (!parent || !parent.id || !parent.filename) {
// make require('./path/to/foo') work - normally the path is taken
// from realpath(__filename) but with eval there is no filename
return [request, ['.'].concat(modulePaths)];
}
// Is the parent an index module? // Is the parent an index module?
// We can assume the parent has a valid extension, // We can assume the parent has a valid extension,
// as it already has been accepted as a module. // as it already has been accepted as a module.
@ -302,8 +310,7 @@ var module = (function () {
sandbox.global = sandbox; sandbox.global = sandbox;
sandbox.root = root; sandbox.root = root;
evals.Script.runInNewContext(content, sandbox, filename); return evals.Script.runInNewContext(content, sandbox, filename);
} else { } else {
debug('load root module'); debug('load root module');
// root module // root module
@ -312,7 +319,8 @@ var module = (function () {
global.__filename = filename; global.__filename = filename;
global.__dirname = dirname; global.__dirname = dirname;
global.module = self; global.module = self;
evals.Script.runInThisContext(content, filename);
return evals.Script.runInThisContext(content, filename);
} }
} else { } else {
@ -325,7 +333,7 @@ var module = (function () {
if (filename === process.argv[1] && global.v8debug) { if (filename === process.argv[1] && global.v8debug) {
global.v8debug.Debug.setBreakPoint(compiledWrapper, 0, 0); global.v8debug.Debug.setBreakPoint(compiledWrapper, 0, 0);
} }
compiledWrapper.apply(self.exports, [self.exports, require, self, filename, dirname]); return compiledWrapper.apply(self.exports, [self.exports, require, self, filename, dirname]);
} }
}; };
@ -362,6 +370,9 @@ var module = (function () {
// bootstrap repl // bootstrap repl
exports.requireRepl = function () { return loadModule("repl", "."); }; exports.requireRepl = function () { return loadModule("repl", "."); };
// export for --eval
exports.Module = Module;
return exports; return exports;
})(); })();
@ -587,9 +598,9 @@ if (process.argv[1]) {
process.nextTick(module.runMain); process.nextTick(module.runMain);
} else if (process._eval) { } else if (process._eval) {
// -e, --eval // -e, --eval
var indirectEval= eval; // so the eval happens in global scope. var rv = new module.Module()._compile('return eval(process._eval)', 'eval');
if (process._eval) console.log(indirectEval(process._eval)); console.log(rv);
} else { } else {
// REPL // REPL
module.requireRepl().start(); module.requireRepl().start();

28
test/simple/test-cli-eval.js

@ -0,0 +1,28 @@
assert = require('assert');
child = require('child_process');
nodejs = process.execPath;
if (module.parent) {
// signal we've been loaded as a module
console.log('Loaded as a module, exiting with status code 42.');
process.exit(42);
}
// assert that the result of the final expression is written to stdout
child.exec(nodejs + ' --eval \'1337; 42\'',
function(err, stdout, stderr) {
assert.equal(parseInt(stdout), 42);
});
// assert that module loading works
child.exec(nodejs + ' --eval \'require("' + __filename + '")\'',
function(status, stdout, stderr) {
assert.equal(status.code, 42);
});
// module path resolve bug, regression test
child.exec(nodejs + ' --eval \'require("./test/simple/test-cli-eval.js")\'',
function(status, stdout, stderr) {
assert.equal(status.code, 42);
});
Loading…
Cancel
Save