Skip to content

Commit

Permalink
Load c++ modules on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Mar 15, 2010
1 parent 3994340 commit 627fb5a
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 103 deletions.
2 changes: 1 addition & 1 deletion lib/dns.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var events = require('events');
process.binding('dns');

exports.resolve = function (domain, type_, callback_) {
var type, callback;
Expand Down
124 changes: 62 additions & 62 deletions lib/fs.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
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) {
content += chunk;
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);
});
}
Expand All @@ -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 {
Expand All @@ -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;
};
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand All @@ -660,32 +660,32 @@ 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) {
if (!this.writeable) {
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);
Expand Down
Loading

0 comments on commit 627fb5a

Please sign in to comment.