Skip to content

Commit

Permalink
src: add V8 fast api to guessHandleType
Browse files Browse the repository at this point in the history
PR-URL: #48349
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
anonrig authored and RafaelGSS committed Jul 3, 2023
1 parent 954e46e commit 4971e46
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> receiver,
bool);
using CFunctionCallbackWithStrings =
bool (*)(v8::Local<v8::Value>, const v8::FastOneByteString& input);
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
const uint32_t input);

// This class manages the external references from the V8 heap
// to the C++ addresses in Node.js.
Expand All @@ -35,6 +37,7 @@ class ExternalReferenceRegistry {
V(CFunctionCallbackWithInt64) \
V(CFunctionCallbackWithBool) \
V(CFunctionCallbackWithStrings) \
V(CFunctionWithUint32) \
V(const v8::CFunctionInfo*) \
V(v8::FunctionCallback) \
V(v8::AccessorGetterCallback) \
Expand Down
42 changes: 41 additions & 1 deletion src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -322,6 +324,38 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(type);
}

static uint32_t FastGuessHandleType(Local<Value> 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<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_GE(args.Length(), 2);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -474,7 +510,11 @@ void Initialize(Local<Object> 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);
}
Expand Down

0 comments on commit 4971e46

Please sign in to comment.