diff --git a/package.json b/package.json index a7c5114..d7abcfd 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name" : "iptrie" , "description" : "IP tries (prefix tree)" , "keywords" : [ "iptrie", "blocklist", "IP", "acl" ] -, "version" : "0.0.7" +, "version" : "0.0.8" , "author" : { "name" : "Theo Schlossnagle" } , "directories": { "lib": "." } , "repository" : diff --git a/src/iptrie.cc b/src/iptrie.cc index b44e918..014bf39 100644 --- a/src/iptrie.cc +++ b/src/iptrie.cc @@ -5,7 +5,7 @@ * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above @@ -16,7 +16,7 @@ * of its contributors may be used to endorse or promote products * derived from this software without specific prior written * permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -35,7 +35,6 @@ #include #include #include -#include using namespace v8; using namespace node; @@ -44,22 +43,27 @@ class IPTrie : public ObjectWrap { public: static Persistent s_ct; static void - Initialize(v8::Handle target) { + Initialize(v8::Local target) { Isolate *isolate = target->GetIsolate(); HandleScope scope(isolate); Local t = FunctionTemplate::New(isolate, New); + Local name = CheckedString(isolate, "IPTrie"); s_ct.Reset(isolate, t); t->InstanceTemplate()->SetInternalFieldCount(3); - t->SetClassName(String::NewFromUtf8(isolate, "IPTrie", String::kInternalizedString)); + t->SetClassName(name); NODE_SET_PROTOTYPE_METHOD(t, "add", Add); NODE_SET_PROTOTYPE_METHOD(t, "del", Del); NODE_SET_PROTOTYPE_METHOD(t, "find", Find); - target->Set(String::NewFromUtf8(isolate, "IPTrie", String::kInternalizedString), - t->GetFunction()); + target->Set(isolate->GetCurrentContext(), name, t->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()) +#ifdef NODE_VERSION_AT_LEAST(14, 0, 0) + .ToChecked(); +#else + .Check(); +#endif } struct obj_baton_t { @@ -80,7 +84,7 @@ class IPTrie : public ObjectWrap { drop_tree(&tree6, delete_baton); } - int Add(const char *ip, int prefix, Handle dv) { + int Add(const char *ip, int prefix, Local dv) { int family, rv; union { struct in_addr addr4; @@ -154,60 +158,68 @@ class IPTrie : public ObjectWrap { args.GetReturnValue().Set(args.This()); } + static Local CheckedString(Isolate *isolate, const char *str) { + return String::NewFromUtf8(isolate, "IPTrie", v8::NewStringType::kNormal) +#ifdef NODE_VERSION_AT_LEAST(14, 0, 0) + .ToLocalChecked() +#endif + ; + } + static void Add(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); HandleScope scope(isolate); if (args.Length() < 1 || !args[0]->IsString()) { isolate->ThrowException( - Exception::TypeError( - String::NewFromUtf8(isolate, "First argument must be an IP."))); + Exception::TypeError( + CheckedString(isolate, "First argument must be an IP."))); return; } - if (args.Length() < 2 || !args[1]->IsNumber()){ + if (args.Length() < 2 || !args[1]->IsNumber()) { isolate->ThrowException( Exception::TypeError( - String::NewFromUtf8(isolate, "Second argument must be a prefix length"))); + CheckedString(isolate, "Second argument must be a prefix length"))); return; } if (args.Length() < 3) { isolate->ThrowException( Exception::TypeError( - String::NewFromUtf8(isolate, "Third argument must exist"))); + CheckedString(isolate, "Third argument must exist"))); return; } - String::Utf8Value ipaddress(args[0]->ToString()); - int prefix_len = args[1]->ToUint32()->Value(); + String::Utf8Value ipaddress(isolate, args[0]->ToString(isolate->GetCurrentContext()).ToLocalChecked()); + int prefix_len = args[1]->Uint32Value(isolate->GetCurrentContext()).ToChecked(); IPTrie *iptrie = ObjectWrap::Unwrap(args.This()); - Handle data = args[2]; - if(iptrie->Add(*ipaddress, prefix_len, data) == 0) { + Localdata = args[2]; + if (iptrie->Add(*ipaddress, prefix_len, data) == 0) { isolate->ThrowException( Exception::TypeError( - String::NewFromUtf8(isolate, "Could not parse IP"))); + CheckedString(isolate, "Could not parse IP"))); return; } } - static void Del(const FunctionCallbackInfo &args) { + static void Del(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); if (args.Length() < 1 || !args[0]->IsString()) { isolate->ThrowException( Exception::TypeError( - String::NewFromUtf8(isolate, "First argument must be an IP."))); + CheckedString(isolate, "First argument must be an IP."))); return; } if (args.Length() < 2 || !args[1]->IsNumber()){ isolate->ThrowException( - Exception::TypeError( - String::NewFromUtf8(isolate, "Second argument must be a prefix length"))); + Exception::TypeError( + CheckedString(isolate, "Second argument must be a prefix length"))); return; } - String::Utf8Value ipaddress(args[0]->ToString()); - int prefix_len = args[1]->ToUint32()->Value(); + String::Utf8Value ipaddress(isolate, args[0]->ToString(isolate->GetCurrentContext()).ToLocalChecked()); + int prefix_len = args[1]->Uint32Value(isolate->GetCurrentContext()).ToChecked(); IPTrie *iptrie = ObjectWrap::Unwrap(args.This()); int success = iptrie->Del(*ipaddress, prefix_len); @@ -220,17 +232,17 @@ class IPTrie : public ObjectWrap { if (args.Length() < 1 || !args[0]->IsString()) { isolate->ThrowException( - Exception::TypeError( - String::NewFromUtf8(isolate, "Required argument: ip address."))); + Exception::TypeError( + CheckedString(isolate, "Required argument: ip address."))); return; } - String::Utf8Value ipaddress(args[0]->ToString()); + String::Utf8Value ipaddress(isolate, args[0]->ToString(isolate->GetCurrentContext()).ToLocalChecked()); IPTrie *iptrie = ObjectWrap::Unwrap(args.This()); obj_baton_t *d = iptrie->Find(*ipaddress); if(d != NULL) { - args.GetReturnValue().Set(d->val); + args.GetReturnValue().Set(d->val.Get(isolate)); } } @@ -242,7 +254,7 @@ class IPTrie : public ObjectWrap { Persistent IPTrie::s_ct; extern "C" { -static void init(Handle target) { +static void init(Local target) { IPTrie::Initialize(target); } NODE_MODULE(iptrie, init);