Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: introduce V8 fast api to guessHandleType #48349

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const {
_createSocketHandle,
newHandle,
} = require('internal/dgram');
const { guessHandleType } = internalBinding('util');
const {
ERR_BUFFER_OUT_OF_BOUNDS,
ERR_INVALID_ARG_TYPE,
Expand All @@ -59,7 +58,7 @@ const {
validatePort,
} = require('internal/validators');
const { Buffer } = require('buffer');
const { deprecate } = require('internal/util');
const { deprecate, guessHandleType } = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
const EventEmitter = require('events');
const {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/bootstrap/switches/is_main_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ process.on('removeListener', stopListeningIfSignal);
// ---- keep the attachment of the wrappers above so that it's easier to ----
// ---- compare the setups side-by-side -----

const { guessHandleType } = internalBinding('util');
const { guessHandleType } = require('internal/util');

function createWritableStdioStream(fd) {
let stream;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {

const { codes } = require('internal/errors');
const { UDP } = internalBinding('udp_wrap');
const { guessHandleType } = internalBinding('util');
const { guessHandleType } = require('internal/util');
const {
isInt32,
validateFunction,
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const {
const { signals } = internalBinding('constants').os;
const {
isArrayBufferDetached: _isArrayBufferDetached,
guessHandleType: _guessHandleType,
privateSymbols: {
arrow_message_private_symbol,
decorated_private_symbol,
Expand Down Expand Up @@ -789,6 +790,13 @@ function setupCoverageHooks(dir) {
return coverageDirectory;
}


const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
function guessHandleType(fd) {
const type = _guessHandleType(fd);
return handleTypes[type];
}

module.exports = {
getLazy,
assertCrypto,
Expand All @@ -812,6 +820,7 @@ module.exports = {
getInternalGlobal,
getSystemErrorMap,
getSystemErrorName,
guessHandleType,
isArrayBufferDetached,
isError,
isInsideNodeModules,
Expand Down
3 changes: 1 addition & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const {
} = internalBinding('uv');

const { Buffer } = require('buffer');
const { guessHandleType } = internalBinding('util');
const { ShutdownWrap } = internalBinding('stream_wrap');
const {
TCP,
Expand Down Expand Up @@ -111,7 +110,7 @@ const {
} = require('internal/errors');
const { isUint8Array } = require('internal/util/types');
const { queueMicrotask } = require('internal/process/task_queues');
const { kEmptyObject } = require('internal/util');
const { kEmptyObject, guessHandleType } = require('internal/util');
const {
validateAbortSignal,
validateBoolean,
Expand Down
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
63 changes: 54 additions & 9 deletions 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 @@ -289,34 +291,71 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(fd, 0);

uv_handle_type t = uv_guess_handle(fd);
const char* type = nullptr;
// TODO(anonrig): We can use an enum here and then create the array in the
// binding, which will remove the hard-coding in C++ and JS land.
uint32_t type{0};
anonrig marked this conversation as resolved.
Show resolved Hide resolved

// Currently, the return type of this function corresponds to the index of the
// array defined in the JS land. This is done as an optimization to reduce the
// string serialization overhead.
switch (t) {
case UV_TCP:
type = "TCP";
type = 0;
anonrig marked this conversation as resolved.
Show resolved Hide resolved
break;
case UV_TTY:
type = "TTY";
type = 1;
break;
case UV_UDP:
type = "UDP";
type = 2;
break;
case UV_FILE:
type = "FILE";
type = 3;
break;
case UV_NAMED_PIPE:
type = "PIPE";
type = 4;
break;
case UV_UNKNOWN_HANDLE:
type = "UNKNOWN";
type = 5;
break;
default:
ABORT();
}

args.GetReturnValue().Set(OneByteString(env->isolate(), type));
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 @@ -366,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 @@ -469,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