HandleScope scope;
- Local<FunctionTemplate> file_template = FunctionTemplate::New(File::New);
- file_template->InstanceTemplate()->SetInternalFieldCount(1);
-
- // file methods
- NODE_SET_PROTOTYPE_METHOD(file_template, "_ffi_open", File::Open);
- NODE_SET_PROTOTYPE_METHOD(file_template, "_ffi_close", File::Close);
- NODE_SET_PROTOTYPE_METHOD(file_template, "_ffi_write", File::Write);
- NODE_SET_PROTOTYPE_METHOD(file_template, "_ffi_read", File::Read);
-
- file_template->InstanceTemplate()->SetAccessor(ENCODING_SYMBOL, File::GetEncoding, File::SetEncoding);
-
- fs = Persistent<Object>::New(file_template->GetFunction());
+ fs = Persistent<Object>::New(target);
InitActionQueue(fs);
- target->Set(String::NewSymbol("File"), fs);
// file system methods
NODE_SET_METHOD(fs, "_ffi_rename", FileSystem::Rename);
fs->Set(String::NewSymbol("STDIN_FILENO"), Integer::New(STDIN_FILENO));
fs->Set(String::NewSymbol("STDOUT_FILENO"), Integer::New(STDOUT_FILENO));
fs->Set(String::NewSymbol("STDERR_FILENO"), Integer::New(STDERR_FILENO));
+
+
+ Local<FunctionTemplate> file_template = FunctionTemplate::New(File::New);
+ file_template->InstanceTemplate()->SetInternalFieldCount(1);
+ // file methods
+ NODE_SET_PROTOTYPE_METHOD(file_template, "_ffi_open", File::Open);
+ NODE_SET_PROTOTYPE_METHOD(file_template, "_ffi_close", File::Close);
+ NODE_SET_PROTOTYPE_METHOD(file_template, "_ffi_write", File::Write);
+ NODE_SET_PROTOTYPE_METHOD(file_template, "_ffi_read", File::Read);
+ file_template->InstanceTemplate()->SetAccessor(ENCODING_SYMBOL, File::GetEncoding, File::SetEncoding);
+ fs->Set(String::NewSymbol("File"), file_template->GetFunction());
}
Handle<Value>
-File.rename = function (file1, file2, callback) {
+node.fs.rename = function (file1, file2, callback) {
this._addAction("rename", [file1, file2], callback);
};
-File.stat = function (path, callback) {
+node.fs.stat = function (path, callback) {
this._addAction("stat", [path], callback);
};
-File.exists = function (path, callback) {
+node.fs.exists = function (path, callback) {
this._addAction("stat", [path], function (status) {
callback(status == 0);
});
}
-File.cat = function (path, encoding, callback) {
- var file = new File();
+node.fs.cat = function (path, encoding, callback) {
+ var file = new node.fs.File();
file.encoding = encoding;
var content = "";
});
}
-File.prototype.puts = function (data, callback) {
+node.fs.File.prototype.puts = function (data, callback) {
this.write(data + "\n", -1, callback);
};
-File.prototype.print = function (data, callback) {
+node.fs.File.prototype.print = function (data, callback) {
this.write(data, -1, callback);
};
-File.prototype.open = function (path, mode, callback) {
+node.fs.File.prototype.open = function (path, mode, callback) {
this._addAction("open", [path, mode], callback);
};
-File.prototype.close = function (callback) {
+node.fs.File.prototype.close = function (callback) {
this._addAction("close", [], callback);
};
-File.prototype.write = function (buf, pos, callback) {
+node.fs.File.prototype.write = function (buf, pos, callback) {
this._addAction("write", [buf, pos], callback);
};
-File.prototype.read = function (length, pos, callback) {
+node.fs.File.prototype.read = function (length, pos, callback) {
this._addAction("read", [length, pos], callback);
};
//
// See File::CallTopCallback() in file.cc to see the other side of the
// binding.
-File._addAction = File.prototype._addAction = function (method, args, callback) {
+node.fs._addAction = node.fs.File.prototype._addAction = function (method, args, callback) {
this._actionQueue.push({ method: method
, callback: callback
, args: args
if (this._actionQueue.length == 1) this._act();
}
-File._act = File.prototype._act = function () {
+node.fs._act = node.fs.File.prototype._act = function () {
var action = this._actionQueue[0];
if (action)
// TODO FIXME what if the action throws an error?
// called from C++ after each action finishes
// (i.e. when it returns from the thread pool)
-File._pollActions = File.prototype._pollActions = function () {
+node.fs._pollActions = node.fs.File.prototype._pollActions = function () {
this._actionQueue.shift();
this._act();
};
-var stdout = new File();
-stdout.fd = File.STDOUT_FILENO;
+stdout = new node.fs.File();
+stdout.fd = node.fs.File.STDOUT_FILENO;
-var stderr = new File();
-stderr.fd = File.STDERR_FILENO;
+stderr = new node.fs.File();
+stderr.fd = node.fs.File.STDERR_FILENO;
-var stdin = new File();
-stdin.fd = File.STDIN_FILENO;
+stdin = new node.fs.File();
+stdin.fd = node.fs.File.STDIN_FILENO;
-this.puts = function (data, callback) {
+puts = function (data, callback) {
stdout.puts(data, callback);
}
-this.p = function (data, callback) {
+p = function (data, callback) {
puts(JSON.stringify(data), callback);
}