diff --git a/src/events.cc b/src/events.cc index 2c46bdd6f3..39fed9db3f 100644 --- a/src/events.cc +++ b/src/events.cc @@ -221,11 +221,7 @@ Promise* Promise::Create(void) { Local handle = Promise::constructor_template->GetFunction()->NewInstance(); - Promise *promise = new Promise(); - promise->Wrap(handle); - promise->Attach(); - - return promise; + return ObjectWrap::Unwrap(handle); } } // namespace node diff --git a/src/events.h b/src/events.h index 8009d0f567..9e3a8fd645 100644 --- a/src/events.h +++ b/src/events.h @@ -23,6 +23,7 @@ class EventEmitter : public ObjectWrap { class Promise : public EventEmitter { public: static void Initialize(v8::Handle target); + static v8::Persistent constructor_template; static Promise* Create(void); diff --git a/src/file.cc b/src/file.cc index ebd550158c..90a914ffbc 100644 --- a/src/file.cc +++ b/src/file.cc @@ -42,20 +42,24 @@ EIOPromise::Detach (void) ev_unref(EV_DEFAULT_UC); } -EIOPromise* -EIOPromise::Create (void) -{ +Handle EIOPromise::New (const v8::Arguments& args) { HandleScope scope; - Local handle = - Promise::constructor_template->GetFunction()->NewInstance(); - EIOPromise *promise = new EIOPromise(); - promise->Wrap(handle); + promise->Wrap(args.This()); promise->Attach(); - return promise; + return args.This(); +} + +EIOPromise* EIOPromise::Create() { + HandleScope scope; + + Local handle = + EIOPromise::constructor_template->GetFunction()->NewInstance(); + + return ObjectWrap::Unwrap(handle); } static Persistent stats_constructor_template; @@ -377,6 +381,8 @@ Read (const Arguments& args) return scope.Close(EIOPromise::Read(fd, len, pos, encoding)); } +Persistent EIOPromise::constructor_template; + void File::Initialize (Handle target) { @@ -397,4 +403,14 @@ File::Initialize (Handle target) stats_constructor_template = Persistent::New(t); target->Set(String::NewSymbol("Stats"), stats_constructor_template->GetFunction()); + + + Local t2 = FunctionTemplate::New(EIOPromise::New); + EIOPromise::constructor_template = Persistent::New(t2); + EIOPromise::constructor_template->Inherit( + Promise::constructor_template); + EIOPromise::constructor_template->InstanceTemplate()-> + SetInternalFieldCount(1); + target->Set(String::NewSymbol("EIOPromise"), + EIOPromise::constructor_template->GetFunction()); } diff --git a/src/file.h b/src/file.h index 5d44695d83..28ec201021 100644 --- a/src/file.h +++ b/src/file.h @@ -27,6 +27,10 @@ public: class EIOPromise : public Promise { public: + static EIOPromise* Create(); + static v8::Persistent constructor_template; + static v8::Handle New(const v8::Arguments& args); + static v8::Handle Open (const char *path, int flags, mode_t mode) { @@ -111,12 +115,10 @@ class EIOPromise : public Promise { return p->handle_; } - static EIOPromise* Create (void); - protected: - void Attach (void); - void Detach (void); + void Attach (); + void Detach (); EIOPromise () : Promise() { }