From e303d950d9d14b0a8597ee3b92365db1acc2733b Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 16 Apr 2009 13:58:10 +0200 Subject: [PATCH] add stdout stderr global file objects. remove node.blocking.print --- src/file.cc | 11 +++++++++++ src/main.js | 35 ++++++++++------------------------- src/node.cc | 15 --------------- test/simple.js | 14 +++++++------- 4 files changed, 28 insertions(+), 47 deletions(-) diff --git a/src/file.cc b/src/file.cc index f395a64..916bc07 100644 --- a/src/file.cc +++ b/src/file.cc @@ -5,6 +5,7 @@ #include #include #include +#include using namespace v8; @@ -428,6 +429,16 @@ NodeInit_file (Handle target) file_template->InstanceTemplate()->SetInternalFieldCount(1); target->Set(String::NewSymbol("File"), file_template->GetFunction()); + // class method for File + file_template->GetFunction()->Set(String::NewSymbol("STDIN_FILENO"), + Integer::New(STDIN_FILENO)); + + file_template->GetFunction()->Set(String::NewSymbol("STDOUT_FILENO"), + Integer::New(STDOUT_FILENO)); + + file_template->GetFunction()->Set(String::NewSymbol("STDERR_FILENO"), + Integer::New(STDERR_FILENO)); + JS_SET_METHOD(file_template->InstanceTemplate(), "open", file_open); JS_SET_METHOD(file_template->InstanceTemplate(), "close", file_close); JS_SET_METHOD(file_template->InstanceTemplate(), "write", file_write); diff --git a/src/main.js b/src/main.js index 63be09f..2333087 100644 --- a/src/main.js +++ b/src/main.js @@ -1,3 +1,12 @@ +File.prototype.puts = function (data, callback) { + this.write(data + "\n", callback); +}; +var stdout = new File(); +stdout.fd = File.STDOUT_FILENO; + +var stderr = new File(); +stderr.fd = File.STDERR_FILENO; + // module search paths node.includes = ["."]; @@ -33,8 +42,6 @@ function __include (module, path) { } } - - function __require (path, loading_file) { var filename = path; @@ -44,18 +51,7 @@ function __require (path, loading_file) { } else { //filename = node.path.join(node.path.dirname(loading_file), path); } - node.blocking.print("require: " + filename); - - /* - for (var i = 0; i < suffixes.length; i++) { - var f = filename + "." + suffixes[i]; - - var stats = node.blocking.stat(f); - for (var j in stats) { - node.blocking.print("stats." + j + " = " + stats[j].toString()); - } - } - */ + stdout.puts("require: " + filename); var source = node.blocking.cat(filename); @@ -77,14 +73,3 @@ function __require (path, loading_file) { // main script execution. __require(ARGV[1], "."); -/* -fs.stat("/tmp/world", function (stat, status, msg) { - for ( var i in stat ) { - node.blocking.print(i + ": " + stat[i]); - } - node.blocking.print("done: " + status.toString() + " " + msg.toString()); - -}); -*/ - -//var f = new File(); diff --git a/src/node.cc b/src/node.cc index d125126..2254679 100644 --- a/src/node.cc +++ b/src/node.cc @@ -83,20 +83,6 @@ ExecuteString(v8::Handle source, return scope.Close(result); } -JS_METHOD(print) -{ - if (args.Length() < 1) return v8::Undefined(); - HandleScope scope; - Handle arg = args[0]; - String::Utf8Value value(arg); - - printf("%s\n", *value); - fflush(stdout); - - return Undefined(); -} - - JS_METHOD(cat) { if (args.Length() < 1) return v8::Undefined(); @@ -242,7 +228,6 @@ main (int argc, char *argv[]) JS_SET_METHOD(blocking, "exec", exec); JS_SET_METHOD(blocking, "cat", cat); - JS_SET_METHOD(blocking, "print", print); Local arguments = Array::New(argc); for (int i = 0; i < argc; i++) { diff --git a/test/simple.js b/test/simple.js index b9981c0..0440065 100644 --- a/test/simple.js +++ b/test/simple.js @@ -2,17 +2,17 @@ var f = new File; f.open("/tmp/world", "a+", function (status) { if (status == 0) { - node.blocking.print("file opened"); + stdout.puts("file open"); f.write("hello world\n", function (status, written) { - node.blocking.print("written. status: " - + status.toString() - + " written: " - + written.toString() - ); + stdout.puts("written. status: " + + status.toString() + + " written: " + + written.toString() + ); f.close(); }); } else { - node.blocking.print("file open failed: " + status.toString()); + stdout.puts("file open failed: " + status.toString()); } }); -- 2.7.4