Browse Source

Move native js files into binding object

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
c90546f138
  1. 48
      src/node.cc
  2. 12
      src/node.js

48
src/node.cc

@ -1117,6 +1117,32 @@ static Handle<Value> Binding(const Arguments& args) {
binding_cache->Set(module, exports); 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 { } else {
assert(0); assert(0);
return ThrowException(Exception::Error(String::New("No such module"))); 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 ChildProcess::Initialize(process); // child_process.cc
DefineConstants(process); // constants.cc DefineConstants(process); // constants.cc
Local<Object> 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 // 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 // string in node_natives.h. 'natve_node' is the string containing that
// source code.) // source code.)

12
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) { function requireNative (id) {
if (internalModuleCache[id]) return internalModuleCache[id].exports; 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); var m = new Module(id);
internalModuleCache[id] = m; internalModuleCache[id] = m;
var e = m._compile(process.natives[id], id); var e = m._compile(natives[id], id);
if (e) throw e; if (e) throw e;
return m.exports; return m.exports;
} }
@ -540,10 +544,10 @@ function loadModule (request, parent, callback) {
if (!cachedModule) { if (!cachedModule) {
// Try to compile from native modules // Try to compile from native modules
if (process.natives[id]) { if (natives[id]) {
debug('load native module ' + id); debug('load native module ' + id);
cachedModule = new 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; if (e) throw e;
internalModuleCache[id] = cachedModule; internalModuleCache[id] = cachedModule;
} }

Loading…
Cancel
Save