Skip to content

Commit ba92de4

Browse files
committed
fold in unmerged postwait#15 (node14) from upstream
1 parent 791d327 commit ba92de4

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{ "name" : "iptrie"
22
, "description" : "IP tries (prefix tree)"
33
, "keywords" : [ "iptrie", "blocklist", "IP", "acl" ]
4-
, "version" : "0.0.7"
4+
, "version" : "0.0.8"
55
, "author" : { "name" : "Theo Schlossnagle" }
66
, "directories": { "lib": "." }
77
, "repository" :

src/iptrie.cc

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Redistribution and use in source and binary forms, with or without
66
* modification, are permitted provided that the following conditions are
77
* met:
8-
*
8+
*
99
* * Redistributions of source code must retain the above copyright
1010
* notice, this list of conditions and the following disclaimer.
1111
* * Redistributions in binary form must reproduce the above
@@ -16,7 +16,7 @@
1616
* of its contributors may be used to endorse or promote products
1717
* derived from this software without specific prior written
1818
* permission.
19-
*
19+
*
2020
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2121
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2222
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -35,7 +35,6 @@
3535
#include <v8.h>
3636
#include <node.h>
3737
#include <node_object_wrap.h>
38-
#include <assert.h>
3938

4039
using namespace v8;
4140
using namespace node;
@@ -44,22 +43,27 @@ class IPTrie : public ObjectWrap {
4443
public:
4544
static Persistent<FunctionTemplate> s_ct;
4645
static void
47-
Initialize(v8::Handle<v8::Object> target) {
46+
Initialize(v8::Local<v8::Object> target) {
4847
Isolate *isolate = target->GetIsolate();
4948
HandleScope scope(isolate);
5049

5150
Local<FunctionTemplate> t = FunctionTemplate::New(isolate, New);
51+
Local<String> name = CheckedString(isolate, "IPTrie");
5252
s_ct.Reset(isolate, t);
5353
t->InstanceTemplate()->SetInternalFieldCount(3);
54-
t->SetClassName(String::NewFromUtf8(isolate, "IPTrie", String::kInternalizedString));
54+
t->SetClassName(name);
5555

5656
NODE_SET_PROTOTYPE_METHOD(t, "add", Add);
5757
NODE_SET_PROTOTYPE_METHOD(t, "del", Del);
5858
NODE_SET_PROTOTYPE_METHOD(t, "find", Find);
5959

6060

61-
target->Set(String::NewFromUtf8(isolate, "IPTrie", String::kInternalizedString),
62-
t->GetFunction());
61+
target->Set(isolate->GetCurrentContext(), name, t->GetFunction(isolate->GetCurrentContext()).ToLocalChecked())
62+
#ifdef NODE_VERSION_AT_LEAST(14, 0, 0)
63+
.ToChecked();
64+
#else
65+
.Check();
66+
#endif
6367
}
6468

6569
struct obj_baton_t {
@@ -80,7 +84,7 @@ class IPTrie : public ObjectWrap {
8084
drop_tree(&tree6, delete_baton);
8185
}
8286

83-
int Add(const char *ip, int prefix, Handle<Value> dv) {
87+
int Add(const char *ip, int prefix, Local<Value> dv) {
8488
int family, rv;
8589
union {
8690
struct in_addr addr4;
@@ -154,60 +158,68 @@ class IPTrie : public ObjectWrap {
154158
args.GetReturnValue().Set(args.This());
155159
}
156160

161+
static Local <String> CheckedString(Isolate *isolate, const char *str) {
162+
return String::NewFromUtf8(isolate, "IPTrie", v8::NewStringType::kNormal)
163+
#ifdef NODE_VERSION_AT_LEAST(14, 0, 0)
164+
.ToLocalChecked()
165+
#endif
166+
;
167+
}
168+
157169
static void Add(const FunctionCallbackInfo<Value> &args) {
158170
Isolate *isolate = args.GetIsolate();
159171
HandleScope scope(isolate);
160172

161173
if (args.Length() < 1 || !args[0]->IsString()) {
162174
isolate->ThrowException(
163-
Exception::TypeError(
164-
String::NewFromUtf8(isolate, "First argument must be an IP.")));
175+
Exception::TypeError(
176+
CheckedString(isolate, "First argument must be an IP.")));
165177
return;
166178
}
167-
if (args.Length() < 2 || !args[1]->IsNumber()){
179+
if (args.Length() < 2 || !args[1]->IsNumber()) {
168180
isolate->ThrowException(
169181
Exception::TypeError(
170-
String::NewFromUtf8(isolate, "Second argument must be a prefix length")));
182+
CheckedString(isolate, "Second argument must be a prefix length")));
171183
return;
172184
}
173185
if (args.Length() < 3) {
174186
isolate->ThrowException(
175187
Exception::TypeError(
176-
String::NewFromUtf8(isolate, "Third argument must exist")));
188+
CheckedString(isolate, "Third argument must exist")));
177189
return;
178190
}
179191

180-
String::Utf8Value ipaddress(args[0]->ToString());
181-
int prefix_len = args[1]->ToUint32()->Value();
192+
String::Utf8Value ipaddress(isolate, args[0]->ToString(isolate->GetCurrentContext()).ToLocalChecked());
193+
int prefix_len = args[1]->Uint32Value(isolate->GetCurrentContext()).ToChecked();
182194

183195
IPTrie *iptrie = ObjectWrap::Unwrap<IPTrie>(args.This());
184-
Handle<Value> data = args[2];
185-
if(iptrie->Add(*ipaddress, prefix_len, data) == 0) {
196+
Local<Value>data = args[2];
197+
if (iptrie->Add(*ipaddress, prefix_len, data) == 0) {
186198
isolate->ThrowException(
187199
Exception::TypeError(
188-
String::NewFromUtf8(isolate, "Could not parse IP")));
200+
CheckedString(isolate, "Could not parse IP")));
189201
return;
190202
}
191203
}
192204

193-
static void Del(const FunctionCallbackInfo<Value> &args) {
205+
static void Del(const FunctionCallbackInfo <Value> &args) {
194206
Isolate *isolate = args.GetIsolate();
195207

196208
if (args.Length() < 1 || !args[0]->IsString()) {
197209
isolate->ThrowException(
198210
Exception::TypeError(
199-
String::NewFromUtf8(isolate, "First argument must be an IP.")));
211+
CheckedString(isolate, "First argument must be an IP.")));
200212
return;
201213
}
202214
if (args.Length() < 2 || !args[1]->IsNumber()){
203215
isolate->ThrowException(
204-
Exception::TypeError(
205-
String::NewFromUtf8(isolate, "Second argument must be a prefix length")));
216+
Exception::TypeError(
217+
CheckedString(isolate, "Second argument must be a prefix length")));
206218
return;
207219
}
208220

209-
String::Utf8Value ipaddress(args[0]->ToString());
210-
int prefix_len = args[1]->ToUint32()->Value();
221+
String::Utf8Value ipaddress(isolate, args[0]->ToString(isolate->GetCurrentContext()).ToLocalChecked());
222+
int prefix_len = args[1]->Uint32Value(isolate->GetCurrentContext()).ToChecked();
211223

212224
IPTrie *iptrie = ObjectWrap::Unwrap<IPTrie>(args.This());
213225
int success = iptrie->Del(*ipaddress, prefix_len);
@@ -220,17 +232,17 @@ class IPTrie : public ObjectWrap {
220232

221233
if (args.Length() < 1 || !args[0]->IsString()) {
222234
isolate->ThrowException(
223-
Exception::TypeError(
224-
String::NewFromUtf8(isolate, "Required argument: ip address.")));
235+
Exception::TypeError(
236+
CheckedString(isolate, "Required argument: ip address.")));
225237
return;
226238
}
227239

228-
String::Utf8Value ipaddress(args[0]->ToString());
240+
String::Utf8Value ipaddress(isolate, args[0]->ToString(isolate->GetCurrentContext()).ToLocalChecked());
229241

230242
IPTrie *iptrie = ObjectWrap::Unwrap<IPTrie>(args.This());
231243
obj_baton_t *d = iptrie->Find(*ipaddress);
232244
if(d != NULL) {
233-
args.GetReturnValue().Set(d->val);
245+
args.GetReturnValue().Set(d->val.Get(isolate));
234246
}
235247
}
236248

@@ -242,7 +254,7 @@ class IPTrie : public ObjectWrap {
242254
Persistent<FunctionTemplate> IPTrie::s_ct;
243255

244256
extern "C" {
245-
static void init(Handle<Object> target) {
257+
static void init(Local<Object> target) {
246258
IPTrie::Initialize(target);
247259
}
248260
NODE_MODULE(iptrie, init);

0 commit comments

Comments
 (0)