Skip to content

Commit ccaa3d5

Browse files
committed
src: add V8 fast api to guessHandleType
1 parent c474cde commit ccaa3d5

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Diff for: src/node_external_reference.h

+3
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) \

Diff for: src/node_util.cc

+42-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "node_errors.h"
44
#include "node_external_reference.h"
55
#include "util-inl.h"
6+
#include "util.h"
7+
#include "v8-fast-api-calls.h"
68

79
namespace node {
810
namespace util {
@@ -12,6 +14,7 @@ using v8::Array;
1214
using v8::ArrayBufferView;
1315
using v8::BigInt;
1416
using v8::Boolean;
17+
using v8::CFunction;
1518
using v8::Context;
1619
using v8::External;
1720
using v8::FunctionCallbackInfo;
@@ -317,6 +320,38 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
317320
args.GetReturnValue().Set(type);
318321
}
319322

323+
static uint32_t FastGuessHandleType(Local<Value> receiver, const uint32_t fd) {
324+
uv_handle_type t = uv_guess_handle(fd);
325+
uint32_t type{0};
326+
327+
switch (t) {
328+
case UV_TCP:
329+
type = 0;
330+
break;
331+
case UV_TTY:
332+
type = 1;
333+
break;
334+
case UV_UDP:
335+
type = 2;
336+
break;
337+
case UV_FILE:
338+
type = 3;
339+
break;
340+
case UV_NAMED_PIPE:
341+
type = 4;
342+
break;
343+
case UV_UNKNOWN_HANDLE:
344+
type = 5;
345+
break;
346+
default:
347+
ABORT();
348+
}
349+
350+
return type;
351+
}
352+
353+
CFunction fast_guess_handle_type_(CFunction::Make(FastGuessHandleType));
354+
320355
static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
321356
Environment* env = Environment::GetCurrent(args);
322357
CHECK_GE(args.Length(), 2);
@@ -366,6 +401,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
366401
registry->Register(WeakReference::IncRef);
367402
registry->Register(WeakReference::DecRef);
368403
registry->Register(GuessHandleType);
404+
registry->Register(FastGuessHandleType);
405+
registry->Register(fast_guess_handle_type_.GetTypeInfo());
369406
registry->Register(ToUSVString);
370407
}
371408

@@ -469,7 +506,11 @@ void Initialize(Local<Object> target,
469506
SetProtoMethod(isolate, weak_ref, "decRef", WeakReference::DecRef);
470507
SetConstructorFunction(context, target, "WeakReference", weak_ref);
471508

472-
SetMethod(context, target, "guessHandleType", GuessHandleType);
509+
SetFastMethodNoSideEffect(context,
510+
target,
511+
"guessHandleType",
512+
GuessHandleType,
513+
&fast_guess_handle_type_);
473514

474515
SetMethodNoSideEffect(context, target, "toUSVString", ToUSVString);
475516
}

0 commit comments

Comments
 (0)