DRY loading native module

This commit is contained in:
Herbert Vojcik 2010-03-19 00:29:49 +01:00 committed by Ryan Dahl
parent e5cbe73a82
commit 9e8afe9133

View File

@ -78,14 +78,19 @@ function createInternalModule (id, constructor) {
// 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 requireNative (id) { function loadNative (id) {
if (internalModuleCache[id]) return internalModuleCache[id].exports;
if (!natives[id]) throw new Error('No such native module ' + id);
var m = new Module(id); var m = new Module(id);
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;
return m.exports; m.loaded = true;
return m;
}
function requireNative (id) {
if (internalModuleCache[id]) return internalModuleCache[id].exports;
if (!natives[id]) throw new Error('No such native module ' + id);
return loadNative(id).exports;
} }
@ -546,10 +551,7 @@ function loadModule (request, parent, callback) {
// Try to compile from native modules // Try to compile from native modules
if (natives[id]) { if (natives[id]) {
debug('load native module ' + id); debug('load native module ' + id);
cachedModule = new Module(id); cachedModule = loadNative(id);
var e = cachedModule._compile(natives[id], id);
if (e) throw e;
internalModuleCache[id] = cachedModule;
} }
} }