Permalink
Browse files

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 <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
joyeecheung committed Jan 16, 2018
1 parent 4af1bba commit e1c29f2c529ffdbf9cf8f05d4ed27ccfcede2719
Showing with 31 additions and 24 deletions.
  1. +31 −24 src/node_file.cc
View
@@ -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,14 +352,15 @@ 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 <typename Func, typename... Args>
inline int SyncCall(Environment* env, Local<Value> ctx, fs_req_wrap* req_wrap,
const char* syscall, Func fn, Args... args) {
env->PrintSyncTrace();
int err = fn(env->event_loop(), &(req_wrap->req), args..., nullptr);
if (err < 0) {
Local<Context> context = env->context();
Local<Object> ctx_obj = ctx->ToObject(context).ToLocalChecked();
Local<Object> ctx_obj = ctx.As<Object>();
Isolate *isolate = env->isolate();
ctx_obj->Set(context,
env->errno_string(),
@@ -391,19 +393,22 @@ inline int SyncCall(Environment* env, Local<Value> ctx, fs_req_wrap* req_wrap,
void Access(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope scope(env->isolate());
Local<Context> 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<Int32>()->Value();
BufferValue path(env->isolate(), args[0]);
int mode = static_cast<int>(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<Value>& args) {
void Close(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Context> 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<int>(args[0]->Int32Value(context).FromJust());
CHECK(args[0]->IsInt32());
int fd = args[0].As<Int32>()->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<Value>& args) {
static void Stat(const FunctionCallbackInfo<Value>& 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<Value>& args) {
static void LStat(const FunctionCallbackInfo<Value>& 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<Value>& args) {
static void FStat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<Context> context = env->context();
CHECK(args[0]->IsInt32());
const int argc = args.Length();
CHECK_GE(argc, 1);
int fd = static_cast<int>(args[0]->Int32Value(context).FromJust());
CHECK(args[0]->IsInt32());
int fd = args[0].As<Int32>()->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) {

0 comments on commit e1c29f2

Please sign in to comment.