Skip to content

Commit 4971e46

Browse files
anonrigRafaelGSS
authored andcommitted
src: add V8 fast api to guessHandleType
PR-URL: #48349 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 954e46e commit 4971e46

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/node_external_reference.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> receiver,
2121
bool);
2222
using CFunctionCallbackWithStrings =
2323
bool (*)(v8::Local<v8::Value>, const v8::FastOneByteString& input);
24+
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
25+
const uint32_t input);
2426

2527
// This class manages the external references from the V8 heap
2628
// to the C++ addresses in Node.js.
@@ -35,6 +37,7 @@ class ExternalReferenceRegistry {
3537
V(CFunctionCallbackWithInt64) \
3638
V(CFunctionCallbackWithBool) \
3739
V(CFunctionCallbackWithStrings) \
40+
V(CFunctionWithUint32) \
3841
V(const v8::CFunctionInfo*) \
3942
V(v8::FunctionCallback) \
4043
V(v8::AccessorGetterCallback) \

src/node_util.cc

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "node_errors.h"
44
#include "node_external_reference.h"
55
#include "util-inl.h"
6+
#include "v8-fast-api-calls.h"
67

78
namespace node {
89
namespace util {
@@ -12,6 +13,7 @@ using v8::Array;
1213
using v8::ArrayBufferView;
1314
using v8::BigInt;
1415
using v8::Boolean;
16+
using v8::CFunction;
1517
using v8::Context;
1618
using v8::External;
1719
using 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+
325359
static 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

Comments
 (0)