38
38
#include " req_wrap-inl.h"
39
39
#include " stream_base-inl.h"
40
40
#include " string_bytes.h"
41
+ #include " v8-fast-api-calls.h"
41
42
42
43
#if defined(__MINGW32__) || defined(_MSC_VER)
43
44
# include < io.h>
@@ -51,6 +52,8 @@ using v8::Array;
51
52
using v8::BigInt;
52
53
using v8::Context;
53
54
using v8::EscapableHandleScope;
55
+ using v8::FastApiCallbackOptions;
56
+ using v8::FastOneByteString;
54
57
using v8::Function;
55
58
using v8::FunctionCallbackInfo;
56
59
using v8::FunctionTemplate;
@@ -1051,6 +1054,34 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
1051
1054
args.GetReturnValue ().Set (rc);
1052
1055
}
1053
1056
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
+
1054
1085
constexpr bool is_uv_error_except_no_entry (int result) {
1055
1086
return result < 0 && result != UV_ENOENT;
1056
1087
}
@@ -3145,7 +3176,11 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
3145
3176
SetMethod (isolate, target, " rmdir" , RMDir);
3146
3177
SetMethod (isolate, target, " mkdir" , MKDir);
3147
3178
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_);
3149
3184
SetMethod (isolate, target, " stat" , Stat);
3150
3185
SetMethod (isolate, target, " lstat" , LStat);
3151
3186
SetMethod (isolate, target, " fstat" , FStat);
@@ -3266,6 +3301,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
3266
3301
registry->Register (MKDir);
3267
3302
registry->Register (ReadDir);
3268
3303
registry->Register (InternalModuleStat);
3304
+ registry->Register (FastInternalModuleStat);
3305
+ registry->Register (fast_internal_module_stat_.GetTypeInfo ());
3269
3306
registry->Register (Stat);
3270
3307
registry->Register (LStat);
3271
3308
registry->Register (FStat);
0 commit comments