@@ -213,28 +213,28 @@ static void GetLoadAvg(const FunctionCallbackInfo<Value>& args) {
213213
214214static void GetInterfaceAddresses (const FunctionCallbackInfo<Value>& args) {
215215 Environment* env = Environment::GetCurrent (args);
216+ Isolate* isolate = env->isolate ();
216217 uv_interface_address_t * interfaces;
217218 int count, i;
218219 char ip[INET6_ADDRSTRLEN];
219220 char netmask[INET6_ADDRSTRLEN];
220221 std::array<char , 18 > mac;
221- Local<Object> ret, o;
222222 Local<String> name, family;
223- Local<Array> ifarr;
224223
225224 int err = uv_interface_addresses (&interfaces, &count);
226225
227- ret = Object::New (env->isolate ());
226+ if (err == UV_ENOSYS)
227+ return args.GetReturnValue ().SetUndefined ();
228228
229- if (err == UV_ENOSYS) {
230- return args.GetReturnValue ().Set (ret);
231- } else if (err) {
229+ if (err) {
232230 CHECK_GE (args.Length (), 1 );
233231 env->CollectUVExceptionInfo (args[args.Length () - 1 ], errno,
234232 " uv_interface_addresses" );
235233 return args.GetReturnValue ().SetUndefined ();
236234 }
237235
236+ Local<Value> no_scope_id = Integer::New (isolate, -1 );
237+ std::vector<Local<Value>> result (count * 7 );
238238 for (i = 0 ; i < count; i++) {
239239 const char * const raw_name = interfaces[i].name ;
240240
@@ -243,17 +243,9 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
243243 // to assume UTF8 as the default as well. It’s what people will expect if
244244 // they name the interface from any input that uses UTF-8, which should be
245245 // the most frequent case by far these days.)
246- name = String::NewFromUtf8 (env-> isolate () , raw_name,
246+ name = String::NewFromUtf8 (isolate, raw_name,
247247 v8::NewStringType::kNormal ).ToLocalChecked ();
248248
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-
257249 snprintf (mac.data (),
258250 mac.size (),
259251 " %02x:%02x:%02x:%02x:%02x:%02x" ,
@@ -277,34 +269,23 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
277269 family = env->unknown_string ();
278270 }
279271
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);
293279 if (interfaces[i].address .address4 .sin_family == AF_INET6) {
294280 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;
297284 }
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 ();
304285 }
305286
306287 uv_free_interface_addresses (interfaces, count);
307- args.GetReturnValue ().Set (ret );
288+ args.GetReturnValue ().Set (Array::New (isolate, result. data (), result. size ()) );
308289}
309290
310291
0 commit comments