mirror of https://github.com/lukechilds/node.git
Ryan Dahl
14 years ago
7 changed files with 149 additions and 95 deletions
@ -0,0 +1,89 @@ |
|||
#include <node.h> |
|||
#include <handle_wrap.h> |
|||
|
|||
namespace node { |
|||
|
|||
using v8::Object; |
|||
using v8::Handle; |
|||
using v8::Local; |
|||
using v8::Persistent; |
|||
using v8::Value; |
|||
using v8::HandleScope; |
|||
using v8::FunctionTemplate; |
|||
using v8::String; |
|||
using v8::Function; |
|||
using v8::TryCatch; |
|||
using v8::Context; |
|||
using v8::Arguments; |
|||
using v8::Integer; |
|||
|
|||
|
|||
#define UNWRAP \ |
|||
assert(!args.Holder().IsEmpty()); \ |
|||
assert(args.Holder()->InternalFieldCount() > 0); \ |
|||
HandleWrap* wrap = \ |
|||
static_cast<HandleWrap*>(args.Holder()->GetPointerFromInternalField(0)); \ |
|||
if (!wrap) { \ |
|||
SetErrno(UV_EBADF); \ |
|||
return scope.Close(Integer::New(-1)); \ |
|||
} |
|||
|
|||
|
|||
void HandleWrap::Initialize(Handle<Object> target) { |
|||
/* Doesn't do anything at the moment. */ |
|||
} |
|||
|
|||
|
|||
Handle<Value> HandleWrap::Close(const Arguments& args) { |
|||
HandleScope scope; |
|||
|
|||
UNWRAP |
|||
|
|||
assert(!wrap->object_.IsEmpty()); |
|||
int r = uv_close(wrap->handle__, OnClose); |
|||
|
|||
wrap->StateChange(); |
|||
|
|||
if (r) { |
|||
SetErrno(uv_last_error().code); |
|||
|
|||
wrap->object_->SetPointerInInternalField(0, NULL); |
|||
wrap->object_.Dispose(); |
|||
wrap->object_.Clear(); |
|||
} |
|||
return scope.Close(Integer::New(r)); |
|||
} |
|||
|
|||
|
|||
HandleWrap::HandleWrap(Handle<Object> object, uv_handle_t* h) { |
|||
handle__ = h; |
|||
h->data = this; |
|||
|
|||
HandleScope scope; |
|||
assert(object_.IsEmpty()); |
|||
assert(object->InternalFieldCount() > 0); |
|||
object_ = v8::Persistent<v8::Object>::New(object); |
|||
object_->SetPointerInInternalField(0, this); |
|||
} |
|||
|
|||
|
|||
HandleWrap::~HandleWrap() { |
|||
assert(object_.IsEmpty()); |
|||
} |
|||
|
|||
|
|||
void HandleWrap::OnClose(uv_handle_t* handle) { |
|||
HandleWrap* wrap = static_cast<HandleWrap*>(handle->data); |
|||
|
|||
// The wrap object should still be there.
|
|||
assert(wrap->object_.IsEmpty() == false); |
|||
|
|||
wrap->object_->SetPointerInInternalField(0, NULL); |
|||
wrap->object_.Dispose(); |
|||
wrap->object_.Clear(); |
|||
|
|||
delete wrap; |
|||
} |
|||
|
|||
|
|||
} // namespace node
|
@ -0,0 +1,30 @@ |
|||
#ifndef HANDLE_WRAP_H_ |
|||
#define HANDLE_WRAP_H_ |
|||
|
|||
namespace node { |
|||
|
|||
class HandleWrap { |
|||
public: |
|||
static void Initialize(v8::Handle<v8::Object> target); |
|||
static v8::Handle<v8::Value> Close(const v8::Arguments& args); |
|||
|
|||
protected: |
|||
HandleWrap(v8::Handle<v8::Object> object, uv_handle_t* handle); |
|||
virtual ~HandleWrap(); |
|||
|
|||
virtual void StateChange() {} |
|||
|
|||
v8::Persistent<v8::Object> object_; |
|||
|
|||
private: |
|||
static void OnClose(uv_handle_t* handle); |
|||
// Using double underscore due to handle_ member in tcp_wrap. Probably
|
|||
// tcp_wrap should rename it's member to 'handle'.
|
|||
uv_handle_t* handle__; |
|||
}; |
|||
|
|||
|
|||
} // namespace node
|
|||
|
|||
|
|||
#endif // HANDLE_WRAP_H_
|
Loading…
Reference in new issue