mirror of
https://github.com/nodejs/node.git
synced 2024-11-29 23:16:30 +01:00
Add __module to reference current node.Module object.
This commit is contained in:
parent
78f8b6771c
commit
d67288b643
@ -60,6 +60,11 @@ error reporting.
|
||||
+__filename+ ::
|
||||
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)+ ::
|
||||
See the modules section.
|
||||
|
||||
|
20
src/node.js
20
src/node.js
@ -120,7 +120,7 @@ if (ENV["NODE_LIBRARY_PATHS"]) {
|
||||
node.Module = function (filename, parent) {
|
||||
node.assert(filename.charAt(0) == "/");
|
||||
this.filename = filename;
|
||||
this.target = {};
|
||||
this.exports = {};
|
||||
this.parent = parent;
|
||||
|
||||
this.loaded = false;
|
||||
@ -137,7 +137,7 @@ node.Module.cache = {};
|
||||
if (fullPath in node.Module.cache) {
|
||||
module = node.Module.cache[fullPath];
|
||||
setTimeout(function () {
|
||||
loadPromise.emitSuccess(module.target);
|
||||
loadPromise.emitSuccess(module.exports);
|
||||
}, 0);
|
||||
} else {
|
||||
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();
|
||||
|
||||
// On success copy the loaded properties into the target
|
||||
// On success copy the loaded properties into the exports
|
||||
loadPromise.addCallback(function (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) {
|
||||
if (does_exist) {
|
||||
self.loaded = true;
|
||||
node.dlopen(self.filename, self.target); // FIXME synchronus
|
||||
loadPromise.emitSuccess(self.target);
|
||||
node.dlopen(self.filename, self.exports); // FIXME synchronus
|
||||
loadPromise.emitSuccess(self.exports);
|
||||
} else {
|
||||
loadPromise.emitError(new Error("Error reading " + self.filename));
|
||||
}
|
||||
@ -263,14 +263,14 @@ node.Module.prototype.loadScript = function (loadPromise) {
|
||||
require.async = requireAsync;
|
||||
|
||||
// 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);
|
||||
|
||||
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.loaded = true;
|
||||
loadPromise.emitSuccess(self.target);
|
||||
loadPromise.emitSuccess(self.exports);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user