0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 15:06:33 +01:00

Namespace File stuff in node.fs

This commit is contained in:
Ryan 2009-05-21 12:49:41 +02:00
parent 6244f77822
commit 58c13e5192
5 changed files with 41 additions and 40 deletions

View File

@ -38,21 +38,9 @@ File::Initialize (Handle<Object> target)
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);
@ -62,6 +50,17 @@ File::Initialize (Handle<Object> target)
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>

View File

@ -1,19 +1,19 @@
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 = "";
@ -48,27 +48,27 @@ File.cat = function (path, encoding, callback) {
});
}
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);
};
@ -97,7 +97,7 @@ File.prototype.read = function (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
@ -105,7 +105,7 @@ File._addAction = File.prototype._addAction = function (method, args, callback)
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?
@ -114,24 +114,24 @@ File._act = File.prototype._act = function () {
// 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);
}

View File

@ -290,7 +290,9 @@ main (int argc, char *argv[])
// BUILT-IN MODULES
Timer::Initialize(node);
File::Initialize(g);
Local<Object> fs = Object::New();
node->Set(String::New("fs"), fs);
File::Initialize(fs);
Local<Object> tcp = Object::New();
node->Set(String::New("tcp"), tcp);

View File

@ -62,7 +62,7 @@ clearInterval = clearTimeout;
var filename = node.path.join(base_directory, name) + ".js";
File.exists(filename, function (status) {
node.fs.exists(filename, function (status) {
callback(status ? filename : null);
});
}
@ -134,9 +134,9 @@ clearInterval = clearTimeout;
}
function loadScript (filename, target, callback) {
File.cat(filename, "utf8", function (status, content) {
node.fs.cat(filename, "utf8", function (status, content) {
if (status != 0) {
stderr.puts("Error reading " + filename + ": " + File.strerror(status));
stderr.puts("Error reading " + filename + ": " + node.fs.strerror(status));
exit(1);
}

View File

@ -6,7 +6,7 @@ function onLoad () {
var fixtures = node.path.join(dirname, "fixtures");
var x = node.path.join(fixtures, "x.txt");
file = new File;
file = new node.fs.File;
file.open(x, "r", function (status) {
assertTrue(status == 0);
assert_count += 1;