Permalink
Browse files

Make UNWRAP macro generic.

  • Loading branch information...
1 parent 81a4edc commit 45de259b436ab1bbeb15d7f582f29e795f162202 @Sannis Sannis committed with bnoordhuis May 21, 2012
Showing with 64 additions and 159 deletions.
  1. +2 −14 src/fs_event_wrap.cc
  2. +13 −23 src/handle_wrap.cc
  3. +13 −0 src/node_internals.h
  4. +5 −17 src/pipe_wrap.cc
  5. +2 −14 src/process_wrap.cc
  6. +5 −18 src/stream_wrap.cc
  7. +10 −22 src/tcp_wrap.cc
  8. +5 −17 src/timer_wrap.cc
  9. +2 −15 src/tty_wrap.cc
  10. +7 −19 src/udp_wrap.cc
View
@@ -30,18 +30,6 @@ namespace node {
static Persistent<String> onchange_sym;
-#define UNWRAP \
- assert(!args.Holder().IsEmpty()); \
- assert(args.Holder()->InternalFieldCount() > 0); \
- FSEventWrap* wrap = \
- static_cast<FSEventWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
- if (!wrap) { \
- uv_err_t err; \
- err.code = UV_EBADF; \
- SetErrno(err); \
- return scope.Close(Integer::New(-1)); \
- }
-
class FSEventWrap: public HandleWrap {
public:
static void Initialize(Handle<Object> target);
@@ -103,7 +91,7 @@ Handle<Value> FSEventWrap::New(const Arguments& args) {
Handle<Value> FSEventWrap::Start(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(FSEventWrap)
if (args.Length() < 1 || !args[0]->IsString()) {
return ThrowException(Exception::TypeError(String::New("Bad arguments")));
@@ -178,7 +166,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
Handle<Value> FSEventWrap::Close(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(FSEventWrap)
if (!wrap->initialized_)
return Undefined();
View
@@ -41,19 +41,6 @@ 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) { \
- uv_err_t err; \
- err.code = UV_EBADF; \
- SetErrno(err); \
- return scope.Close(Integer::New(-1)); \
- }
-
-
// defined in node.cc
extern ngx_queue_t handle_wrap_queue;
@@ -68,7 +55,7 @@ void HandleWrap::Initialize(Handle<Object> target) {
Handle<Value> HandleWrap::Unref(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(HandleWrap)
// Calling unnecessarily is a no-op
if (wrap->unref) {
@@ -86,7 +73,7 @@ Handle<Value> HandleWrap::Unref(const Arguments& args) {
Handle<Value> HandleWrap::Ref(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(HandleWrap)
// Calling multiple times is a no-op
if (!wrap->unref) {
@@ -103,17 +90,20 @@ Handle<Value> HandleWrap::Ref(const Arguments& args) {
Handle<Value> HandleWrap::Close(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ HandleWrap *wrap = static_cast<HandleWrap*>(
+ args.Holder()->GetPointerFromInternalField(0));
@bnoordhuis
bnoordhuis May 21, 2012 Node.js Foundation member

By the way, why doesn't this use UNWRAP?

@Sannis
Sannis May 22, 2012

As @ssuda pointed: #3048 (comment)
I can look into real reason. Also there is wrappers for "file" handlers with different unwrap, writed as a class method.

- // guard against uninitialized handle or double close
- if (wrap->handle__ == NULL) return v8::Null();
- assert(!wrap->object_.IsEmpty());
- uv_close(wrap->handle__, OnClose);
- wrap->handle__ = NULL;
+ if (wrap) {
+ // guard against uninitialized handle or double close
+ if (wrap->handle__ == NULL) return v8::Null();
+ assert(!wrap->object_.IsEmpty());
+ uv_close(wrap->handle__, OnClose);
+ wrap->handle__ = NULL;
- HandleWrap::Ref(args);
+ HandleWrap::Ref(args);
- wrap->StateChange();
+ wrap->StateChange();
+ }
return v8::Null();
}
View
@@ -22,6 +22,8 @@
#ifndef SRC_NODE_INTERNALS_H_
#define SRC_NODE_INTERNALS_H_
+#include <stdlib.h>
+
#include "v8.h"
namespace node {
@@ -81,6 +83,17 @@ inline static v8::Handle<v8::Value> ThrowRangeError(const char* errmsg) {
THROW_ERROR(v8::Exception::RangeError);
}
+#define UNWRAP(type) \
+ assert(!args.Holder().IsEmpty()); \
+ assert(args.Holder()->InternalFieldCount() > 0); \
+ type* wrap = \
+ static_cast<type*>(args.Holder()->GetPointerFromInternalField(0)); \
+ if (!wrap) { \
+ fprintf(stderr, #type ": Aborting due to unwrap failure at %s:%d\n", \
+ __FILE__, __LINE__); \
+ abort(); \
+ }
+
} // namespace node
#endif // SRC_NODE_INTERNALS_H_
View
@@ -26,18 +26,6 @@
#include "stream_wrap.h"
#include "pipe_wrap.h"
-#define UNWRAP \
- assert(!args.Holder().IsEmpty()); \
- assert(args.Holder()->InternalFieldCount() > 0); \
- PipeWrap* wrap = \
- static_cast<PipeWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
- if (!wrap) { \
- uv_err_t err; \
- err.code = UV_EBADF; \
- SetErrno(err); \
- return scope.Close(Integer::New(-1)); \
- }
-
namespace node {
using v8::Object;
@@ -149,7 +137,7 @@ PipeWrap::PipeWrap(Handle<Object> object, bool ipc)
Handle<Value> PipeWrap::Bind(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(PipeWrap)
String::AsciiValue name(args[0]);
@@ -166,7 +154,7 @@ Handle<Value> PipeWrap::Bind(const Arguments& args) {
Handle<Value> PipeWrap::SetPendingInstances(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(PipeWrap)
int instances = args[0]->Int32Value();
@@ -180,7 +168,7 @@ Handle<Value> PipeWrap::SetPendingInstances(const Arguments& args) {
Handle<Value> PipeWrap::Listen(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(PipeWrap)
int backlog = args[0]->Int32Value();
@@ -269,7 +257,7 @@ void PipeWrap::AfterConnect(uv_connect_t* req, int status) {
Handle<Value> PipeWrap::Open(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(PipeWrap)
int fd = args[0]->IntegerValue();
@@ -282,7 +270,7 @@ Handle<Value> PipeWrap::Open(const Arguments& args) {
Handle<Value> PipeWrap::Connect(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(PipeWrap)
String::AsciiValue name(args[0]);
View
@@ -25,18 +25,6 @@
#include <string.h>
#include <stdlib.h>
-#define UNWRAP \
- assert(!args.Holder().IsEmpty()); \
- assert(args.Holder()->InternalFieldCount() > 0); \
- ProcessWrap* wrap = \
- static_cast<ProcessWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
- if (!wrap) { \
- uv_err_t err; \
- err.code = UV_EBADF; \
- SetErrno(err); \
- return scope.Close(Integer::New(-1)); \
- }
-
namespace node {
using v8::Object;
@@ -97,7 +85,7 @@ class ProcessWrap : public HandleWrap {
static Handle<Value> Spawn(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(ProcessWrap)
Local<Object> js_options = args[0]->ToObject();
@@ -238,7 +226,7 @@ class ProcessWrap : public HandleWrap {
static Handle<Value> Kill(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(ProcessWrap)
int signal = args[0]->Int32Value();
View
@@ -53,19 +53,6 @@ using v8::Number;
using v8::Exception;
-#define UNWRAP \
- assert(!args.Holder().IsEmpty()); \
- assert(args.Holder()->InternalFieldCount() > 0); \
- StreamWrap* wrap = \
- static_cast<StreamWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
- if (!wrap) { \
- uv_err_t err; \
- err.code = UV_EBADF; \
- SetErrno(err); \
- return scope.Close(Integer::New(-1)); \
- }
-
-
typedef class ReqWrap<uv_shutdown_t> ShutdownWrap;
class WriteWrap: public ReqWrap<uv_write_t> {
@@ -134,7 +121,7 @@ void StreamWrap::UpdateWriteQueueSize() {
Handle<Value> StreamWrap::ReadStart(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(StreamWrap)
bool ipc_pipe = wrap->stream_->type == UV_NAMED_PIPE &&
((uv_pipe_t*)wrap->stream_)->ipc;
@@ -155,7 +142,7 @@ Handle<Value> StreamWrap::ReadStart(const Arguments& args) {
Handle<Value> StreamWrap::ReadStop(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(StreamWrap)
int r = uv_read_stop(wrap->stream_);
@@ -248,7 +235,7 @@ void StreamWrap::OnRead2(uv_pipe_t* handle, ssize_t nread, uv_buf_t buf,
Handle<Value> StreamWrap::WriteBuffer(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(StreamWrap)
// The first argument is a buffer.
assert(args.Length() >= 1 && Buffer::HasInstance(args[0]));
@@ -299,7 +286,7 @@ Handle<Value> StreamWrap::WriteStringImpl(const Arguments& args) {
HandleScope scope;
int r;
- UNWRAP
+ UNWRAP(StreamWrap)
if (args.Length() < 1)
return ThrowTypeError("Not enough arguments");
@@ -474,7 +461,7 @@ void StreamWrap::AfterWrite(uv_write_t* req, int status) {
Handle<Value> StreamWrap::Shutdown(const Arguments& args) {
HandleScope scope;
- UNWRAP
+ UNWRAP(StreamWrap)
ShutdownWrap* req_wrap = new ShutdownWrap();
Oops, something went wrong.

0 comments on commit 45de259

Please sign in to comment.