From 9e8afe9133f18382e787fd84a405f04990259d3e Mon Sep 17 00:00:00 2001 From: Herbert Vojcik Date: Fri, 19 Mar 2010 00:29:49 +0100 Subject: [PATCH] DRY loading native module --- src/node.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/node.js b/src/node.js index 0b529665598..4a9a2348b61 100644 --- a/src/node.js +++ b/src/node.js @@ -78,14 +78,19 @@ function createInternalModule (id, constructor) { // Like, natives.fs is the contents of lib/fs.js var natives = process.binding('natives'); -function requireNative (id) { - if (internalModuleCache[id]) return internalModuleCache[id].exports; - if (!natives[id]) throw new Error('No such native module ' + id); +function loadNative (id) { var m = new Module(id); internalModuleCache[id] = m; var e = m._compile(natives[id], id); 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 if (natives[id]) { debug('load native module ' + id); - cachedModule = new Module(id); - var e = cachedModule._compile(natives[id], id); - if (e) throw e; - internalModuleCache[id] = cachedModule; + cachedModule = loadNative(id); } }