@@ -213,28 +213,28 @@ static void GetLoadAvg(const FunctionCallbackInfo<Value>& args) {
213
213
214
214
static void GetInterfaceAddresses (const FunctionCallbackInfo<Value>& args) {
215
215
Environment* env = Environment::GetCurrent (args);
216
+ Isolate* isolate = env->isolate ();
216
217
uv_interface_address_t * interfaces;
217
218
int count, i;
218
219
char ip[INET6_ADDRSTRLEN];
219
220
char netmask[INET6_ADDRSTRLEN];
220
221
std::array<char , 18 > mac;
221
- Local<Object> ret, o;
222
222
Local<String> name, family;
223
- Local<Array> ifarr;
224
223
225
224
int err = uv_interface_addresses (&interfaces, &count);
226
225
227
- ret = Object::New (env->isolate ());
226
+ if (err == UV_ENOSYS)
227
+ return args.GetReturnValue ().SetUndefined ();
228
228
229
- if (err == UV_ENOSYS) {
230
- return args.GetReturnValue ().Set (ret);
231
- } else if (err) {
229
+ if (err) {
232
230
CHECK_GE (args.Length (), 1 );
233
231
env->CollectUVExceptionInfo (args[args.Length () - 1 ], errno,
234
232
" uv_interface_addresses" );
235
233
return args.GetReturnValue ().SetUndefined ();
236
234
}
237
235
236
+ Local<Value> no_scope_id = Integer::New (isolate, -1 );
237
+ std::vector<Local<Value>> result (count * 7 );
238
238
for (i = 0 ; i < count; i++) {
239
239
const char * const raw_name = interfaces[i].name ;
240
240
@@ -243,17 +243,9 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
243
243
// to assume UTF8 as the default as well. It’s what people will expect if
244
244
// they name the interface from any input that uses UTF-8, which should be
245
245
// the most frequent case by far these days.)
246
- name = String::NewFromUtf8 (env-> isolate () , raw_name,
246
+ name = String::NewFromUtf8 (isolate, raw_name,
247
247
v8::NewStringType::kNormal ).ToLocalChecked ();
248
248
249
- if (ret->Has (env->context (), name).FromJust ()) {
250
- ifarr = Local<Array>::Cast (ret->Get (env->context (),
251
- name).ToLocalChecked ());
252
- } else {
253
- ifarr = Array::New (env->isolate ());
254
- ret->Set (env->context (), name, ifarr).FromJust ();
255
- }
256
-
257
249
snprintf (mac.data (),
258
250
mac.size (),
259
251
" %02x:%02x:%02x:%02x:%02x:%02x" ,
@@ -277,34 +269,23 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
277
269
family = env->unknown_string ();
278
270
}
279
271
280
- o = Object::New (env->isolate ());
281
- o->Set (env->context (),
282
- env->address_string (),
283
- OneByteString (env->isolate (), ip)).FromJust ();
284
- o->Set (env->context (),
285
- env->netmask_string (),
286
- OneByteString (env->isolate (), netmask)).FromJust ();
287
- o->Set (env->context (),
288
- env->family_string (), family).FromJust ();
289
- o->Set (env->context (),
290
- env->mac_string (),
291
- FIXED_ONE_BYTE_STRING (env->isolate (), mac)).FromJust ();
292
-
272
+ result[i * 7 ] = name;
273
+ result[i * 7 + 1 ] = OneByteString (isolate, ip);
274
+ result[i * 7 + 2 ] = OneByteString (isolate, netmask);
275
+ result[i * 7 + 3 ] = family;
276
+ result[i * 7 + 4 ] = FIXED_ONE_BYTE_STRING (isolate, mac);
277
+ result[i * 7 + 5 ] =
278
+ interfaces[i].is_internal ? True (isolate) : False (isolate);
293
279
if (interfaces[i].address .address4 .sin_family == AF_INET6) {
294
280
uint32_t scopeid = interfaces[i].address .address6 .sin6_scope_id ;
295
- o->Set (env->context (), env->scopeid_string (),
296
- Integer::NewFromUnsigned (env->isolate (), scopeid)).FromJust ();
281
+ result[i * 7 + 6 ] = Integer::NewFromUnsigned (isolate, scopeid);
282
+ } else {
283
+ result[i * 7 + 6 ] = no_scope_id;
297
284
}
298
-
299
- const bool internal = interfaces[i].is_internal ;
300
- o->Set (env->context (), env->internal_string (),
301
- internal ? True (env->isolate ()) : False (env->isolate ())).FromJust ();
302
-
303
- ifarr->Set (env->context (), ifarr->Length (), o).FromJust ();
304
285
}
305
286
306
287
uv_free_interface_addresses (interfaces, count);
307
- args.GetReturnValue ().Set (ret );
288
+ args.GetReturnValue ().Set (Array::New (isolate, result. data (), result. size ()) );
308
289
}
309
290
310
291
0 commit comments