33#include " node_errors.h"
44#include " node_external_reference.h"
55#include " util-inl.h"
6+ #include " v8-fast-api-calls.h"
67
78namespace node {
89namespace util {
@@ -12,6 +13,7 @@ using v8::Array;
1213using v8::ArrayBufferView;
1314using v8::BigInt;
1415using v8::Boolean;
16+ using v8::CFunction;
1517using v8::Context;
1618using v8::External;
1719using v8::FunctionCallbackInfo;
@@ -322,6 +324,38 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
322324 args.GetReturnValue ().Set (type);
323325}
324326
327+ static uint32_t FastGuessHandleType (Local<Value> receiver, const uint32_t fd) {
328+ uv_handle_type t = uv_guess_handle (fd);
329+ uint32_t type{0 };
330+
331+ switch (t) {
332+ case UV_TCP:
333+ type = 0 ;
334+ break ;
335+ case UV_TTY:
336+ type = 1 ;
337+ break ;
338+ case UV_UDP:
339+ type = 2 ;
340+ break ;
341+ case UV_FILE:
342+ type = 3 ;
343+ break ;
344+ case UV_NAMED_PIPE:
345+ type = 4 ;
346+ break ;
347+ case UV_UNKNOWN_HANDLE:
348+ type = 5 ;
349+ break ;
350+ default :
351+ ABORT ();
352+ }
353+
354+ return type;
355+ }
356+
357+ CFunction fast_guess_handle_type_ (CFunction::Make(FastGuessHandleType));
358+
325359static void ToUSVString (const FunctionCallbackInfo<Value>& args) {
326360 Environment* env = Environment::GetCurrent (args);
327361 CHECK_GE (args.Length (), 2 );
@@ -371,6 +405,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
371405 registry->Register (WeakReference::IncRef);
372406 registry->Register (WeakReference::DecRef);
373407 registry->Register (GuessHandleType);
408+ registry->Register (FastGuessHandleType);
409+ registry->Register (fast_guess_handle_type_.GetTypeInfo ());
374410 registry->Register (ToUSVString);
375411}
376412
@@ -474,7 +510,11 @@ void Initialize(Local<Object> target,
474510 SetProtoMethod (isolate, weak_ref, " decRef" , WeakReference::DecRef);
475511 SetConstructorFunction (context, target, " WeakReference" , weak_ref);
476512
477- SetMethod (context, target, " guessHandleType" , GuessHandleType);
513+ SetFastMethodNoSideEffect (context,
514+ target,
515+ " guessHandleType" ,
516+ GuessHandleType,
517+ &fast_guess_handle_type_);
478518
479519 SetMethodNoSideEffect (context, target, " toUSVString" , ToUSVString);
480520}
0 commit comments