3838#include " req_wrap-inl.h"
3939#include " stream_base-inl.h"
4040#include " string_bytes.h"
41+ #include " v8-fast-api-calls.h"
4142
4243#if defined(__MINGW32__) || defined(_MSC_VER)
4344# include < io.h>
@@ -51,6 +52,8 @@ using v8::Array;
5152using v8::BigInt;
5253using v8::Context;
5354using v8::EscapableHandleScope;
55+ using v8::FastApiCallbackOptions;
56+ using v8::FastOneByteString;
5457using v8::Function;
5558using v8::FunctionCallbackInfo;
5659using v8::FunctionTemplate;
@@ -1051,6 +1054,34 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
10511054 args.GetReturnValue ().Set (rc);
10521055}
10531056
1057+ static int32_t FastInternalModuleStat (
1058+ Local<Object> recv,
1059+ const FastOneByteString& input,
1060+ // NOLINTNEXTLINE(runtime/references) This is V8 api.
1061+ FastApiCallbackOptions& options) {
1062+ Environment* env = Environment::GetCurrent (recv->GetCreationContextChecked ());
1063+
1064+ std::string_view path (input.data , input.length );
1065+ if (UNLIKELY (!env->permission ()->is_granted (
1066+ permission::PermissionScope::kFileSystemRead , path))) {
1067+ options.fallback = true ;
1068+ return -1 ;
1069+ }
1070+
1071+ uv_fs_t req;
1072+ int rc = uv_fs_stat (env->event_loop (), &req, path.data (), nullptr );
1073+ if (rc == 0 ) {
1074+ const uv_stat_t * const s = static_cast <const uv_stat_t *>(req.ptr );
1075+ rc = !!(s->st_mode & S_IFDIR);
1076+ }
1077+ uv_fs_req_cleanup (&req);
1078+
1079+ return rc;
1080+ }
1081+
1082+ v8::CFunction fast_internal_module_stat_ (
1083+ v8::CFunction::Make (FastInternalModuleStat));
1084+
10541085constexpr bool is_uv_error_except_no_entry (int result) {
10551086 return result < 0 && result != UV_ENOENT;
10561087}
@@ -3145,7 +3176,11 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
31453176 SetMethod (isolate, target, " rmdir" , RMDir);
31463177 SetMethod (isolate, target, " mkdir" , MKDir);
31473178 SetMethod (isolate, target, " readdir" , ReadDir);
3148- SetMethod (isolate, target, " internalModuleStat" , InternalModuleStat);
3179+ SetFastMethod (isolate,
3180+ target,
3181+ " internalModuleStat" ,
3182+ InternalModuleStat,
3183+ &fast_internal_module_stat_);
31493184 SetMethod (isolate, target, " stat" , Stat);
31503185 SetMethod (isolate, target, " lstat" , LStat);
31513186 SetMethod (isolate, target, " fstat" , FStat);
@@ -3266,6 +3301,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
32663301 registry->Register (MKDir);
32673302 registry->Register (ReadDir);
32683303 registry->Register (InternalModuleStat);
3304+ registry->Register (FastInternalModuleStat);
3305+ registry->Register (fast_internal_module_stat_.GetTypeInfo ());
32693306 registry->Register (Stat);
32703307 registry->Register (LStat);
32713308 registry->Register (FStat);
0 commit comments