@@ -1090,21 +1090,28 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) {
1090
1090
static void ReadDir (const FunctionCallbackInfo<Value>& args) {
1091
1091
Environment* env = Environment::GetCurrent (args);
1092
1092
1093
- CHECK_GE (args.Length (), 1 );
1093
+ const int argc = args.Length ();
1094
+ CHECK_GE (argc, 3 );
1094
1095
1095
1096
BufferValue path (env->isolate (), args[0 ]);
1096
1097
CHECK_NE (*path, nullptr );
1097
1098
1098
1099
const enum encoding encoding = ParseEncoding (env->isolate (), args[1 ], UTF8);
1099
1100
1100
1101
FSReqBase* req_wrap = GetReqWrap (env, args[2 ]);
1101
- if (req_wrap != nullptr ) {
1102
+ if (req_wrap != nullptr ) { // readdir(path, encoding, req)
1102
1103
AsyncCall (env, req_wrap, args, " scandir" , encoding, AfterScanDir,
1103
1104
uv_fs_scandir, *path, 0 /* flags*/ );
1104
- } else {
1105
- SYNC_CALL (scandir, *path, *path, 0 /* flags*/ )
1105
+ } else { // readdir(path, encoding, undefined, ctx)
1106
+ CHECK_EQ (argc, 4 );
1107
+ fs_req_wrap req_wrap;
1108
+ int err = SyncCall (env, args[3 ], &req_wrap, " scandir" ,
1109
+ uv_fs_scandir, *path, 0 /* flags*/ );
1110
+ if (err < 0 ) {
1111
+ return ; // syscall failed, no need to continue, error info is in ctx
1112
+ }
1106
1113
1107
- CHECK_GE (SYNC_REQ .result , 0 );
1114
+ CHECK_GE (req_wrap. req .result , 0 );
1108
1115
int r;
1109
1116
Local<Array> names = Array::New (env->isolate (), 0 );
1110
1117
Local<Function> fn = env->push_values_to_array_function ();
@@ -1114,19 +1121,26 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
1114
1121
for (int i = 0 ; ; i++) {
1115
1122
uv_dirent_t ent;
1116
1123
1117
- r = uv_fs_scandir_next (&SYNC_REQ , &ent);
1124
+ r = uv_fs_scandir_next (&(req_wrap. req ) , &ent);
1118
1125
if (r == UV_EOF)
1119
1126
break ;
1120
- if (r != 0 )
1121
- return env->ThrowUVException (r, " readdir" , " " , *path);
1127
+ if (r != 0 ) {
1128
+ Local<Object> ctx = args[3 ].As <Object>();
1129
+ ctx->Set (env->context (), env->errno_string (),
1130
+ Integer::New (env->isolate (), r)).FromJust ();
1131
+ ctx->Set (env->context (), env->syscall_string (),
1132
+ OneByteString (env->isolate (), " readdir" )).FromJust ();
1133
+ return ;
1134
+ }
1122
1135
1123
1136
Local<Value> error;
1124
1137
MaybeLocal<Value> filename = StringBytes::Encode (env->isolate (),
1125
1138
ent.name ,
1126
1139
encoding,
1127
1140
&error);
1128
1141
if (filename.IsEmpty ()) {
1129
- env->isolate ()->ThrowException (error);
1142
+ Local<Object> ctx = args[3 ].As <Object>();
1143
+ ctx->Set (env->context (), env->error_string (), error).FromJust ();
1130
1144
return ;
1131
1145
}
1132
1146
0 commit comments