From: Daniel Ennis Date: Tue, 25 Jan 2011 01:52:38 +0000 (-0500) Subject: Adding support for require-like initialization of node, X-Git-Tag: v0.3.7~31 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=52f93185c783beb71535f59e478fcac8912d0c54;p=platform%2Fupstream%2Fnodejs.git 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. --- diff --git a/lib/module.js b/lib/module.js index d13bb38..fbb494a 100644 --- a/lib/module.js +++ b/lib/module.js @@ -150,8 +150,9 @@ Module._resolveLookupPaths = function(request, parent) { Module._load = function(request, parent) { - debug('Module._load REQUEST ' + (request) + - ' parent: ' + parent.id); + if (parent) { + debug('Module._load REQUEST ' + (request) + ' parent: ' + parent.id); + } var resolved = Module._resolveFilename(request, parent); var id = resolved[0]; @@ -300,7 +301,7 @@ Module._extensions['.node'] = function(module, filename) { Module.runMain = function() { // Load the main module--the command line argument. process.mainModule = new Module('.'); - process.mainModule.load(process.argv[1]); + Module._load(process.argv[1]); }; Module._initPaths = function() { diff --git a/test/fixtures/test-init-index/index.js b/test/fixtures/test-init-index/index.js new file mode 100644 index 0000000..1531994 --- /dev/null +++ b/test/fixtures/test-init-index/index.js @@ -0,0 +1,3 @@ +(function() { + require('util').print('Loaded successfully!'); +})(); \ No newline at end of file diff --git a/test/fixtures/test-init-native/fs.js b/test/fixtures/test-init-native/fs.js new file mode 100644 index 0000000..512a18e --- /dev/null +++ b/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'); + } +})(); \ No newline at end of file diff --git a/test/message/undefined_reference_in_new_context.out b/test/message/undefined_reference_in_new_context.out index 7237047..83fe1da 100644 --- a/test/message/undefined_reference_in_new_context.out +++ b/test/message/undefined_reference_in_new_context.out @@ -9,5 +9,6 @@ ReferenceError: foo is not defined at Module._compile (module.js:*) at Object..js (module.js:*) at Module.load (module.js:*) + at Function._load (module.js:178:10) at Array. (module.js:*) at EventEmitter._tickCallback (node.js:*) diff --git a/test/simple/test-init.js b/test/simple/test-init.js new file mode 100644 index 0000000..e6f3b21 --- /dev/null +++ b/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!'); + }); + } +})();