Browse Source

Adding support for require-like initialization of node,

so `node foo`
will load one of:
./foo.js
./foo.node
./foo/index.js
./foo/index.node

Test cases added.
Ensured no conflict with native names.
v0.7.4-release
Daniel Ennis 14 years ago
committed by Ryan Dahl
parent
commit
52f93185c7
  1. 7
      lib/module.js
  2. 3
      test/fixtures/test-init-index/index.js
  3. 6
      test/fixtures/test-init-native/fs.js
  4. 1
      test/message/undefined_reference_in_new_context.out
  5. 39
      test/simple/test-init.js

7
lib/module.js

@ -150,8 +150,9 @@ Module._resolveLookupPaths = function(request, parent) {
Module._load = function(request, parent) { Module._load = function(request, parent) {
debug('Module._load REQUEST ' + (request) + if (parent) {
' parent: ' + parent.id); debug('Module._load REQUEST ' + (request) + ' parent: ' + parent.id);
}
var resolved = Module._resolveFilename(request, parent); var resolved = Module._resolveFilename(request, parent);
var id = resolved[0]; var id = resolved[0];
@ -300,7 +301,7 @@ Module._extensions['.node'] = function(module, filename) {
Module.runMain = function() { Module.runMain = function() {
// Load the main module--the command line argument. // Load the main module--the command line argument.
process.mainModule = new Module('.'); process.mainModule = new Module('.');
process.mainModule.load(process.argv[1]); Module._load(process.argv[1]);
}; };
Module._initPaths = function() { Module._initPaths = function() {

3
test/fixtures/test-init-index/index.js

@ -0,0 +1,3 @@
(function() {
require('util').print('Loaded successfully!');
})();

6
test/fixtures/test-init-native/fs.js

@ -0,0 +1,6 @@
(function() {
var fs = require('fs');
if (fs.readFile) {
require('util').print('fs loaded successfully');
}
})();

1
test/message/undefined_reference_in_new_context.out

@ -9,5 +9,6 @@ ReferenceError: foo is not defined
at Module._compile (module.js:*) at Module._compile (module.js:*)
at Object..js (module.js:*) at Object..js (module.js:*)
at Module.load (module.js:*) at Module.load (module.js:*)
at Function._load (module.js:178:10)
at Array.<anonymous> (module.js:*) at Array.<anonymous> (module.js:*)
at EventEmitter._tickCallback (node.js:*) at EventEmitter._tickCallback (node.js:*)

39
test/simple/test-init.js

@ -0,0 +1,39 @@
(function() {
var assert = require('assert'),
child = require('child_process'),
util = require('util'),
common = require('../common');
if (process.env['TEST_INIT']) {
util.print('Loaded successfully!');
} else {
// change CWD as we do this test so its not dependant on current CWD
// being in the test folder
process.chdir(__dirname);
child.exec(process.execPath + ' test-init',{env:{'TEST_INIT':1}},
function(err, stdout, stderr) {
assert.equal(stdout, 'Loaded successfully!', '`node test-init` failed!');
});
child.exec(process.execPath + ' test-init.js', {env:{'TEST_INIT':1}},
function(err, stdout, stderr) {
assert.equal(stdout, 'Loaded successfully!', '`node test-init.js` failed!');
});
// test-init-index is in fixtures dir as requested by ry, so go there
process.chdir(common.fixturesDir);
child.exec(process.execPath + ' test-init-index',{env:{'TEST_INIT':1}},
function(err, stdout, stderr) {
assert.equal(stdout, 'Loaded successfully!', '`node test-init-index failed!');
});
// ensures that `node fs` does not mistakenly load the native 'fs' module
// instead of the desired file and that the fs module loads as expected in node
process.chdir(common.fixturesDir + '/test-init-native/');
child.exec(process.execPath + ' fs', {env:{'TEST_INIT':1}},
function(err, stdout, stderr) {
assert.equal(stdout, 'fs loaded successfully', '`node fs` failed!');
});
}
})();
Loading…
Cancel
Save