3939#include " stream_base-inl.h"
4040#include " string_bytes.h"
4141#include " uv.h"
42+ #include " v8-fast-api-calls.h"
4243
4344#if defined(__MINGW32__) || defined(_MSC_VER)
4445# include < io.h>
@@ -52,6 +53,8 @@ using v8::Array;
5253using v8::BigInt;
5354using v8::Context;
5455using v8::EscapableHandleScope;
56+ using v8::FastApiCallbackOptions;
57+ using v8::FastOneByteString;
5558using v8::Function;
5659using v8::FunctionCallbackInfo;
5760using v8::FunctionTemplate;
@@ -1038,6 +1041,35 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
10381041 args.GetReturnValue ().Set (rc);
10391042}
10401043
1044+ static int32_t FastInternalModuleStat (
1045+ Local<Object> recv,
1046+ const FastOneByteString& input,
1047+ // NOLINTNEXTLINE(runtime/references) This is V8 api.
1048+ FastApiCallbackOptions& options) {
1049+ Environment* env = Environment::GetCurrent (recv->GetCreationContextChecked ());
1050+
1051+ auto path = std::filesystem::path (input.data , input.data + input.length );
1052+ if (UNLIKELY (!env->permission ()->is_granted (
1053+ env, permission::PermissionScope::kFileSystemRead , path.string ()))) {
1054+ options.fallback = true ;
1055+ return -1 ;
1056+ }
1057+
1058+ auto file = std::filesystem::status (path);
1059+ auto file_type = file.type ();
1060+
1061+ if (file_type == std::filesystem::file_type::directory) {
1062+ return 1 ;
1063+ } else if (file_type == std::filesystem::file_type::regular) {
1064+ return 0 ;
1065+ }
1066+
1067+ return -1 ;
1068+ }
1069+
1070+ v8::CFunction fast_internal_module_stat_ (
1071+ v8::CFunction::Make (FastInternalModuleStat));
1072+
10411073constexpr bool is_uv_error_except_no_entry (int result) {
10421074 return result < 0 && result != UV_ENOENT;
10431075}
@@ -3261,7 +3293,11 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
32613293 SetMethod (isolate, target, " rmdir" , RMDir);
32623294 SetMethod (isolate, target, " mkdir" , MKDir);
32633295 SetMethod (isolate, target, " readdir" , ReadDir);
3264- SetMethod (isolate, target, " internalModuleStat" , InternalModuleStat);
3296+ SetFastMethod (isolate,
3297+ target,
3298+ " internalModuleStat" ,
3299+ InternalModuleStat,
3300+ &fast_internal_module_stat_);
32653301 SetMethod (isolate, target, " stat" , Stat);
32663302 SetMethod (isolate, target, " lstat" , LStat);
32673303 SetMethod (isolate, target, " fstat" , FStat);
@@ -3382,6 +3418,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
33823418 registry->Register (MKDir);
33833419 registry->Register (ReadDir);
33843420 registry->Register (InternalModuleStat);
3421+ registry->Register (FastInternalModuleStat);
3422+ registry->Register (fast_internal_module_stat_.GetTypeInfo ());
33853423 registry->Register (Stat);
33863424 registry->Register (LStat);
33873425 registry->Register (FStat);
0 commit comments