From 869e2a477c7affddba130b365b3849dc4a39671d Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Sun, 12 Jul 2015 21:53:59 +1000 Subject: [PATCH] upgrade to NAN v2 --- .dntrc | 18 ++-- package.json | 2 +- src/async.h | 10 +- src/batch.cc | 74 ++++++--------- src/batch.h | 2 +- src/batch_async.cc | 2 +- src/batch_async.h | 2 +- src/common.h | 4 +- src/database.cc | 207 +++++++++++++++++------------------------ src/database.h | 12 +-- src/database_async.cc | 51 +++++----- src/database_async.h | 16 ++-- src/iterator.cc | 168 ++++++++++++++++----------------- src/iterator.h | 4 +- src/iterator_async.cc | 26 +++--- src/iterator_async.h | 4 +- src/leveldown.cc | 36 +++---- src/leveldown.h | 46 ++++----- src/leveldown_async.cc | 8 +- src/leveldown_async.h | 12 +-- test/iterator-test.js | 2 +- 21 files changed, 334 insertions(+), 372 deletions(-) diff --git a/.dntrc b/.dntrc index a956f18c..caf606c8 100644 --- a/.dntrc +++ b/.dntrc @@ -2,18 +2,22 @@ ## see https://github.com/rvagg/dnt NODE_VERSIONS="\ - v0.10.31 \ - v0.11.13 \ + v0.10.40 \ + v0.12.7 \ " IOJS_VERSIONS="\ - v1.0.1-release \ + v1.8.4 \ + v2.0.1 \ + v2.3.4 \ + v3.0.0-rc.3 \ " OUTPUT_PREFIX="leveldown-" TEST_CMD="\ cd /dnt/ && \ npm install && \ - node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js \ - --nodedir /usr/src/node/ rebuild && \ - node_modules/.bin/tap test/*-test.js; \ + npm run-script prebuild \ + --nodedir=/usr/src/node/ && \ + node_modules/.bin/tape test/*-test.js \ " - +# for tape +LOG_OK_CMD="tail -2 | head -1 | sed -r 's/^#+\s+//'" diff --git a/package.json b/package.json index 91d3dead..254ed776 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "abstract-leveldown": "~2.4.0", "bindings": "~1.2.1", "fast-future": "~1.0.0", - "nan": "~1.8.4", + "nan": "nodejs/nan#next", "node-pre-gyp": "~0.6.2" }, "devDependencies": { diff --git a/src/async.h b/src/async.h index 54bf40c3..bcce316b 100644 --- a/src/async.h +++ b/src/async.h @@ -14,16 +14,12 @@ namespace leveldown { class Database; -/* abstract */ class AsyncWorker : public NanAsyncWorker { +/* abstract */ class AsyncWorker : public Nan::AsyncWorker { public: AsyncWorker ( leveldown::Database* database - , NanCallback *callback - ) : NanAsyncWorker(callback), database(database) { - NanScope(); - v8::Local obj = NanNew(); - NanAssignPersistent(persistentHandle, obj); - } + , Nan::Callback *callback + ) : Nan::AsyncWorker(callback), database(database) { } protected: void SetStatus(leveldb::Status status) { diff --git a/src/batch.cc b/src/batch.cc index 081e0216..b079ed8a 100644 --- a/src/batch.cc +++ b/src/batch.cc @@ -9,7 +9,7 @@ namespace leveldown { -static v8::Persistent batch_constructor; +static Nan::Persistent batch_constructor; Batch::Batch (leveldown::Database* database, bool sync) : database(database) { options = new leveldb::WriteOptions(); @@ -28,32 +28,30 @@ leveldb::Status Batch::Write () { } void Batch::Init () { - v8::Local tpl = NanNew(Batch::New); - NanAssignPersistent(batch_constructor, tpl); - tpl->SetClassName(NanNew("Batch")); + v8::Local tpl = Nan::New(Batch::New); + batch_constructor.Reset(tpl); + tpl->SetClassName(Nan::New("Batch").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); - NODE_SET_PROTOTYPE_METHOD(tpl, "put", Batch::Put); - NODE_SET_PROTOTYPE_METHOD(tpl, "del", Batch::Del); - NODE_SET_PROTOTYPE_METHOD(tpl, "clear", Batch::Clear); - NODE_SET_PROTOTYPE_METHOD(tpl, "write", Batch::Write); + Nan::SetPrototypeMethod(tpl, "put", Batch::Put); + Nan::SetPrototypeMethod(tpl, "del", Batch::Del); + Nan::SetPrototypeMethod(tpl, "clear", Batch::Clear); + Nan::SetPrototypeMethod(tpl, "write", Batch::Write); } NAN_METHOD(Batch::New) { - NanScope(); - - Database* database = node::ObjectWrap::Unwrap(args[0]->ToObject()); + Database* database = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); v8::Local optionsObj; - if (args.Length() > 1 && args[1]->IsObject()) { - optionsObj = v8::Local::Cast(args[1]); + if (info.Length() > 1 && info[1]->IsObject()) { + optionsObj = v8::Local::Cast(info[1]); } bool sync = BooleanOptionValue(optionsObj, "sync"); Batch* batch = new Batch(database, sync); - batch->Wrap(args.This()); + batch->Wrap(info.This()); - NanReturnValue(args.This()); + info.GetReturnValue().Set(info.This()); } v8::Handle Batch::NewInstance ( @@ -61,12 +59,12 @@ v8::Handle Batch::NewInstance ( , v8::Handle optionsObj ) { - NanEscapableScope(); + Nan::EscapableHandleScope scope; v8::Local instance; v8::Local constructorHandle = - NanNew(batch_constructor); + Nan::New(batch_constructor); if (optionsObj.IsEmpty()) { v8::Handle argv[1] = { database }; @@ -76,17 +74,15 @@ v8::Handle Batch::NewInstance ( instance = constructorHandle->GetFunction()->NewInstance(2, argv); } - return NanEscapeScope(instance); + return scope.Escape(instance); } NAN_METHOD(Batch::Put) { - NanScope(); - - Batch* batch = ObjectWrap::Unwrap(args.Holder()); + Batch* batch = ObjectWrap::Unwrap(info.Holder()); v8::Handle callback; // purely for the error macros - v8::Local keyBuffer = args[0]; - v8::Local valueBuffer = args[1]; + v8::Local keyBuffer = info[0]; + v8::Local valueBuffer = info[1]; LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key) LD_STRING_OR_BUFFER_TO_SLICE(value, valueBuffer, value) @@ -97,17 +93,15 @@ NAN_METHOD(Batch::Put) { DisposeStringOrBufferFromSlice(keyBuffer, key); DisposeStringOrBufferFromSlice(valueBuffer, value); - NanReturnValue(args.Holder()); + info.GetReturnValue().Set(info.Holder()); } NAN_METHOD(Batch::Del) { - NanScope(); - - Batch* batch = ObjectWrap::Unwrap(args.Holder()); + Batch* batch = ObjectWrap::Unwrap(info.Holder()); v8::Handle callback; // purely for the error macros - v8::Local keyBuffer = args[0]; + v8::Local keyBuffer = info[0]; LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key) batch->batch->Delete(key); @@ -116,38 +110,32 @@ NAN_METHOD(Batch::Del) { DisposeStringOrBufferFromSlice(keyBuffer, key); - NanReturnValue(args.Holder()); + info.GetReturnValue().Set(info.Holder()); } NAN_METHOD(Batch::Clear) { - NanScope(); - - Batch* batch = ObjectWrap::Unwrap(args.Holder()); + Batch* batch = ObjectWrap::Unwrap(info.Holder()); batch->batch->Clear(); batch->hasData = false; - NanReturnValue(args.Holder()); + info.GetReturnValue().Set(info.Holder()); } NAN_METHOD(Batch::Write) { - NanScope(); - - Batch* batch = ObjectWrap::Unwrap(args.Holder()); + Batch* batch = ObjectWrap::Unwrap(info.Holder()); if (batch->hasData) { - NanCallback *callback = - new NanCallback(v8::Local::Cast(args[0])); + Nan::Callback *callback = + new Nan::Callback(v8::Local::Cast(info[0])); BatchWriteWorker* worker = new BatchWriteWorker(batch, callback); // persist to prevent accidental GC - v8::Local _this = args.This(); + v8::Local _this = info.This(); worker->SaveToPersistent("batch", _this); - NanAsyncQueueWorker(worker); + Nan::AsyncQueueWorker(worker); } else { - LD_RUN_CALLBACK(v8::Local::Cast(args[0]), 0, NULL); + LD_RUN_CALLBACK(v8::Local::Cast(info[0]), 0, NULL); } - - NanReturnUndefined(); } } // namespace leveldown diff --git a/src/batch.h b/src/batch.h index b2b18806..f8e1a91e 100644 --- a/src/batch.h +++ b/src/batch.h @@ -10,7 +10,7 @@ namespace leveldown { -class Batch : public node::ObjectWrap { +class Batch : public Nan::ObjectWrap { public: static void Init(); static v8::Handle NewInstance ( diff --git a/src/batch_async.cc b/src/batch_async.cc index c3863191..f840f24e 100644 --- a/src/batch_async.cc +++ b/src/batch_async.cc @@ -14,7 +14,7 @@ namespace leveldown { BatchWriteWorker::BatchWriteWorker ( Batch* batch - , NanCallback *callback + , Nan::Callback *callback ) : AsyncWorker(NULL, callback) , batch(batch) {}; diff --git a/src/batch_async.h b/src/batch_async.h index 218591fa..72587b96 100644 --- a/src/batch_async.h +++ b/src/batch_async.h @@ -19,7 +19,7 @@ class BatchWriteWorker : public AsyncWorker { public: BatchWriteWorker ( Batch* batch - , NanCallback *callback + , Nan::Callback *callback ); virtual ~BatchWriteWorker (); diff --git a/src/common.h b/src/common.h index af844990..bc62d275 100644 --- a/src/common.h +++ b/src/common.h @@ -13,7 +13,7 @@ namespace leveldown { NAN_INLINE bool BooleanOptionValue(v8::Local options, const char* _key, bool def = false) { - v8::Handle key = NanNew(_key); + v8::Handle key = Nan::New(_key).ToLocalChecked(); return !options.IsEmpty() && options->Has(key) ? options->Get(key)->BooleanValue() @@ -23,7 +23,7 @@ NAN_INLINE bool BooleanOptionValue(v8::Local options, NAN_INLINE uint32_t UInt32OptionValue(v8::Local options, const char* _key, uint32_t def) { - v8::Handle key = NanNew(_key); + v8::Handle key = Nan::New(_key).ToLocalChecked(); return !options.IsEmpty() && options->Has(key) && options->Get(key)->IsNumber() diff --git a/src/database.cc b/src/database.cc index 827e4360..d9db8f56 100644 --- a/src/database.cc +++ b/src/database.cc @@ -19,10 +19,10 @@ namespace leveldown { -static v8::Persistent database_constructor; +static Nan::Persistent database_constructor; Database::Database (const v8::Handle& from) - : location(new NanUtf8String(from)) + : location(new Nan::Utf8String(from)) , db(NULL) , currentIteratorId(0) , pendingCloseWorker(NULL) @@ -106,7 +106,7 @@ void Database::ReleaseIterator (uint32_t id) { // iterators to end before we can close them iterators.erase(id); if (iterators.empty() && pendingCloseWorker != NULL) { - NanAsyncQueueWorker((AsyncWorker*)pendingCloseWorker); + Nan::AsyncQueueWorker((AsyncWorker*)pendingCloseWorker); pendingCloseWorker = NULL; } } @@ -127,54 +127,48 @@ void Database::CloseDatabase () { /* V8 exposed functions *****************************/ NAN_METHOD(LevelDOWN) { - NanScope(); - - v8::Local location = args[0].As(); - NanReturnValue(Database::NewInstance(location)); + v8::Local location = info[0].As(); + info.GetReturnValue().Set(Database::NewInstance(location)); } void Database::Init () { - v8::Local tpl = NanNew(Database::New); - NanAssignPersistent(database_constructor, tpl); - tpl->SetClassName(NanNew("Database")); + v8::Local tpl = Nan::New(Database::New); + database_constructor.Reset(tpl); + tpl->SetClassName(Nan::New("Database").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); - NODE_SET_PROTOTYPE_METHOD(tpl, "open", Database::Open); - NODE_SET_PROTOTYPE_METHOD(tpl, "close", Database::Close); - NODE_SET_PROTOTYPE_METHOD(tpl, "put", Database::Put); - NODE_SET_PROTOTYPE_METHOD(tpl, "get", Database::Get); - NODE_SET_PROTOTYPE_METHOD(tpl, "del", Database::Delete); - NODE_SET_PROTOTYPE_METHOD(tpl, "batch", Database::Batch); - NODE_SET_PROTOTYPE_METHOD(tpl, "approximateSize", Database::ApproximateSize); - NODE_SET_PROTOTYPE_METHOD(tpl, "getProperty", Database::GetProperty); - NODE_SET_PROTOTYPE_METHOD(tpl, "iterator", Database::Iterator); + Nan::SetPrototypeMethod(tpl, "open", Database::Open); + Nan::SetPrototypeMethod(tpl, "close", Database::Close); + Nan::SetPrototypeMethod(tpl, "put", Database::Put); + Nan::SetPrototypeMethod(tpl, "get", Database::Get); + Nan::SetPrototypeMethod(tpl, "del", Database::Delete); + Nan::SetPrototypeMethod(tpl, "batch", Database::Batch); + Nan::SetPrototypeMethod(tpl, "approximateSize", Database::ApproximateSize); + Nan::SetPrototypeMethod(tpl, "getProperty", Database::GetProperty); + Nan::SetPrototypeMethod(tpl, "iterator", Database::Iterator); } NAN_METHOD(Database::New) { - NanScope(); - - Database* obj = new Database(args[0]); - obj->Wrap(args.This()); + Database* obj = new Database(info[0]); + obj->Wrap(info.This()); - NanReturnValue(args.This()); + info.GetReturnValue().Set(info.This()); } v8::Handle Database::NewInstance (v8::Local &location) { - NanEscapableScope(); + Nan::EscapableHandleScope scope; v8::Local instance; v8::Local constructorHandle = - NanNew(database_constructor); + Nan::New(database_constructor); v8::Handle argv[] = { location }; instance = constructorHandle->GetFunction()->NewInstance(1, argv); - return NanEscapeScope(instance); + return scope.Escape(instance); } NAN_METHOD(Database::Open) { - NanScope(); - LD_METHOD_SETUP_COMMON(open, 0, 1) bool createIfMissing = BooleanOptionValue(optionsObj, "createIfMissing", true); @@ -200,7 +194,7 @@ NAN_METHOD(Database::Open) { OpenWorker* worker = new OpenWorker( database - , new NanCallback(callback) + , new Nan::Callback(callback) , database->blockCache , database->filterPolicy , createIfMissing @@ -212,24 +206,24 @@ NAN_METHOD(Database::Open) { , blockRestartInterval ); // persist to prevent accidental GC - v8::Local _this = args.This(); + v8::Local _this = info.This(); worker->SaveToPersistent("database", _this); - NanAsyncQueueWorker(worker); + Nan::AsyncQueueWorker(worker); +} - NanReturnUndefined(); +// for an empty callback to iterator.end() +NAN_METHOD(EmptyMethod) { } NAN_METHOD(Database::Close) { - NanScope(); - LD_METHOD_SETUP_COMMON_ONEARG(close) CloseWorker* worker = new CloseWorker( database - , new NanCallback(callback) + , new Nan::Callback(callback) ); // persist to prevent accidental GC - v8::Local _this = args.This(); + v8::Local _this = info.This(); worker->SaveToPersistent("database", _this); if (!database->iterators.empty()) { @@ -250,23 +244,17 @@ NAN_METHOD(Database::Close) { // function and wait for it to hit ReleaseIterator() where our // CloseWorker will be invoked - /* - v8::Local localHandle = NanNew(it->second); - leveldown::Iterator* iterator = - node::ObjectWrap::Unwrap(localHandle-> - Get(NanNew("iterator")).As()); - */ leveldown::Iterator *iterator = it->second; if (!iterator->ended) { v8::Local end = - v8::Local::Cast(NanObjectWrapHandle(iterator)->Get( - NanNew("end"))); + v8::Local::Cast(iterator->handle()->Get( + Nan::New("end").ToLocalChecked())); v8::Local argv[] = { - NanNew()->GetFunction() // empty callback + Nan::New(EmptyMethod)->GetFunction() // empty callback }; - NanMakeCallback( - NanObjectWrapHandle(iterator) + Nan::MakeCallback( + iterator->handle() , end , 1 , argv @@ -274,19 +262,15 @@ NAN_METHOD(Database::Close) { } } } else { - NanAsyncQueueWorker(worker); + Nan::AsyncQueueWorker(worker); } - - NanReturnUndefined(); } NAN_METHOD(Database::Put) { - NanScope(); - LD_METHOD_SETUP_COMMON(put, 2, 3) - v8::Local keyHandle = args[0].As(); - v8::Local valueHandle = args[1].As(); + v8::Local keyHandle = info[0].As(); + v8::Local valueHandle = info[1].As(); LD_STRING_OR_BUFFER_TO_SLICE(key, keyHandle, key); LD_STRING_OR_BUFFER_TO_SLICE(value, valueHandle, value); @@ -294,7 +278,7 @@ NAN_METHOD(Database::Put) { WriteWorker* worker = new WriteWorker( database - , new NanCallback(callback) + , new Nan::Callback(callback) , key , value , sync @@ -303,19 +287,15 @@ NAN_METHOD(Database::Put) { ); // persist to prevent accidental GC - v8::Local _this = args.This(); + v8::Local _this = info.This(); worker->SaveToPersistent("database", _this); - NanAsyncQueueWorker(worker); - - NanReturnUndefined(); + Nan::AsyncQueueWorker(worker); } NAN_METHOD(Database::Get) { - NanScope(); - LD_METHOD_SETUP_COMMON(get, 1, 2) - v8::Local keyHandle = args[0].As(); + v8::Local keyHandle = info[0].As(); LD_STRING_OR_BUFFER_TO_SLICE(key, keyHandle, key); bool asBuffer = BooleanOptionValue(optionsObj, "asBuffer", true); @@ -323,61 +303,54 @@ NAN_METHOD(Database::Get) { ReadWorker* worker = new ReadWorker( database - , new NanCallback(callback) + , new Nan::Callback(callback) , key , asBuffer , fillCache , keyHandle ); // persist to prevent accidental GC - v8::Local _this = args.This(); + v8::Local _this = info.This(); worker->SaveToPersistent("database", _this); - NanAsyncQueueWorker(worker); - - NanReturnUndefined(); + Nan::AsyncQueueWorker(worker); } NAN_METHOD(Database::Delete) { - NanScope(); - LD_METHOD_SETUP_COMMON(del, 1, 2) - v8::Local keyHandle = args[0].As(); + v8::Local keyHandle = info[0].As(); LD_STRING_OR_BUFFER_TO_SLICE(key, keyHandle, key); bool sync = BooleanOptionValue(optionsObj, "sync"); DeleteWorker* worker = new DeleteWorker( database - , new NanCallback(callback) + , new Nan::Callback(callback) , key , sync , keyHandle ); // persist to prevent accidental GC - v8::Local _this = args.This(); + v8::Local _this = info.This(); worker->SaveToPersistent("database", _this); - NanAsyncQueueWorker(worker); - - NanReturnUndefined(); + Nan::AsyncQueueWorker(worker); } NAN_METHOD(Database::Batch) { - NanScope(); - - if ((args.Length() == 0 || args.Length() == 1) && !args[0]->IsArray()) { + if ((info.Length() == 0 || info.Length() == 1) && !info[0]->IsArray()) { v8::Local optionsObj; - if (args.Length() > 0 && args[0]->IsObject()) { - optionsObj = args[0].As(); + if (info.Length() > 0 && info[0]->IsObject()) { + optionsObj = info[0].As(); } - NanReturnValue(Batch::NewInstance(args.This(), optionsObj)); + info.GetReturnValue().Set(Batch::NewInstance(info.This(), optionsObj)); + return; } LD_METHOD_SETUP_COMMON(batch, 1, 2); bool sync = BooleanOptionValue(optionsObj, "sync"); - v8::Local array = v8::Local::Cast(args[0]); + v8::Local array = v8::Local::Cast(info[0]); leveldb::WriteBatch* batch = new leveldb::WriteBatch(); bool hasData = false; @@ -387,10 +360,10 @@ NAN_METHOD(Database::Batch) { continue; v8::Local obj = v8::Local::Cast(array->Get(i)); - v8::Local keyBuffer = obj->Get(NanNew("key")); - v8::Local type = obj->Get(NanNew("type")); + v8::Local keyBuffer = obj->Get(Nan::New("key").ToLocalChecked()); + v8::Local type = obj->Get(Nan::New("type").ToLocalChecked()); - if (type->StrictEquals(NanNew("del"))) { + if (type->StrictEquals(Nan::New("del").ToLocalChecked())) { LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key) batch->Delete(key); @@ -398,8 +371,8 @@ NAN_METHOD(Database::Batch) { hasData = true; DisposeStringOrBufferFromSlice(keyBuffer, key); - } else if (type->StrictEquals(NanNew("put"))) { - v8::Local valueBuffer = obj->Get(NanNew("value")); + } else if (type->StrictEquals(Nan::New("put").ToLocalChecked())) { + v8::Local valueBuffer = obj->Get(Nan::New("value").ToLocalChecked()); LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key) LD_STRING_OR_BUFFER_TO_SLICE(value, valueBuffer, value) @@ -416,26 +389,22 @@ NAN_METHOD(Database::Batch) { if (hasData) { BatchWorker* worker = new BatchWorker( database - , new NanCallback(callback) + , new Nan::Callback(callback) , batch , sync ); // persist to prevent accidental GC - v8::Local _this = args.This(); + v8::Local _this = info.This(); worker->SaveToPersistent("database", _this); - NanAsyncQueueWorker(worker); + Nan::AsyncQueueWorker(worker); } else { LD_RUN_CALLBACK(callback, 0, NULL); } - - NanReturnUndefined(); } NAN_METHOD(Database::ApproximateSize) { - NanScope(); - - v8::Local startHandle = args[0].As(); - v8::Local endHandle = args[1].As(); + v8::Local startHandle = info[0].As(); + v8::Local endHandle = info[1].As(); LD_METHOD_SETUP_COMMON(approximateSize, -1, 2) @@ -444,49 +413,43 @@ NAN_METHOD(Database::ApproximateSize) { ApproximateSizeWorker* worker = new ApproximateSizeWorker( database - , new NanCallback(callback) + , new Nan::Callback(callback) , start , end , startHandle , endHandle ); // persist to prevent accidental GC - v8::Local _this = args.This(); + v8::Local _this = info.This(); worker->SaveToPersistent("database", _this); - NanAsyncQueueWorker(worker); - - NanReturnUndefined(); + Nan::AsyncQueueWorker(worker); } NAN_METHOD(Database::GetProperty) { - NanScope(); - - v8::Local propertyHandle = args[0].As(); + v8::Local propertyHandle = info[0].As(); v8::Local callback; // for LD_STRING_OR_BUFFER_TO_SLICE LD_STRING_OR_BUFFER_TO_SLICE(property, propertyHandle, property) leveldown::Database* database = - node::ObjectWrap::Unwrap(args.This()); + Nan::ObjectWrap::Unwrap(info.This()); std::string* value = new std::string(); database->GetPropertyFromDatabase(property, value); v8::Local returnValue - = NanNew(value->c_str(), value->length()); + = Nan::New(value->c_str(), value->length()).ToLocalChecked(); delete value; delete[] property.data(); - NanReturnValue(returnValue); + info.GetReturnValue().Set(returnValue); } NAN_METHOD(Database::Iterator) { - NanScope(); - - Database* database = node::ObjectWrap::Unwrap(args.This()); + Database* database = Nan::ObjectWrap::Unwrap(info.This()); v8::Local optionsObj; - if (args.Length() > 0 && args[0]->IsObject()) { - optionsObj = v8::Local::Cast(args[0]); + if (info.Length() > 0 && info[0]->IsObject()) { + optionsObj = v8::Local::Cast(info[0]); } // each iterator gets a unique id for this Database, so we can @@ -494,31 +457,31 @@ NAN_METHOD(Database::Iterator) { uint32_t id = database->currentIteratorId++; v8::TryCatch try_catch; v8::Local iteratorHandle = Iterator::NewInstance( - args.This() - , NanNew(id) + info.This() + , Nan::New(id) , optionsObj ); if (try_catch.HasCaught()) { // NB: node::FatalException can segfault here if there is no room on stack. - return NanThrowError("Fatal Error in Database::Iterator!"); + return Nan::ThrowError("Fatal Error in Database::Iterator!"); } leveldown::Iterator *iterator = - node::ObjectWrap::Unwrap(iteratorHandle); + Nan::ObjectWrap::Unwrap(iteratorHandle); database->iterators[id] = iterator; // register our iterator /* - v8::Local obj = NanNew(); - obj->Set(NanNew("iterator"), iteratorHandle); - v8::Persistent persistent; + v8::Local obj = Nan::New(); + obj->Set(Nan::New("iterator"), iteratorHandle); + Nan::Persistent persistent; persistent.Reset(nan_isolate, obj); - database->iterators.insert(std::pair< uint32_t, v8::Persistent & > + database->iterators.insert(std::pair< uint32_t, Nan::Persistent & > (id, persistent)); */ - NanReturnValue(iteratorHandle); + info.GetReturnValue().Set(iteratorHandle); } diff --git a/src/database.h b/src/database.h index 7940d44c..955d3b9e 100644 --- a/src/database.h +++ b/src/database.h @@ -23,13 +23,13 @@ namespace leveldown { NAN_METHOD(LevelDOWN); struct Reference { - v8::Persistent handle; + Nan::Persistent handle; leveldb::Slice slice; Reference(v8::Local obj, leveldb::Slice slice) : slice(slice) { - v8::Local _obj = NanNew(); - _obj->Set(NanNew("obj"), obj); - NanAssignPersistent(handle, _obj); + v8::Local _obj = Nan::New(); + _obj->Set(Nan::New("obj").ToLocalChecked(), obj); + handle.Reset(_obj); }; }; @@ -43,7 +43,7 @@ static inline void ClearReferences (std::vector *references) { delete references; } -class Database : public node::ObjectWrap { +class Database : public Nan::ObjectWrap { public: static void Init (); static v8::Handle NewInstance (v8::Local &location); @@ -79,7 +79,7 @@ class Database : public node::ObjectWrap { ~Database (); private: - NanUtf8String* location; + Nan::Utf8String* location; leveldb::DB* db; uint32_t currentIteratorId; void(*pendingCloseWorker); diff --git a/src/database_async.cc b/src/database_async.cc index 2a97a3d6..b5533bd2 100644 --- a/src/database_async.cc +++ b/src/database_async.cc @@ -20,7 +20,7 @@ namespace leveldown { OpenWorker::OpenWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Cache* blockCache , const leveldb::FilterPolicy* filterPolicy , bool createIfMissing @@ -58,7 +58,7 @@ void OpenWorker::Execute () { CloseWorker::CloseWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback ) : AsyncWorker(database, callback) {}; @@ -69,7 +69,7 @@ void CloseWorker::Execute () { } void CloseWorker::WorkComplete () { - NanScope(); + Nan::HandleScope scope; HandleOKCallback(); delete callback; callback = NULL; @@ -79,13 +79,13 @@ void CloseWorker::WorkComplete () { IOWorker::IOWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice key , v8::Local &keyHandle ) : AsyncWorker(database, callback) , key(key) { - NanScope(); + Nan::HandleScope scope; SaveToPersistent("key", keyHandle); }; @@ -93,7 +93,7 @@ IOWorker::IOWorker ( IOWorker::~IOWorker () {} void IOWorker::WorkComplete () { - NanScope(); + Nan::HandleScope scope; DisposeStringOrBufferFromSlice(GetFromPersistent("key"), key); AsyncWorker::WorkComplete(); @@ -103,7 +103,7 @@ void IOWorker::WorkComplete () { ReadWorker::ReadWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice key , bool asBuffer , bool fillCache @@ -111,7 +111,7 @@ ReadWorker::ReadWorker ( ) : IOWorker(database, callback, key, keyHandle) , asBuffer(asBuffer) { - NanScope(); + Nan::HandleScope scope; options = new leveldb::ReadOptions(); options->fill_cache = fillCache; @@ -127,16 +127,19 @@ void ReadWorker::Execute () { } void ReadWorker::HandleOKCallback () { - NanScope(); + Nan::HandleScope scope; v8::Local returnValue; if (asBuffer) { - returnValue = NanNewBufferHandle((char*)value.data(), value.size()); + //TODO: could use NewBuffer if we carefully manage the lifecycle of `value` + //and avoid an an extra allocation. We'd have to clean up properly when not OK + //and let the new Buffer manage the data when OK + returnValue = Nan::CopyBuffer((char*)value.data(), value.size()).ToLocalChecked(); } else { - returnValue = NanNew((char*)value.data(), value.size()); + returnValue = Nan::New((char*)value.data(), value.size()).ToLocalChecked(); } v8::Local argv[] = { - NanNull() + Nan::Null() , returnValue }; callback->Call(2, argv); @@ -146,13 +149,13 @@ void ReadWorker::HandleOKCallback () { DeleteWorker::DeleteWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice key , bool sync , v8::Local &keyHandle ) : IOWorker(database, callback, key, keyHandle) { - NanScope(); + Nan::HandleScope scope; options = new leveldb::WriteOptions(); options->sync = sync; @@ -171,7 +174,7 @@ void DeleteWorker::Execute () { WriteWorker::WriteWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice key , leveldb::Slice value , bool sync @@ -180,7 +183,7 @@ WriteWorker::WriteWorker ( ) : DeleteWorker(database, callback, key, sync, keyHandle) , value(value) { - NanScope(); + Nan::HandleScope scope; SaveToPersistent("value", valueHandle); }; @@ -192,7 +195,7 @@ void WriteWorker::Execute () { } void WriteWorker::WorkComplete () { - NanScope(); + Nan::HandleScope scope; DisposeStringOrBufferFromSlice(GetFromPersistent("value"), value); IOWorker::WorkComplete(); @@ -202,7 +205,7 @@ void WriteWorker::WorkComplete () { BatchWorker::BatchWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::WriteBatch* batch , bool sync ) : AsyncWorker(database, callback) @@ -225,7 +228,7 @@ void BatchWorker::Execute () { ApproximateSizeWorker::ApproximateSizeWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice start , leveldb::Slice end , v8::Local &startHandle @@ -233,7 +236,7 @@ ApproximateSizeWorker::ApproximateSizeWorker ( ) : AsyncWorker(database, callback) , range(start, end) { - NanScope(); + Nan::HandleScope scope; SaveToPersistent("start", startHandle); SaveToPersistent("end", endHandle); @@ -246,7 +249,7 @@ void ApproximateSizeWorker::Execute () { } void ApproximateSizeWorker::WorkComplete() { - NanScope(); + Nan::HandleScope scope; DisposeStringOrBufferFromSlice(GetFromPersistent("start"), range.start); DisposeStringOrBufferFromSlice(GetFromPersistent("end"), range.limit); @@ -254,11 +257,11 @@ void ApproximateSizeWorker::WorkComplete() { } void ApproximateSizeWorker::HandleOKCallback () { - NanScope(); + Nan::HandleScope scope; - v8::Local returnValue = NanNew((double) size); + v8::Local returnValue = Nan::New((double) size); v8::Local argv[] = { - NanNull() + Nan::Null() , returnValue }; callback->Call(2, argv); diff --git a/src/database_async.h b/src/database_async.h index 60b7d1b0..e0a8f80d 100644 --- a/src/database_async.h +++ b/src/database_async.h @@ -19,7 +19,7 @@ class OpenWorker : public AsyncWorker { public: OpenWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Cache* blockCache , const leveldb::FilterPolicy* filterPolicy , bool createIfMissing @@ -42,7 +42,7 @@ class CloseWorker : public AsyncWorker { public: CloseWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback ); virtual ~CloseWorker (); @@ -54,7 +54,7 @@ class IOWorker : public AsyncWorker { public: IOWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice key , v8::Local &keyHandle ); @@ -70,7 +70,7 @@ class ReadWorker : public IOWorker { public: ReadWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice key , bool asBuffer , bool fillCache @@ -91,7 +91,7 @@ class DeleteWorker : public IOWorker { public: DeleteWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice key , bool sync , v8::Local &keyHandle @@ -108,7 +108,7 @@ class WriteWorker : public DeleteWorker { public: WriteWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice key , leveldb::Slice value , bool sync @@ -128,7 +128,7 @@ class BatchWorker : public AsyncWorker { public: BatchWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::WriteBatch* batch , bool sync ); @@ -145,7 +145,7 @@ class ApproximateSizeWorker : public AsyncWorker { public: ApproximateSizeWorker ( Database *database - , NanCallback *callback + , Nan::Callback *callback , leveldb::Slice start , leveldb::Slice end , v8::Local &startHandle diff --git a/src/iterator.cc b/src/iterator.cc index 01fc02c5..90f8a0d0 100644 --- a/src/iterator.cc +++ b/src/iterator.cc @@ -13,7 +13,7 @@ namespace leveldown { -static v8::Persistent iterator_constructor; +static Nan::Persistent iterator_constructor; Iterator::Iterator ( Database* database @@ -49,12 +49,12 @@ Iterator::Iterator ( , keyAsBuffer(keyAsBuffer) , valueAsBuffer(valueAsBuffer) { - NanScope(); + Nan::HandleScope scope; - v8::Local obj = NanNew(); + v8::Local obj = Nan::New(); if (!startHandle.IsEmpty()) - obj->Set(NanNew("start"), startHandle); - NanAssignPersistent(persistentHandle, obj); + obj->Set(Nan::New("start").ToLocalChecked(), startHandle); + persistentHandle.Reset(obj); options = new leveldb::ReadOptions(); options->fill_cache = fillCache; @@ -71,7 +71,7 @@ Iterator::Iterator ( Iterator::~Iterator () { delete options; if (!persistentHandle.IsEmpty()) - NanDisposePersistent(persistentHandle); + persistentHandle.Reset(); if (start != NULL) delete start; if (end != NULL) @@ -198,18 +198,16 @@ void Iterator::Release () { void checkEndCallback (Iterator* iterator) { iterator->nexting = false; if (iterator->endWorker != NULL) { - NanAsyncQueueWorker(iterator->endWorker); + Nan::AsyncQueueWorker(iterator->endWorker); iterator->endWorker = NULL; } } NAN_METHOD(Iterator::Seek) { - NanScope(); - - Iterator* iterator = node::ObjectWrap::Unwrap(args.This()); + Iterator* iterator = Nan::ObjectWrap::Unwrap(info.This()); iterator->GetIterator(); leveldb::Iterator* dbIterator = iterator->dbIterator; - NanUtf8String key(args[0]); + Nan::Utf8String key(info[0]); dbIterator->Seek(*key); iterator->seeking = true; @@ -239,65 +237,71 @@ NAN_METHOD(Iterator::Seek) { } } - NanReturnValue(args.Holder()); + info.GetReturnValue().Set(info.Holder()); } NAN_METHOD(Iterator::Next) { - NanScope(); + Iterator* iterator = Nan::ObjectWrap::Unwrap(info.This()); - Iterator* iterator = node::ObjectWrap::Unwrap(args.This()); + if (!info[0]->IsFunction()) { + return Nan::ThrowError("next() requires a callback argument"); + } - v8::Local callback = args[0].As(); + v8::Local callback = info[0].As(); NextWorker* worker = new NextWorker( iterator - , new NanCallback(callback) + , new Nan::Callback(callback) , checkEndCallback ); // persist to prevent accidental GC - v8::Local _this = args.This(); + v8::Local _this = info.This(); worker->SaveToPersistent("iterator", _this); iterator->nexting = true; - NanAsyncQueueWorker(worker); + Nan::AsyncQueueWorker(worker); - NanReturnValue(args.Holder()); + info.GetReturnValue().Set(info.Holder()); } NAN_METHOD(Iterator::End) { - NanScope(); - - Iterator* iterator = node::ObjectWrap::Unwrap(args.This()); - - v8::Local callback = v8::Local::Cast(args[0]); + Iterator* iterator = Nan::ObjectWrap::Unwrap(info.This()); - EndWorker* worker = new EndWorker( - iterator - , new NanCallback(callback) - ); - // persist to prevent accidental GC - v8::Local _this = args.This(); - worker->SaveToPersistent("iterator", _this); - iterator->ended = true; + if (!info[0]->IsFunction()) { + return Nan::ThrowError("end() requires a callback argument"); + } - if (iterator->nexting) { - // waiting for a next() to return, queue the end - iterator->endWorker = worker; - } else { - NanAsyncQueueWorker(worker); + if (!iterator->ended) { + v8::Local callback = v8::Local::Cast(info[0]); + + EndWorker* worker = new EndWorker( + iterator + , new Nan::Callback(callback) + ); + // persist to prevent accidental GC + v8::Local _this = info.This(); + worker->SaveToPersistent("iterator", _this); + iterator->ended = true; + + if (iterator->nexting) { + // waiting for a next() to return, queue the end + iterator->endWorker = worker; + } else { + Nan::AsyncQueueWorker(worker); + } } - NanReturnValue(args.Holder()); + info.GetReturnValue().Set(info.Holder()); } void Iterator::Init () { v8::Local tpl = - NanNew(Iterator::New); - NanAssignPersistent(iterator_constructor, tpl); - tpl->SetClassName(NanNew("Iterator")); + Nan::New(Iterator::New); + iterator_constructor.Reset(tpl); + tpl->SetClassName(Nan::New("Iterator").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); - NODE_SET_PROTOTYPE_METHOD(tpl, "seek", Iterator::Seek); - NODE_SET_PROTOTYPE_METHOD(tpl, "next", Iterator::Next); - NODE_SET_PROTOTYPE_METHOD(tpl, "end", Iterator::End); + Nan::SetPrototypeMethod(tpl, "seek", Iterator::Seek); + Nan::SetPrototypeMethod(tpl, "next", Iterator::Next); + Nan::SetPrototypeMethod(tpl, "end", Iterator::End); } v8::Local Iterator::NewInstance ( @@ -306,11 +310,11 @@ v8::Local Iterator::NewInstance ( , v8::Local optionsObj ) { - NanEscapableScope(); + Nan::EscapableHandleScope scope; v8::Local instance; v8::Local constructorHandle = - NanNew(iterator_constructor); + Nan::New(iterator_constructor); if (optionsObj.IsEmpty()) { v8::Handle argv[2] = { database, id }; @@ -320,13 +324,11 @@ v8::Local Iterator::NewInstance ( instance = constructorHandle->GetFunction()->NewInstance(3, argv); } - return NanEscapeScope(instance); + return scope.Escape(instance); } NAN_METHOD(Iterator::New) { - NanScope(); - - Database* database = node::ObjectWrap::Unwrap(args[0]->ToObject()); + Database* database = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); //TODO: remove this, it's only here to make LD_STRING_OR_BUFFER_TO_SLICE happy v8::Handle callback; @@ -338,7 +340,7 @@ NAN_METHOD(Iterator::New) { // default highWaterMark from Readble-streams size_t highWaterMark = 16 * 1024; - v8::Local id = args[1]; + v8::Local id = info[1]; v8::Local optionsObj; @@ -355,16 +357,16 @@ NAN_METHOD(Iterator::New) { //default to forward. bool reverse = false; - if (args.Length() > 1 && args[2]->IsObject()) { - optionsObj = v8::Local::Cast(args[2]); + if (info.Length() > 1 && info[2]->IsObject()) { + optionsObj = v8::Local::Cast(info[2]); reverse = BooleanOptionValue(optionsObj, "reverse"); - if (optionsObj->Has(NanNew("start")) - && (node::Buffer::HasInstance(optionsObj->Get(NanNew("start"))) - || optionsObj->Get(NanNew("start"))->IsString())) { + if (optionsObj->Has(Nan::New("start").ToLocalChecked()) + && (node::Buffer::HasInstance(optionsObj->Get(Nan::New("start").ToLocalChecked())) + || optionsObj->Get(Nan::New("start").ToLocalChecked())->IsString())) { - startHandle = optionsObj->Get(NanNew("start")).As(); + startHandle = optionsObj->Get(Nan::New("start").ToLocalChecked()).As(); // ignore start if it has size 0 since a Slice can't have length 0 if (StringOrBufferLength(startHandle) > 0) { @@ -373,11 +375,11 @@ NAN_METHOD(Iterator::New) { } } - if (optionsObj->Has(NanNew("end")) - && (node::Buffer::HasInstance(optionsObj->Get(NanNew("end"))) - || optionsObj->Get(NanNew("end"))->IsString())) { + if (optionsObj->Has(Nan::New("end").ToLocalChecked()) + && (node::Buffer::HasInstance(optionsObj->Get(Nan::New("end").ToLocalChecked())) + || optionsObj->Get(Nan::New("end").ToLocalChecked())->IsString())) { - v8::Local endBuffer = optionsObj->Get(NanNew("end")); + v8::Local endBuffer = optionsObj->Get(Nan::New("end").ToLocalChecked()); // ignore end if it has size 0 since a Slice can't have length 0 if (StringOrBufferLength(endBuffer) > 0) { @@ -386,21 +388,21 @@ NAN_METHOD(Iterator::New) { } } - if (!optionsObj.IsEmpty() && optionsObj->Has(NanNew("limit"))) { + if (!optionsObj.IsEmpty() && optionsObj->Has(Nan::New("limit").ToLocalChecked())) { limit = v8::Local::Cast(optionsObj->Get( - NanNew("limit")))->Value(); + Nan::New("limit").ToLocalChecked()))->Value(); } - if (optionsObj->Has(NanNew("highWaterMark"))) { + if (optionsObj->Has(Nan::New("highWaterMark").ToLocalChecked())) { highWaterMark = v8::Local::Cast(optionsObj->Get( - NanNew("highWaterMark")))->Value(); + Nan::New("highWaterMark").ToLocalChecked()))->Value(); } - if (optionsObj->Has(NanNew("lt")) - && (node::Buffer::HasInstance(optionsObj->Get(NanNew("lt"))) - || optionsObj->Get(NanNew("lt"))->IsString())) { + if (optionsObj->Has(Nan::New("lt").ToLocalChecked()) + && (node::Buffer::HasInstance(optionsObj->Get(Nan::New("lt").ToLocalChecked())) + || optionsObj->Get(Nan::New("lt").ToLocalChecked())->IsString())) { - v8::Local ltBuffer = optionsObj->Get(NanNew("lt")); + v8::Local ltBuffer = optionsObj->Get(Nan::New("lt").ToLocalChecked()); // ignore end if it has size 0 since a Slice can't have length 0 if (StringOrBufferLength(ltBuffer) > 0) { @@ -411,11 +413,11 @@ NAN_METHOD(Iterator::New) { } } - if (optionsObj->Has(NanNew("lte")) - && (node::Buffer::HasInstance(optionsObj->Get(NanNew("lte"))) - || optionsObj->Get(NanNew("lte"))->IsString())) { + if (optionsObj->Has(Nan::New("lte").ToLocalChecked()) + && (node::Buffer::HasInstance(optionsObj->Get(Nan::New("lte").ToLocalChecked())) + || optionsObj->Get(Nan::New("lte").ToLocalChecked())->IsString())) { - v8::Local lteBuffer = optionsObj->Get(NanNew("lte")); + v8::Local lteBuffer = optionsObj->Get(Nan::New("lte").ToLocalChecked()); // ignore end if it has size 0 since a Slice can't have length 0 if (StringOrBufferLength(lteBuffer) > 0) { @@ -426,11 +428,11 @@ NAN_METHOD(Iterator::New) { } } - if (optionsObj->Has(NanNew("gt")) - && (node::Buffer::HasInstance(optionsObj->Get(NanNew("gt"))) - || optionsObj->Get(NanNew("gt"))->IsString())) { + if (optionsObj->Has(Nan::New("gt").ToLocalChecked()) + && (node::Buffer::HasInstance(optionsObj->Get(Nan::New("gt").ToLocalChecked())) + || optionsObj->Get(Nan::New("gt").ToLocalChecked())->IsString())) { - v8::Local gtBuffer = optionsObj->Get(NanNew("gt")); + v8::Local gtBuffer = optionsObj->Get(Nan::New("gt").ToLocalChecked()); // ignore end if it has size 0 since a Slice can't have length 0 if (StringOrBufferLength(gtBuffer) > 0) { @@ -441,11 +443,11 @@ NAN_METHOD(Iterator::New) { } } - if (optionsObj->Has(NanNew("gte")) - && (node::Buffer::HasInstance(optionsObj->Get(NanNew("gte"))) - || optionsObj->Get(NanNew("gte"))->IsString())) { + if (optionsObj->Has(Nan::New("gte").ToLocalChecked()) + && (node::Buffer::HasInstance(optionsObj->Get(Nan::New("gte").ToLocalChecked())) + || optionsObj->Get(Nan::New("gte").ToLocalChecked())->IsString())) { - v8::Local gteBuffer = optionsObj->Get(NanNew("gte")); + v8::Local gteBuffer = optionsObj->Get(Nan::New("gte").ToLocalChecked()); // ignore end if it has size 0 since a Slice can't have length 0 if (StringOrBufferLength(gteBuffer) > 0) { @@ -483,9 +485,9 @@ NAN_METHOD(Iterator::New) { , startHandle , highWaterMark ); - iterator->Wrap(args.This()); + iterator->Wrap(info.This()); - NanReturnValue(args.This()); + info.GetReturnValue().Set(info.This()); } } // namespace leveldown diff --git a/src/iterator.h b/src/iterator.h index 7384c6b3..f38c58cf 100644 --- a/src/iterator.h +++ b/src/iterator.h @@ -19,7 +19,7 @@ namespace leveldown { class Database; class AsyncWorker; -class Iterator : public node::ObjectWrap { +class Iterator : public Nan::ObjectWrap { public: static void Init (); static v8::Local NewInstance ( @@ -82,7 +82,7 @@ class Iterator : public node::ObjectWrap { AsyncWorker* endWorker; private: - v8::Persistent persistentHandle; + Nan::Persistent persistentHandle; bool Read (std::string& key, std::string& value); bool GetIterator (); diff --git a/src/iterator_async.cc b/src/iterator_async.cc index fffbbd4c..6da6e65f 100644 --- a/src/iterator_async.cc +++ b/src/iterator_async.cc @@ -17,7 +17,7 @@ namespace leveldown { NextWorker::NextWorker ( Iterator* iterator - , NanCallback *callback + , Nan::Callback *callback , void (*localCallback)(Iterator*) ) : AsyncWorker(NULL, callback) , iterator(iterator) @@ -36,7 +36,7 @@ void NextWorker::HandleOKCallback () { size_t idx = 0; size_t arraySize = result.size() * 2; - v8::Local returnArray = NanNew(arraySize); + v8::Local returnArray = Nan::New(arraySize); for(idx = 0; idx < result.size(); ++idx) { std::pair row = result[idx]; @@ -45,31 +45,33 @@ void NextWorker::HandleOKCallback () { v8::Local returnKey; if (iterator->keyAsBuffer) { - returnKey = NanNewBufferHandle((char*)key.data(), key.size()); + //TODO: use NewBuffer, see database_async.cc + returnKey = Nan::CopyBuffer((char*)key.data(), key.size()).ToLocalChecked(); } else { - returnKey = NanNew((char*)key.data(), key.size()); + returnKey = Nan::New((char*)key.data(), key.size()).ToLocalChecked(); } v8::Local returnValue; if (iterator->valueAsBuffer) { - returnValue = NanNewBufferHandle((char*)value.data(), value.size()); + //TODO: use NewBuffer, see database_async.cc + returnValue = Nan::CopyBuffer((char*)value.data(), value.size()).ToLocalChecked(); } else { - returnValue = NanNew((char*)value.data(), value.size()); + returnValue = Nan::New((char*)value.data(), value.size()).ToLocalChecked(); } // put the key & value in a descending order, so that they can be .pop:ed in javascript-land - returnArray->Set(NanNew(static_cast(arraySize - idx * 2 - 1)), returnKey); - returnArray->Set(NanNew(static_cast(arraySize - idx * 2 - 2)), returnValue); + returnArray->Set(Nan::New(static_cast(arraySize - idx * 2 - 1)), returnKey); + returnArray->Set(Nan::New(static_cast(arraySize - idx * 2 - 2)), returnValue); } // clean up & handle the next/end state see iterator.cc/checkEndCallback localCallback(iterator); v8::Local argv[] = { - NanNull() + Nan::Null() , returnArray // when ok === false all data has been read, so it's then finished - , NanNew(!ok) + , Nan::New(!ok) }; callback->Call(3, argv); } @@ -78,12 +80,12 @@ void NextWorker::HandleOKCallback () { EndWorker::EndWorker ( Iterator* iterator - , NanCallback *callback + , Nan::Callback *callback ) : AsyncWorker(NULL, callback) , iterator(iterator) {}; -EndWorker::~EndWorker () {} +EndWorker::~EndWorker () { } void EndWorker::Execute () { iterator->IteratorEnd(); diff --git a/src/iterator_async.h b/src/iterator_async.h index 9d6c7a3c..d4fa47b7 100644 --- a/src/iterator_async.h +++ b/src/iterator_async.h @@ -18,7 +18,7 @@ class NextWorker : public AsyncWorker { public: NextWorker ( Iterator* iterator - , NanCallback *callback + , Nan::Callback *callback , void (*localCallback)(Iterator*) ); @@ -37,7 +37,7 @@ class EndWorker : public AsyncWorker { public: EndWorker ( Iterator* iterator - , NanCallback *callback + , Nan::Callback *callback ); virtual ~EndWorker (); diff --git a/src/leveldown.cc b/src/leveldown.cc index 2a4ed317..8b46b8e4 100644 --- a/src/leveldown.cc +++ b/src/leveldown.cc @@ -14,39 +14,39 @@ namespace leveldown { NAN_METHOD(DestroyDB) { - NanScope(); + Nan::HandleScope scope; - NanUtf8String* location = new NanUtf8String(args[0]); + Nan::Utf8String* location = new Nan::Utf8String(info[0]); - NanCallback* callback = new NanCallback( - v8::Local::Cast(args[1])); + Nan::Callback* callback = new Nan::Callback( + v8::Local::Cast(info[1])); DestroyWorker* worker = new DestroyWorker( location , callback ); - NanAsyncQueueWorker(worker); + Nan::AsyncQueueWorker(worker); - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } NAN_METHOD(RepairDB) { - NanScope(); + Nan::HandleScope scope; - NanUtf8String* location = new NanUtf8String(args[0]); + Nan::Utf8String* location = new Nan::Utf8String(info[0]); - NanCallback* callback = new NanCallback( - v8::Local::Cast(args[1])); + Nan::Callback* callback = new Nan::Callback( + v8::Local::Cast(info[1])); RepairWorker* worker = new RepairWorker( location , callback ); - NanAsyncQueueWorker(worker); + Nan::AsyncQueueWorker(worker); - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } void Init (v8::Handle target) { @@ -55,19 +55,19 @@ void Init (v8::Handle target) { leveldown::Batch::Init(); v8::Local leveldown = - NanNew(LevelDOWN)->GetFunction(); + Nan::New(LevelDOWN)->GetFunction(); leveldown->Set( - NanNew("destroy") - , NanNew(DestroyDB)->GetFunction() + Nan::New("destroy").ToLocalChecked() + , Nan::New(DestroyDB)->GetFunction() ); leveldown->Set( - NanNew("repair") - , NanNew(RepairDB)->GetFunction() + Nan::New("repair").ToLocalChecked() + , Nan::New(RepairDB)->GetFunction() ); - target->Set(NanNew("leveldown"), leveldown); + target->Set(Nan::New("leveldown").ToLocalChecked(), leveldown); } NODE_MODULE(leveldown, Init) diff --git a/src/leveldown.h b/src/leveldown.h index a704c066..43dbaa0a 100644 --- a/src/leveldown.h +++ b/src/leveldown.h @@ -11,6 +11,8 @@ #include static inline size_t StringOrBufferLength(v8::Local obj) { + Nan::HandleScope scope; + return (!obj->ToObject().IsEmpty() && node::Buffer::HasInstance(obj->ToObject())) ? node::Buffer::Length(obj->ToObject()) @@ -20,16 +22,17 @@ static inline size_t StringOrBufferLength(v8::Local obj) { // NOTE: this MUST be called on objects created by // LD_STRING_OR_BUFFER_TO_SLICE static inline void DisposeStringOrBufferFromSlice( - v8::Persistent &handle + Nan::Persistent &handle , leveldb::Slice slice) { + Nan::HandleScope scope; if (!slice.empty()) { - v8::Local obj = NanNew(handle)->Get(NanNew("obj")); + v8::Local obj = Nan::New(handle)->Get(Nan::New("obj").ToLocalChecked()); if (!node::Buffer::HasInstance(obj)) delete[] slice.data(); } - NanDisposePersistent(handle); + handle.Reset(); } static inline void DisposeStringOrBufferFromSlice( @@ -66,41 +69,42 @@ static inline void DisposeStringOrBufferFromSlice( #define LD_RETURN_CALLBACK_OR_ERROR(callback, msg) \ if (!callback.IsEmpty() && callback->IsFunction()) { \ v8::Local argv[] = { \ - NanError(msg) \ + Nan::Error(msg) \ }; \ LD_RUN_CALLBACK(callback, 1, argv) \ - NanReturnUndefined(); \ + info.GetReturnValue().SetUndefined(); \ + return; \ } \ - return NanThrowError(msg); + return Nan::ThrowError(msg); #define LD_RUN_CALLBACK(callback, argc, argv) \ - NanMakeCallback( \ - NanGetCurrentContext()->Global(), callback, argc, argv); + Nan::MakeCallback( \ + Nan::GetCurrentContext()->Global(), callback, argc, argv); /* LD_METHOD_SETUP_COMMON setup the following objects: * - Database* database * - v8::Local optionsObj (may be empty) - * - v8::Persistent callback (won't be empty) + * - Nan::Persistent callback (won't be empty) * Will throw/return if there isn't a callback in arg 0 or 1 */ #define LD_METHOD_SETUP_COMMON(name, optionPos, callbackPos) \ - if (args.Length() == 0) \ - return NanThrowError(#name "() requires a callback argument"); \ + if (info.Length() == 0) \ + return Nan::ThrowError(#name "() requires a callback argument"); \ leveldown::Database* database = \ - node::ObjectWrap::Unwrap(args.This()); \ + Nan::ObjectWrap::Unwrap(info.This()); \ v8::Local optionsObj; \ v8::Local callback; \ - if (optionPos == -1 && args[callbackPos]->IsFunction()) { \ - callback = args[callbackPos].As(); \ - } else if (optionPos != -1 && args[callbackPos - 1]->IsFunction()) { \ - callback = args[callbackPos - 1].As(); \ + if (optionPos == -1 && info[callbackPos]->IsFunction()) { \ + callback = info[callbackPos].As(); \ + } else if (optionPos != -1 && info[callbackPos - 1]->IsFunction()) { \ + callback = info[callbackPos - 1].As(); \ } else if (optionPos != -1 \ - && args[optionPos]->IsObject() \ - && args[callbackPos]->IsFunction()) { \ - optionsObj = args[optionPos].As(); \ - callback = args[callbackPos].As(); \ + && info[optionPos]->IsObject() \ + && info[callbackPos]->IsFunction()) { \ + optionsObj = info[optionPos].As(); \ + callback = info[callbackPos].As(); \ } else { \ - return NanThrowError(#name "() requires a callback argument"); \ + return Nan::ThrowError(#name "() requires a callback argument"); \ } #define LD_METHOD_SETUP_COMMON_ONEARG(name) LD_METHOD_SETUP_COMMON(name, -1, 0) diff --git a/src/leveldown_async.cc b/src/leveldown_async.cc index 675a2347..dfaa5a36 100644 --- a/src/leveldown_async.cc +++ b/src/leveldown_async.cc @@ -13,8 +13,8 @@ namespace leveldown { /** DESTROY WORKER **/ DestroyWorker::DestroyWorker ( - NanUtf8String* location - , NanCallback *callback + Nan::Utf8String* location + , Nan::Callback *callback ) : AsyncWorker(NULL, callback) , location(location) {}; @@ -31,8 +31,8 @@ void DestroyWorker::Execute () { /** REPAIR WORKER **/ RepairWorker::RepairWorker ( - NanUtf8String* location - , NanCallback *callback + Nan::Utf8String* location + , Nan::Callback *callback ) : AsyncWorker(NULL, callback) , location(location) {}; diff --git a/src/leveldown_async.h b/src/leveldown_async.h index 689342dc..53196e5d 100644 --- a/src/leveldown_async.h +++ b/src/leveldown_async.h @@ -15,29 +15,29 @@ namespace leveldown { class DestroyWorker : public AsyncWorker { public: DestroyWorker ( - NanUtf8String* location - , NanCallback *callback + Nan::Utf8String* location + , Nan::Callback *callback ); virtual ~DestroyWorker (); virtual void Execute (); private: - NanUtf8String* location; + Nan::Utf8String* location; }; class RepairWorker : public AsyncWorker { public: RepairWorker ( - NanUtf8String* location - , NanCallback *callback + Nan::Utf8String* location + , Nan::Callback *callback ); virtual ~RepairWorker (); virtual void Execute (); private: - NanUtf8String* location; + Nan::Utf8String* location; }; } // namespace leveldown diff --git a/test/iterator-test.js b/test/iterator-test.js index c15eed83..87db93b7 100644 --- a/test/iterator-test.js +++ b/test/iterator-test.js @@ -66,4 +66,4 @@ make('reverse seek from invalid range', function (db, t, done) { t.same(value.toString(), '2', 'end of iterator') ite.end(done) }) -}) \ No newline at end of file +})