Skip to content

Commit 8354379

Browse files
committed
fs: add v8 fast api to closeSync
1 parent f5ed338 commit 8354379

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/node_external_reference.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ using CFunctionCallbackWithUint8ArrayUint32Int64Bool =
4747
uint32_t,
4848
int64_t,
4949
bool);
50+
using CFunctionWithObjectInt32Fallback = void (*)(v8::Local<v8::Object>,
51+
const int32_t input,
52+
v8::FastApiCallbackOptions&);
5053
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
5154
const uint32_t input);
5255
using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>,
@@ -75,6 +78,7 @@ class ExternalReferenceRegistry {
7578
V(CFunctionCallbackWithTwoUint8Arrays) \
7679
V(CFunctionCallbackWithTwoUint8ArraysFallback) \
7780
V(CFunctionCallbackWithUint8ArrayUint32Int64Bool) \
81+
V(CFunctionWithObjectInt32Fallback) \
7882
V(CFunctionWithUint32) \
7983
V(CFunctionWithDoubleReturnDouble) \
8084
V(CFunctionWithInt64Fallback) \

src/node_file.cc

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace fs {
5454

5555
using v8::Array;
5656
using v8::BigInt;
57+
using v8::CFunction;
5758
using v8::Context;
5859
using v8::EscapableHandleScope;
5960
using v8::FastApiCallbackOptions;
@@ -304,7 +305,7 @@ FileHandle::TransferData::~TransferData() {
304305

305306
BaseObjectPtr<BaseObject> FileHandle::TransferData::Deserialize(
306307
Environment* env,
307-
v8::Local<v8::Context> context,
308+
Local<v8::Context> context,
308309
std::unique_ptr<worker::TransferData> self) {
309310
BindingData* bd = Realm::GetBindingData<BindingData>(context);
310311
if (bd == nullptr) return {};
@@ -966,7 +967,7 @@ void Access(const FunctionCallbackInfo<Value>& args) {
966967
}
967968
}
968969

969-
void Close(const FunctionCallbackInfo<Value>& args) {
970+
static void Close(const FunctionCallbackInfo<Value>& args) {
970971
Environment* env = Environment::GetCurrent(args);
971972

972973
const int argc = args.Length();
@@ -992,6 +993,28 @@ void Close(const FunctionCallbackInfo<Value>& args) {
992993
}
993994
}
994995

996+
static void FastClose(Local<Object> recv,
997+
const int32_t fd,
998+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
999+
v8::FastApiCallbackOptions& options) {
1000+
Environment* env = Environment::GetCurrent(recv->GetCreationContextChecked());
1001+
1002+
uv_fs_t req;
1003+
FS_SYNC_TRACE_BEGIN(close);
1004+
int err = uv_fs_close(nullptr, &req, fd, nullptr) < 0;
1005+
FS_SYNC_TRACE_END(close);
1006+
uv_fs_req_cleanup(&req);
1007+
1008+
if (err < 0) {
1009+
options.fallback = true;
1010+
} else {
1011+
// Only remove unmanaged fds if the close was successful.
1012+
env->RemoveUnmanagedFd(fd);
1013+
}
1014+
}
1015+
1016+
CFunction fast_close_ = CFunction::Make(FastClose);
1017+
9951018
static void ExistsSync(const FunctionCallbackInfo<Value>& args) {
9961019
Environment* env = Environment::GetCurrent(args);
9971020
Isolate* isolate = env->isolate();
@@ -3311,7 +3334,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
33113334
"getFormatOfExtensionlessFile",
33123335
GetFormatOfExtensionlessFile);
33133336
SetMethod(isolate, target, "access", Access);
3314-
SetMethod(isolate, target, "close", Close);
3337+
SetFastMethod(isolate, target, "close", Close, &fast_close_);
33153338
SetMethod(isolate, target, "existsSync", ExistsSync);
33163339
SetMethod(isolate, target, "open", Open);
33173340
SetMethod(isolate, target, "openFileHandle", OpenFileHandle);
@@ -3436,6 +3459,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
34363459

34373460
registry->Register(GetFormatOfExtensionlessFile);
34383461
registry->Register(Close);
3462+
registry->Register(FastClose);
3463+
registry->Register(fast_close_.GetTypeInfo());
34393464
registry->Register(ExistsSync);
34403465
registry->Register(Open);
34413466
registry->Register(OpenFileHandle);

0 commit comments

Comments
 (0)