summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2010-03-15 13:48:03 -0700
committerRyan Dahl <ry@tinyclouds.org>2010-03-15 14:24:15 -0700
commit627fb5adbbc324fae657d2c88523234e6d83cc1c (patch)
treef29a1ebd52770fa7ba8a903b8fb685a514d31dcb
parent3994340a4567d10a855f0c1f112f0965517e3e52 (diff)
downloadnode-627fb5adbbc324fae657d2c88523234e6d83cc1c.tar.gz
Load c++ modules on demand
-rw-r--r--lib/dns.js2
-rw-r--r--lib/fs.js124
-rw-r--r--lib/http.js22
-rw-r--r--lib/tcp.js6
-rw-r--r--src/node.cc107
-rw-r--r--src/node.js5
-rw-r--r--src/node_stat.cc4
7 files changed, 167 insertions, 103 deletions
diff --git a/lib/dns.js b/lib/dns.js
index 9996d2479..dd3770e0f 100644
--- a/lib/dns.js
+++ b/lib/dns.js
@@ -1,4 +1,4 @@
-var events = require('events');
+process.binding('dns');
exports.resolve = function (domain, type_, callback_) {
var type, callback;
diff --git a/lib/fs.js b/lib/fs.js
index de40a8a97..f9fc33999 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -1,45 +1,45 @@
var sys = require('sys'),
events = require('events');
-var fs = exports;
+var fs = process.binding('fs');
-exports.Stats = process.Stats;
+exports.Stats = fs.Stats;
-process.Stats.prototype._checkModeProperty = function (property) {
+fs.Stats.prototype._checkModeProperty = function (property) {
return ((this.mode & property) === property);
};
-process.Stats.prototype.isDirectory = function () {
+fs.Stats.prototype.isDirectory = function () {
return this._checkModeProperty(process.S_IFDIR);
};
-process.Stats.prototype.isFile = function () {
+fs.Stats.prototype.isFile = function () {
return this._checkModeProperty(process.S_IFREG);
};
-process.Stats.prototype.isBlockDevice = function () {
+fs.Stats.prototype.isBlockDevice = function () {
return this._checkModeProperty(process.S_IFBLK);
};
-process.Stats.prototype.isCharacterDevice = function () {
+fs.Stats.prototype.isCharacterDevice = function () {
return this._checkModeProperty(process.S_IFCHR);
};
-process.Stats.prototype.isSymbolicLink = function () {
+fs.Stats.prototype.isSymbolicLink = function () {
return this._checkModeProperty(process.S_IFLNK);
};
-process.Stats.prototype.isFIFO = function () {
+fs.Stats.prototype.isFIFO = function () {
return this._checkModeProperty(process.S_IFIFO);
};
-process.Stats.prototype.isSocket = function () {
+fs.Stats.prototype.isSocket = function () {
return this._checkModeProperty(process.S_IFSOCK);
};
function readAll (fd, pos, content, encoding, callback) {
- process.fs.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) {
+ fs.read(fd, 4*1024, pos, encoding, function (err, chunk, bytesRead) {
if (err) {
if (callback) callback(err);
} else if (chunk) {
@@ -47,7 +47,7 @@ function readAll (fd, pos, content, encoding, callback) {
pos += bytesRead;
readAll(fd, pos, content, encoding, callback);
} else {
- process.fs.close(fd, function (err) {
+ fs.close(fd, function (err) {
if (callback) callback(err, content);
});
}
@@ -58,7 +58,7 @@ exports.readFile = function (path, encoding_, callback) {
var encoding = typeof(encoding_) == 'string' ? encoding_ : 'utf8';
var callback_ = arguments[arguments.length - 1];
var callback = (typeof(callback_) == 'function' ? callback_ : null);
- process.fs.open(path, process.O_RDONLY, 0666, function (err, fd) {
+ fs.open(path, process.O_RDONLY, 0666, function (err, fd) {
if (err) {
if (callback) callback(err);
} else {
@@ -70,17 +70,17 @@ exports.readFile = function (path, encoding_, callback) {
exports.readFileSync = function (path, encoding) {
encoding = encoding || "utf8"; // default to utf8
- var fd = process.fs.open(path, process.O_RDONLY, 0666);
+ var fd = fs.open(path, process.O_RDONLY, 0666);
var content = '';
var pos = 0;
var r;
- while ((r = process.fs.read(fd, 4*1024, pos, encoding)) && r[0]) {
+ while ((r = fs.read(fd, 4*1024, pos, encoding)) && r[0]) {
content += r[0];
pos += r[1]
}
- process.fs.close(fd);
+ fs.close(fd);
return content;
};
@@ -109,145 +109,145 @@ function noop () {}
// list to make the arguments clear.
exports.close = function (fd, callback) {
- process.fs.close(fd, callback || noop);
+ fs.close(fd, callback || noop);
};
exports.closeSync = function (fd) {
- return process.fs.close(fd);
+ return fs.close(fd);
};
exports.open = function (path, flags, mode, callback) {
if (mode === undefined) { mode = 0666; }
- process.fs.open(path, stringToFlags(flags), mode, callback || noop);
+ fs.open(path, stringToFlags(flags), mode, callback || noop);
};
exports.openSync = function (path, flags, mode) {
if (mode === undefined) { mode = 0666; }
- return process.fs.open(path, stringToFlags(flags), mode);
+ return fs.open(path, stringToFlags(flags), mode);
};
exports.read = function (fd, length, position, encoding, callback) {
encoding = encoding || "binary";
- process.fs.read(fd, length, position, encoding, callback || noop);
+ fs.read(fd, length, position, encoding, callback || noop);
};
exports.readSync = function (fd, length, position, encoding) {
encoding = encoding || "binary";
- return process.fs.read(fd, length, position, encoding);
+ return fs.read(fd, length, position, encoding);
};
exports.write = function (fd, data, position, encoding, callback) {
encoding = encoding || "binary";
- process.fs.write(fd, data, position, encoding, callback || noop);
+ fs.write(fd, data, position, encoding, callback || noop);
};
exports.writeSync = function (fd, data, position, encoding) {
encoding = encoding || "binary";
- return process.fs.write(fd, data, position, encoding);
+ return fs.write(fd, data, position, encoding);
};
exports.rename = function (oldPath, newPath, callback) {
- process.fs.rename(oldPath, newPath, callback || noop);
+ fs.rename(oldPath, newPath, callback || noop);
};
exports.renameSync = function (oldPath, newPath) {
- return process.fs.rename(oldPath, newPath);
+ return fs.rename(oldPath, newPath);
};
exports.truncate = function (fd, len, callback) {
- process.fs.truncate(fd, len, callback || noop);
+ fs.truncate(fd, len, callback || noop);
};
exports.truncateSync = function (fd, len) {
- return process.fs.truncate(fd, len);
+ return fs.truncate(fd, len);
};
exports.rmdir = function (path, callback) {
- process.fs.rmdir(path, callback || noop);
+ fs.rmdir(path, callback || noop);
};
exports.rmdirSync = function (path) {
- return process.fs.rmdir(path);
+ return fs.rmdir(path);
};
exports.mkdir = function (path, mode, callback) {
- process.fs.mkdir(path, mode, callback || noop);
+ fs.mkdir(path, mode, callback || noop);
};
exports.mkdirSync = function (path, mode) {
- return process.fs.mkdir(path, mode);
+ return fs.mkdir(path, mode);
};
exports.sendfile = function (outFd, inFd, inOffset, length, callback) {
- process.fs.sendfile(outFd, inFd, inOffset, length, callback || noop);
+ fs.sendfile(outFd, inFd, inOffset, length, callback || noop);
};
exports.sendfileSync = function (outFd, inFd, inOffset, length) {
- return process.fs.sendfile(outFd, inFd, inOffset, length);
+ return fs.sendfile(outFd, inFd, inOffset, length);
};
exports.readdir = function (path, callback) {
- process.fs.readdir(path, callback || noop);
+ fs.readdir(path, callback || noop);
};
exports.readdirSync = function (path) {
- return process.fs.readdir(path);
+ return fs.readdir(path);
};
exports.lstat = function (path, callback) {
- process.fs.lstat(path, callback || noop);
+ fs.lstat(path, callback || noop);
};
exports.stat = function (path, callback) {
- process.fs.stat(path, callback || noop);
+ fs.stat(path, callback || noop);
};
exports.lstatSync = function (path) {
- return process.fs.lstat(path);
+ return fs.lstat(path);
};
exports.statSync = function (path) {
- return process.fs.stat(path);
+ return fs.stat(path);
};
exports.readlink = function (path, callback) {
- process.fs.readlink(path, callback || noop);
+ fs.readlink(path, callback || noop);
};
exports.readlinkSync = function (path) {
- return process.fs.readlink(path);
+ return fs.readlink(path);
};
exports.symlink = function (destination, path, callback) {
- process.fs.symlink(destination, path, callback || noop);
+ fs.symlink(destination, path, callback || noop);
};
exports.symlinkSync = function (destination, path) {
- return process.fs.symlink(destination, path);
+ return fs.symlink(destination, path);
};
exports.link = function (srcpath, dstpath, callback) {
- process.fs.link(srcpath, dstpath, callback || noop);
+ fs.link(srcpath, dstpath, callback || noop);
};
exports.linkSync = function (srcpath, dstpath) {
- return process.fs.link(srcpath, dstpath);
+ return fs.link(srcpath, dstpath);
};
exports.unlink = function (path, callback) {
- process.fs.unlink(path, callback || noop);
+ fs.unlink(path, callback || noop);
};
exports.unlinkSync = function (path) {
- return process.fs.unlink(path);
+ return fs.unlink(path);
};
exports.chmod = function (path, mode, callback) {
- process.fs.chmod(path, mode, callback || noop);
+ fs.chmod(path, mode, callback || noop);
};
exports.chmodSync = function (path, mode) {
- return process.fs.chmod(path, mode);
+ return fs.chmod(path, mode);
};
function writeAll (fd, data, encoding, callback) {
@@ -322,7 +322,7 @@ exports.watchFile = function (filename) {
if (filename in statWatchers) {
stat = statWatchers[filename];
} else {
- statWatchers[filename] = new process.Stat();
+ statWatchers[filename] = new fs.StatWatcher();
stat = statWatchers[filename];
stat.start(filename, options.persistent, options.interval);
}
@@ -500,7 +500,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
return;
}
- fs.read(self.fd, self.bufferSize, undefined, self.encoding, function(err, data, bytesRead) {
+ exports.read(self.fd, self.bufferSize, undefined, self.encoding, function(err, data, bytesRead) {
if (err) {
self.emit('error', err);
self.readable = false;
@@ -529,7 +529,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
});
}
- fs.open(this.path, this.flags, this.mode, function(err, fd) {
+ exports.open(this.path, this.flags, this.mode, function(err, fd) {
if (err) {
self.emit('error', err);
self.readable = false;
@@ -545,7 +545,7 @@ var FileReadStream = exports.FileReadStream = function(path, options) {
this.readable = false;
function close() {
- fs.close(self.fd, function(err) {
+ exports.close(self.fd, function(err) {
if (err) {
if (cb) {
cb(err);
@@ -608,7 +608,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
queue = [],
busy = false;
- queue.push([fs.open, this.path, this.flags, this.mode, undefined]);
+ queue.push([exports.open, this.path, this.flags, this.mode, undefined]);
function flush() {
if (busy) {
@@ -639,7 +639,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
}
// stop flushing after close
- if (method === fs.close) {
+ if (method === exports.close) {
if (cb) {
cb(null);
}
@@ -648,7 +648,7 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
}
// save reference for file pointer
- if (method === fs.open) {
+ if (method === exports.open) {
self.fd = arguments[1];
self.emit('open', self.fd);
} else if (cb) {
@@ -660,11 +660,11 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
});
// Inject the file pointer
- if (method !== fs.open) {
+ if (method !== exports.open) {
args.unshift(self.fd);
}
- method.apply(null, args);
+ method.apply(this, args);
};
this.write = function(data, cb) {
@@ -672,20 +672,20 @@ var FileWriteStream = exports.FileWriteStream = function(path, options) {
throw new Error('stream not writeable');
}
- queue.push([fs.write, data, undefined, this.encoding, cb]);
+ queue.push([exports.write, data, undefined, this.encoding, cb]);
flush();
return false;
};
this.close = function(cb) {
this.writeable = false;
- queue.push([fs.close, cb]);
+ queue.push([exports.close, cb]);
flush();
};
this.forceClose = function(cb) {
this.writeable = false;
- fs.close(self.fd, function(err) {
+ exports.close(self.fd, function(err) {
if (err) {
if (cb) {
cb(err);
diff --git a/lib/http.js b/lib/http.js
index 624e4573d..832df6b2c 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -1,6 +1,12 @@
var sys = require('sys');
var events = require('events');
+// FIXME: The TCP binding isn't actually used here, but it needs to be
+// loaded before the http binding.
+process.binding('tcp');
+
+var http = process.binding('http');
+
var CRLF = "\r\n";
var STATUS_CODES = exports.STATUS_CODES = {
100 : 'Continue',
@@ -417,7 +423,7 @@ function flushMessageQueue (connection, queue) {
exports.createServer = function (requestListener, options) {
- var server = new process.http.Server();
+ var server = new http.Server();
//server.setOptions(options);
server.addListener("request", requestListener);
server.addListener("connection", connectionListener);
@@ -459,7 +465,7 @@ function connectionListener (connection) {
exports.createClient = function (port, host) {
- var client = new process.http.Client();
+ var client = new http.Client();
var secure_credentials={ secure : false };
var requests = [];
@@ -541,27 +547,27 @@ exports.createClient = function (port, host) {
return client;
};
-process.http.Client.prototype.get = function () {
+http.Client.prototype.get = function () {
throw new Error("client.get(...) is now client.request('GET', ...)");
};
-process.http.Client.prototype.head = function () {
+http.Client.prototype.head = function () {
throw new Error("client.head(...) is now client.request('HEAD', ...)");
};
-process.http.Client.prototype.post = function () {
+http.Client.prototype.post = function () {
throw new Error("client.post(...) is now client.request('POST', ...)");
};
-process.http.Client.prototype.del = function () {
+http.Client.prototype.del = function () {
throw new Error("client.del(...) is now client.request('DELETE', ...)");
};
-process.http.Client.prototype.put = function () {
+http.Client.prototype.put = function () {
throw new Error("client.put(...) is now client.request('PUT', ...)");
};
-process.http.Client.prototype.request = function (method, url, headers) {
+http.Client.prototype.request = function (method, url, headers) {
if (typeof(url) != "string") { // assume method was omitted, shift arguments
headers = url;
url = method;
diff --git a/lib/tcp.js b/lib/tcp.js
index a01b04f10..a27a32761 100644
--- a/lib/tcp.js
+++ b/lib/tcp.js
@@ -1,3 +1,5 @@
+var tcp = process.binding('tcp');
+
var TLS_STATUS_CODES = {
1 : 'JS_GNUTLS_CERT_VALIDATED',
0 : 'JS_GNUTLS_CERT_UNDEFINED',
@@ -11,14 +13,14 @@ TLS_STATUS_CODES[-105] = 'JS_GNUTLS_CERT_REVOKED';
TLS_STATUS_CODES[-106] = 'JS_GNUTLS_CERT_DOES_NOT_MATCH_HOSTNAME';
exports.createServer = function (on_connection, options) {
- var server = new process.tcp.Server();
+ var server = new tcp.Server();
server.addListener("connection", on_connection);
//server.setOptions(options);
return server;
};
exports.createConnection = function (port, host) {
- var connection = new process.tcp.Connection();
+ var connection = new tcp.Connection();
connection.connect(port, host);
return connection;
};
diff --git a/src/node.cc b/src/node.cc
index 957524b41..798c1cc3a 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -1046,6 +1046,85 @@ static Handle<Value> CheckBreak(const Arguments& args) {
return Undefined();
}
+Persistent<Object> binding_cache;
+
+static Handle<Value> Binding(const Arguments& args) {
+ HandleScope scope;
+
+ Local<String> module = args[0]->ToString();
+ String::Utf8Value module_v(module);
+
+ if (binding_cache.IsEmpty()) {
+ binding_cache = Persistent<Object>::New(Object::New());
+ }
+
+ Local<Object> exports;
+
+ if (!strcmp(*module_v, "http")) {
+ if (binding_cache->Has(module)) {
+ exports = binding_cache->Get(module)->ToObject();
+ } else {
+ // Warning: When calling requireBinding('http') from javascript then
+ // be sure that you call requireBinding('tcp') before it.
+ assert(binding_cache->Has(String::New("tcp")));
+ exports = Object::New();
+ HTTPServer::Initialize(exports);
+ HTTPConnection::Initialize(exports);
+ binding_cache->Set(module, exports);
+ }
+
+ } else if (!strcmp(*module_v, "tcp")) {
+ if (binding_cache->Has(module)) {
+ exports = binding_cache->Get(module)->ToObject();
+ } else {
+ exports = Object::New();
+ Server::Initialize(exports);
+ Connection::Initialize(exports);
+ binding_cache->Set(module, exports);
+ }
+
+ } else if (!strcmp(*module_v, "dns")) {
+ if (binding_cache->Has(module)) {
+ exports = binding_cache->Get(module)->ToObject();
+ } else {
+ exports = Object::New();
+ DNS::Initialize(exports);
+ binding_cache->Set(module, exports);
+ }
+
+ } else if (!strcmp(*module_v, "fs")) {
+ if (binding_cache->Has(module)) {
+ exports = binding_cache->Get(module)->ToObject();
+ } else {
+ exports = Object::New();
+
+ // Initialize the stats object
+ Local<FunctionTemplate> stat_templ = FunctionTemplate::New();
+ stats_constructor_template = Persistent<FunctionTemplate>::New(stat_templ);
+ exports->Set(String::NewSymbol("Stats"),
+ stats_constructor_template->GetFunction());
+ Stat::Initialize(exports);
+ File::Initialize(exports);
+ binding_cache->Set(module, exports);
+ }
+
+ } else if (!strcmp(*module_v, "signal_handler")) {
+ if (binding_cache->Has(module)) {
+ exports = binding_cache->Get(module)->ToObject();
+ } else {
+ exports = Object::New();
+ SignalHandler::Initialize(exports);
+ binding_cache->Set(module, exports);
+ }
+
+ } else {
+ assert(0);
+ return ThrowException(Exception::Error(String::New("No such module")));
+ }
+
+ return scope.Close(exports);
+}
+
static void Load(int argc, char *argv[]) {
HandleScope scope;
@@ -1122,44 +1201,20 @@ static void Load(int argc, char *argv[]) {
NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
NODE_SET_METHOD(process, "checkBreak", CheckBreak);
+ NODE_SET_METHOD(process, "binding", Binding);
+
// Assign the EventEmitter. It was created in main().
process->Set(String::NewSymbol("EventEmitter"),
EventEmitter::constructor_template->GetFunction());
- // Initialize the stats object
- Local<FunctionTemplate> stat_templ = FunctionTemplate::New();
- stats_constructor_template = Persistent<FunctionTemplate>::New(stat_templ);
- process->Set(String::NewSymbol("Stats"),
- stats_constructor_template->GetFunction());
// Initialize the C++ modules..................filename of module
IdleWatcher::Initialize(process); // idle_watcher.cc
Stdio::Initialize(process); // stdio.cc
Timer::Initialize(process); // timer.cc
- SignalHandler::Initialize(process); // signal_handler.cc
- Stat::Initialize(process); // stat.cc
ChildProcess::Initialize(process); // child_process.cc
DefineConstants(process); // constants.cc
- // Create node.dns
- Local<Object> dns = Object::New();
- process->Set(String::NewSymbol("dns"), dns);
- DNS::Initialize(dns); // dns.cc
- Local<Object> fs = Object::New();
- process->Set(String::NewSymbol("fs"), fs);
- File::Initialize(fs); // file.cc
- // Create node.tcp. Note this separate from lib/tcp.js which is the public
- // frontend.
- Local<Object> tcp = Object::New();
- process->Set(String::New("tcp"), tcp);
- Server::Initialize(tcp); // tcp.cc
- Connection::Initialize(tcp); // tcp.cc
- // Create node.http. Note this separate from lib/http.js which is the
- // public frontend.
- Local<Object> http = Object::New();
- process->Set(String::New("http"), http);
- HTTPServer::Initialize(http); // http.cc
- HTTPConnection::Initialize(http); // http.cc
Local<Object> natives = Object::New();
diff --git a/src/node.js b/src/node.js
index f23d1bb74..83bfb5e0e 100644
--- a/src/node.js
+++ b/src/node.js
@@ -281,7 +281,8 @@ function isSignal (event) {
process.addListener("newListener", function (event) {
if (isSignal(event) && process.listeners(event).length === 0) {
- var handler = new process.SignalHandler(process[event]);
+ var b = process.binding('signal_handler');
+ var handler = new b.SignalHandler(process[event]);
handler.addListener("signal", function () {
process.emit(event);
});
@@ -407,7 +408,7 @@ var path = pathModule.exports;
function existsSync (path) {
try {
- process.fs.stat(path);
+ process.binding('fs').stat(path);
return true;
} catch (e) {
return false;
diff --git a/src/node_stat.cc b/src/node_stat.cc
index d48bd037f..f166b7a92 100644
--- a/src/node_stat.cc
+++ b/src/node_stat.cc
@@ -21,7 +21,7 @@ void Stat::Initialize(Handle<Object> target) {
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->Inherit(EventEmitter::constructor_template);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
- constructor_template->SetClassName(String::NewSymbol("Stat"));
+ constructor_template->SetClassName(String::NewSymbol("StatWatcher"));
change_symbol = NODE_PSYMBOL("change");
stop_symbol = NODE_PSYMBOL("stop");
@@ -29,7 +29,7 @@ void Stat::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", Stat::Start);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", Stat::Stop);
- target->Set(String::NewSymbol("Stat"), constructor_template->GetFunction());
+ target->Set(String::NewSymbol("StatWatcher"), constructor_template->GetFunction());
}