Permalink
Browse files

Remove require.async

This patch removes require.async from nodejs.

1. It complicated the code unnecessarily.
2. Everyone uses sync require anyway.
3. It's got a lot of weird edge cases when mixed with sync require.
4. It is many months behind the commonjs spec anyhow.
  • Loading branch information...
1 parent 2d348bb commit bb08f0c219bf4ca99b1129f54ef415ce46b01f22 @isaacs isaacs committed with ry Sep 19, 2010
Showing with 28 additions and 188 deletions.
  1. +26 −155 src/node.js
  2. +2 −2 test/message/undefined_reference_in_new_context.out
  3. +0 −31 test/simple/test-module-loading.js
View
@@ -106,7 +106,6 @@ var module = (function () {
internalModuleCache[id] = m;
var e = m._compile(natives[id], id);
if (e) throw e;
- m.loaded = true;
return m;
}
@@ -172,44 +171,19 @@ var module = (function () {
};
}
- /* Sync unless callback given */
- function findModulePath (id, dirs, callback) {
+ function findModulePath (id, dirs) {
process.assert(Array.isArray(dirs));
- if (/^https?:\/\//.exec(id)) {
- if (callback) {
- callback(id);
- } else {
- throw new Error("Sync http require not allowed.");
- }
- return;
- }
-
var nextLoc = traverser(id, id.charAt(0) === '/' ? [''] : dirs);
var fs = requireNative('fs');
- function searchLocations () {
- var location, stats;
- while (location = nextLoc()) {
- try { stats = fs.statSync(location); } catch(e) { continue; }
- if (stats && !stats.isDirectory()) return location;
- }
- return false;
+ var location, stats;
+ while (location = nextLoc()) {
+ try { stats = fs.statSync(location); } catch(e) { continue; }
+ if (stats && !stats.isDirectory()) return location;
}
-
- function searchLocationsAsync (cb) {
- var location = nextLoc();
-
- if (!location) { cb(false); return; }
-
- fs.stat(location, function (err, stats) {
- if (stats && !stats.isDirectory()) { cb(location); }
- else { searchLocationsAsync(cb); }
- });
- }
-
- return callback ? searchLocationsAsync(callback) : searchLocations();
+ return false;
}
@@ -235,7 +209,7 @@ var module = (function () {
}
- function loadModule (request, parent, callback) {
+ function loadModule (request, parent) {
var resolvedModule = resolveModulePath(request, parent),
id = resolvedModule[0],
paths = resolvedModule[1];
@@ -245,45 +219,27 @@ var module = (function () {
// native modules always take precedence.
var cachedNative = internalModuleCache[id];
if (cachedNative) {
- return callback ? callback(null, cachedNative.exports) : cachedNative.exports;
+ return cachedNative.exports;
}
if (natives[id]) {
debug('load native module ' + id);
- var nativeMod = loadNative(id);
- return callback ? callback(null, nativeMod.exports) : nativeMod.exports;
+ return loadNative(id).exports;
}
// look up the filename first, since that's the cache key.
debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));
- if (!callback) {
- // sync
- var filename = findModulePath(request, paths);
- if (!filename) {
- throw new Error("Cannot find module '" + request + "'");
- }
-
- var cachedModule = parent.moduleCache[filename];
- if (cachedModule) return cachedModule.exports;
-
- var module = new Module(id, parent);
- module.moduleCache[filename] = module;
- module.loadSync(filename);
- return module.exports;
+ var filename = findModulePath(request, paths);
+ if (!filename) {
+ throw new Error("Cannot find module '" + request + "'");
}
- // async
- findModulePath(request, paths, function (filename) {
- if (!filename) {
- var err = new Error("Cannot find module '" + request + "'");
- return callback(err);
- }
- var cachedModule = parent.moduleCache[filename];
- if (cachedModule) return callback(null, cachedModule.exports);
+ var cachedModule = parent.moduleCache[filename];
+ if (cachedModule) return cachedModule.exports;
- var module = new Module(id, parent);
- module.moduleCache[filename] = module;
- module.load(filename, callback);
- });
+ var module = new Module(id, parent);
+ module.moduleCache[filename] = module;
+ module.load(filename);
+ return module.exports;
};
@@ -313,63 +269,27 @@ var module = (function () {
}
- Module.prototype.loadSync = function (filename) {
- debug("loadSync " + JSON.stringify(filename) + " for module " + JSON.stringify(this.id));
-
- process.assert(!this.loaded);
- this.filename = filename;
-
- if (filename.match(/\.node$/)) {
- this._loadObjectSync(filename);
- } else {
- this._loadScriptSync(filename);
- }
- };
-
-
- Module.prototype.load = function (filename, callback) {
+ Module.prototype.load = function (filename) {
debug("load " + JSON.stringify(filename) + " for module " + JSON.stringify(this.id));
process.assert(!this.loaded);
-
this.filename = filename;
if (filename.match(/\.node$/)) {
- this._loadObject(filename, callback);
+ this._loadObject(filename);
} else {
- this._loadScript(filename, callback);
+ this._loadScript(filename);
}
};
- Module.prototype._loadObjectSync = function (filename) {
- this.loaded = true;
+ Module.prototype._loadObject = function (filename) {
process.dlopen(filename, this.exports);
};
- Module.prototype._loadObject = function (filename, callback) {
- var self = this;
- // XXX Not yet supporting loading from HTTP. would need to download the
- // file, store it to tmp then run dlopen on it.
- self.loaded = true;
- process.dlopen(filename, self.exports); // FIXME synchronus
- if (callback) callback(null, self.exports);
- };
-
-
- function cat (id, callback) {
- if (id.match(/^http:\/\//)) {
- loadModule('http', process.mainModule, function (err, http) {
- if (err) {
- if (callback) callback(err);
- } else {
- http.cat(id, callback);
- }
- });
- } else {
- requireNative('fs').readFile(id, 'utf8', callback);
- }
+ function cat (id) {
+ requireNative('fs').readFile(id, 'utf8');
}
@@ -390,16 +310,11 @@ var module = (function () {
return;
}
- function requireAsync (url, cb) {
- loadModule(url, self, cb);
- }
-
function require (path) {
return loadModule(path, self);
}
require.paths = modulePaths;
- require.async = requireAsync;
require.main = process.mainModule;
require.registerExtension = registerExtension;
@@ -451,62 +366,18 @@ var module = (function () {
};
- Module.prototype._loadScriptSync = function (filename) {
+ Module.prototype._loadScript = function (filename) {
var content = requireNative('fs').readFileSync(filename, 'utf8');
this._compile(content, filename);
this.loaded = true;
};
- Module.prototype._loadScript = function (filename, callback) {
- var self = this;
- cat(filename, function (err, content) {
- debug('cat done');
- if (err) {
- if (callback) callback(err);
- } else {
- try {
- self._compile(content, filename);
- } catch (err) {
- if (callback) callback(err);
- return;
- }
-
- self._waitChildrenLoad(function () {
- self.loaded = true;
- if (self.onload) self.onload();
- if (callback) callback(null, self.exports);
- });
- }
- });
- };
-
-
- Module.prototype._waitChildrenLoad = function (callback) {
- var nloaded = 0;
- var children = this.children;
- for (var i = 0; i < children.length; i++) {
- var child = children[i];
- if (child.loaded) {
- nloaded++;
- } else {
- child.onload = function () {
- child.onload = null;
- nloaded++;
- if (children.length == nloaded && callback) callback();
- };
- }
- }
- if (children.length == nloaded && callback) callback();
- };
-
-
-
// bootstrap main module.
exports.runMain = function () {
// Load the main module--the command line argument.
process.mainModule = new Module(".");
- process.mainModule.loadSync(process.argv[1]);
+ process.mainModule.load(process.argv[1]);
}
return exports;
@@ -7,8 +7,8 @@ ReferenceError: foo is not defined
at evalmachine.<anonymous>:*
at *test/message/undefined_reference_in_new_context.js:*
at Module._compile (node.js:*)
- at Module._loadScriptSync (node.js:*)
- at Module.loadSync (node.js:*)
+ at Module._loadScript (node.js:*)
+ at Module.load (node.js:*)
at Object.runMain (node.js:*)
at Array.<anonymous> (node.js:*)
at EventEmitter._tickCallback (node.js:*)
@@ -64,23 +64,8 @@ try {
assert.equal("blah", e.message);
}
-var errorThrownAsync = false;
-require.async("../fixtures/throws_error1", function(err, a) {
- if (err) {
- errorThrownAsync = true;
- assert.equal("blah", err.message);
- }
-});
-
assert.equal(require('path').dirname(__filename), __dirname);
-var asyncRun = false;
-require.async('../fixtures/a1', function (err, a) {
- if (err) throw err;
- assert.equal("A", a.A());
- asyncRun = true;
-});
-
common.debug('load custom file types with registerExtension');
require.registerExtension('.test', function(content) {
assert.equal("this is custom source\n", content);
@@ -113,16 +98,6 @@ try {
assert.equal(err.message, "Cannot find module '../fixtures/empty'");
}
-var asyncRequireDir = false;
-require.async("../fixtures/empty", function (err, a) {
- assert.ok(err);
-
- if (err) {
- asyncRequireDir = true;
- assert.equal(err.message, "Cannot find module '../fixtures/empty'");
- }
-});
-
// Check load order is as expected
common.debug('load order');
@@ -169,11 +144,5 @@ process.addListener("exit", function () {
assert.equal(true, errorThrown);
- assert.equal(true, asyncRun);
-
- assert.equal(true, errorThrownAsync);
-
- assert.equal(true, asyncRequireDir);
-
console.log("exit");
});

1 comment on commit bb08f0c

@jiyinyiyong

:(
Sad that it was removed. Related discussion here: island205/bodule#3
If Node is compatible with the syntax require "name", ->, we can do some hacks to reuse browser code in Node, and per se.
The door was closed long time ago.

Please sign in to comment.