Browse Source

Implemented __dirname

It seems that the current __filename module global is mainly used to
determine the directory the current module is in. To make that
easier, this patch adds support for a __dirname module global
directly.
v0.7.4-release
Felix Geisendörfer 15 years ago
committed by Ryan Dahl
parent
commit
a76c7a89ce
  1. 3
      doc/api.txt
  2. 4
      src/node.js
  3. 4
      test/mjsunit/test-module-loading.js

3
doc/api.txt

@ -64,6 +64,9 @@ The search path for absolute path arguments to +require()+.
+__filename+ :: +__filename+ ::
The filename of the script being executed. The filename of the script being executed.
+__dirname+ ::
The dirname of the script being executed.
+module+ :: +module+ ::
A reference to the current module (of type +process.Module+). In particular A reference to the current module (of type +process.Module+). In particular
+module.exports+ is the same as the +exports+ object. See +src/process.js+ for +module.exports+ is the same as the +exports+ object. See +src/process.js+ for

4
src/node.js

@ -919,13 +919,13 @@ Module.prototype.loadScript = function (filename, loadPromise) {
require.async = requireAsync; require.async = requireAsync;
require.main = process.mainModule; require.main = process.mainModule;
// create wrapper function // create wrapper function
var wrapper = "var __wrap__ = function (exports, require, module, __filename) { " var wrapper = "var __wrap__ = function (exports, require, module, __filename, __dirname) { "
+ content + content
+ "\n}; __wrap__;"; + "\n}; __wrap__;";
try { try {
var compiledWrapper = process.compile(wrapper, filename); var compiledWrapper = process.compile(wrapper, filename);
compiledWrapper.apply(self.exports, [self.exports, require, self, filename]); compiledWrapper.apply(self.exports, [self.exports, require, self, filename, path.dirname(filename)]);
} catch (e) { } catch (e) {
loadPromise.emitError(e); loadPromise.emitError(e);
return; return;

4
test/mjsunit/test-module-loading.js

@ -6,7 +6,7 @@ var a = require("./fixtures/a");
var d = require("./fixtures/b/d"); var d = require("./fixtures/b/d");
var d2 = require("./fixtures/b/d"); var d2 = require("./fixtures/b/d");
// Absolute // Absolute
var d3 = require(require('path').dirname(__filename)+"/fixtures/b/d"); var d3 = require(__dirname+"/fixtures/b/d");
// Relative // Relative
var d4 = require("../mjsunit/fixtures/b/d"); var d4 = require("../mjsunit/fixtures/b/d");
@ -52,6 +52,8 @@ try {
assert.equal("blah", e.message); assert.equal("blah", e.message);
} }
assert.equal(require('path').dirname(__filename), __dirname);
process.addListener("exit", function () { process.addListener("exit", function () {
assert.equal(true, a.A instanceof Function); assert.equal(true, a.A instanceof Function);
assert.equal("A done", a.A()); assert.equal("A done", a.A());

Loading…
Cancel
Save