Skip to content

Commit

Permalink
src: clean up argument assertions in node_file.cc
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
joyeecheung committed Jan 20, 2018
1 parent 4af1bba commit e1c29f2
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit e1c29f2

Please sign in to comment.