Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
upgrade to NAN@0.9-wip for Node 0.11.13 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Apr 13, 2014
1 parent 5658134 commit 7cb2528
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Database;
, NanCallback *callback
) : NanAsyncWorker(callback), database(database) {
NanScope();
v8::Local<v8::Object> obj = v8::Object::New();
NanAssignPersistent(v8::Object, persistentHandle, obj);
v8::Local<v8::Object> obj = NanNew<v8::Object>();
NanAssignPersistent(persistentHandle, obj);
}

protected:
Expand Down
10 changes: 5 additions & 5 deletions src/batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ leveldb::Status Batch::Write () {
}

void Batch::Init () {
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(Batch::New);
NanAssignPersistent(v8::FunctionTemplate, batch_constructor, tpl);
v8::Local<v8::FunctionTemplate> tpl = NanNew<v8::FunctionTemplate>(Batch::New);
NanAssignPersistent(batch_constructor, tpl);
tpl->SetClassName(NanSymbol("Batch"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(tpl, "put", Batch::Put);
Expand Down Expand Up @@ -62,12 +62,12 @@ v8::Handle<v8::Value> Batch::NewInstance (
, v8::Handle<v8::Object> optionsObj
) {

NanScope();
NanEscapableScope();

v8::Local<v8::Object> instance;

v8::Local<v8::FunctionTemplate> constructorHandle =
NanPersistentToLocal(batch_constructor);
NanNew<v8::FunctionTemplate>(batch_constructor);

if (optionsObj.IsEmpty()) {
v8::Handle<v8::Value> argv[1] = { database };
Expand All @@ -77,7 +77,7 @@ v8::Handle<v8::Value> Batch::NewInstance (
instance = constructorHandle->GetFunction()->NewInstance(2, argv);
}

return scope.Close(instance);
return NanEscapeScope(instance);
}

NAN_METHOD(Batch::Put) {
Expand Down
26 changes: 13 additions & 13 deletions src/database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ NAN_METHOD(LevelDOWN) {
}

void Database::Init () {
v8::Local<v8::FunctionTemplate> tpl = v8::FunctionTemplate::New(Database::New);
NanAssignPersistent(v8::FunctionTemplate, database_constructor, tpl);
v8::Local<v8::FunctionTemplate> tpl = NanNew<v8::FunctionTemplate>(Database::New);
NanAssignPersistent(database_constructor, tpl);
tpl->SetClassName(NanSymbol("Database"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(tpl, "open", Database::Open);
Expand All @@ -163,7 +163,7 @@ NAN_METHOD(Database::New) {
if (!args[0]->IsString())
return NanThrowError("constructor requires a location string argument");

char* location = NanFromV8String(args[0].As<v8::Object>(), Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);
char* location = NanCString(args[0].As<v8::Object>(), NULL, NULL, 0, v8::String::NO_OPTIONS);

Database* obj = new Database(location);
obj->Wrap(args.This());
Expand All @@ -172,12 +172,12 @@ NAN_METHOD(Database::New) {
}

v8::Handle<v8::Value> Database::NewInstance (v8::Local<v8::String> &location) {
NanScope();
NanEscapableScope();

v8::Local<v8::Object> instance;

v8::Local<v8::FunctionTemplate> constructorHandle =
NanPersistentToLocal(database_constructor);
NanNew<v8::FunctionTemplate>(database_constructor);

if (location.IsEmpty()) {
instance = constructorHandle->GetFunction()->NewInstance(0, NULL);
Expand All @@ -186,7 +186,7 @@ v8::Handle<v8::Value> Database::NewInstance (v8::Local<v8::String> &location) {
instance = constructorHandle->GetFunction()->NewInstance(1, argv);
}

return instance;
return NanEscapeScope(instance);
}

NAN_METHOD(Database::Open) {
Expand Down Expand Up @@ -286,7 +286,7 @@ NAN_METHOD(Database::Close) {
// CloseWorker will be invoked

/*
v8::Local<v8::Object> localHandle = NanPersistentToLocal(it->second);
v8::Local<v8::Object> localHandle = NanNew(it->second);
leveldown::Iterator* iterator =
node::ObjectWrap::Unwrap<leveldown::Iterator>(localHandle->
Get(NanSymbol("iterator")).As<v8::Object>());
Expand All @@ -296,11 +296,11 @@ NAN_METHOD(Database::Close) {
if (!iterator->ended) {
v8::Local<v8::Function> end =
v8::Local<v8::Function>::Cast(NanObjectWrapHandle(iterator)->Get(
v8::String::NewSymbol("end")));
NanNew<v8::String>("end")));
v8::Local<v8::Value> argv[] = {
v8::FunctionTemplate::New()->GetFunction() // empty callback
NanNew<v8::FunctionTemplate>()->GetFunction() // empty callback
};
node::MakeCallback(
NanMakeCallback(
NanObjectWrapHandle(iterator)
, end
, 1
Expand Down Expand Up @@ -536,7 +536,7 @@ NAN_METHOD(Database::GetProperty) {
std::string* value = new std::string();
database->GetPropertyFromDatabase(property, value);
v8::Local<v8::String> returnValue
= v8::String::New(value->c_str(), value->length());
= NanNew<v8::String>(value->c_str(), value->length());
delete value;
delete[] property.data();

Expand All @@ -559,7 +559,7 @@ NAN_METHOD(Database::Iterator) {
v8::TryCatch try_catch;
v8::Local<v8::Object> iteratorHandle = Iterator::NewInstance(
args.This()
, v8::Number::New(id)
, NanNew<v8::Number>(id)
, optionsObj
);
if (try_catch.HasCaught()) {
Expand All @@ -573,7 +573,7 @@ NAN_METHOD(Database::Iterator) {

// register our iterator
/*
v8::Local<v8::Object> obj = v8::Object::New();
v8::Local<v8::Object> obj = NanNew<v8::Object>();
obj->Set(NanSymbol("iterator"), iteratorHandle);
v8::Persistent<v8::Object> persistent;
persistent.Reset(nan_isolate, obj);
Expand Down
4 changes: 2 additions & 2 deletions src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ struct Reference {
leveldb::Slice slice;

Reference(v8::Local<v8::Value> obj, leveldb::Slice slice) : slice(slice) {
v8::Local<v8::Object> _obj = v8::Object::New();
v8::Local<v8::Object> _obj = NanNew<v8::Object>();
_obj->Set(NanSymbol("obj"), obj);
NanAssignPersistent(v8::Object, handle, _obj);
NanAssignPersistent(handle, _obj);
};
};

Expand Down
8 changes: 4 additions & 4 deletions src/database_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ void ReadWorker::HandleOKCallback () {
if (asBuffer) {
returnValue = NanNewBufferHandle((char*)value.data(), value.size());
} else {
returnValue = v8::String::New((char*)value.data(), value.size());
returnValue = NanNew<v8::String>((char*)value.data(), value.size());
}
v8::Local<v8::Value> argv[] = {
NanNewLocal<v8::Value>(v8::Null())
NanNew<v8::Primitive>(NanNull())
, returnValue
};
callback->Call(2, argv);
Expand Down Expand Up @@ -258,9 +258,9 @@ void ApproximateSizeWorker::WorkComplete() {
void ApproximateSizeWorker::HandleOKCallback () {
NanScope();

v8::Local<v8::Value> returnValue = v8::Number::New((double) size);
v8::Local<v8::Value> returnValue = NanNew<v8::Number>((double) size);
v8::Local<v8::Value> argv[] = {
NanNewLocal<v8::Value>(v8::Null())
NanNew<v8::Primitive>(NanNull())
, returnValue
};
callback->Call(2, argv);
Expand Down
31 changes: 13 additions & 18 deletions src/iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ Iterator::Iterator (
{
NanScope();

v8::Local<v8::Object> obj = v8::Object::New();
v8::Local<v8::Object> obj = NanNew<v8::Object>();
if (!startHandle.IsEmpty())
obj->Set(NanSymbol("start"), startHandle);
NanAssignPersistent(v8::Object, persistentHandle, obj);
NanAssignPersistent(persistentHandle, obj);

options = new leveldb::ReadOptions();
options->fill_cache = fillCache;
Expand All @@ -65,7 +65,7 @@ Iterator::Iterator (
Iterator::~Iterator () {
delete options;
if (!persistentHandle.IsEmpty())
NanDispose(persistentHandle);
NanDisposePersistent(persistentHandle);
if (start != NULL)
delete start;
if (end != NULL)
Expand Down Expand Up @@ -243,8 +243,8 @@ NAN_METHOD(Iterator::End) {

void Iterator::Init () {
v8::Local<v8::FunctionTemplate> tpl =
v8::FunctionTemplate::New(Iterator::New);
NanAssignPersistent(v8::FunctionTemplate, iterator_constructor, tpl);
NanNew<v8::FunctionTemplate>(Iterator::New);
NanAssignPersistent(iterator_constructor, tpl);
tpl->SetClassName(NanSymbol("Iterator"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(tpl, "next", Iterator::Next);
Expand All @@ -257,11 +257,11 @@ v8::Local<v8::Object> Iterator::NewInstance (
, v8::Local<v8::Object> optionsObj
) {

NanScope();
NanEscapableScope();

v8::Local<v8::Object> instance;
v8::Local<v8::FunctionTemplate> constructorHandle =
NanPersistentToLocal(iterator_constructor);
NanNew<v8::FunctionTemplate>(iterator_constructor);

if (optionsObj.IsEmpty()) {
v8::Handle<v8::Value> argv[2] = { database, id };
Expand All @@ -271,7 +271,7 @@ v8::Local<v8::Object> Iterator::NewInstance (
instance = constructorHandle->GetFunction()->NewInstance(3, argv);
}

return instance;
return NanEscapeScope(instance);
}

NAN_METHOD(Iterator::New) {
Expand Down Expand Up @@ -326,8 +326,7 @@ NAN_METHOD(Iterator::New) {
&& (node::Buffer::HasInstance(optionsObj->Get(NanSymbol("end")))
|| optionsObj->Get(NanSymbol("end"))->IsString())) {

v8::Local<v8::Value> endBuffer =
NanNewLocal<v8::Value>(optionsObj->Get(NanSymbol("end")));
v8::Local<v8::Value> endBuffer = optionsObj->Get(NanSymbol("end"));

// ignore end if it has size 0 since a Slice can't have length 0
if (StringOrBufferLength(endBuffer) > 0) {
Expand All @@ -345,8 +344,7 @@ NAN_METHOD(Iterator::New) {
&& (node::Buffer::HasInstance(optionsObj->Get(NanSymbol("lt")))
|| optionsObj->Get(NanSymbol("lt"))->IsString())) {

v8::Local<v8::Value> ltBuffer =
NanNewLocal<v8::Value>(optionsObj->Get(NanSymbol("lt")));
v8::Local<v8::Value> ltBuffer = optionsObj->Get(NanSymbol("lt"));

// ignore end if it has size 0 since a Slice can't have length 0
if (StringOrBufferLength(ltBuffer) > 0) {
Expand All @@ -361,8 +359,7 @@ NAN_METHOD(Iterator::New) {
&& (node::Buffer::HasInstance(optionsObj->Get(NanSymbol("lte")))
|| optionsObj->Get(NanSymbol("lte"))->IsString())) {

v8::Local<v8::Value> lteBuffer =
NanNewLocal<v8::Value>(optionsObj->Get(NanSymbol("lte")));
v8::Local<v8::Value> lteBuffer = optionsObj->Get(NanSymbol("lte"));

// ignore end if it has size 0 since a Slice can't have length 0
if (StringOrBufferLength(lteBuffer) > 0) {
Expand All @@ -377,8 +374,7 @@ NAN_METHOD(Iterator::New) {
&& (node::Buffer::HasInstance(optionsObj->Get(NanSymbol("gt")))
|| optionsObj->Get(NanSymbol("gt"))->IsString())) {

v8::Local<v8::Value> gtBuffer =
NanNewLocal<v8::Value>(optionsObj->Get(NanSymbol("gt")));
v8::Local<v8::Value> gtBuffer = optionsObj->Get(NanSymbol("gt"));

// ignore end if it has size 0 since a Slice can't have length 0
if (StringOrBufferLength(gtBuffer) > 0) {
Expand All @@ -393,8 +389,7 @@ NAN_METHOD(Iterator::New) {
&& (node::Buffer::HasInstance(optionsObj->Get(NanSymbol("gte")))
|| optionsObj->Get(NanSymbol("gte"))->IsString())) {

v8::Local<v8::Value> gteBuffer =
NanNewLocal<v8::Value>(optionsObj->Get(NanSymbol("gte")));
v8::Local<v8::Value> gteBuffer = optionsObj->Get(NanSymbol("gte"));

// ignore end if it has size 0 since a Slice can't have length 0
if (StringOrBufferLength(gteBuffer) > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/iterator_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ void NextWorker::HandleOKCallback () {
if (iterator->keyAsBuffer) {
returnKey = NanNewBufferHandle((char*)key.data(), key.size());
} else {
returnKey = v8::String::New((char*)key.data(), key.size());
returnKey = NanNew<v8::String>((char*)key.data(), key.size());
}

v8::Local<v8::Value> returnValue;
if (iterator->valueAsBuffer) {
returnValue = NanNewBufferHandle((char*)value.data(), value.size());
} else {
returnValue = v8::String::New((char*)value.data(), value.size());
returnValue = NanNew<v8::String>((char*)value.data(), value.size());
}

// clean up & handle the next/end state see iterator.cc/checkEndCallback
localCallback(iterator);

if (ok) {
v8::Local<v8::Value> argv[] = {
NanNewLocal<v8::Value>(v8::Null())
NanNew<v8::Primitive>(NanNull())
, returnKey
, returnValue
};
Expand Down
10 changes: 5 additions & 5 deletions src/leveldown.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ NAN_METHOD(DestroyDB) {
return NanThrowError("destroy() requires a callback function argument");
}

char* location = NanFromV8String(args[0].As<v8::Object>(), Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);
char* location = NanCString(args[0].As<v8::Object>(), NULL, NULL, 0, v8::String::NO_OPTIONS);

NanCallback* callback = new NanCallback(
v8::Local<v8::Function>::Cast(args[1]));
Expand Down Expand Up @@ -58,7 +58,7 @@ NAN_METHOD(RepairDB) {
return NanThrowError("repair() requires a callback function argument");
}

char* location = NanFromV8String(args[0].As<v8::Object>(), Nan::UTF8, NULL, NULL, 0, v8::String::NO_OPTIONS);
char* location = NanCString(args[0].As<v8::Object>(), NULL, NULL, 0, v8::String::NO_OPTIONS);

NanCallback* callback = new NanCallback(
v8::Local<v8::Function>::Cast(args[1]));
Expand All @@ -79,16 +79,16 @@ void Init (v8::Handle<v8::Object> target) {
leveldown::Batch::Init();

v8::Local<v8::Function> leveldown =
v8::FunctionTemplate::New(LevelDOWN)->GetFunction();
NanNew<v8::FunctionTemplate>(LevelDOWN)->GetFunction();

leveldown->Set(
NanSymbol("destroy")
, v8::FunctionTemplate::New(DestroyDB)->GetFunction()
, NanNew<v8::FunctionTemplate>(DestroyDB)->GetFunction()
);

leveldown->Set(
NanSymbol("repair")
, v8::FunctionTemplate::New(RepairDB)->GetFunction()
, NanNew<v8::FunctionTemplate>(RepairDB)->GetFunction()
);

target->Set(NanSymbol("leveldown"), leveldown);
Expand Down
12 changes: 5 additions & 7 deletions src/leveldown.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ static inline void DisposeStringOrBufferFromSlice(
v8::Persistent<v8::Object> &handle
, leveldb::Slice slice) {

if (!node::Buffer::HasInstance(NanPersistentToLocal(handle)->Get(NanSymbol("obj"))))
if (!node::Buffer::HasInstance(NanNew<v8::Object>(handle)->Get(NanSymbol("obj"))))
delete[] slice.data();
NanDispose(handle);
NanDisposePersistent(handle);
}

static inline void DisposeStringOrBufferFromSlice(
Expand Down Expand Up @@ -69,18 +69,16 @@ static inline void DisposeStringOrBufferFromSlice(
#define LD_RETURN_CALLBACK_OR_ERROR(callback, msg) \
if (!callback.IsEmpty() && callback->IsFunction()) { \
v8::Local<v8::Value> argv[] = { \
NanNewLocal<v8::Value>(v8::Exception::Error( \
v8::String::New(msg)) \
) \
NanError(msg) \
}; \
LD_RUN_CALLBACK(callback, 1, argv) \
NanReturnUndefined(); \
} \
return NanThrowError(msg);

#define LD_RUN_CALLBACK(callback, argc, argv) \
node::MakeCallback( \
v8::Context::GetCurrent()->Global(), callback, argc, argv);
NanMakeCallback( \
NanGetCurrentContext()->Global(), callback, argc, argv);

/* LD_METHOD_SETUP_COMMON setup the following objects:
* - Database* database
Expand Down

0 comments on commit 7cb2528

Please sign in to comment.