39
39
#include " stream_base-inl.h"
40
40
#include " string_bytes.h"
41
41
#include " uv.h"
42
+ #include " v8-fast-api-calls.h"
43
+
44
+ #include < filesystem>
42
45
43
46
#if defined(__MINGW32__) || defined(_MSC_VER)
44
47
# include < io.h>
@@ -52,6 +55,8 @@ using v8::Array;
52
55
using v8::BigInt;
53
56
using v8::Context;
54
57
using v8::EscapableHandleScope;
58
+ using v8::FastApiCallbackOptions;
59
+ using v8::FastOneByteString;
55
60
using v8::Function;
56
61
using v8::FunctionCallbackInfo;
57
62
using v8::FunctionTemplate;
@@ -1038,6 +1043,33 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
1038
1043
args.GetReturnValue ().Set (rc);
1039
1044
}
1040
1045
1046
+ static int32_t FastInternalModuleStat (
1047
+ Local<Object> recv,
1048
+ const FastOneByteString& input,
1049
+ // NOLINTNEXTLINE(runtime/references) This is V8 api.
1050
+ FastApiCallbackOptions& options) {
1051
+ Environment* env = Environment::GetCurrent (recv->GetCreationContextChecked ());
1052
+
1053
+ auto path = std::filesystem::path (input.data , input.data + input.length );
1054
+ if (UNLIKELY (!env->permission ()->is_granted (
1055
+ env, permission::PermissionScope::kFileSystemRead , path.string ()))) {
1056
+ options.fallback = true ;
1057
+ return -1 ;
1058
+ }
1059
+
1060
+ switch (std::filesystem::status (path).type ()) {
1061
+ case std::filesystem::file_type::directory:
1062
+ return 1 ;
1063
+ case std::filesystem::file_type::regular:
1064
+ return 0 ;
1065
+ default :
1066
+ return -1 ;
1067
+ }
1068
+ }
1069
+
1070
+ v8::CFunction fast_internal_module_stat_ (
1071
+ v8::CFunction::Make (FastInternalModuleStat));
1072
+
1041
1073
constexpr bool is_uv_error_except_no_entry (int result) {
1042
1074
return result < 0 && result != UV_ENOENT;
1043
1075
}
@@ -3261,7 +3293,11 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
3261
3293
SetMethod (isolate, target, " rmdir" , RMDir);
3262
3294
SetMethod (isolate, target, " mkdir" , MKDir);
3263
3295
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_);
3265
3301
SetMethod (isolate, target, " stat" , Stat);
3266
3302
SetMethod (isolate, target, " lstat" , LStat);
3267
3303
SetMethod (isolate, target, " fstat" , FStat);
@@ -3382,6 +3418,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
3382
3418
registry->Register (MKDir);
3383
3419
registry->Register (ReadDir);
3384
3420
registry->Register (InternalModuleStat);
3421
+ registry->Register (FastInternalModuleStat);
3422
+ registry->Register (fast_internal_module_stat_.GetTypeInfo ());
3385
3423
registry->Register (Stat);
3386
3424
registry->Register (LStat);
3387
3425
registry->Register (FStat);
0 commit comments