Skip to content

Commit

Permalink
src: use function to get internal pointer
Browse files Browse the repository at this point in the history
Remove the NODE_{WRAP,UNWRAP} macros and instead use template functions.
  • Loading branch information
trevnorris committed Oct 29, 2013
1 parent 4b84e42 commit 93f75a8
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 202 deletions.
8 changes: 4 additions & 4 deletions src/fs_event_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "env.h"
#include "env-inl.h"
#include "util.h"
#include "util-inl.h"
#include "node.h"
#include "handle_wrap.h"

Expand Down Expand Up @@ -98,8 +100,7 @@ void FSEventWrap::New(const FunctionCallbackInfo<Value>& args) {
void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

FSEventWrap* wrap;
NODE_UNWRAP(args.This(), FSEventWrap, wrap);
FSEventWrap* wrap = UnwrapObject<FSEventWrap>(args.This());

if (args.Length() < 1 || !args[0]->IsString()) {
return ThrowTypeError("Bad arguments");
Expand Down Expand Up @@ -178,8 +179,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
void FSEventWrap::Close(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

FSEventWrap* wrap;
NODE_UNWRAP_NO_ABORT(args.This(), FSEventWrap, wrap);
FSEventWrap* wrap = UnwrapObject<FSEventWrap>(args.This());

if (wrap == NULL || wrap->initialized_ == false)
return;
Expand Down
13 changes: 6 additions & 7 deletions src/handle_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "handle_wrap.h"
#include "env.h"
#include "env-inl.h"
#include "util.h"
#include "util-inl.h"
#include "node.h"
#include "queue.h"

Expand All @@ -42,8 +44,7 @@ extern QUEUE handle_wrap_queue;
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

HandleWrap* wrap;
NODE_UNWRAP_NO_ABORT(args.This(), HandleWrap, wrap);
HandleWrap* wrap = UnwrapObject<HandleWrap>(args.This());

if (wrap != NULL && wrap->handle__ != NULL) {
uv_ref(wrap->handle__);
Expand All @@ -55,8 +56,7 @@ void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

HandleWrap* wrap;
NODE_UNWRAP_NO_ABORT(args.This(), HandleWrap, wrap);
HandleWrap* wrap = UnwrapObject<HandleWrap>(args.This());

if (wrap != NULL && wrap->handle__ != NULL) {
uv_unref(wrap->handle__);
Expand All @@ -68,8 +68,7 @@ void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

HandleWrap* wrap;
NODE_UNWRAP_NO_ABORT(args.This(), HandleWrap, wrap);
HandleWrap* wrap = UnwrapObject<HandleWrap>(args.This());

// guard against uninitialized handle or double close
if (wrap == NULL || wrap->handle__ == NULL)
Expand All @@ -96,7 +95,7 @@ HandleWrap::HandleWrap(Environment* env,
handle__->data = this;
HandleScope scope(node_isolate);
persistent().Reset(node_isolate, object);
NODE_WRAP(object, this);
WrapObject<HandleWrap>(object, this);
QUEUE_INSERT_TAIL(&handle_wrap_queue, &handle_wrap_queue_);
}

Expand Down
24 changes: 13 additions & 11 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "node_watchdog.h"
#include "env.h"
#include "env-inl.h"
#include "util.h"
#include "util-inl.h"
#include "weak-object.h"
#include "weak-object-inl.h"

Expand Down Expand Up @@ -178,7 +180,7 @@ class ContextifyContext {
HandleScope scope(node_isolate);
Local<Object> wrapper =
env->script_data_constructor_function()->NewInstance();
NODE_WRAP(wrapper, this);
WrapObject<ContextifyContext>(wrapper, this);
return scope.Close(wrapper);
}

Expand Down Expand Up @@ -297,8 +299,8 @@ class ContextifyContext {
const PropertyCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

ContextifyContext* ctx = NULL;
NODE_UNWRAP(args.Data().As<Object>(), ContextifyContext, ctx);
ContextifyContext* ctx =
UnwrapObject<ContextifyContext>(args.Data().As<Object>());

Local<Object> sandbox = PersistentToLocal(node_isolate, ctx->sandbox_);
Local<Value> rv = sandbox->GetRealNamedProperty(property);
Expand All @@ -321,8 +323,8 @@ class ContextifyContext {
const PropertyCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

ContextifyContext* ctx = NULL;
NODE_UNWRAP(args.Data().As<Object>(), ContextifyContext, ctx);
ContextifyContext* ctx =
UnwrapObject<ContextifyContext>(args.Data().As<Object>());

PersistentToLocal(node_isolate, ctx->sandbox_)->Set(property, value);
}
Expand All @@ -333,8 +335,8 @@ class ContextifyContext {
const PropertyCallbackInfo<Integer>& args) {
HandleScope scope(node_isolate);

ContextifyContext* ctx = NULL;
NODE_UNWRAP(args.Data().As<Object>(), ContextifyContext, ctx);
ContextifyContext* ctx =
UnwrapObject<ContextifyContext>(args.Data().As<Object>());

Local<Object> sandbox = PersistentToLocal(node_isolate, ctx->sandbox_);
Local<Object> proxy_global = PersistentToLocal(node_isolate,
Expand All @@ -354,8 +356,8 @@ class ContextifyContext {
const PropertyCallbackInfo<Boolean>& args) {
HandleScope scope(node_isolate);

ContextifyContext* ctx = NULL;
NODE_UNWRAP(args.Data().As<Object>(), ContextifyContext, ctx);
ContextifyContext* ctx =
UnwrapObject<ContextifyContext>(args.Data().As<Object>());

bool success = PersistentToLocal(node_isolate,
ctx->sandbox_)->Delete(property);
Expand All @@ -371,8 +373,8 @@ class ContextifyContext {
const PropertyCallbackInfo<Array>& args) {
HandleScope scope(node_isolate);

ContextifyContext* ctx = NULL;
NODE_UNWRAP(args.Data().As<Object>(), ContextifyContext, ctx);
ContextifyContext* ctx =
UnwrapObject<ContextifyContext>(args.Data().As<Object>());

Local<Object> sandbox = PersistentToLocal(node_isolate, ctx->sandbox_);
args.GetReturnValue().Set(sandbox->GetPropertyNames());
Expand Down
47 changes: 17 additions & 30 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "env.h"
#include "env-inl.h"
#include "string_bytes.h"
#include "util.h"
#include "util-inl.h"
#include "v8.h"

#include <errno.h>
Expand Down Expand Up @@ -761,8 +763,7 @@ void SecureContext::GetTicketKeys(const FunctionCallbackInfo<Value>& args) {
#if !defined(OPENSSL_NO_TLSEXT) && defined(SSL_CTX_get_tlsext_ticket_keys)
HandleScope handle_scope(args.GetIsolate());

SecureContext* wrap;
NODE_UNWRAP(args.This(), SecureContext, wrap);
SecureContext* wrap = UnwrapObject<SecureContext>(args.This());

Local<Object> buff = Buffer::New(wrap->env(), 48);
if (SSL_CTX_get_tlsext_ticket_keys(wrap->ctx_,
Expand All @@ -786,8 +787,7 @@ void SecureContext::SetTicketKeys(const FunctionCallbackInfo<Value>& args) {
return ThrowTypeError("Bad argument");
}

SecureContext* wrap;
NODE_UNWRAP(args.This(), SecureContext, wrap);
SecureContext* wrap = UnwrapObject<SecureContext>(args.This());

if (SSL_CTX_set_tlsext_ticket_keys(wrap->ctx_,
Buffer::Data(args[0]),
Expand Down Expand Up @@ -915,8 +915,7 @@ void SSLWrap<Base>::GetPeerCertificate(
const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());
Environment* env = w->env();

Local<Object> info = Object::New();
Expand Down Expand Up @@ -1050,8 +1049,7 @@ template <class Base>
void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());

SSL_SESSION* sess = SSL_get_session(w->ssl_);
if (sess == NULL)
Expand All @@ -1072,8 +1070,7 @@ template <class Base>
void SSLWrap<Base>::SetSession(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());

if (args.Length() < 1 ||
(!args[0]->IsString() && !Buffer::HasInstance(args[0]))) {
Expand Down Expand Up @@ -1111,8 +1108,7 @@ template <class Base>
void SSLWrap<Base>::LoadSession(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());
Environment* env = w->env();

if (args.Length() >= 1 && Buffer::HasInstance(args[0])) {
Expand Down Expand Up @@ -1144,8 +1140,7 @@ void SSLWrap<Base>::LoadSession(const FunctionCallbackInfo<Value>& args) {
template <class Base>
void SSLWrap<Base>::IsSessionReused(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);
Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());
bool yes = SSL_session_reused(w->ssl_);
args.GetReturnValue().Set(yes);
}
Expand All @@ -1154,8 +1149,7 @@ void SSLWrap<Base>::IsSessionReused(const FunctionCallbackInfo<Value>& args) {
template <class Base>
void SSLWrap<Base>::ReceivedShutdown(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);
Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());
bool yes = SSL_get_shutdown(w->ssl_) == SSL_RECEIVED_SHUTDOWN;
args.GetReturnValue().Set(yes);
}
Expand All @@ -1164,8 +1158,7 @@ void SSLWrap<Base>::ReceivedShutdown(const FunctionCallbackInfo<Value>& args) {
template <class Base>
void SSLWrap<Base>::EndParser(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);
Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());
w->hello_parser_.End();
}

Expand All @@ -1174,8 +1167,7 @@ template <class Base>
void SSLWrap<Base>::Renegotiate(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());

ClearErrorOnReturn clear_error_on_return;
(void) &clear_error_on_return; // Silence unused variable warning.
Expand All @@ -1188,8 +1180,7 @@ void SSLWrap<Base>::Renegotiate(const FunctionCallbackInfo<Value>& args) {
template <class Base>
void SSLWrap<Base>::IsInitFinished(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);
Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());
bool yes = SSL_is_init_finished(w->ssl_);
args.GetReturnValue().Set(yes);
}
Expand All @@ -1200,8 +1191,7 @@ template <class Base>
void SSLWrap<Base>::VerifyError(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());

// XXX(indutny) Do this check in JS land?
X509* peer_cert = SSL_get_peer_certificate(w->ssl_);
Expand Down Expand Up @@ -1267,8 +1257,7 @@ template <class Base>
void SSLWrap<Base>::GetCurrentCipher(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());
Environment* env = w->env();

OPENSSL_CONST SSL_CIPHER* c = SSL_get_current_cipher(w->ssl_);
Expand Down Expand Up @@ -1363,8 +1352,7 @@ void SSLWrap<Base>::GetNegotiatedProto(
const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());

if (w->is_client()) {
if (w->selected_npn_proto_.IsEmpty() == false) {
Expand All @@ -1390,8 +1378,7 @@ template <class Base>
void SSLWrap<Base>::SetNPNProtocols(const FunctionCallbackInfo<Value>& args) {
HandleScope scope(node_isolate);

Base* w = NULL;
NODE_UNWRAP(args.This(), Base, w);
Base* w = UnwrapObject<Base>(args.This());

if (args.Length() < 1 || !Buffer::HasInstance(args[0]))
return ThrowTypeError("Must give a Buffer as first argument");
Expand Down
31 changes: 0 additions & 31 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,37 +173,6 @@ NO_RETURN void FatalError(const char* location, const char* message);

v8::Local<v8::Object> BuildStatsObject(Environment* env, const uv_stat_t* s);

#define NODE_WRAP(Object, Pointer) \
do { \
assert(!Object.IsEmpty()); \
assert(Object->InternalFieldCount() > 0); \
Object->SetAlignedPointerInInternalField(0, Pointer); \
} \
while (0)

#define NODE_UNWRAP(Object, TypeName, Var) \
do { \
assert(!Object.IsEmpty()); \
assert(Object->InternalFieldCount() > 0); \
Var = static_cast<TypeName*>( \
Object->GetAlignedPointerFromInternalField(0)); \
if (!Var) { \
fprintf(stderr, #TypeName ": Aborting due to unwrap failure at %s:%d\n", \
__FILE__, __LINE__); \
abort(); \
} \
} \
while (0)

#define NODE_UNWRAP_NO_ABORT(Object, TypeName, Var) \
do { \
assert(!Object.IsEmpty()); \
assert(Object->InternalFieldCount() > 0); \
Var = static_cast<TypeName*>( \
Object->GetAlignedPointerFromInternalField(0)); \
} \
while (0)

enum Endianness {
kLittleEndian, // _Not_ LITTLE_ENDIAN, clashes with endian.h.
kBigEndian
Expand Down
4 changes: 2 additions & 2 deletions src/node_stat_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
#ifndef SRC_NODE_STAT_WATCHER_H_
#define SRC_NODE_STAT_WATCHER_H_

#include "env.h"
#include "node.h"
#include "env.h"
#include "weak-object.h"
#include "uv.h"
#include "v8.h"
#include "weak-object.h"

namespace node {

Expand Down
Loading

0 comments on commit 93f75a8

Please sign in to comment.