@@ -304,74 +304,44 @@ void WeakReference::DecRef(const FunctionCallbackInfo<Value>& args) {
304304 v8::Number::New (args.GetIsolate (), weak_ref->reference_count_ ));
305305}
306306
307- static void GuessHandleType (const FunctionCallbackInfo<Value>& args) {
308- Environment* env = Environment::GetCurrent (args);
309- int fd;
310- if (!args[0 ]->Int32Value (env->context ()).To (&fd)) return ;
311- CHECK_GE (fd, 0 );
312-
313- uv_handle_type t = uv_guess_handle (fd);
307+ static uint32_t GetUVHandleTypeCode (const uv_handle_type type) {
314308 // TODO(anonrig): We can use an enum here and then create the array in the
315309 // binding, which will remove the hard-coding in C++ and JS land.
316- uint32_t type{0 };
317310
318311 // Currently, the return type of this function corresponds to the index of the
319312 // array defined in the JS land. This is done as an optimization to reduce the
320313 // string serialization overhead.
321- switch (t ) {
314+ switch (type ) {
322315 case UV_TCP:
323- type = 0 ;
324- break ;
316+ return 0 ;
325317 case UV_TTY:
326- type = 1 ;
327- break ;
318+ return 1 ;
328319 case UV_UDP:
329- type = 2 ;
330- break ;
320+ return 2 ;
331321 case UV_FILE:
332- type = 3 ;
333- break ;
322+ return 3 ;
334323 case UV_NAMED_PIPE:
335- type = 4 ;
336- break ;
324+ return 4 ;
337325 case UV_UNKNOWN_HANDLE:
338- type = 5 ;
339- break ;
326+ return 5 ;
340327 default :
341328 ABORT ();
342329 }
330+ }
331+
332+ static void GuessHandleType (const FunctionCallbackInfo<Value>& args) {
333+ Environment* env = Environment::GetCurrent (args);
334+ int fd;
335+ if (!args[0 ]->Int32Value (env->context ()).To (&fd)) return ;
336+ CHECK_GE (fd, 0 );
343337
344- args.GetReturnValue ().Set (type);
338+ uv_handle_type t = uv_guess_handle (fd);
339+ args.GetReturnValue ().Set (GetUVHandleTypeCode (t));
345340}
346341
347342static uint32_t FastGuessHandleType (Local<Value> receiver, const uint32_t fd) {
348343 uv_handle_type t = uv_guess_handle (fd);
349- uint32_t type{0 };
350-
351- switch (t) {
352- case UV_TCP:
353- type = 0 ;
354- break ;
355- case UV_TTY:
356- type = 1 ;
357- break ;
358- case UV_UDP:
359- type = 2 ;
360- break ;
361- case UV_FILE:
362- type = 3 ;
363- break ;
364- case UV_NAMED_PIPE:
365- type = 4 ;
366- break ;
367- case UV_UNKNOWN_HANDLE:
368- type = 5 ;
369- break ;
370- default :
371- ABORT ();
372- }
373-
374- return type;
344+ return GetUVHandleTypeCode (t);
375345}
376346
377347CFunction fast_guess_handle_type_ (CFunction::Make(FastGuessHandleType));
0 commit comments