Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Make UNWRAP macro generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sannis authored and bnoordhuis committed May 21, 2012
1 parent 81a4edc commit 45de259
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 159 deletions.
16 changes: 2 additions & 14 deletions src/fs_event_wrap.cc
Expand Up @@ -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);
Expand Down Expand Up @@ -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")));
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 13 additions & 23 deletions src/handle_wrap.cc
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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));

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis May 21, 2012

Member

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

This comment has been minimized.

Copy link
@Sannis

Sannis May 22, 2012

Author

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();
}
Expand Down
13 changes: 13 additions & 0 deletions src/node_internals.h
Expand Up @@ -22,6 +22,8 @@
#ifndef SRC_NODE_INTERNALS_H_
#define SRC_NODE_INTERNALS_H_

#include <stdlib.h>

#include "v8.h"

namespace node {
Expand Down Expand Up @@ -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_
22 changes: 5 additions & 17 deletions src/pipe_wrap.cc
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);

Expand All @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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]);

Expand Down
16 changes: 2 additions & 14 deletions src/process_wrap.cc
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
23 changes: 5 additions & 18 deletions src/stream_wrap.cc
Expand Up @@ -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> {
Expand Down Expand Up @@ -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;
Expand All @@ -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_);

Expand Down Expand Up @@ -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]));
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 45de259

Please sign in to comment.