@@ -54,6 +54,7 @@ namespace fs {
5454
5555using  v8::Array;
5656using  v8::BigInt;
57+ using  v8::CFunction;
5758using  v8::Context;
5859using  v8::EscapableHandleScope;
5960using  v8::FastApiCallbackOptions;
@@ -304,7 +305,7 @@ FileHandle::TransferData::~TransferData() {
304305
305306BaseObjectPtr<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+ 
9951018static  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