Browse Source

Namespace File stuff in node.fs

v0.7.4-release
Ryan 16 years ago
parent
commit
58c13e5192
  1. 25
      src/file.cc
  2. 44
      src/file.js
  3. 4
      src/node.cc
  4. 6
      src/node.js
  5. 2
      test/test-file-open.js

25
src/file.cc

@ -38,21 +38,9 @@ File::Initialize (Handle<Object> target)
HandleScope scope; HandleScope scope;
Local<FunctionTemplate> file_template = FunctionTemplate::New(File::New); fs = Persistent<Object>::New(target);
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());
InitActionQueue(fs); InitActionQueue(fs);
target->Set(String::NewSymbol("File"), fs);
// file system methods // file system methods
NODE_SET_METHOD(fs, "_ffi_rename", FileSystem::Rename); 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("STDIN_FILENO"), Integer::New(STDIN_FILENO));
fs->Set(String::NewSymbol("STDOUT_FILENO"), Integer::New(STDOUT_FILENO)); fs->Set(String::NewSymbol("STDOUT_FILENO"), Integer::New(STDOUT_FILENO));
fs->Set(String::NewSymbol("STDERR_FILENO"), Integer::New(STDERR_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> Handle<Value>

44
src/file.js

@ -1,19 +1,19 @@
File.rename = function (file1, file2, callback) { node.fs.rename = function (file1, file2, callback) {
this._addAction("rename", [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); this._addAction("stat", [path], callback);
}; };
File.exists = function (path, callback) { node.fs.exists = function (path, callback) {
this._addAction("stat", [path], function (status) { this._addAction("stat", [path], function (status) {
callback(status == 0); callback(status == 0);
}); });
} }
File.cat = function (path, encoding, callback) { node.fs.cat = function (path, encoding, callback) {
var file = new File(); var file = new node.fs.File();
file.encoding = encoding; file.encoding = encoding;
var content = ""; 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); 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); 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); this._addAction("open", [path, mode], callback);
}; };
File.prototype.close = function (callback) { node.fs.File.prototype.close = function (callback) {
this._addAction("close", [], 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); 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); 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 // See File::CallTopCallback() in file.cc to see the other side of the
// binding. // 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 this._actionQueue.push({ method: method
, callback: callback , callback: callback
, args: args , args: args
@ -105,7 +105,7 @@ File._addAction = File.prototype._addAction = function (method, args, callback)
if (this._actionQueue.length == 1) this._act(); 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]; var action = this._actionQueue[0];
if (action) if (action)
// TODO FIXME what if the action throws an error? // 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 // called from C++ after each action finishes
// (i.e. when it returns from the thread pool) // (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._actionQueue.shift();
this._act(); this._act();
}; };
var stdout = new File(); stdout = new node.fs.File();
stdout.fd = File.STDOUT_FILENO; stdout.fd = node.fs.File.STDOUT_FILENO;
var stderr = new File(); stderr = new node.fs.File();
stderr.fd = File.STDERR_FILENO; stderr.fd = node.fs.File.STDERR_FILENO;
var stdin = new File(); stdin = new node.fs.File();
stdin.fd = File.STDIN_FILENO; stdin.fd = node.fs.File.STDIN_FILENO;
this.puts = function (data, callback) { puts = function (data, callback) {
stdout.puts(data, callback); stdout.puts(data, callback);
} }
this.p = function (data, callback) { p = function (data, callback) {
puts(JSON.stringify(data), callback); puts(JSON.stringify(data), callback);
} }

4
src/node.cc

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

6
src/node.js

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

2
test/test-file-open.js

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

Loading…
Cancel
Save