diff --git a/src/GNConstants.cpp b/src/GNConstants.cpp index 25550c9..ded1aff 100644 --- a/src/GNConstants.cpp +++ b/src/GNConstants.cpp @@ -32,14 +32,14 @@ using namespace v8; -static inline void SetConstant(const char* name, int value, Handle exports) { - exports->ForceSet(NanNew(name), NanNew(value), ReadOnly); +static inline void SetConstant(const char* name, int value, Local exports) { + exports->ForceSet(Nan::New(name).ToLocalChecked(), Nan::New(value), ReadOnly); } -void GNConstants::Init(Handle target) { +void GNConstants::Init(Local target) { - Local exports = NanNew(); - target->ForceSet(NanNew("constants"), exports, ReadOnly); + Local exports = Nan::New(); + target->ForceSet(Nan::New("constants").ToLocalChecked(), exports, ReadOnly); SetConstant("RETURN_GOOD",GETDNS_RETURN_GOOD,exports); SetConstant("RETURN_GENERIC_ERROR",GETDNS_RETURN_GENERIC_ERROR,exports); diff --git a/src/GNConstants.h b/src/GNConstants.h index 14d2987..1c6d34c 100644 --- a/src/GNConstants.h +++ b/src/GNConstants.h @@ -34,7 +34,7 @@ class GNConstants { public: // Node module initializer - static void Init(v8::Handle exports); + static void Init(v8::Local exports); private: GNConstants() { } ~GNConstants() { } diff --git a/src/GNContext.cpp b/src/GNContext.cpp index f01cd97..8a104a8 100644 --- a/src/GNContext.cpp +++ b/src/GNContext.cpp @@ -49,15 +49,15 @@ typedef enum LookupType { // Callback data passed to getdns callback as userarg typedef struct CallbackData { - NanCallback* callback; + Nan::Callback* callback; GNContext* ctx; } CallbackData; // Helper to create an error object for lookup callbacks -static Handle makeErrorObj(const char* msg, int code) { - Handle obj = NanNew(); - obj->Set(NanNew("msg"), NanNew(msg)); - obj->Set(NanNew("code"), NanNew(code)); +static Local makeErrorObj(const char* msg, int code) { + Local obj = Nan::New(); + obj->Set(Nan::New("msg").ToLocalChecked(), Nan::New(msg).ToLocalChecked()); + obj->Set(Nan::New("code").ToLocalChecked(), Nan::New(code)); return obj; } @@ -110,30 +110,30 @@ static getdns_dict* getdns_util_create_ip(const char* ip) { typedef getdns_return_t (*getdns_context_uint8_t_setter)(getdns_context*, uint8_t); typedef getdns_return_t (*getdns_context_uint16_t_setter)(getdns_context*, uint16_t); -static void setTransport(getdns_context* context, Handle opt) { +static void setTransport(getdns_context* context, Local opt) { if (opt->IsNumber()) { uint32_t num = opt->Uint32Value(); getdns_context_set_dns_transport(context, (getdns_transport_t) num); } } -static void setRedirects(getdns_context* context, Handle opt) { +static void setRedirects(getdns_context* context, Local opt) { if (opt->IsNumber()) { uint32_t num = opt->Uint32Value(); getdns_context_set_follow_redirects(context, (getdns_redirects_t) num); } } -static void setTlsAuthentication(getdns_context* context, Handle opt) { +static void setTlsAuthentication(getdns_context* context, Local opt) { if (opt->IsNumber()) { uint32_t num = opt->Uint32Value(); getdns_context_set_tls_authentication(context, (getdns_tls_authentication_t) num); } } -static void setTransportList(getdns_context* context, Handle opt) { +static void setTransportList(getdns_context* context, Local opt) { if (opt->IsArray()) { - Handle transportList = Handle::Cast(opt); + Local transportList = Local::Cast(opt); uint32_t numTransports = transportList->Length(); // create a getdns_transport_list_t* getdns_transport_list_t* transports = new getdns_transport_list_t[numTransports]; @@ -148,9 +148,9 @@ static void setTransportList(getdns_context* context, Handle opt) { } } -static void setNamespaceList(getdns_context* context, Handle opt) { +static void setNamespaceList(getdns_context* context, Local opt) { if (opt->IsArray()) { - Handle namespaceList = Handle::Cast(opt); + Local namespaceList = Local::Cast(opt); uint32_t numNamespaces = namespaceList->Length(); // create a getdns_namespace_list_t* getdns_namespace_t* namespaces = new getdns_namespace_t[numNamespaces]; @@ -165,7 +165,7 @@ static void setNamespaceList(getdns_context* context, Handle opt) { } } -static void setStub(getdns_context* context, Handle opt) { +static void setStub(getdns_context* context, Local opt) { if (opt->IsTrue()) { getdns_context_set_resolution_type(context, GETDNS_RESOLUTION_STUB); } else { @@ -173,7 +173,7 @@ static void setStub(getdns_context* context, Handle opt) { } } -static void setResolutionType(getdns_context* context, Handle opt) { +static void setResolutionType(getdns_context* context, Local opt) { if (opt->IsNumber()) { uint32_t num = opt->Uint32Value(); getdns_context_set_resolution_type(context, (getdns_resolution_t) num); @@ -181,7 +181,7 @@ static void setResolutionType(getdns_context* context, Handle opt) { } -static void setAppendName(getdns_context* context, Handle opt) { +static void setAppendName(getdns_context* context, Local opt) { if (opt->IsNumber()) { uint32_t num = opt->Uint32Value(); getdns_context_set_append_name(context, (getdns_append_name_t) num); @@ -316,18 +316,18 @@ static void setSuffixesHelper(getdns_context* context, char* buf) { getdns_list_destroy(suffixes); } -static void setSuffixes(getdns_context* context, Handle opt) { +static void setSuffixes(getdns_context* context, Local opt) { if (opt->IsString()) { - NanUtf8String suff(opt->ToString()); + Nan::Utf8String suff(opt->ToString()); setSuffixesHelper(context, (char *)*suff); } } #define EXAMPLE_PIN "pin-sha256=\"E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=\"" -static void setPinset(getdns_context* context, Handle opt) { +static void setPinset(getdns_context* context, Local opt) { if (opt->IsString()) { - NanUtf8String pin(opt->ToString()); + Nan::Utf8String pin(opt->ToString()); getdns_dict *pubkey_pin = NULL; static getdns_list *pubkey_pinset = NULL; static size_t pincount = 0; @@ -359,10 +359,10 @@ static void setPinset(getdns_context* context, Handle opt) { } } -static void setUpstreams(getdns_context* context, Handle opt) { +static void setUpstreams(getdns_context* context, Local opt) { if (opt->IsArray()) { getdns_list* upstreams = getdns_list_create(); - Handle values = Handle::Cast(opt); + Local values = Local::Cast(opt); for (uint32_t i = 0; i < values->Length(); ++i) { Local ipOrTuple = values->Get(i); getdns_dict* ipDict = NULL; @@ -371,9 +371,9 @@ static void setUpstreams(getdns_context* context, Handle opt) { // optional tuple - TLS Hostname // optional tuple - TSIG name:algorithm:secret // optional tuple - Search Suffix - Handle tuple = Handle::Cast(ipOrTuple); + Local tuple = Local::Cast(ipOrTuple); if (tuple->Length() > 0) { - NanUtf8String asciiStr(tuple->Get(0)->ToString()); + Nan::Utf8String asciiStr(tuple->Get(0)->ToString()); ipDict = getdns_util_create_ip(*asciiStr); if (ipDict && tuple->Length() > 1 && tuple->Get(1)->IsNumber()) { @@ -381,7 +381,7 @@ static void setUpstreams(getdns_context* context, Handle opt) { uint32_t port = tuple->Get(1)->Uint32Value(); getdns_dict_set_int(ipDict, "port", port); // TLS hostname or TSIG (TODO: fix to allow this if optional port is not set) - NanUtf8String asciiBuffer(tuple->Get(2)->ToString()); + Nan::Utf8String asciiBuffer(tuple->Get(2)->ToString()); if (((char *)*asciiBuffer)[0] == '^') { // tsig tsigHelper(ipDict, *asciiBuffer); @@ -394,7 +394,7 @@ static void setUpstreams(getdns_context* context, Handle opt) { } } } else { - NanUtf8String asciiStr(ipOrTuple->ToString()); + Nan::Utf8String asciiStr(ipOrTuple->ToString()); if (((char *)*asciiStr)[0] == '^') { // tsig tsigHelper(ipDict, *asciiStr); } else if (((char *)*asciiStr)[0] == '~') { // suffix @@ -408,51 +408,51 @@ static void setUpstreams(getdns_context* context, Handle opt) { getdns_list_set_dict(upstreams, len, ipDict); getdns_dict_destroy(ipDict); } else { - NanUtf8String msg(String::Concat(NanNew("Upstream value is invalid: "), ipOrTuple->ToString())); - NanThrowTypeError(*msg); + Nan::Utf8String msg(String::Concat(Nan::New("Upstream value is invalid: ").ToLocalChecked(), ipOrTuple->ToString())); + Nan::ThrowTypeError(*msg); } } getdns_return_t r = getdns_context_set_upstream_recursive_servers(context, upstreams); getdns_list_destroy(upstreams); if (r != GETDNS_RETURN_GOOD) { - NanThrowTypeError("Failed to set upstreams."); + Nan::ThrowTypeError("Failed to set upstreams."); } } } -static void setTimeout(getdns_context* context, Handle opt) { +static void setTimeout(getdns_context* context, Local opt) { if (opt->IsNumber()) { uint32_t num = opt->Uint32Value(); getdns_context_set_timeout(context, num); } } -static void setDnssecAllowedSkew(getdns_context* context, Handle opt) { +static void setDnssecAllowedSkew(getdns_context* context, Local opt) { if (opt->IsNumber()) { uint32_t num = opt->Uint32Value(); getdns_context_set_dnssec_allowed_skew(context, num); } } -static void setUseThreads(getdns_context* context, Handle opt) { +static void setUseThreads(getdns_context* context, Local opt) { int val = opt->IsTrue() ? 1 : 0; getdns_context_set_use_threads(context, val); } -static void setReturnDnssecStatus(getdns_context* context, Handle opt) { +static void setReturnDnssecStatus(getdns_context* context, Local opt) { int val = opt->IsTrue() ? GETDNS_EXTENSION_TRUE : GETDNS_EXTENSION_FALSE; getdns_context_set_return_dnssec_status(context, val); } // set alternate root servers from a file passed in -static void setDnsRootServers(getdns_context* context, Handle opt) +static void setDnsRootServers(getdns_context* context, Local opt) { FILE *fh; getdns_list *hints = NULL; if (opt->IsString()) { - NanUtf8String roots(opt->ToString()); + Nan::Utf8String roots(opt->ToString()); if (!(fh = fopen((char *)*roots, "r"))) { fprintf(stderr, "Could not open %s\n", (char *)*roots); return; @@ -478,12 +478,12 @@ static void setDnsRootServers(getdns_context* context, Handle opt) // Read the location of the Trust anchor and pass it to getdns. // TODO: actually pass in the trust anchor. -static void setTrustAnchor(getdns_context *context, Handle opt) +static void setTrustAnchor(getdns_context *context, Local opt) { FILE *fh; getdns_list *tas = NULL; if (opt->IsString()) { - NanUtf8String ta(opt->ToString()); + Nan::Utf8String ta(opt->ToString()); if (!(fh = fopen((char *)*ta, "r"))) { fprintf(stderr, "Could not open %s\n", (char *)*ta); @@ -508,7 +508,7 @@ static void setTrustAnchor(getdns_context *context, Handle opt) } -typedef void (*context_setter)(getdns_context* context, Handle opt); +typedef void (*context_setter)(getdns_context* context, Local opt); typedef struct OptionSetter { const char* opt_name; context_setter setter; @@ -566,15 +566,15 @@ static size_t NUM_UINT16_SETTERS = sizeof(UINT16_OPTION_SETTERS) / sizeof(Uint16 // End setters NAN_GETTER(GNContext::GetContextValue) { // context has no getters yet - NanScope(); - NanReturnValue(NanNew(-1)); + Nan::HandleScope scope; + info.GetReturnValue().Set(Nan::New(-1)); } NAN_SETTER(GNContext::SetContextValue) { // walk setters - NanUtf8String name(property); - GNContext* ctx = node::ObjectWrap::Unwrap(args.This()); + Nan::Utf8String name(property); + GNContext* ctx = Nan::ObjectWrap::Unwrap(info.This()); if (!ctx) { - NanThrowError("Context is invalid."); + Nan::ThrowError("Context is invalid."); } size_t s = 0; bool found = false; @@ -604,18 +604,18 @@ NAN_SETTER(GNContext::SetContextValue) { } } -void GNContext::InitProperties(Handle ctx) { +void GNContext::InitProperties(Local ctx) { size_t s = 0; for (s = 0; s < NUM_SETTERS; ++s) { - ctx->SetAccessor(NanNew(SETTERS[s].opt_name), + Nan::SetAccessor(ctx, Nan::New(SETTERS[s].opt_name).ToLocalChecked(), GNContext::GetContextValue, GNContext::SetContextValue); } for (s = 0; s < NUM_UINT8_SETTERS; ++s) { - ctx->SetAccessor(NanNew(UINT8_OPTION_SETTERS[s].opt_name), + Nan::SetAccessor(ctx, Nan::New(UINT8_OPTION_SETTERS[s].opt_name).ToLocalChecked(), GNContext::GetContextValue, GNContext::SetContextValue); } for (s = 0; s < NUM_UINT16_SETTERS; ++s) { - ctx->SetAccessor(NanNew(UINT16_OPTION_SETTERS[s].opt_name), + Nan::SetAccessor(ctx, Nan::New(UINT16_OPTION_SETTERS[s].opt_name).ToLocalChecked(), GNContext::GetContextValue, GNContext::SetContextValue); } @@ -627,7 +627,7 @@ GNContext::~GNContext() { context_ = NULL; } -void GNContext::ApplyOptions(Handle self, Handle optsV) { +void GNContext::ApplyOptions(Local self, Local optsV) { if (!GNUtil::isDictionaryObject(optsV)) { return; } @@ -647,25 +647,25 @@ void GNContext::ApplyOptions(Handle self, Handle optsV) { } // Module initialization -void GNContext::Init(Handle target) { +void GNContext::Init(Local target) { // prepare context object template - Local jsContextTpl = NanNew(GNContext::New); - jsContextTpl->SetClassName(NanNew("Context")); + Local jsContextTpl = Nan::New(GNContext::New); + jsContextTpl->SetClassName(Nan::New("Context").ToLocalChecked()); jsContextTpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype - NODE_SET_PROTOTYPE_METHOD(jsContextTpl, "lookup", GNContext::Lookup); - NODE_SET_PROTOTYPE_METHOD(jsContextTpl, "cancel", GNContext::Cancel); - NODE_SET_PROTOTYPE_METHOD(jsContextTpl, "destroy", GNContext::Destroy); + Nan::SetPrototypeMethod(jsContextTpl, "lookup", GNContext::Lookup); + Nan::SetPrototypeMethod(jsContextTpl, "cancel", GNContext::Cancel); + Nan::SetPrototypeMethod(jsContextTpl, "destroy", GNContext::Destroy); // Helpers - delegate to the same function w/ different data - jsContextTpl->PrototypeTemplate()->Set(NanNew("getAddress"), - NanNew(GNContext::HelperLookup, NanNew(GNAddress))->GetFunction()); - jsContextTpl->PrototypeTemplate()->Set(NanNew("getHostname"), - NanNew(GNContext::HelperLookup, NanNew(GNHostname))->GetFunction()); - jsContextTpl->PrototypeTemplate()->Set(NanNew("getService"), - NanNew(GNContext::HelperLookup, NanNew(GNService))->GetFunction()); + jsContextTpl->PrototypeTemplate()->Set(Nan::New("getAddress").ToLocalChecked(), + Nan::New(GNContext::HelperLookup, Nan::New(GNAddress))->GetFunction()); + jsContextTpl->PrototypeTemplate()->Set(Nan::New("getHostname").ToLocalChecked(), + Nan::New(GNContext::HelperLookup, Nan::New(GNHostname))->GetFunction()); + jsContextTpl->PrototypeTemplate()->Set(Nan::New("getService").ToLocalChecked(), + Nan::New(GNContext::HelperLookup, Nan::New(GNService))->GetFunction()); // Add the constructor - target->Set(NanNew("Context"), jsContextTpl->GetFunction()); + target->Set(Nan::New("Context").ToLocalChecked(), jsContextTpl->GetFunction()); // Export constants GNConstants::Init(target); @@ -673,27 +673,27 @@ void GNContext::Init(Handle target) { // Explicity destroy the context NAN_METHOD(GNContext::Destroy) { - NanScope(); - GNContext* ctx = ObjectWrap::Unwrap(args.This()); + Nan::HandleScope scope; + GNContext* ctx = Nan::ObjectWrap::Unwrap(info.This()); if (!ctx) { - NanThrowError(NanNew("Context is invalid.")); + Nan::ThrowError(Nan::New("Context is invalid.").ToLocalChecked()); } getdns_context_destroy(ctx->context_); ctx->context_ = NULL; - NanReturnValue(NanTrue()); + info.GetReturnValue().Set(Nan::True()); } // Create a context (new op) NAN_METHOD(GNContext::New) { - NanScope(); - if (args.IsConstructCall()) { + Nan::HandleScope scope; + if (info.IsConstructCall()) { // new obj GNContext* ctx = new GNContext(); getdns_return_t r = getdns_context_create(&ctx->context_, 1); if (r != GETDNS_RETURN_GOOD) { // Failed to create an underlying context delete ctx; - NanThrowError(NanNew("Unable to create GNContext.")); + Nan::ThrowError(Nan::New("Unable to create GNContext.").ToLocalChecked()); } // Attach the context to node @@ -701,29 +701,29 @@ NAN_METHOD(GNContext::New) { if (!attached) { // Bail delete ctx; - NanThrowError(NanNew("Unable to attach to Node.")); - NanReturnUndefined(); + Nan::ThrowError(Nan::New("Unable to attach to Node.").ToLocalChecked()); + return; } - ctx->Wrap(args.This()); + ctx->Wrap(info.This()); // add setters - GNContext::InitProperties(args.This()); + GNContext::InitProperties(info.This()); // Apply options if needed - if (args.Length() > 0) { + if (info.Length() > 0) { // could throw an TryCatch try_catch; - GNContext::ApplyOptions(args.This(), args[0]); + GNContext::ApplyOptions(info.This(), info[0]); if (try_catch.HasCaught()) { // Need to bail delete ctx; try_catch.ReThrow(); - NanReturnUndefined(); + return; } } - NanReturnThis(); + info.GetReturnValue().Set(info.This()); } else { - NanThrowError(NanNew("Must use new.")); + Nan::ThrowError(Nan::New("Must use new.").ToLocalChecked()); } - NanReturnUndefined(); + return; } void GNContext::Callback(getdns_context *context, @@ -733,18 +733,18 @@ void GNContext::Callback(getdns_context *context, getdns_transaction_t transId) { CallbackData* data = static_cast(userArg); // Setup the callback arguments - Handle argv[3]; + Local argv[3]; if (cbType == GETDNS_CALLBACK_COMPLETE) { - argv[0] = NanNull(); + argv[0] = Nan::Null(); argv[1] = GNUtil::convertToJSObj(response); getdns_dict_destroy(response); } else { argv[0] = makeErrorObj("Lookup failed.", cbType); - argv[1] = NanNull(); + argv[1] = Nan::Null(); } TryCatch try_catch; argv[2] = GNUtil::convertToBuffer(&transId, 8); - data->callback->Call(NanGetCurrentContext()->Global(), 3, argv); + data->callback->Call(Nan::GetCurrentContext()->Global(), 3, argv); if (try_catch.HasCaught()) node::FatalException(try_catch); @@ -757,63 +757,63 @@ void GNContext::Callback(getdns_context *context, // Cancel a req. Expect it to be a transaction id as a buffer NAN_METHOD(GNContext::Cancel) { - NanScope(); - GNContext* ctx = node::ObjectWrap::Unwrap(args.This()); + Nan::HandleScope scope; + GNContext* ctx = Nan::ObjectWrap::Unwrap(info.This()); if (!ctx || !ctx->context_) { - NanReturnValue(NanFalse()); + info.GetReturnValue().Set(Nan::False()); } - if (args.Length() < 1) { - NanReturnValue(NanFalse()); + if (info.Length() < 1) { + info.GetReturnValue().Set(Nan::False()); } - if (node::Buffer::Length(args[0]) != 8) { - NanReturnValue(NanFalse()); + if (node::Buffer::Length(info[0]) != 8) { + info.GetReturnValue().Set(Nan::False()); } uint64_t transId; - memcpy(&transId, node::Buffer::Data(args[0]), 8); + memcpy(&transId, node::Buffer::Data(info[0]), 8); getdns_return_t r = getdns_cancel_callback(ctx->context_, transId); - NanReturnValue(r == GETDNS_RETURN_GOOD ? NanTrue() : NanFalse()); + info.GetReturnValue().Set(r == GETDNS_RETURN_GOOD ? Nan::True() : Nan::False()); } // Handle getdns general NAN_METHOD(GNContext::Lookup) { - NanScope(); + Nan::HandleScope scope; // name, type, and callback are required - if (args.Length() < 3) { - NanThrowTypeError("At least 3 arguments are required."); + if (info.Length() < 3) { + Nan::ThrowTypeError("At least 3 arguments are required."); } // last arg must be a callback - Local last = args[args.Length() - 1]; + Local last = info[info.Length() - 1]; if (!last->IsFunction()) { - NanThrowTypeError("Final argument must be a function."); + Nan::ThrowTypeError("Final argument must be a function."); } Local localCb = Local::Cast(last); - GNContext* ctx = node::ObjectWrap::Unwrap(args.This()); + GNContext* ctx = Nan::ObjectWrap::Unwrap(info.This()); if (!ctx || !ctx->context_) { - Handle err = makeErrorObj("Context is invalid", GETDNS_RETURN_GENERIC_ERROR); - Handle cbArgs[] = { err }; - NanMakeCallback(NanGetCurrentContext()->Global(), localCb, 1, cbArgs); - NanReturnUndefined(); + Local err = makeErrorObj("Context is invalid", GETDNS_RETURN_GENERIC_ERROR); + Local cbArgs[] = { err }; + Nan::MakeCallback(Nan::GetCurrentContext()->Global(), localCb, 1, cbArgs); + return; } // take first arg and make it a string - String::Utf8Value name(args[0]->ToString()); + String::Utf8Value name(info[0]->ToString()); // second arg must be a number - if (!args[1]->IsNumber()) { - Handle err = makeErrorObj("Second argument must be a number", GETDNS_RETURN_INVALID_PARAMETER); - Handle cbArgs[] = { err }; - NanMakeCallback(NanGetCurrentContext()->Global(), localCb, 1, cbArgs); - NanReturnUndefined(); + if (!info[1]->IsNumber()) { + Local err = makeErrorObj("Second argument must be a number", GETDNS_RETURN_INVALID_PARAMETER); + Local cbArgs[] = { err }; + Nan::MakeCallback(Nan::GetCurrentContext()->Global(), localCb, 1, cbArgs); + return; } - uint16_t type = (uint16_t) args[1]->Uint32Value(); + uint16_t type = (uint16_t) info[1]->Uint32Value(); // optional third arg is an object getdns_dict* extension = NULL; - if (args.Length() > 3 && args[2]->IsObject()) { - extension = GNUtil::convertToDict(args[2]->ToObject()); + if (info.Length() > 3 && info[2]->IsObject()) { + extension = GNUtil::convertToDict(info[2]->ToObject()); } // create callback data CallbackData *data = new CallbackData(); - data->callback = new NanCallback(localCb); + data->callback = new Nan::Callback(localCb); data->ctx = ctx; ctx->Ref(); @@ -828,13 +828,13 @@ NAN_METHOD(GNContext::Lookup) { data->ctx->Unref(); delete data; - Handle err = makeErrorObj("Error issuing query", r); - Handle cbArgs[] = { err }; - localCb->Call(NanGetCurrentContext()->Global(), 1, cbArgs); - NanReturnUndefined(); + Local err = makeErrorObj("Error issuing query", r); + Local cbArgs[] = { err }; + localCb->Call(Nan::GetCurrentContext()->Global(), 1, cbArgs); + return; } // done. - NanReturnValue(GNUtil::convertToBuffer(&transId, 8)); + info.GetReturnValue().Set(GNUtil::convertToBuffer(&transId, 8)); } // Common function to handle getdns_address/service/hostname @@ -842,39 +842,39 @@ NAN_METHOD(GNContext::HelperLookup) { // first argument is a string // last argument must be a callback // optional argument of extensions - NanScope(); + Nan::HandleScope scope; // name, type, and callback are required - if (args.Length() < 2) { - NanThrowTypeError("At least 2 arguments are required."); + if (info.Length() < 2) { + Nan::ThrowTypeError("At least 2 arguments are required."); } // last arg must be a callback - Local last = args[args.Length() - 1]; + Local last = info[info.Length() - 1]; if (!last->IsFunction()) { - NanThrowTypeError("Final argument must be a function."); + Nan::ThrowTypeError("Final argument must be a function."); } Local localCb = Local::Cast(last); - GNContext* ctx = node::ObjectWrap::Unwrap(args.This()); + GNContext* ctx = Nan::ObjectWrap::Unwrap(info.This()); if (!ctx || !ctx->context_) { - Handle err = makeErrorObj("Context is invalid", GETDNS_RETURN_GENERIC_ERROR); - Handle cbArgs[] = { err }; - localCb->Call(NanGetCurrentContext()->Global(), 1, cbArgs); - NanReturnUndefined(); + Local err = makeErrorObj("Context is invalid", GETDNS_RETURN_GENERIC_ERROR); + Local cbArgs[] = { err }; + localCb->Call(Nan::GetCurrentContext()->Global(), 1, cbArgs); + return; } // take first arg and make it a string - String::Utf8Value name(args[0]->ToString()); + String::Utf8Value name(info[0]->ToString()); // 2nd arg could be extensions // optional third arg is an object getdns_dict* extension = NULL; - if (args.Length() > 2 && args[1]->IsObject()) { - extension = GNUtil::convertToDict(args[1]->ToObject()); + if (info.Length() > 2 && info[1]->IsObject()) { + extension = GNUtil::convertToDict(info[1]->ToObject()); } // figure out what called us - uint32_t funcType = args.Data()->Uint32Value(); + uint32_t funcType = info.Data()->Uint32Value(); // create callback data CallbackData *data = new CallbackData(); - data->callback = new NanCallback(localCb); + data->callback = new Nan::Callback(localCb); data->ctx = ctx; ctx->Ref(); @@ -905,13 +905,13 @@ NAN_METHOD(GNContext::HelperLookup) { data->ctx->Unref(); delete data; - Handle err = makeErrorObj("Error issuing query", r); - Handle cbArgs[] = { err }; - localCb->Call(NanGetCurrentContext()->Global(), 1, cbArgs); - NanReturnUndefined(); + Local err = makeErrorObj("Error issuing query", r); + Local cbArgs[] = { err }; + localCb->Call(Nan::GetCurrentContext()->Global(), 1, cbArgs); + return; } // done. return as buffer - NanReturnValue(GNUtil::convertToBuffer(&transId, 8)); + info.GetReturnValue().Set(GNUtil::convertToBuffer(&transId, 8)); } // Init the module diff --git a/src/GNContext.h b/src/GNContext.h index 81f327d..604daaf 100644 --- a/src/GNContext.h +++ b/src/GNContext.h @@ -33,18 +33,18 @@ #include // Getdns Context wrapper for Node -class GNContext : public node::ObjectWrap { +class GNContext : public Nan::ObjectWrap { public: // Node module initializer - static void Init(v8::Handle target); + static void Init(v8::Local target); private: GNContext(); ~GNContext(); // set options on the context - static void ApplyOptions(v8::Handle self, - v8::Handle opts); + static void ApplyOptions(v8::Local self, + v8::Local opts); // JS Functions static NAN_METHOD(New); @@ -53,7 +53,7 @@ class GNContext : public node::ObjectWrap { static NAN_METHOD(HelperLookup); static NAN_METHOD(Cancel); - static void InitProperties(v8::Handle self); + static void InitProperties(v8::Local self); static NAN_GETTER(GetContextValue); static NAN_SETTER(SetContextValue); diff --git a/src/GNUtil.cpp b/src/GNUtil.cpp index a32678b..3fd5499 100644 --- a/src/GNUtil.cpp +++ b/src/GNUtil.cpp @@ -253,7 +253,7 @@ priv_getdns_bindata_is_dname(struct getdns_bindata *bindata) // Convert bindata into a good representational string or // into a buffer. Handles dname, printable, ".", // and an ip address if it is under a known key -static Handle convertBinData(getdns_bindata* data, +static Local convertBinData(getdns_bindata* data, const char* key) { bool printable = true; for (size_t i = 0; i < data->size; ++i) { @@ -268,16 +268,16 @@ static Handle convertBinData(getdns_bindata* data, } // basic string? if (printable) { - return NanNew( (char*) data->data, data->size ); + return Nan::New( (char*).ToLocalChecked() data->data, data->size ); // the root } else if (data->size == 1 && data->data[0] == 0) { - return NanNew("."); + return Nan::New(".").ToLocalChecked(); // dname } else if (priv_getdns_bindata_is_dname(data)) { char* dname = NULL; if (getdns_convert_dns_name_to_fqdn(data, &dname) == GETDNS_RETURN_GOOD) { - Handle result = NanNew(dname); + Local result = Nan::New(dname).ToLocalChecked(); free(dname); return result; } @@ -287,7 +287,7 @@ static Handle convertBinData(getdns_bindata* data, strcmp(key, "ipv6_address") == 0)) { char* ipStr = getdns_display_ip_address(data); if (ipStr) { - Handle result = NanNew(ipStr); + Local result = Nan::New(ipStr).ToLocalChecked(); free(ipStr); return result; } @@ -297,23 +297,23 @@ static Handle convertBinData(getdns_bindata* data, return GNUtil::convertToBuffer(data->data, data->size); } -Handle GNUtil::convertToBuffer(void* data, size_t size) { +Local GNUtil::convertToBuffer(void* data, size_t size) { //construct a new buffer of the size we need. - Local nodeBuffer = NanNewBufferHandle(size); + Local nodeBuffer = Nan::NewBuffer(size).ToLocalChecked(); //Copy the contents of our payload into the buffer memcpy(node::Buffer::Data(nodeBuffer), data, size); return nodeBuffer; } -Handle GNUtil::convertToJSArray(struct getdns_list* list) { - NanEscapableScope(); +Local GNUtil::convertToJSArray(struct getdns_list* list) { + Nan::EscapableHandleScope scope; if (!list) { - return NanEscapeScope(NanNull()); + return scope.Escape(Nan::Null()); } size_t len; getdns_list_get_length(list, &len); - Handle array = NanNew(); + Local array = Nan::New(); for (size_t i = 0; i < len; ++i) { getdns_data_type type; getdns_list_get_data_type(list, i, &type); @@ -329,7 +329,7 @@ Handle GNUtil::convertToJSArray(struct getdns_list* list) { { uint32_t res = 0; getdns_list_get_int(list, i, &res); - array->Set(i, NanNew(res)); + array->Set(i, Nan::New(res)); break; } case t_dict: @@ -350,7 +350,7 @@ Handle GNUtil::convertToJSArray(struct getdns_list* list) { break; } } - return NanEscapeScope(array); + return scope.Escape(array); } // potential helper to get the ip string of a dict @@ -377,29 +377,29 @@ char* getdns_dict_to_ip_string(getdns_dict* dict) { } -Handle GNUtil::convertToJSObj(struct getdns_dict* dict) { - NanEscapableScope(); +Local GNUtil::convertToJSObj(struct getdns_dict* dict) { + Nan::EscapableHandleScope scope; if (!dict) { - return NanEscapeScope(NanNull()); + return scope.Escape(Nan::Null()); } // try it as an IP char* ipStr = getdns_dict_to_ip_string(dict); if (ipStr) { - Handle result = NanNew(ipStr); + Local result = Nan::New(ipStr).ToLocalChecked(); free(ipStr); - return NanEscapeScope(result); + return scope.Escape(result); } getdns_list* names; getdns_dict_get_names(dict, &names); size_t len = 0; - Handle result = NanNew(); + Local result = Nan::New(); getdns_list_get_length(names, &len); for (size_t i = 0; i < len; ++i) { getdns_bindata* nameBin; getdns_list_get_bindata(names, i, &nameBin); - Handle name = NanNew((char*) nameBin->data); + Local name = Nan::New((char*).ToLocalChecked() nameBin->data); getdns_data_type type; getdns_dict_get_data_type(dict, (char*)nameBin->data, &type); switch (type) { @@ -414,7 +414,7 @@ Handle GNUtil::convertToJSObj(struct getdns_dict* dict) { { uint32_t res = 0; getdns_dict_get_int(dict, (char*)nameBin->data, &res); - result->Set(name, NanNew(res)); + result->Set(name, Nan::New(res)); break; } case t_dict: @@ -436,7 +436,7 @@ Handle GNUtil::convertToJSObj(struct getdns_dict* dict) { } } getdns_list_destroy(names); - return NanEscapeScope(result); + return scope.Escape(result); } // Enums to determine what type a JSValue is @@ -450,13 +450,13 @@ typedef enum GetdnsType { UnknownType } GetdnsType; -bool GNUtil::isDictionaryObject(Handle obj) { +bool GNUtil::isDictionaryObject(Local obj) { return obj->IsObject() && !(obj->IsRegExp() || obj->IsDate() || obj->IsFunction() || obj->IsArray()); } -static GetdnsType getGetdnsType(Handle value) { +static GetdnsType getGetdnsType(Local value) { if (value->IsNumber() || value->IsNumberObject()) { return IntType; } else if (value->IsBoolean() || value->IsBooleanObject()) { @@ -476,7 +476,7 @@ static GetdnsType getGetdnsType(Handle value) { return UnknownType; } -getdns_list* GNUtil::convertToList(Handle array) { +getdns_list* GNUtil::convertToList(Local array) { uint32_t len = array->Length(); getdns_list* result = getdns_list_create(); for (uint32_t i = 0; i < len; ++i) { @@ -514,7 +514,7 @@ getdns_list* GNUtil::convertToList(Handle array) { break; case ListType: { - Handle subArray = Handle::Cast(val); + Local subArray = Local::Cast(val); struct getdns_list* sublist = GNUtil::convertToList(subArray); getdns_list_set_list(result, idx, sublist); getdns_list_destroy(sublist); @@ -522,7 +522,7 @@ getdns_list* GNUtil::convertToList(Handle array) { break; case DictType: { - Handle subObj = val->ToObject(); + Local subObj = val->ToObject(); struct getdns_dict* subdict = GNUtil::convertToDict(subObj); if (subdict) { getdns_list_set_dict(result, idx, subdict); @@ -537,7 +537,7 @@ getdns_list* GNUtil::convertToList(Handle array) { return result; } -getdns_dict* GNUtil::convertToDict(Handle obj) { +getdns_dict* GNUtil::convertToDict(Local obj) { if (obj->IsRegExp() || obj->IsDate() || obj->IsFunction() || obj->IsUndefined() || obj->IsNull() || obj->IsArray()) { @@ -547,7 +547,7 @@ getdns_dict* GNUtil::convertToDict(Handle obj) { getdns_dict* result = getdns_dict_create(); for(unsigned int i = 0; i < names->Length(); i++) { Local nameVal = names->Get(i); - NanUtf8String name(nameVal); + Nan::Utf8String name(nameVal); Local val = obj->Get(nameVal); GetdnsType type = getGetdnsType(val); switch (type) { @@ -581,7 +581,7 @@ getdns_dict* GNUtil::convertToDict(Handle obj) { break; case ListType: { - Handle subArray = Handle::Cast(val); + Local subArray = Local::Cast(val); struct getdns_list* sublist = GNUtil::convertToList(subArray); getdns_dict_set_list(result, *name, sublist); getdns_list_destroy(sublist); @@ -589,7 +589,7 @@ getdns_dict* GNUtil::convertToDict(Handle obj) { break; case DictType: { - Handle subObj = val->ToObject(); + Local subObj = val->ToObject(); struct getdns_dict* subdict = GNUtil::convertToDict(subObj); if (subdict) { getdns_dict_set_dict(result, *name, subdict); diff --git a/src/GNUtil.h b/src/GNUtil.h index 4ccbd0d..9eec5d9 100644 --- a/src/GNUtil.h +++ b/src/GNUtil.h @@ -44,16 +44,16 @@ class GNUtil { static bool attachContextToNode(struct getdns_context* context); // Conversions from getdns -> JS - static Handle convertToJSArray(struct getdns_list* list); - static Handle convertToJSObj(struct getdns_dict* dict); - static Handle convertToBuffer(void* data, size_t size); + static Local convertToJSArray(struct getdns_list* list); + static Local convertToJSObj(struct getdns_dict* dict); + static Local convertToBuffer(void* data, size_t size); // Conversions from JS -> getdns - static struct getdns_list* convertToList(Handle array); - static struct getdns_dict* convertToDict(Handle obj); + static struct getdns_list* convertToList(Local array); + static struct getdns_dict* convertToDict(Local obj); // Helper to determine if an object is a plain dict - static bool isDictionaryObject(Handle obj); + static bool isDictionaryObject(Local obj); private: