diff --git a/src/node_external_reference.h b/src/node_external_reference.h index d7bc8ed343fe19..1586b15553d6a2 100644 --- a/src/node_external_reference.h +++ b/src/node_external_reference.h @@ -21,6 +21,8 @@ using CFunctionCallbackWithBool = void (*)(v8::Local receiver, bool); using CFunctionCallbackWithStrings = bool (*)(v8::Local, const v8::FastOneByteString& input); +using CFunctionWithUint32 = uint32_t (*)(v8::Local, + const uint32_t input); // This class manages the external references from the V8 heap // to the C++ addresses in Node.js. @@ -35,6 +37,7 @@ class ExternalReferenceRegistry { V(CFunctionCallbackWithInt64) \ V(CFunctionCallbackWithBool) \ V(CFunctionCallbackWithStrings) \ + V(CFunctionWithUint32) \ V(const v8::CFunctionInfo*) \ V(v8::FunctionCallback) \ V(v8::AccessorGetterCallback) \ diff --git a/src/node_util.cc b/src/node_util.cc index ef78ed99eeb3e2..ea991873b9a033 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -3,6 +3,7 @@ #include "node_errors.h" #include "node_external_reference.h" #include "util-inl.h" +#include "v8-fast-api-calls.h" namespace node { namespace util { @@ -12,6 +13,7 @@ using v8::Array; using v8::ArrayBufferView; using v8::BigInt; using v8::Boolean; +using v8::CFunction; using v8::Context; using v8::External; using v8::FunctionCallbackInfo; @@ -322,6 +324,38 @@ static void GuessHandleType(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(type); } +static uint32_t FastGuessHandleType(Local receiver, const uint32_t fd) { + uv_handle_type t = uv_guess_handle(fd); + uint32_t type{0}; + + switch (t) { + case UV_TCP: + type = 0; + break; + case UV_TTY: + type = 1; + break; + case UV_UDP: + type = 2; + break; + case UV_FILE: + type = 3; + break; + case UV_NAMED_PIPE: + type = 4; + break; + case UV_UNKNOWN_HANDLE: + type = 5; + break; + default: + ABORT(); + } + + return type; +} + +CFunction fast_guess_handle_type_(CFunction::Make(FastGuessHandleType)); + static void ToUSVString(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); CHECK_GE(args.Length(), 2); @@ -371,6 +405,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(WeakReference::IncRef); registry->Register(WeakReference::DecRef); registry->Register(GuessHandleType); + registry->Register(FastGuessHandleType); + registry->Register(fast_guess_handle_type_.GetTypeInfo()); registry->Register(ToUSVString); } @@ -474,7 +510,11 @@ void Initialize(Local target, SetProtoMethod(isolate, weak_ref, "decRef", WeakReference::DecRef); SetConstructorFunction(context, target, "WeakReference", weak_ref); - SetMethod(context, target, "guessHandleType", GuessHandleType); + SetFastMethodNoSideEffect(context, + target, + "guessHandleType", + GuessHandleType, + &fast_guess_handle_type_); SetMethodNoSideEffect(context, target, "toUSVString", ToUSVString); }