From 734d1baa43941b54f20c918049c7cec46db30dfb Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Fri, 28 Jun 2024 10:58:01 -0400 Subject: [PATCH] fs: add v8 fast api to closeSync --- src/node_external_reference.h | 4 ++++ src/node_file.cc | 26 +++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/node_external_reference.h b/src/node_external_reference.h index b80b8727c23fd1..142ce1bfaaf505 100644 --- a/src/node_external_reference.h +++ b/src/node_external_reference.h @@ -47,6 +47,9 @@ using CFunctionCallbackWithUint8ArrayUint32Int64Bool = uint32_t, int64_t, bool); +using CFunctionWithObjectInt32Fallback = void (*)(v8::Local, + const int32_t input, + v8::FastApiCallbackOptions&); using CFunctionWithUint32 = uint32_t (*)(v8::Local, const uint32_t input); using CFunctionWithDoubleReturnDouble = double (*)(v8::Local, @@ -75,6 +78,7 @@ class ExternalReferenceRegistry { V(CFunctionCallbackWithTwoUint8Arrays) \ V(CFunctionCallbackWithTwoUint8ArraysFallback) \ V(CFunctionCallbackWithUint8ArrayUint32Int64Bool) \ + V(CFunctionWithObjectInt32Fallback) \ V(CFunctionWithUint32) \ V(CFunctionWithDoubleReturnDouble) \ V(CFunctionWithInt64Fallback) \ diff --git a/src/node_file.cc b/src/node_file.cc index 3800a8e5f600ae..33d40c8a5c764d 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -54,6 +54,7 @@ namespace fs { using v8::Array; using v8::BigInt; +using v8::CFunction; using v8::Context; using v8::EscapableHandleScope; using v8::FastApiCallbackOptions; @@ -304,7 +305,7 @@ FileHandle::TransferData::~TransferData() { BaseObjectPtr FileHandle::TransferData::Deserialize( Environment* env, - v8::Local context, + Local context, std::unique_ptr self) { BindingData* bd = Realm::GetBindingData(context); if (bd == nullptr) return {}; @@ -966,7 +967,7 @@ void Access(const FunctionCallbackInfo& args) { } } -void Close(const FunctionCallbackInfo& args) { +static void Close(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); const int argc = args.Length(); @@ -992,6 +993,23 @@ void Close(const FunctionCallbackInfo& args) { } } +static void FastClose(Local recv, + const int32_t fd, + // NOLINTNEXTLINE(runtime/references) This is V8 api. + v8::FastApiCallbackOptions& options) { + Environment* env = Environment::GetCurrent(recv->GetCreationContextChecked()); + + env->RemoveUnmanagedFd(fd); + + uv_fs_t req; + FS_SYNC_TRACE_BEGIN(close); + options.fallback = uv_fs_close(nullptr, &req, fd, nullptr) < 0; + FS_SYNC_TRACE_END(close); + uv_fs_req_cleanup(&req); +} + +CFunction fast_close_ = CFunction::Make(FastClose); + static void ExistsSync(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); @@ -3311,7 +3329,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data, "getFormatOfExtensionlessFile", GetFormatOfExtensionlessFile); SetMethod(isolate, target, "access", Access); - SetMethod(isolate, target, "close", Close); + SetFastMethod(isolate, target, "close", Close, &fast_close_); SetMethod(isolate, target, "existsSync", ExistsSync); SetMethod(isolate, target, "open", Open); SetMethod(isolate, target, "openFileHandle", OpenFileHandle); @@ -3436,6 +3454,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(GetFormatOfExtensionlessFile); registry->Register(Close); + registry->Register(FastClose); + registry->Register(fast_close_.GetTypeInfo()); registry->Register(ExistsSync); registry->Register(Open); registry->Register(OpenFileHandle);