Skip to content

Commit

Permalink
src: replace assert() with CHECK()
Browse files Browse the repository at this point in the history
Mechanically replace assert() statements with UNREACHABLE(), CHECK(),
or CHECK_{EQ,NE,LT,GT,LE,GE}() statements.

The exceptions are src/node.h and src/node_object_wrap.h because they
are public headers.

PR-URL: node-forward/node#16
Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
bnoordhuis authored and indutny committed Oct 11, 2014
1 parent 75a461d commit 5fdff38
Show file tree
Hide file tree
Showing 36 changed files with 348 additions and 359 deletions.
10 changes: 4 additions & 6 deletions src/async-wrap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
#include "env-inl.h"
#include "util.h"
#include "util-inl.h"

#include "v8.h"
#include <assert.h>

namespace node {

Expand Down Expand Up @@ -74,7 +72,7 @@ inline v8::Handle<v8::Value> AsyncWrap::MakeDomainCallback(
const v8::Handle<v8::Function> cb,
int argc,
v8::Handle<v8::Value>* argv) {
assert(env()->context() == env()->isolate()->GetCurrentContext());
CHECK_EQ(env()->context(), env()->isolate()->GetCurrentContext());

v8::Local<v8::Object> context = object();
v8::Local<v8::Object> process = env()->process_object();
Expand Down Expand Up @@ -169,7 +167,7 @@ inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
if (env()->using_domains())
return MakeDomainCallback(cb, argc, argv);

assert(env()->context() == env()->isolate()->GetCurrentContext());
CHECK_EQ(env()->context(), env()->isolate()->GetCurrentContext());

v8::Local<v8::Object> context = object();
v8::Local<v8::Object> process = env()->process_object();
Expand Down Expand Up @@ -235,7 +233,7 @@ inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
v8::Handle<v8::Value>* argv) {
v8::Local<v8::Value> cb_v = object()->Get(symbol);
v8::Local<v8::Function> cb = cb_v.As<v8::Function>();
assert(cb->IsFunction());
CHECK(cb->IsFunction());

return MakeCallback(cb, argc, argv);
}
Expand All @@ -247,7 +245,7 @@ inline v8::Handle<v8::Value> AsyncWrap::MakeCallback(
v8::Handle<v8::Value>* argv) {
v8::Local<v8::Value> cb_v = object()->Get(index);
v8::Local<v8::Function> cb = cb_v.As<v8::Function>();
assert(cb->IsFunction());
CHECK(cb->IsFunction());

return MakeCallback(cb, argc, argv);
}
Expand Down
8 changes: 3 additions & 5 deletions src/base-object-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@
#include "util-inl.h"
#include "v8.h"

#include <assert.h>

namespace node {

inline BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> handle)
: handle_(env->isolate(), handle),
env_(env) {
assert(!handle.IsEmpty());
CHECK_EQ(false, handle.IsEmpty());
}


inline BaseObject::~BaseObject() {
assert(handle_.IsEmpty());
CHECK(handle_.IsEmpty());
}


Expand Down Expand Up @@ -71,7 +69,7 @@ template <typename Type>
inline void BaseObject::MakeWeak(Type* ptr) {
v8::HandleScope scope(env_->isolate());
v8::Local<v8::Object> handle = object();
assert(handle->InternalFieldCount() > 0);
CHECK_GT(handle->InternalFieldCount(), 0);
Wrap<Type>(handle, ptr);
handle_.MarkIndependent();
handle_.SetWeak<Type>(ptr, WeakCallback<Type>);
Expand Down
57 changes: 28 additions & 29 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "util.h"
#include "uv.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -85,7 +84,7 @@ RB_GENERATE_STATIC(ares_task_list, ares_task_t, node, cmp_ares_tasks)
/* call back into c-ares for possibly processing timeouts. */
static void ares_timeout(uv_timer_t* handle) {
Environment* env = Environment::from_cares_timer_handle(handle);
assert(!RB_EMPTY(env->cares_task_list()));
CHECK_EQ(false, RB_EMPTY(env->cares_task_list()));
ares_process_fd(env->cares_channel(), ARES_SOCKET_BAD, ARES_SOCKET_BAD);
}

Expand Down Expand Up @@ -159,7 +158,7 @@ static void ares_sockstate_cb(void* data,
/* If this is the first socket then start the timer. */
uv_timer_t* timer_handle = env->cares_timer_handle();
if (!uv_is_active(reinterpret_cast<uv_handle_t*>(timer_handle))) {
assert(RB_EMPTY(env->cares_task_list()));
CHECK(RB_EMPTY(env->cares_task_list()));
uv_timer_start(timer_handle, ares_timeout, 1000, 1000);
}

Expand All @@ -184,8 +183,8 @@ static void ares_sockstate_cb(void* data,
/* read == 0 and write == 0 this is c-ares's way of notifying us that */
/* the socket is now closed. We must free the data associated with */
/* socket. */
assert(task &&
"When an ares socket is closed we should have a handle for it");
CHECK(task &&
"When an ares socket is closed we should have a handle for it");

RB_REMOVE(ares_task_list, env->cares_task_list(), task);
uv_close(reinterpret_cast<uv_handle_t*>(&task->poll_watcher),
Expand Down Expand Up @@ -233,18 +232,18 @@ class QueryWrap : public AsyncWrap {
}

virtual ~QueryWrap() {
assert(!persistent().IsEmpty());
CHECK_EQ(false, persistent().IsEmpty());
persistent().Reset();
}

// Subclasses should implement the appropriate Send method.
virtual int Send(const char* name) {
assert(0);
UNREACHABLE();
return 0;
}

virtual int Send(const char* name, int family) {
assert(0);
UNREACHABLE();
return 0;
}

Expand Down Expand Up @@ -301,7 +300,7 @@ class QueryWrap : public AsyncWrap {
}

void ParseError(int status) {
assert(status != ARES_SUCCESS);
CHECK_NE(status, ARES_SUCCESS);
HandleScope handle_scope(env()->isolate());
Context::Scope context_scope(env()->context());
Local<Value> arg;
Expand Down Expand Up @@ -344,11 +343,11 @@ class QueryWrap : public AsyncWrap {

// Subclasses should implement the appropriate Parse method.
virtual void Parse(unsigned char* buf, int len) {
assert(0);
UNREACHABLE();
};

virtual void Parse(struct hostent* host) {
assert(0);
UNREACHABLE();
};
};

Expand Down Expand Up @@ -843,9 +842,9 @@ template <class Wrap>
static void Query(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());

assert(!args.IsConstructCall());
assert(args[0]->IsObject());
assert(args[1]->IsString());
CHECK_EQ(false, args.IsConstructCall());
CHECK(args[0]->IsObject());
CHECK(args[1]->IsString());

Local<Object> req_wrap_obj = args[0].As<Object>();
Local<String> string = args[1].As<String>();
Expand Down Expand Up @@ -894,7 +893,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
// strings for each IP and filling the results array.
address = res;
while (address) {
assert(address->ai_socktype == SOCK_STREAM);
CHECK_EQ(address->ai_socktype, SOCK_STREAM);

// Ignore random ai_family types.
if (address->ai_family == AF_INET) {
Expand All @@ -921,7 +920,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
// Iterate over the IPv6 responses putting them in the array.
address = res;
while (address) {
assert(address->ai_socktype == SOCK_STREAM);
CHECK_EQ(address->ai_socktype, SOCK_STREAM);

// Ignore random ai_family types.
if (address->ai_family == AF_INET6) {
Expand Down Expand Up @@ -1008,9 +1007,9 @@ static void IsIP(const FunctionCallbackInfo<Value>& args) {
static void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());

assert(args[0]->IsObject());
assert(args[1]->IsString());
assert(args[2]->IsInt32());
CHECK(args[0]->IsObject());
CHECK(args[1]->IsString());
CHECK(args[2]->IsInt32());
Local<Object> req_wrap_obj = args[0].As<Object>();
node::Utf8Value hostname(args[1]);

Expand All @@ -1028,7 +1027,7 @@ static void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
family = AF_INET6;
break;
default:
assert(0 && "bad address family");
CHECK(0 && "bad address family");
abort();
}

Expand Down Expand Up @@ -1097,7 +1096,7 @@ static void GetServers(const FunctionCallbackInfo<Value>& args) {
ares_addr_node* servers;

int r = ares_get_servers(env->cares_channel(), &servers);
assert(r == ARES_SUCCESS);
CHECK_EQ(r, ARES_SUCCESS);

ares_addr_node* cur = servers;

Expand All @@ -1106,7 +1105,7 @@ static void GetServers(const FunctionCallbackInfo<Value>& args) {

const void* caddr = static_cast<const void*>(&cur->addr);
int err = uv_inet_ntop(cur->family, caddr, ip, sizeof(ip));
assert(err == 0);
CHECK_EQ(err, 0);

Local<String> addr = OneByteString(env->isolate(), ip);
server_array->Set(i, addr);
Expand All @@ -1121,7 +1120,7 @@ static void GetServers(const FunctionCallbackInfo<Value>& args) {
static void SetServers(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());

assert(args[0]->IsArray());
CHECK(args[0]->IsArray());

Local<Array> arr = Local<Array>::Cast(args[0]);

Expand All @@ -1138,12 +1137,12 @@ static void SetServers(const FunctionCallbackInfo<Value>& args) {
int err;

for (uint32_t i = 0; i < len; i++) {
assert(arr->Get(i)->IsArray());
CHECK(arr->Get(i)->IsArray());

Local<Array> elm = Local<Array>::Cast(arr->Get(i));

assert(elm->Get(0)->Int32Value());
assert(elm->Get(1)->IsString());
CHECK(elm->Get(0)->Int32Value());
CHECK(elm->Get(1)->IsString());

int fam = elm->Get(0)->Int32Value();
node::Utf8Value ip(elm->Get(1));
Expand All @@ -1160,7 +1159,7 @@ static void SetServers(const FunctionCallbackInfo<Value>& args) {
err = uv_inet_pton(AF_INET6, *ip, &cur->addr);
break;
default:
assert(0 && "Bad address family.");
CHECK(0 && "Bad address family.");
abort();
}

Expand Down Expand Up @@ -1213,7 +1212,7 @@ static void Initialize(Handle<Object> target,
Environment* env = Environment::GetCurrent(context);

int r = ares_library_init(ARES_LIB_INIT_ALL);
assert(r == ARES_SUCCESS);
CHECK_EQ(r, ARES_SUCCESS);

struct ares_options options;
memset(&options, 0, sizeof(options));
Expand All @@ -1225,7 +1224,7 @@ static void Initialize(Handle<Object> target,
r = ares_init_options(env->cares_channel_ptr(),
&options,
ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB);
assert(r == ARES_SUCCESS);
CHECK_EQ(r, ARES_SUCCESS);

/* Initialize the timeout timer. The timer won't be started until the */
/* first socket is opened. */
Expand Down
8 changes: 4 additions & 4 deletions src/fs_event_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ FSEventWrap::FSEventWrap(Environment* env, Handle<Object> object)


FSEventWrap::~FSEventWrap() {
assert(initialized_ == false);
CHECK_EQ(initialized_, false);
}


Expand All @@ -95,7 +95,7 @@ void FSEventWrap::Initialize(Handle<Object> target,


void FSEventWrap::New(const FunctionCallbackInfo<Value>& args) {
assert(args.IsConstructCall());
CHECK(args.IsConstructCall());
Environment* env = Environment::GetCurrent(args.GetIsolate());
new FSEventWrap(env, args.This());
}
Expand Down Expand Up @@ -144,7 +144,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());

assert(wrap->persistent().IsEmpty() == false);
CHECK_EQ(wrap->persistent().IsEmpty(), false);

// We're in a bind here. libuv can set both UV_RENAME and UV_CHANGE but
// the Node API only lets us pass a single event to JS land.
Expand All @@ -165,7 +165,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
} else if (events & UV_CHANGE) {
event_string = env->change_string();
} else {
assert(0 && "bad fs events flag");
CHECK(0 && "bad fs events flag");
abort();
}

Expand Down
8 changes: 4 additions & 4 deletions src/handle_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void HandleWrap::Close(const FunctionCallbackInfo<Value>& args) {
if (wrap == NULL || wrap->handle__ == NULL)
return;

assert(!wrap->persistent().IsEmpty());
CHECK_EQ(false, wrap->persistent().IsEmpty());
uv_close(wrap->handle__, OnClose);
wrap->handle__ = NULL;

Expand All @@ -95,7 +95,7 @@ HandleWrap::HandleWrap(Environment* env,


HandleWrap::~HandleWrap() {
assert(persistent().IsEmpty());
CHECK(persistent().IsEmpty());
QUEUE_REMOVE(&handle_wrap_queue_);
}

Expand All @@ -106,10 +106,10 @@ void HandleWrap::OnClose(uv_handle_t* handle) {
HandleScope scope(env->isolate());

// The wrap object should still be there.
assert(wrap->persistent().IsEmpty() == false);
CHECK_EQ(wrap->persistent().IsEmpty(), false);

// But the handle pointer should be gone.
assert(wrap->handle__ == NULL);
CHECK_EQ(wrap->handle__, NULL);

HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
Expand Down
Loading

0 comments on commit 5fdff38

Please sign in to comment.