From c0b90ca763b3d619782e1ebb5cd761f81d4286c8 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 16 Apr 2009 13:42:34 +0200 Subject: [PATCH] only store fd in javascript (not in c++) --- src/file.cc | 33 +++++++++++++++++---------------- test/simple.js | 2 +- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/file.cc b/src/file.cc index 3b1f2123bf..f395a642af 100644 --- a/src/file.cc +++ b/src/file.cc @@ -6,11 +6,10 @@ #include #include - using namespace v8; -#define ON_OPEN v8::String::NewSymbol("onOpen") - +#define ON_OPEN_SYMBOL v8::String::NewSymbol("onOpen") +#define FD_SYMBOL v8::String::NewSymbol("fd") class File; class Callback { @@ -171,14 +170,12 @@ public: private: static void MakeWeak (Persistent _, void *data); - int fd_; Persistent handle_; }; File::File (Handle handle) { HandleScope scope; - fd_ = -1; handle_ = Persistent::New(handle); Handle external = External::New(this); handle_->SetInternalField(0, external); @@ -215,8 +212,7 @@ File::AfterClose (eio_req *req) File *file = static_cast(req->data); if (req->result == 0) { - file->fd_ = -1; - file->handle_->Delete(String::NewSymbol("fd")); + file->handle_->Delete(FD_SYMBOL); } // TODO @@ -227,7 +223,10 @@ File::AfterClose (eio_req *req) void File::Close () { - eio_req *req = eio_close (fd_, EIO_PRI_DEFAULT, File::AfterClose, this); + Handle fd_value = handle_->Get(FD_SYMBOL); + int fd = fd_value->IntegerValue(); + + eio_req *req = eio_close (fd, EIO_PRI_DEFAULT, File::AfterClose, this); node_eio_submit(req); } @@ -238,15 +237,14 @@ File::AfterOpen (eio_req *req) HandleScope scope; if(req->result >= 0) { - file->fd_ = static_cast(req->result); - file->handle_->Set(String::NewSymbol("fd"), Integer::New(file->fd_)); + file->handle_->Set(FD_SYMBOL, Integer::New(req->result)); } - Handle callback_value = file->handle_->Get(ON_OPEN); + Handle callback_value = file->handle_->Get(ON_OPEN_SYMBOL); if (!callback_value->IsFunction()) return 0; Handle callback = Handle::Cast(callback_value); - file->handle_->Delete(ON_OPEN); + file->handle_->Delete(ON_OPEN_SYMBOL); const int argc = 1; Handle argv[argc]; @@ -264,11 +262,11 @@ Handle File::Open (const char *path, const char *mode, Handle callback) { // make sure that we don't already have a pending open - if (handle_->Has(ON_OPEN)) { + if (handle_->Has(ON_OPEN_SYMBOL)) { return ThrowException(String::New("File object is already being opened.")); } - if (callback->IsFunction()) handle_->Set(ON_OPEN, callback); + if (callback->IsFunction()) handle_->Set(ON_OPEN_SYMBOL, callback); // Get the current umask mode_t mask = umask(0); @@ -324,11 +322,14 @@ File::Write (char *buf, size_t length, Callback *callback) if (callback) callback->file = this; // NOTE: -1 offset in eio_write() invokes write() instead of pwrite() - if (fd_ < 0) { + if (handle_->Has(FD_SYMBOL) == false) { printf("trying to write to a bad fd!\n"); return; } - eio_req *req = eio_write(fd_, buf, length, -1, EIO_PRI_DEFAULT, File::AfterWrite, callback); + Handle fd_value = handle_->Get(FD_SYMBOL); + int fd = fd_value->IntegerValue(); + + eio_req *req = eio_write(fd, buf, length, -1, EIO_PRI_DEFAULT, File::AfterWrite, callback); node_eio_submit(req); } diff --git a/test/simple.js b/test/simple.js index 6920f88bc7..b9981c07c6 100644 --- a/test/simple.js +++ b/test/simple.js @@ -1,6 +1,6 @@ var f = new File; -f.open("/tmp/world", "r+", function (status) { +f.open("/tmp/world", "a+", function (status) { if (status == 0) { node.blocking.print("file opened"); f.write("hello world\n", function (status, written) {