From e1c29f2c529ffdbf9cf8f05d4ed27ccfcede2719 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 17 Jan 2018 06:49:45 +0800 Subject: [PATCH] src: clean up argument assertions in node_file.cc - Cache `args.Length()` - Use `value.As()` to cast the arguments PR-URL: https://github.com/nodejs/node/pull/18192 Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- src/node_file.cc | 55 +++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 3edf09b647935c..c721c4ba88d0e0 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -87,6 +87,7 @@ using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; +using v8::Int32; using v8::Integer; using v8::Isolate; using v8::Local; @@ -351,6 +352,7 @@ inline FSReqWrap* AsyncCall(Environment* env, // Template counterpart of SYNC_CALL, except that it only puts // the error number and the syscall in the context instead of // creating an error in the C++ land. +// ctx must be checked using value->IsObject() before being passed. template inline int SyncCall(Environment* env, Local ctx, fs_req_wrap* req_wrap, const char* syscall, Func fn, Args... args) { @@ -358,7 +360,7 @@ inline int SyncCall(Environment* env, Local ctx, fs_req_wrap* req_wrap, int err = fn(env->event_loop(), &(req_wrap->req), args..., nullptr); if (err < 0) { Local context = env->context(); - Local ctx_obj = ctx->ToObject(context).ToLocalChecked(); + Local ctx_obj = ctx.As(); Isolate *isolate = env->isolate(); ctx_obj->Set(context, env->errno_string(), @@ -391,19 +393,22 @@ inline int SyncCall(Environment* env, Local ctx, fs_req_wrap* req_wrap, void Access(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args.GetIsolate()); HandleScope scope(env->isolate()); - Local context = env->context(); - CHECK_GE(args.Length(), 2); + + const int argc = args.Length(); + CHECK_GE(argc, 2); + CHECK(args[1]->IsInt32()); + int mode = args[1].As()->Value(); BufferValue path(env->isolate(), args[0]); - int mode = static_cast(args[1]->Int32Value(context).FromJust()); + CHECK_NE(*path, nullptr); if (args[2]->IsObject()) { // access(path, mode, req) - CHECK_EQ(args.Length(), 3); + CHECK_EQ(argc, 3); AsyncCall(env, args, "access", UTF8, AfterNoArgs, uv_fs_access, *path, mode); } else { // access(path, mode, undefined, ctx) - CHECK_EQ(args.Length(), 4); + CHECK_EQ(argc, 4); fs_req_wrap req_wrap; SyncCall(env, args[3], &req_wrap, "access", uv_fs_access, *path, mode); } @@ -412,20 +417,19 @@ void Access(const FunctionCallbackInfo& args) { void Close(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - Local context = env->context(); - int length = args.Length(); - CHECK_GE(length, 2); - CHECK(args[0]->IsInt32()); + const int argc = args.Length(); + CHECK_GE(argc, 2); - int fd = static_cast(args[0]->Int32Value(context).FromJust()); + CHECK(args[0]->IsInt32()); + int fd = args[0].As()->Value(); if (args[1]->IsObject()) { // close(fd, req) - CHECK_EQ(args.Length(), 2); + CHECK_EQ(argc, 2); AsyncCall(env, args, "close", UTF8, AfterNoArgs, uv_fs_close, fd); } else { // close(fd, undefined, ctx) - CHECK_EQ(args.Length(), 3); + CHECK_EQ(argc, 3); fs_req_wrap req_wrap; SyncCall(env, args[2], &req_wrap, "close", uv_fs_close, fd); } @@ -519,17 +523,18 @@ static void InternalModuleStat(const FunctionCallbackInfo& args) { static void Stat(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - CHECK_GE(args.Length(), 1); + const int argc = args.Length(); + CHECK_GE(argc, 1); BufferValue path(env->isolate(), args[0]); CHECK_NE(*path, nullptr); if (args[1]->IsObject()) { // stat(path, req) - CHECK_EQ(args.Length(), 2); + CHECK_EQ(argc, 2); AsyncCall(env, args, "stat", UTF8, AfterStat, uv_fs_stat, *path); } else { // stat(path, undefined, ctx) - CHECK_EQ(args.Length(), 3); + CHECK_EQ(argc, 3); fs_req_wrap req_wrap; int err = SyncCall(env, args[2], &req_wrap, "stat", uv_fs_stat, *path); if (err == 0) { @@ -542,17 +547,18 @@ static void Stat(const FunctionCallbackInfo& args) { static void LStat(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - CHECK_GE(args.Length(), 1); + const int argc = args.Length(); + CHECK_GE(argc, 1); BufferValue path(env->isolate(), args[0]); CHECK_NE(*path, nullptr); if (args[1]->IsObject()) { // lstat(path, req) - CHECK_EQ(args.Length(), 2); + CHECK_EQ(argc, 2); AsyncCall(env, args, "lstat", UTF8, AfterStat, uv_fs_lstat, *path); } else { // lstat(path, undefined, ctx) - CHECK_EQ(args.Length(), 3); + CHECK_EQ(argc, 3); fs_req_wrap req_wrap; int err = SyncCall(env, args[2], &req_wrap, "lstat", uv_fs_lstat, *path); if (err == 0) { @@ -564,18 +570,19 @@ static void LStat(const FunctionCallbackInfo& args) { static void FStat(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - Local context = env->context(); - CHECK(args[0]->IsInt32()); + const int argc = args.Length(); + CHECK_GE(argc, 1); - int fd = static_cast(args[0]->Int32Value(context).FromJust()); + CHECK(args[0]->IsInt32()); + int fd = args[0].As()->Value(); if (args[1]->IsObject()) { // fstat(fd, req) - CHECK_EQ(args.Length(), 2); + CHECK_EQ(argc, 2); AsyncCall(env, args, "fstat", UTF8, AfterStat, uv_fs_fstat, fd); } else { // fstat(fd, undefined, ctx) - CHECK_EQ(args.Length(), 3); + CHECK_EQ(argc, 3); fs_req_wrap req_wrap; int err = SyncCall(env, args[2], &req_wrap, "fstat", uv_fs_fstat, fd); if (err == 0) {