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.
This commit is contained in:
parent
2d348bb985
commit
bb08f0c219
181
src/node.js
181
src/node.js
@ -106,7 +106,6 @@ var module = (function () {
|
|||||||
internalModuleCache[id] = m;
|
internalModuleCache[id] = m;
|
||||||
var e = m._compile(natives[id], id);
|
var e = m._compile(natives[id], id);
|
||||||
if (e) throw e;
|
if (e) throw e;
|
||||||
m.loaded = true;
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,44 +171,19 @@ var module = (function () {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sync unless callback given */
|
function findModulePath (id, dirs) {
|
||||||
function findModulePath (id, dirs, callback) {
|
|
||||||
process.assert(Array.isArray(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 nextLoc = traverser(id, id.charAt(0) === '/' ? [''] : dirs);
|
||||||
|
|
||||||
var fs = requireNative('fs');
|
var fs = requireNative('fs');
|
||||||
|
|
||||||
function searchLocations () {
|
var location, stats;
|
||||||
var location, stats;
|
while (location = nextLoc()) {
|
||||||
while (location = nextLoc()) {
|
try { stats = fs.statSync(location); } catch(e) { continue; }
|
||||||
try { stats = fs.statSync(location); } catch(e) { continue; }
|
if (stats && !stats.isDirectory()) return location;
|
||||||
if (stats && !stats.isDirectory()) return location;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -235,7 +209,7 @@ var module = (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function loadModule (request, parent, callback) {
|
function loadModule (request, parent) {
|
||||||
var resolvedModule = resolveModulePath(request, parent),
|
var resolvedModule = resolveModulePath(request, parent),
|
||||||
id = resolvedModule[0],
|
id = resolvedModule[0],
|
||||||
paths = resolvedModule[1];
|
paths = resolvedModule[1];
|
||||||
@ -245,45 +219,27 @@ var module = (function () {
|
|||||||
// native modules always take precedence.
|
// native modules always take precedence.
|
||||||
var cachedNative = internalModuleCache[id];
|
var cachedNative = internalModuleCache[id];
|
||||||
if (cachedNative) {
|
if (cachedNative) {
|
||||||
return callback ? callback(null, cachedNative.exports) : cachedNative.exports;
|
return cachedNative.exports;
|
||||||
}
|
}
|
||||||
if (natives[id]) {
|
if (natives[id]) {
|
||||||
debug('load native module ' + id);
|
debug('load native module ' + id);
|
||||||
var nativeMod = loadNative(id);
|
return loadNative(id).exports;
|
||||||
return callback ? callback(null, nativeMod.exports) : nativeMod.exports;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// look up the filename first, since that's the cache key.
|
// look up the filename first, since that's the cache key.
|
||||||
debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));
|
debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));
|
||||||
if (!callback) {
|
var filename = findModulePath(request, paths);
|
||||||
// sync
|
if (!filename) {
|
||||||
var filename = findModulePath(request, paths);
|
throw new Error("Cannot find module '" + request + "'");
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
// async
|
|
||||||
findModulePath(request, paths, function (filename) {
|
|
||||||
if (!filename) {
|
|
||||||
var err = new Error("Cannot find module '" + request + "'");
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
var cachedModule = parent.moduleCache[filename];
|
var cachedModule = parent.moduleCache[filename];
|
||||||
if (cachedModule) return callback(null, cachedModule.exports);
|
if (cachedModule) return cachedModule.exports;
|
||||||
|
|
||||||
var module = new Module(id, parent);
|
var module = new Module(id, parent);
|
||||||
module.moduleCache[filename] = module;
|
module.moduleCache[filename] = module;
|
||||||
module.load(filename, callback);
|
module.load(filename);
|
||||||
});
|
return module.exports;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -313,63 +269,27 @@ var module = (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Module.prototype.loadSync = function (filename) {
|
Module.prototype.load = 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) {
|
|
||||||
debug("load " + JSON.stringify(filename) + " for module " + JSON.stringify(this.id));
|
debug("load " + JSON.stringify(filename) + " for module " + JSON.stringify(this.id));
|
||||||
|
|
||||||
process.assert(!this.loaded);
|
process.assert(!this.loaded);
|
||||||
|
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
|
|
||||||
if (filename.match(/\.node$/)) {
|
if (filename.match(/\.node$/)) {
|
||||||
this._loadObject(filename, callback);
|
this._loadObject(filename);
|
||||||
} else {
|
} else {
|
||||||
this._loadScript(filename, callback);
|
this._loadScript(filename);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Module.prototype._loadObjectSync = function (filename) {
|
Module.prototype._loadObject = function (filename) {
|
||||||
this.loaded = true;
|
|
||||||
process.dlopen(filename, this.exports);
|
process.dlopen(filename, this.exports);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Module.prototype._loadObject = function (filename, callback) {
|
function cat (id) {
|
||||||
var self = this;
|
requireNative('fs').readFile(id, 'utf8');
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -390,16 +310,11 @@ var module = (function () {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
function requireAsync (url, cb) {
|
|
||||||
loadModule(url, self, cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
function require (path) {
|
function require (path) {
|
||||||
return loadModule(path, self);
|
return loadModule(path, self);
|
||||||
}
|
}
|
||||||
|
|
||||||
require.paths = modulePaths;
|
require.paths = modulePaths;
|
||||||
require.async = requireAsync;
|
|
||||||
require.main = process.mainModule;
|
require.main = process.mainModule;
|
||||||
require.registerExtension = registerExtension;
|
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');
|
var content = requireNative('fs').readFileSync(filename, 'utf8');
|
||||||
this._compile(content, filename);
|
this._compile(content, filename);
|
||||||
this.loaded = true;
|
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.
|
// bootstrap main module.
|
||||||
exports.runMain = function () {
|
exports.runMain = function () {
|
||||||
// Load the main module--the command line argument.
|
// Load the main module--the command line argument.
|
||||||
process.mainModule = new Module(".");
|
process.mainModule = new Module(".");
|
||||||
process.mainModule.loadSync(process.argv[1]);
|
process.mainModule.load(process.argv[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return exports;
|
return exports;
|
||||||
|
@ -7,8 +7,8 @@ ReferenceError: foo is not defined
|
|||||||
at evalmachine.<anonymous>:*
|
at evalmachine.<anonymous>:*
|
||||||
at *test/message/undefined_reference_in_new_context.js:*
|
at *test/message/undefined_reference_in_new_context.js:*
|
||||||
at Module._compile (node.js:*)
|
at Module._compile (node.js:*)
|
||||||
at Module._loadScriptSync (node.js:*)
|
at Module._loadScript (node.js:*)
|
||||||
at Module.loadSync (node.js:*)
|
at Module.load (node.js:*)
|
||||||
at Object.runMain (node.js:*)
|
at Object.runMain (node.js:*)
|
||||||
at Array.<anonymous> (node.js:*)
|
at Array.<anonymous> (node.js:*)
|
||||||
at EventEmitter._tickCallback (node.js:*)
|
at EventEmitter._tickCallback (node.js:*)
|
||||||
|
@ -64,23 +64,8 @@ try {
|
|||||||
assert.equal("blah", e.message);
|
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);
|
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');
|
common.debug('load custom file types with registerExtension');
|
||||||
require.registerExtension('.test', function(content) {
|
require.registerExtension('.test', function(content) {
|
||||||
assert.equal("this is custom source\n", content);
|
assert.equal("this is custom source\n", content);
|
||||||
@ -113,16 +98,6 @@ try {
|
|||||||
assert.equal(err.message, "Cannot find module '../fixtures/empty'");
|
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
|
// Check load order is as expected
|
||||||
common.debug('load order');
|
common.debug('load order');
|
||||||
|
|
||||||
@ -169,11 +144,5 @@ process.addListener("exit", function () {
|
|||||||
|
|
||||||
assert.equal(true, errorThrown);
|
assert.equal(true, errorThrown);
|
||||||
|
|
||||||
assert.equal(true, asyncRun);
|
|
||||||
|
|
||||||
assert.equal(true, errorThrownAsync);
|
|
||||||
|
|
||||||
assert.equal(true, asyncRequireDir);
|
|
||||||
|
|
||||||
console.log("exit");
|
console.log("exit");
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user