5
5
* Redistribution and use in source and binary forms, with or without
6
6
* modification, are permitted provided that the following conditions are
7
7
* met:
8
- *
8
+ *
9
9
* * Redistributions of source code must retain the above copyright
10
10
* notice, this list of conditions and the following disclaimer.
11
11
* * Redistributions in binary form must reproduce the above
16
16
* of its contributors may be used to endorse or promote products
17
17
* derived from this software without specific prior written
18
18
* permission.
19
- *
19
+ *
20
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
21
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
22
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35
35
#include < v8.h>
36
36
#include < node.h>
37
37
#include < node_object_wrap.h>
38
- #include < assert.h>
39
38
40
39
using namespace v8 ;
41
40
using namespace node ;
@@ -44,22 +43,27 @@ class IPTrie : public ObjectWrap {
44
43
public:
45
44
static Persistent<FunctionTemplate> s_ct;
46
45
static void
47
- Initialize (v8::Handle <v8::Object> target) {
46
+ Initialize (v8::Local <v8::Object> target) {
48
47
Isolate *isolate = target->GetIsolate ();
49
48
HandleScope scope (isolate);
50
49
51
50
Local<FunctionTemplate> t = FunctionTemplate::New (isolate, New);
51
+ Local<String> name = CheckedString (isolate, " IPTrie" );
52
52
s_ct.Reset (isolate, t);
53
53
t->InstanceTemplate ()->SetInternalFieldCount (3 );
54
- t->SetClassName (String::NewFromUtf8 (isolate, " IPTrie " , String:: kInternalizedString ) );
54
+ t->SetClassName (name );
55
55
56
56
NODE_SET_PROTOTYPE_METHOD (t, " add" , Add);
57
57
NODE_SET_PROTOTYPE_METHOD (t, " del" , Del);
58
58
NODE_SET_PROTOTYPE_METHOD (t, " find" , Find);
59
59
60
60
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
63
67
}
64
68
65
69
struct obj_baton_t {
@@ -80,7 +84,7 @@ class IPTrie : public ObjectWrap {
80
84
drop_tree (&tree6, delete_baton);
81
85
}
82
86
83
- int Add (const char *ip, int prefix, Handle <Value> dv) {
87
+ int Add (const char *ip, int prefix, Local <Value> dv) {
84
88
int family, rv;
85
89
union {
86
90
struct in_addr addr4;
@@ -154,60 +158,68 @@ class IPTrie : public ObjectWrap {
154
158
args.GetReturnValue ().Set (args.This ());
155
159
}
156
160
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
+
157
169
static void Add (const FunctionCallbackInfo<Value> &args) {
158
170
Isolate *isolate = args.GetIsolate ();
159
171
HandleScope scope (isolate);
160
172
161
173
if (args.Length () < 1 || !args[0 ]->IsString ()) {
162
174
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." )));
165
177
return ;
166
178
}
167
- if (args.Length () < 2 || !args[1 ]->IsNumber ()){
179
+ if (args.Length () < 2 || !args[1 ]->IsNumber ()) {
168
180
isolate->ThrowException (
169
181
Exception::TypeError (
170
- String::NewFromUtf8 (isolate, " Second argument must be a prefix length" )));
182
+ CheckedString (isolate, " Second argument must be a prefix length" )));
171
183
return ;
172
184
}
173
185
if (args.Length () < 3 ) {
174
186
isolate->ThrowException (
175
187
Exception::TypeError (
176
- String::NewFromUtf8 (isolate, " Third argument must exist" )));
188
+ CheckedString (isolate, " Third argument must exist" )));
177
189
return ;
178
190
}
179
191
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 ();
182
194
183
195
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 ) {
186
198
isolate->ThrowException (
187
199
Exception::TypeError (
188
- String::NewFromUtf8 (isolate, " Could not parse IP" )));
200
+ CheckedString (isolate, " Could not parse IP" )));
189
201
return ;
190
202
}
191
203
}
192
204
193
- static void Del (const FunctionCallbackInfo<Value> &args) {
205
+ static void Del (const FunctionCallbackInfo <Value> &args) {
194
206
Isolate *isolate = args.GetIsolate ();
195
207
196
208
if (args.Length () < 1 || !args[0 ]->IsString ()) {
197
209
isolate->ThrowException (
198
210
Exception::TypeError (
199
- String::NewFromUtf8 (isolate, " First argument must be an IP." )));
211
+ CheckedString (isolate, " First argument must be an IP." )));
200
212
return ;
201
213
}
202
214
if (args.Length () < 2 || !args[1 ]->IsNumber ()){
203
215
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" )));
206
218
return ;
207
219
}
208
220
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 ();
211
223
212
224
IPTrie *iptrie = ObjectWrap::Unwrap<IPTrie>(args.This ());
213
225
int success = iptrie->Del (*ipaddress, prefix_len);
@@ -220,17 +232,17 @@ class IPTrie : public ObjectWrap {
220
232
221
233
if (args.Length () < 1 || !args[0 ]->IsString ()) {
222
234
isolate->ThrowException (
223
- Exception::TypeError (
224
- String::NewFromUtf8 (isolate, " Required argument: ip address." )));
235
+ Exception::TypeError (
236
+ CheckedString (isolate, " Required argument: ip address." )));
225
237
return ;
226
238
}
227
239
228
- String::Utf8Value ipaddress (args[0 ]->ToString ());
240
+ String::Utf8Value ipaddress (isolate, args[0 ]->ToString (isolate-> GetCurrentContext ()). ToLocalChecked ());
229
241
230
242
IPTrie *iptrie = ObjectWrap::Unwrap<IPTrie>(args.This ());
231
243
obj_baton_t *d = iptrie->Find (*ipaddress);
232
244
if (d != NULL ) {
233
- args.GetReturnValue ().Set (d->val );
245
+ args.GetReturnValue ().Set (d->val . Get (isolate) );
234
246
}
235
247
}
236
248
@@ -242,7 +254,7 @@ class IPTrie : public ObjectWrap {
242
254
Persistent<FunctionTemplate> IPTrie::s_ct;
243
255
244
256
extern " C" {
245
- static void init (Handle <Object> target) {
257
+ static void init (Local <Object> target) {
246
258
IPTrie::Initialize (target);
247
259
}
248
260
NODE_MODULE (iptrie, init);
0 commit comments