add stdout stderr global file objects. remove node.blocking.print
authorRyan <ry@tinyclouds.org>
Thu, 16 Apr 2009 11:58:10 +0000 (13:58 +0200)
committerRyan <ry@tinyclouds.org>
Thu, 16 Apr 2009 11:58:10 +0000 (13:58 +0200)
src/file.cc
src/main.js
src/node.cc
test/simple.js

index f395a64..916bc07 100644 (file)
@@ -5,6 +5,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 using namespace v8;
 
@@ -428,6 +429,16 @@ NodeInit_file (Handle<Object> 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);
index 63be09f..2333087 100644 (file)
@@ -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();
index d125126..2254679 100644 (file)
@@ -83,20 +83,6 @@ ExecuteString(v8::Handle<v8::String> source,
   return scope.Close(result);
 }
 
-JS_METHOD(print) 
-{
-  if (args.Length() < 1) return v8::Undefined();
-  HandleScope scope;
-  Handle<Value> 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<Array> arguments = Array::New(argc);
   for (int i = 0; i < argc; i++) {
index b9981c0..0440065 100644 (file)
@@ -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());
   }
 });