From c90546f138e42ee191a9ef240a0168007b243604 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 15 Mar 2010 14:22:50 -0700 Subject: [PATCH] Move native js files into binding object --- src/node.cc | 48 ++++++++++++++++++++++++++---------------------- src/node.js | 12 ++++++++---- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/node.cc b/src/node.cc index 26fd61eefd..2d47b78804 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1117,6 +1117,32 @@ static Handle Binding(const Arguments& args) { binding_cache->Set(module, exports); } + } else if (!strcmp(*module_v, "natives")) { + if (binding_cache->Has(module)) { + exports = binding_cache->Get(module)->ToObject(); + } else { + exports = Object::New(); + // Explicitly define native sources. + // TODO DRY/automate this? + exports->Set(String::New("assert"), String::New(native_assert)); + exports->Set(String::New("dns"), String::New(native_dns)); + exports->Set(String::New("file"), String::New(native_file)); + exports->Set(String::New("fs"), String::New(native_fs)); + exports->Set(String::New("http"), String::New(native_http)); + exports->Set(String::New("ini"), String::New(native_ini)); + exports->Set(String::New("mjsunit"), String::New(native_mjsunit)); + exports->Set(String::New("multipart"), String::New(native_multipart)); + exports->Set(String::New("posix"), String::New(native_posix)); + exports->Set(String::New("querystring"), String::New(native_querystring)); + exports->Set(String::New("repl"), String::New(native_repl)); + exports->Set(String::New("sys"), String::New(native_sys)); + exports->Set(String::New("tcp"), String::New(native_tcp)); + exports->Set(String::New("uri"), String::New(native_uri)); + exports->Set(String::New("url"), String::New(native_url)); + exports->Set(String::New("utils"), String::New(native_utils)); + binding_cache->Set(module, exports); + } + } else { assert(0); return ThrowException(Exception::Error(String::New("No such module"))); @@ -1216,28 +1242,6 @@ static void Load(int argc, char *argv[]) { ChildProcess::Initialize(process); // child_process.cc DefineConstants(process); // constants.cc - - Local natives = Object::New(); - process->Set(String::New("natives"), natives); - // Explicitly define native sources. - natives->Set(String::New("assert"), String::New(native_assert)); - natives->Set(String::New("dns"), String::New(native_dns)); - natives->Set(String::New("file"), String::New(native_file)); - natives->Set(String::New("fs"), String::New(native_fs)); - natives->Set(String::New("http"), String::New(native_http)); - natives->Set(String::New("ini"), String::New(native_ini)); - natives->Set(String::New("mjsunit"), String::New(native_mjsunit)); - natives->Set(String::New("multipart"), String::New(native_multipart)); - natives->Set(String::New("posix"), String::New(native_posix)); - natives->Set(String::New("querystring"), String::New(native_querystring)); - natives->Set(String::New("repl"), String::New(native_repl)); - natives->Set(String::New("sys"), String::New(native_sys)); - natives->Set(String::New("tcp"), String::New(native_tcp)); - natives->Set(String::New("uri"), String::New(native_uri)); - natives->Set(String::New("url"), String::New(native_url)); - natives->Set(String::New("utils"), String::New(native_utils)); - - // Compile, execute the src/node.js file. (Which was included as static C // string in node_natives.h. 'natve_node' is the string containing that // source code.) diff --git a/src/node.js b/src/node.js index b2079e2f87..22df434e15 100644 --- a/src/node.js +++ b/src/node.js @@ -74,12 +74,16 @@ function createInternalModule (id, constructor) { }; +// This contains the source code for the files in lib/ +// 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 (!process.natives[id]) throw new Error('No such native module ' + id); + if (!natives[id]) throw new Error('No such native module ' + id); var m = new Module(id); internalModuleCache[id] = m; - var e = m._compile(process.natives[id], id); + var e = m._compile(natives[id], id); if (e) throw e; return m.exports; } @@ -540,10 +544,10 @@ function loadModule (request, parent, callback) { if (!cachedModule) { // Try to compile from native modules - if (process.natives[id]) { + if (natives[id]) { debug('load native module ' + id); cachedModule = new Module(id); - var e = cachedModule._compile(process.natives[id], id); + var e = cachedModule._compile(natives[id], id); if (e) throw e; internalModuleCache[id] = cachedModule; }