Skip to content

Commit 7074cd9

Browse files
committed
fixup! fs: move getValidMode to c++ for better performance
1 parent 354e343 commit 7074cd9

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

src/node_file.cc

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ using v8::HandleScope;
6666
using v8::Int32;
6767
using v8::Integer;
6868
using v8::Isolate;
69+
using v8::Just;
6970
using v8::JustVoid;
7071
using v8::Local;
7172
using v8::Maybe;
@@ -89,6 +90,13 @@ constexpr char kPathSeparator = '/';
8990
const char* const kPathSeparator = "\\/";
9091
#endif
9192

93+
// F_OK etc. constants
94+
#ifdef _WIN32
95+
#include "uv.h"
96+
#else
97+
#include <unistd.h>
98+
#endif
99+
92100
// The access modes can be any of F_OK, R_OK, W_OK or X_OK. Some might not be
93101
// available on specific systems. They can be used in combination as well
94102
// (F_OK | R_OK | W_OK | X_OK).
@@ -872,41 +880,39 @@ void FromNamespacedPath(std::string* path) {
872880
#endif
873881
}
874882

875-
static inline int GetValidMode(Environment* env,
876-
Local<Value> mode_v,
877-
std::string_view type) {
883+
static inline Maybe<int> GetValidMode(Environment* env,
884+
Local<Value> mode_v,
885+
uv_fs_type type) {
878886
if (!mode_v->IsInt32() && !mode_v->IsNullOrUndefined()) {
879887
THROW_ERR_INVALID_ARG_TYPE(env, "mode must be int32 or null/undefined");
880-
return -1;
888+
return Nothing<int>();
881889
}
882890

883891
int min = kMinimumAccessMode;
884892
int max = kMaximumAccessMode;
885893
int def = F_OK;
886894

887-
if (type == "copyFile") {
895+
CHECK(type == UV_FS_ACCESS || type == UV_FS_COPYFILE);
896+
897+
if (type == UV_FS_COPYFILE) {
888898
min = kMinimumCopyMode;
889899
max = kMaximumCopyMode;
890900
def = mode_v->IsNullOrUndefined() ? kDefaultCopyMode
891901
: mode_v.As<Int32>()->Value();
892-
} else if (type != "access") {
893-
THROW_ERR_INVALID_ARG_TYPE(
894-
env, "type must be equal to \"copyFile\" or \"access\"");
895-
return -1;
896902
}
897903

898904
if (mode_v->IsNullOrUndefined()) {
899-
return def;
905+
return Just(def);
900906
}
901907

902908
const int mode = mode_v.As<Int32>()->Value();
903909
if (mode < min || mode > max) {
904910
THROW_ERR_OUT_OF_RANGE(
905911
env, "mode is out of range: >= %d && <= %d", min, max);
906-
return -1;
912+
return Nothing<int>();
907913
}
908914

909-
return mode;
915+
return Just(mode);
910916
}
911917

912918
void AfterMkdirp(uv_fs_t* req) {
@@ -1028,8 +1034,8 @@ void Access(const FunctionCallbackInfo<Value>& args) {
10281034
const int argc = args.Length();
10291035
CHECK_GE(argc, 2);
10301036

1031-
int mode = GetValidMode(env, args[1], "access");
1032-
if (mode == -1) return;
1037+
Maybe<int> mode = GetValidMode(env, args[1], UV_FS_ACCESS);
1038+
if (mode.IsNothing()) return;
10331039

10341040
BufferValue path(isolate, args[0]);
10351041
CHECK_NOT_NULL(*path);
@@ -1041,12 +1047,20 @@ void Access(const FunctionCallbackInfo<Value>& args) {
10411047
CHECK_NOT_NULL(req_wrap_async);
10421048
FS_ASYNC_TRACE_BEGIN1(
10431049
UV_FS_ACCESS, req_wrap_async, "path", TRACE_STR_COPY(*path))
1044-
AsyncCall(env, req_wrap_async, args, "access", UTF8, AfterNoArgs,
1045-
uv_fs_access, *path, mode);
1050+
AsyncCall(env,
1051+
req_wrap_async,
1052+
args,
1053+
"access",
1054+
UTF8,
1055+
AfterNoArgs,
1056+
uv_fs_access,
1057+
*path,
1058+
mode.FromJust());
10461059
} else { // access(path, mode)
10471060
FSReqWrapSync req_wrap_sync("access", *path);
10481061
FS_SYNC_TRACE_BEGIN(access);
1049-
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_access, *path, mode);
1062+
SyncCallAndThrowOnError(
1063+
env, &req_wrap_sync, uv_fs_access, *path, mode.FromJust());
10501064
FS_SYNC_TRACE_END(access);
10511065
}
10521066
}
@@ -2141,8 +2155,8 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
21412155
const int argc = args.Length();
21422156
CHECK_GE(argc, 3);
21432157

2144-
const int flags = GetValidMode(env, args[2], "copyFile");
2145-
if (flags == -1) return;
2158+
Maybe<int> flags = GetValidMode(env, args[2], UV_FS_COPYFILE);
2159+
if (flags.IsNothing()) return;
21462160

21472161
BufferValue src(isolate, args[0]);
21482162
CHECK_NOT_NULL(*src);
@@ -2162,14 +2176,23 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
21622176
TRACE_STR_COPY(*src),
21632177
"dest",
21642178
TRACE_STR_COPY(*dest))
2165-
AsyncDestCall(env, req_wrap_async, args, "copyfile",
2166-
*dest, dest.length(), UTF8, AfterNoArgs,
2167-
uv_fs_copyfile, *src, *dest, flags);
2179+
AsyncDestCall(env,
2180+
req_wrap_async,
2181+
args,
2182+
"copyfile",
2183+
*dest,
2184+
dest.length(),
2185+
UTF8,
2186+
AfterNoArgs,
2187+
uv_fs_copyfile,
2188+
*src,
2189+
*dest,
2190+
flags.FromJust());
21682191
} else { // copyFile(src, dest, flags)
21692192
FSReqWrapSync req_wrap_sync("copyfile", *src, *dest);
21702193
FS_SYNC_TRACE_BEGIN(copyfile);
21712194
SyncCallAndThrowOnError(
2172-
env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags);
2195+
env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags.FromJust());
21732196
FS_SYNC_TRACE_END(copyfile);
21742197
}
21752198
}

src/node_file.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ enum class FsStatFsOffset {
5252
kFsStatFsFieldsNumber
5353
};
5454

55+
enum class ModeOpType { access = 0, copyFile };
56+
5557
constexpr size_t kFsStatFsBufferLength =
5658
static_cast<size_t>(FsStatFsOffset::kFsStatFsFieldsNumber);
5759

0 commit comments

Comments
 (0)