Browse Source

Add __module to reference current node.Module object.

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
d67288b643
  1. 5
      doc/api.txt
  2. 20
      src/node.js

5
doc/api.txt

@ -60,6 +60,11 @@ error reporting.
+__filename+ :: +__filename+ ::
The filename of the script being executed. The filename of the script being executed.
+__module+ ::
A reference to the current module (of type +node.Module+). In particular
+__module.exports+ is the same as the +exports+ object. See +src/node.js+ for
more information.
+require(path)+ :: +require(path)+ ::
See the modules section. See the modules section.

20
src/node.js

@ -120,7 +120,7 @@ if (ENV["NODE_LIBRARY_PATHS"]) {
node.Module = function (filename, parent) { node.Module = function (filename, parent) {
node.assert(filename.charAt(0) == "/"); node.assert(filename.charAt(0) == "/");
this.filename = filename; this.filename = filename;
this.target = {}; this.exports = {};
this.parent = parent; this.parent = parent;
this.loaded = false; this.loaded = false;
@ -137,7 +137,7 @@ node.Module.cache = {};
if (fullPath in node.Module.cache) { if (fullPath in node.Module.cache) {
module = node.Module.cache[fullPath]; module = node.Module.cache[fullPath];
setTimeout(function () { setTimeout(function () {
loadPromise.emitSuccess(module.target); loadPromise.emitSuccess(module.exports);
}, 0); }, 0);
} else { } else {
module = new node.Module(fullPath, parent); module = new node.Module(fullPath, parent);
@ -167,13 +167,13 @@ node.Module.cache = {};
} }
} }
node.loadModule = function (requestedPath, target, parent) { node.loadModule = function (requestedPath, exports, parent) {
var loadPromise = new node.Promise(); var loadPromise = new node.Promise();
// On success copy the loaded properties into the target // On success copy the loaded properties into the exports
loadPromise.addCallback(function (t) { loadPromise.addCallback(function (t) {
for (var prop in t) { for (var prop in t) {
if (t.hasOwnProperty(prop)) target[prop] = t[prop]; if (t.hasOwnProperty(prop)) exports[prop] = t[prop];
} }
}); });
@ -232,8 +232,8 @@ node.Module.prototype.loadObject = function (loadPromise) {
node.fs.exists(self.filename, function (does_exist) { node.fs.exists(self.filename, function (does_exist) {
if (does_exist) { if (does_exist) {
self.loaded = true; self.loaded = true;
node.dlopen(self.filename, self.target); // FIXME synchronus node.dlopen(self.filename, self.exports); // FIXME synchronus
loadPromise.emitSuccess(self.target); loadPromise.emitSuccess(self.exports);
} else { } else {
loadPromise.emitError(new Error("Error reading " + self.filename)); loadPromise.emitError(new Error("Error reading " + self.filename));
} }
@ -263,14 +263,14 @@ node.Module.prototype.loadScript = function (loadPromise) {
require.async = requireAsync; require.async = requireAsync;
// create wrapper function // create wrapper function
var wrapper = "function (__filename, exports, require) { " + content + "\n};"; var wrapper = "function (__module, __filename, exports, require) { " + content + "\n};";
var compiled_wrapper = node.compile(wrapper, self.filename); var compiled_wrapper = node.compile(wrapper, self.filename);
compiled_wrapper.apply(self.target, [self.filename, self.target, require]); compiled_wrapper.apply(self.exports, [self, self.filename, self.exports, require]);
self.waitChildrenLoad(function () { self.waitChildrenLoad(function () {
self.loaded = true; self.loaded = true;
loadPromise.emitSuccess(self.target); loadPromise.emitSuccess(self.exports);
}); });
}); });
}; };

Loading…
Cancel
Save