Skip to content

Commit

Permalink
fs: add v8 fast api to closeSync
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jun 28, 2024
1 parent f5ed338 commit 734d1ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ using CFunctionCallbackWithUint8ArrayUint32Int64Bool =
uint32_t,
int64_t,
bool);
using CFunctionWithObjectInt32Fallback = void (*)(v8::Local<v8::Object>,
const int32_t input,
v8::FastApiCallbackOptions&);
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
const uint32_t input);
using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>,
Expand Down Expand Up @@ -75,6 +78,7 @@ class ExternalReferenceRegistry {
V(CFunctionCallbackWithTwoUint8Arrays) \
V(CFunctionCallbackWithTwoUint8ArraysFallback) \
V(CFunctionCallbackWithUint8ArrayUint32Int64Bool) \
V(CFunctionWithObjectInt32Fallback) \
V(CFunctionWithUint32) \
V(CFunctionWithDoubleReturnDouble) \
V(CFunctionWithInt64Fallback) \
Expand Down
26 changes: 23 additions & 3 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace fs {

using v8::Array;
using v8::BigInt;
using v8::CFunction;
using v8::Context;
using v8::EscapableHandleScope;
using v8::FastApiCallbackOptions;
Expand Down Expand Up @@ -304,7 +305,7 @@ FileHandle::TransferData::~TransferData() {

BaseObjectPtr<BaseObject> FileHandle::TransferData::Deserialize(
Environment* env,
v8::Local<v8::Context> context,
Local<v8::Context> context,
std::unique_ptr<worker::TransferData> self) {
BindingData* bd = Realm::GetBindingData<BindingData>(context);
if (bd == nullptr) return {};
Expand Down Expand Up @@ -966,7 +967,7 @@ void Access(const FunctionCallbackInfo<Value>& args) {
}
}

void Close(const FunctionCallbackInfo<Value>& args) {
static void Close(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

const int argc = args.Length();
Expand All @@ -992,6 +993,23 @@ void Close(const FunctionCallbackInfo<Value>& args) {
}
}

static void FastClose(Local<Object> 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<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 734d1ba

Please sign in to comment.