Skip to content

Commit e1c29f2

Browse files
committed
src: clean up argument assertions in node_file.cc
- Cache `args.Length()` - Use `value.As<T>()` to cast the arguments PR-URL: #18192 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4af1bba commit e1c29f2

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

src/node_file.cc

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ using v8::Function;
8787
using v8::FunctionCallbackInfo;
8888
using v8::FunctionTemplate;
8989
using v8::HandleScope;
90+
using v8::Int32;
9091
using v8::Integer;
9192
using v8::Isolate;
9293
using v8::Local;
@@ -351,14 +352,15 @@ inline FSReqWrap* AsyncCall(Environment* env,
351352
// Template counterpart of SYNC_CALL, except that it only puts
352353
// the error number and the syscall in the context instead of
353354
// creating an error in the C++ land.
355+
// ctx must be checked using value->IsObject() before being passed.
354356
template <typename Func, typename... Args>
355357
inline int SyncCall(Environment* env, Local<Value> ctx, fs_req_wrap* req_wrap,
356358
const char* syscall, Func fn, Args... args) {
357359
env->PrintSyncTrace();
358360
int err = fn(env->event_loop(), &(req_wrap->req), args..., nullptr);
359361
if (err < 0) {
360362
Local<Context> context = env->context();
361-
Local<Object> ctx_obj = ctx->ToObject(context).ToLocalChecked();
363+
Local<Object> ctx_obj = ctx.As<Object>();
362364
Isolate *isolate = env->isolate();
363365
ctx_obj->Set(context,
364366
env->errno_string(),
@@ -391,19 +393,22 @@ inline int SyncCall(Environment* env, Local<Value> ctx, fs_req_wrap* req_wrap,
391393
void Access(const FunctionCallbackInfo<Value>& args) {
392394
Environment* env = Environment::GetCurrent(args.GetIsolate());
393395
HandleScope scope(env->isolate());
394-
Local<Context> context = env->context();
395-
CHECK_GE(args.Length(), 2);
396+
397+
const int argc = args.Length();
398+
CHECK_GE(argc, 2);
399+
396400
CHECK(args[1]->IsInt32());
401+
int mode = args[1].As<Int32>()->Value();
397402

398403
BufferValue path(env->isolate(), args[0]);
399-
int mode = static_cast<int>(args[1]->Int32Value(context).FromJust());
404+
CHECK_NE(*path, nullptr);
400405

401406
if (args[2]->IsObject()) { // access(path, mode, req)
402-
CHECK_EQ(args.Length(), 3);
407+
CHECK_EQ(argc, 3);
403408
AsyncCall(env, args, "access", UTF8, AfterNoArgs,
404409
uv_fs_access, *path, mode);
405410
} else { // access(path, mode, undefined, ctx)
406-
CHECK_EQ(args.Length(), 4);
411+
CHECK_EQ(argc, 4);
407412
fs_req_wrap req_wrap;
408413
SyncCall(env, args[3], &req_wrap, "access", uv_fs_access, *path, mode);
409414
}
@@ -412,20 +417,19 @@ void Access(const FunctionCallbackInfo<Value>& args) {
412417

413418
void Close(const FunctionCallbackInfo<Value>& args) {
414419
Environment* env = Environment::GetCurrent(args);
415-
Local<Context> context = env->context();
416420

417-
int length = args.Length();
418-
CHECK_GE(length, 2);
419-
CHECK(args[0]->IsInt32());
421+
const int argc = args.Length();
422+
CHECK_GE(argc, 2);
420423

421-
int fd = static_cast<int>(args[0]->Int32Value(context).FromJust());
424+
CHECK(args[0]->IsInt32());
425+
int fd = args[0].As<Int32>()->Value();
422426

423427
if (args[1]->IsObject()) { // close(fd, req)
424-
CHECK_EQ(args.Length(), 2);
428+
CHECK_EQ(argc, 2);
425429
AsyncCall(env, args, "close", UTF8, AfterNoArgs,
426430
uv_fs_close, fd);
427431
} else { // close(fd, undefined, ctx)
428-
CHECK_EQ(args.Length(), 3);
432+
CHECK_EQ(argc, 3);
429433
fs_req_wrap req_wrap;
430434
SyncCall(env, args[2], &req_wrap, "close", uv_fs_close, fd);
431435
}
@@ -519,17 +523,18 @@ static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
519523
static void Stat(const FunctionCallbackInfo<Value>& args) {
520524
Environment* env = Environment::GetCurrent(args);
521525

522-
CHECK_GE(args.Length(), 1);
526+
const int argc = args.Length();
527+
CHECK_GE(argc, 1);
523528

524529
BufferValue path(env->isolate(), args[0]);
525530
CHECK_NE(*path, nullptr);
526531

527532
if (args[1]->IsObject()) { // stat(path, req)
528-
CHECK_EQ(args.Length(), 2);
533+
CHECK_EQ(argc, 2);
529534
AsyncCall(env, args, "stat", UTF8, AfterStat,
530535
uv_fs_stat, *path);
531536
} else { // stat(path, undefined, ctx)
532-
CHECK_EQ(args.Length(), 3);
537+
CHECK_EQ(argc, 3);
533538
fs_req_wrap req_wrap;
534539
int err = SyncCall(env, args[2], &req_wrap, "stat", uv_fs_stat, *path);
535540
if (err == 0) {
@@ -542,17 +547,18 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
542547
static void LStat(const FunctionCallbackInfo<Value>& args) {
543548
Environment* env = Environment::GetCurrent(args);
544549

545-
CHECK_GE(args.Length(), 1);
550+
const int argc = args.Length();
551+
CHECK_GE(argc, 1);
546552

547553
BufferValue path(env->isolate(), args[0]);
548554
CHECK_NE(*path, nullptr);
549555

550556
if (args[1]->IsObject()) { // lstat(path, req)
551-
CHECK_EQ(args.Length(), 2);
557+
CHECK_EQ(argc, 2);
552558
AsyncCall(env, args, "lstat", UTF8, AfterStat,
553559
uv_fs_lstat, *path);
554560
} else { // lstat(path, undefined, ctx)
555-
CHECK_EQ(args.Length(), 3);
561+
CHECK_EQ(argc, 3);
556562
fs_req_wrap req_wrap;
557563
int err = SyncCall(env, args[2], &req_wrap, "lstat", uv_fs_lstat, *path);
558564
if (err == 0) {
@@ -564,18 +570,19 @@ static void LStat(const FunctionCallbackInfo<Value>& args) {
564570

565571
static void FStat(const FunctionCallbackInfo<Value>& args) {
566572
Environment* env = Environment::GetCurrent(args);
567-
Local<Context> context = env->context();
568573

569-
CHECK(args[0]->IsInt32());
574+
const int argc = args.Length();
575+
CHECK_GE(argc, 1);
570576

571-
int fd = static_cast<int>(args[0]->Int32Value(context).FromJust());
577+
CHECK(args[0]->IsInt32());
578+
int fd = args[0].As<Int32>()->Value();
572579

573580
if (args[1]->IsObject()) { // fstat(fd, req)
574-
CHECK_EQ(args.Length(), 2);
581+
CHECK_EQ(argc, 2);
575582
AsyncCall(env, args, "fstat", UTF8, AfterStat,
576583
uv_fs_fstat, fd);
577584
} else { // fstat(fd, undefined, ctx)
578-
CHECK_EQ(args.Length(), 3);
585+
CHECK_EQ(argc, 3);
579586
fs_req_wrap req_wrap;
580587
int err = SyncCall(env, args[2], &req_wrap, "fstat", uv_fs_fstat, fd);
581588
if (err == 0) {

0 commit comments

Comments
 (0)