mirror of https://github.com/nodejs/node.git
requireNative doesn't depend on rest of module system
This commit is contained in:
parent
11b2ee7632
commit
bb6d468dd8
34
src/node.js
34
src/node.js
|
@ -104,22 +104,32 @@ var module = (function () {
|
||||||
// Like, natives.fs is the contents of lib/fs.js
|
// Like, natives.fs is the contents of lib/fs.js
|
||||||
var natives = process.binding('natives');
|
var natives = process.binding('natives');
|
||||||
|
|
||||||
function loadNative (id) {
|
// Native modules don't need a full require function. So we can bootstrap
|
||||||
var m = new Module(id);
|
// most of the system with this mini-require.
|
||||||
internalModuleCache[id] = m;
|
|
||||||
var e = m._compile(natives[id], id+".js");
|
|
||||||
if (e) throw e; // error compiling native module
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.requireNative = requireNative;
|
|
||||||
|
|
||||||
function requireNative (id) {
|
function requireNative (id) {
|
||||||
if (internalModuleCache[id]) return internalModuleCache[id].exports;
|
if (internalModuleCache[id]) return internalModuleCache[id].exports;
|
||||||
if (!natives[id]) throw new Error('No such native module ' + id);
|
if (!natives[id]) throw new Error('No such native module ' + id);
|
||||||
return loadNative(id).exports;
|
|
||||||
|
// REPL is a special case, because it needs the real require.
|
||||||
|
if (id == 'repl') {
|
||||||
|
var replModule = new Module("repl");
|
||||||
|
replModule._compile(natives.repl, 'repl.js');
|
||||||
|
internalModuleCache.repl = replModule;
|
||||||
|
return replModule.exports;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fn = process.compile(
|
||||||
|
"(function (exports, require) {" + natives[id] + "\n})",
|
||||||
|
id + '.js');
|
||||||
|
var m = new Module(id);
|
||||||
|
fn(m.exports, requireNative);
|
||||||
|
m.loaded = true;
|
||||||
|
internalModuleCache[id] = m;
|
||||||
|
return m.exports;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.requireNative = requireNative;
|
||||||
|
|
||||||
|
|
||||||
// Modules
|
// Modules
|
||||||
|
|
||||||
|
@ -230,7 +240,7 @@ var module = (function () {
|
||||||
}
|
}
|
||||||
if (natives[id]) {
|
if (natives[id]) {
|
||||||
debug('load native module ' + request);
|
debug('load native module ' + request);
|
||||||
return loadNative(id).exports;
|
return requireNative(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
var cachedModule = moduleCache[filename];
|
var cachedModule = moduleCache[filename];
|
||||||
|
|
Loading…
Reference in New Issue