Skip to content

Instantly share code, notes, and snippets.

@indutny

indutny/1.diff Secret

Created October 24, 2014 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indutny/159a035e3cdeb9dcab2e to your computer and use it in GitHub Desktop.
Save indutny/159a035e3cdeb9dcab2e to your computer and use it in GitHub Desktop.
diff --git a/src/handle_wrap.cc b/src/handle_wrap.cc
index 595ea7d..3e348a8 100644
--- a/src/handle_wrap.cc
+++ b/src/handle_wrap.cc
@@ -43,7 +43,7 @@ using v8::Value;
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
- if (wrap != nullptr && wrap->handle__ != nullptr) {
+ if (IsAlive(wrap)) {
uv_ref(wrap->handle__);
wrap->flags_ &= ~kUnref;
}
@@ -53,7 +53,7 @@ void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
- if (wrap != nullptr && wrap->handle__ != nullptr) {
+ if (IsAlive(wrap)) {
uv_unref(wrap->handle__);
wrap->flags_ |= kUnref;
}
@@ -66,7 +66,7 @@ void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
HandleWrap* wrap = Unwrap<HandleWrap>(args.Holder());
// guard against uninitialized handle or double close
- if (wrap == nullptr || wrap->handle__ == nullptr)
+ if (!IsAlive(wrap))
return;
CHECK_EQ(false, wrap->persistent().IsEmpty());
diff --git a/src/handle_wrap.h b/src/handle_wrap.h
index 40ee75c..d6ca425 100644
--- a/src/handle_wrap.h
+++ b/src/handle_wrap.h
@@ -57,6 +57,10 @@ class HandleWrap : public AsyncWrap {
static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static inline bool IsAlive(HandleWrap* wrap) {
+ return wrap != nullptr && wrap->GetHandle() != nullptr;
+ }
+
inline uv_handle_t* GetHandle() { return handle__; }
protected:
diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc
index a6c64ac..19bc534 100644
--- a/src/stream_wrap.cc
+++ b/src/stream_wrap.cc
@@ -90,6 +90,9 @@ void StreamWrap::UpdateWriteQueueSize() {
void StreamWrap::ReadStart(const FunctionCallbackInfo<Value>& args) {
StreamWrap* wrap = Unwrap<StreamWrap>(args.Holder());
+ if (!IsAlive(wrap))
+ return;
+
int err = uv_read_start(wrap->stream(), OnAlloc, OnRead);
args.GetReturnValue().Set(err);
}
@@ -97,6 +100,8 @@ void StreamWrap::ReadStart(const FunctionCallbackInfo<Value>& args) {
void StreamWrap::ReadStop(const FunctionCallbackInfo<Value>& args) {
StreamWrap* wrap = Unwrap<StreamWrap>(args.Holder());
+ if (!IsAlive(wrap))
+ return;
int err = uv_read_stop(wrap->stream());
args.GetReturnValue().Set(err);
}
@@ -183,6 +188,8 @@ void StreamWrap::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
StreamWrap* wrap = Unwrap<StreamWrap>(args.Holder());
+ if (!IsAlive(wrap))
+ return;
CHECK(args[0]->IsObject());
CHECK(Buffer::HasInstance(args[1]));
@@ -240,6 +247,8 @@ void StreamWrap::WriteStringImpl(const FunctionCallbackInfo<Value>& args) {
int err;
StreamWrap* wrap = Unwrap<StreamWrap>(args.Holder());
+ if (!IsAlive(wrap))
+ return;
CHECK(args[0]->IsObject());
CHECK(args[1]->IsString());
@@ -367,6 +376,8 @@ void StreamWrap::Writev(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
StreamWrap* wrap = Unwrap<StreamWrap>(args.Holder());
+ if (!IsAlive(wrap))
+ return;
CHECK(args[0]->IsObject());
CHECK(args[1]->IsArray());
@@ -493,6 +504,8 @@ void StreamWrap::WriteBinaryString(const FunctionCallbackInfo<Value>& args) {
void StreamWrap::SetBlocking(const FunctionCallbackInfo<Value>& args) {
StreamWrap* wrap = Unwrap<StreamWrap>(args.Holder());
+ if (!IsAlive(wrap))
+ return;
CHECK_GT(args.Length(), 0);
int err = uv_stream_set_blocking(wrap->stream(), args[0]->IsTrue());
args.GetReturnValue().Set(err);
@@ -537,6 +550,8 @@ void StreamWrap::Shutdown(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
StreamWrap* wrap = Unwrap<StreamWrap>(args.Holder());
+ if (!IsAlive(wrap))
+ return;
CHECK(args[0]->IsObject());
Local<Object> req_wrap_obj = args[0].As<Object>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment