Browse Source

module.name renamed to module.id for commonjs compat

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
f683867b98
  1. 4
      doc/api.txt
  2. 54
      src/node.js

4
doc/api.txt

@ -64,9 +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.
+__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
more information. more information.

54
src/node.js

@ -181,8 +181,8 @@ function debug (x) {
// private constructor // private constructor
function Module (name, parent) { function Module (id, parent) {
this.name = name; this.id = id;
this.exports = {}; this.exports = {};
this.parent = parent; this.parent = parent;
@ -195,19 +195,19 @@ function Module (name, parent) {
var moduleCache = {}; var moduleCache = {};
function createModule (name, parent) { function createModule (id, parent) {
if (name in moduleCache) { if (id in moduleCache) {
debug("found " + JSON.stringify(name) + " in cache"); debug("found " + JSON.stringify(id) + " in cache");
return moduleCache[name]; return moduleCache[id];
} }
debug("didn't found " + JSON.stringify(name) + " in cache. creating new module"); debug("didn't found " + JSON.stringify(id) + " in cache. creating new module");
var m = new Module(name, parent); var m = new Module(id, parent);
moduleCache[name] = m; moduleCache[id] = m;
return m; return m;
}; };
function createInternalModule (name, constructor) { function createInternalModule (id, constructor) {
var m = createModule(name); var m = createModule(id);
constructor(m.exports); constructor(m.exports);
m.loaded = true; m.loaded = true;
return m; return m;
@ -270,10 +270,10 @@ if (process.ENV["NODE_PATH"]) {
} }
function findModulePath (name, dirs, callback) { function findModulePath (id, dirs, callback) {
process.assert(dirs.constructor == Array); process.assert(dirs.constructor == Array);
if (/.(js|node)$/.exec(name)) { if (/.(js|node)$/.exec(id)) {
throw new Error("No longer accepting filename extension in module names"); throw new Error("No longer accepting filename extension in module names");
} }
@ -285,10 +285,10 @@ function findModulePath (name, dirs, callback) {
var dir = dirs[0]; var dir = dirs[0];
var rest = dirs.slice(1, dirs.length); var rest = dirs.slice(1, dirs.length);
var js = path.join(dir, name + ".js"); var js = path.join(dir, id + ".js");
var addon = path.join(dir, name + ".node"); var addon = path.join(dir, id + ".node");
var indexJs = path.join(dir, name, "index.js"); var indexJs = path.join(dir, id, "index.js");
var indexAddon = path.join(dir, name, "index.addon"); var indexAddon = path.join(dir, id, "index.addon");
// TODO clean up the following atrocity! // TODO clean up the following atrocity!
@ -312,7 +312,7 @@ function findModulePath (name, dirs, callback) {
callback(indexAddon); callback(indexAddon);
return; return;
} }
findModulePath(name, rest, callback); findModulePath(id, rest, callback);
}); });
}); });
}); });
@ -330,31 +330,31 @@ function loadModule (request, parent) {
debug("loadModule REQUEST " + JSON.stringify(request) + " parent: " + JSON.stringify(parent)); debug("loadModule REQUEST " + JSON.stringify(request) + " parent: " + JSON.stringify(parent));
var name, paths; var id, paths;
if (request.charAt(0) == "." && request.charAt(1) == "/") { if (request.charAt(0) == "." && request.charAt(1) == "/") {
// Relative request // Relative request
name = path.join(path.dirname(parent.name), request); id = path.join(path.dirname(parent.id), request);
paths = [path.dirname(parent.filename)]; paths = [path.dirname(parent.filename)];
} else { } else {
name = request; id = request;
paths = modulePaths; paths = modulePaths;
} }
if (name in moduleCache) { if (id in moduleCache) {
debug("found " + JSON.stringify(name) + " in cache"); debug("found " + JSON.stringify(id) + " in cache");
// In cache // In cache
var module = moduleCache[name]; var module = moduleCache[id];
setTimeout(function () { setTimeout(function () {
loadPromise.emitSuccess(module.exports); loadPromise.emitSuccess(module.exports);
}, 0); }, 0);
} else { } else {
debug("looking for " + JSON.stringify(name) + " in " + JSON.stringify(paths)); debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));
// Not in cache // Not in cache
findModulePath(request, paths, function (filename) { findModulePath(request, paths, function (filename) {
if (!filename) { if (!filename) {
loadPromise.emitError(new Error("Cannot find module '" + request + "'")); loadPromise.emitError(new Error("Cannot find module '" + request + "'"));
} else { } else {
var module = createModule(name, parent); var module = createModule(id, parent);
module.load(filename, loadPromise); module.load(filename, loadPromise);
} }
}); });
@ -364,7 +364,7 @@ function loadModule (request, parent) {
}; };
Module.prototype.load = function (filename, loadPromise) { Module.prototype.load = function (filename, loadPromise) {
debug("load " + JSON.stringify(filename) + " for module " + JSON.stringify(this.name)); debug("load " + JSON.stringify(filename) + " for module " + JSON.stringify(this.id));
process.assert(!this.loaded); process.assert(!this.loaded);
process.assert(!this.loadPromise); process.assert(!this.loadPromise);

Loading…
Cancel
Save