Skip to content

Commit bb6d468

Browse files
committed
requireNative doesn't depend on rest of module system
1 parent 11b2ee7 commit bb6d468

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/node.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,32 @@ var module = (function () {
104104
// Like, natives.fs is the contents of lib/fs.js
105105
var natives = process.binding('natives');
106106

107-
function loadNative (id) {
107+
// Native modules don't need a full require function. So we can bootstrap
108+
// most of the system with this mini-require.
109+
function requireNative (id) {
110+
if (internalModuleCache[id]) return internalModuleCache[id].exports;
111+
if (!natives[id]) throw new Error('No such native module ' + id);
112+
113+
// REPL is a special case, because it needs the real require.
114+
if (id == 'repl') {
115+
var replModule = new Module("repl");
116+
replModule._compile(natives.repl, 'repl.js');
117+
internalModuleCache.repl = replModule;
118+
return replModule.exports;
119+
}
120+
121+
var fn = process.compile(
122+
"(function (exports, require) {" + natives[id] + "\n})",
123+
id + '.js');
108124
var m = new Module(id);
125+
fn(m.exports, requireNative);
126+
m.loaded = true;
109127
internalModuleCache[id] = m;
110-
var e = m._compile(natives[id], id+".js");
111-
if (e) throw e; // error compiling native module
112-
return m;
128+
return m.exports;
113129
}
114130

115131
exports.requireNative = requireNative;
116132

117-
function requireNative (id) {
118-
if (internalModuleCache[id]) return internalModuleCache[id].exports;
119-
if (!natives[id]) throw new Error('No such native module ' + id);
120-
return loadNative(id).exports;
121-
}
122-
123133

124134
// Modules
125135

@@ -230,7 +240,7 @@ var module = (function () {
230240
}
231241
if (natives[id]) {
232242
debug('load native module ' + request);
233-
return loadNative(id).exports;
243+
return requireNative(id);
234244
}
235245

236246
var cachedModule = moduleCache[filename];

0 commit comments

Comments
 (0)