Skip to content

Commit

Permalink
upgrade to NAN v2
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jul 24, 2015
1 parent c9fe03d commit bdb6bd2
Show file tree
Hide file tree
Showing 21 changed files with 334 additions and 372 deletions.
18 changes: 11 additions & 7 deletions .dntrc
Original file line number Diff line number Diff line change
Expand Up @@ -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 's/^# //'"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 3 additions & 7 deletions src/async.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Object> obj = NanNew<v8::Object>();
NanAssignPersistent(persistentHandle, obj);
}
, Nan::Callback *callback
) : Nan::AsyncWorker(callback), database(database) { }

protected:
void SetStatus(leveldb::Status status) {
Expand Down
74 changes: 31 additions & 43 deletions src/batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace leveldown {

static v8::Persistent<v8::FunctionTemplate> batch_constructor;
static Nan::Persistent<v8::FunctionTemplate> batch_constructor;

Batch::Batch (leveldown::Database* database, bool sync) : database(database) {
options = new leveldb::WriteOptions();
Expand All @@ -28,45 +28,43 @@ leveldb::Status Batch::Write () {
}

void Batch::Init () {
v8::Local<v8::FunctionTemplate> tpl = NanNew<v8::FunctionTemplate>(Batch::New);
NanAssignPersistent(batch_constructor, tpl);
tpl->SetClassName(NanNew("Batch"));
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(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<Database>(args[0]->ToObject());
Database* database = Nan::ObjectWrap::Unwrap<Database>(info[0]->ToObject());
v8::Local<v8::Object> optionsObj;

if (args.Length() > 1 && args[1]->IsObject()) {
optionsObj = v8::Local<v8::Object>::Cast(args[1]);
if (info.Length() > 1 && info[1]->IsObject()) {
optionsObj = v8::Local<v8::Object>::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<v8::Value> Batch::NewInstance (
v8::Handle<v8::Object> database
, v8::Handle<v8::Object> optionsObj
) {

NanEscapableScope();
Nan::EscapableHandleScope scope;

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

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

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

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

NAN_METHOD(Batch::Put) {
NanScope();

Batch* batch = ObjectWrap::Unwrap<Batch>(args.Holder());
Batch* batch = ObjectWrap::Unwrap<Batch>(info.Holder());
v8::Handle<v8::Function> callback; // purely for the error macros

v8::Local<v8::Value> keyBuffer = args[0];
v8::Local<v8::Value> valueBuffer = args[1];
v8::Local<v8::Value> keyBuffer = info[0];
v8::Local<v8::Value> valueBuffer = info[1];
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key)
LD_STRING_OR_BUFFER_TO_SLICE(value, valueBuffer, value)

Expand All @@ -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<Batch>(args.Holder());
Batch* batch = ObjectWrap::Unwrap<Batch>(info.Holder());

v8::Handle<v8::Function> callback; // purely for the error macros

v8::Local<v8::Value> keyBuffer = args[0];
v8::Local<v8::Value> keyBuffer = info[0];
LD_STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, key)

batch->batch->Delete(key);
Expand All @@ -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<Batch>(args.Holder());
Batch* batch = ObjectWrap::Unwrap<Batch>(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<Batch>(args.Holder());
Batch* batch = ObjectWrap::Unwrap<Batch>(info.Holder());

if (batch->hasData) {
NanCallback *callback =
new NanCallback(v8::Local<v8::Function>::Cast(args[0]));
Nan::Callback *callback =
new Nan::Callback(v8::Local<v8::Function>::Cast(info[0]));
BatchWriteWorker* worker = new BatchWriteWorker(batch, callback);
// persist to prevent accidental GC
v8::Local<v8::Object> _this = args.This();
v8::Local<v8::Object> _this = info.This();
worker->SaveToPersistent("batch", _this);
NanAsyncQueueWorker(worker);
Nan::AsyncQueueWorker(worker);
} else {
LD_RUN_CALLBACK(v8::Local<v8::Function>::Cast(args[0]), 0, NULL);
LD_RUN_CALLBACK(v8::Local<v8::Function>::Cast(info[0]), 0, NULL);
}

NanReturnUndefined();
}

} // namespace leveldown
2 changes: 1 addition & 1 deletion src/batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace leveldown {

class Batch : public node::ObjectWrap {
class Batch : public Nan::ObjectWrap {
public:
static void Init();
static v8::Handle<v8::Value> NewInstance (
Expand Down
2 changes: 1 addition & 1 deletion src/batch_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace leveldown {

BatchWriteWorker::BatchWriteWorker (
Batch* batch
, NanCallback *callback
, Nan::Callback *callback
) : AsyncWorker(NULL, callback)
, batch(batch)
{};
Expand Down
2 changes: 1 addition & 1 deletion src/batch_async.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BatchWriteWorker : public AsyncWorker {
public:
BatchWriteWorker (
Batch* batch
, NanCallback *callback
, Nan::Callback *callback
);

virtual ~BatchWriteWorker ();
Expand Down
4 changes: 2 additions & 2 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace leveldown {
NAN_INLINE bool BooleanOptionValue(v8::Local<v8::Object> options,
const char* _key,
bool def = false) {
v8::Handle<v8::String> key = NanNew(_key);
v8::Handle<v8::String> key = Nan::New(_key).ToLocalChecked();
return !options.IsEmpty()
&& options->Has(key)
? options->Get(key)->BooleanValue()
Expand All @@ -23,7 +23,7 @@ NAN_INLINE bool BooleanOptionValue(v8::Local<v8::Object> options,
NAN_INLINE uint32_t UInt32OptionValue(v8::Local<v8::Object> options,
const char* _key,
uint32_t def) {
v8::Handle<v8::String> key = NanNew(_key);
v8::Handle<v8::String> key = Nan::New(_key).ToLocalChecked();
return !options.IsEmpty()
&& options->Has(key)
&& options->Get(key)->IsNumber()
Expand Down
Loading

0 comments on commit bdb6bd2

Please sign in to comment.