@@ -36,6 +36,7 @@ using v8::Isolate;
3636using v8::Local;
3737using v8::MaybeLocal;
3838using v8::Null;
39+ using v8::Number;
3940using v8::Object;
4041using v8::ObjectTemplate;
4142using v8::String;
@@ -59,8 +60,8 @@ DirHandle::DirHandle(Environment* env, Local<Object> obj, uv_dir_t* dir)
5960 dir_ (dir) {
6061 MakeWeak ();
6162
62- dir_->nentries = arraysize (dirents_) ;
63- dir_->dirents = dirents_ ;
63+ dir_->nentries = 0 ;
64+ dir_->dirents = nullptr ;
6465}
6566
6667DirHandle* DirHandle::New (Environment* env, uv_dir_t * dir) {
@@ -230,22 +231,31 @@ void DirHandle::Read(const FunctionCallbackInfo<Value>& args) {
230231 Isolate* isolate = env->isolate ();
231232
232233 const int argc = args.Length ();
233- CHECK_GE (argc, 2 );
234+ CHECK_GE (argc, 3 );
234235
235236 const enum encoding encoding = ParseEncoding (isolate, args[0 ], UTF8);
236237
237238 DirHandle* dir;
238239 ASSIGN_OR_RETURN_UNWRAP (&dir, args.Holder ());
239240
240- FSReqBase* req_wrap_async = GetReqWrap (env, args[1 ]);
241- if (req_wrap_async != nullptr ) { // dir.read(encoding, req)
241+ CHECK (args[1 ]->IsNumber ());
242+ uint64_t buffer_size = args[1 ].As <Number>()->Value ();
243+
244+ if (buffer_size != dir->dirents_ .size ()) {
245+ dir->dirents_ .resize (buffer_size);
246+ dir->dir_ ->nentries = buffer_size;
247+ dir->dir_ ->dirents = dir->dirents_ .data ();
248+ }
249+
250+ FSReqBase* req_wrap_async = GetReqWrap (env, args[2 ]);
251+ if (req_wrap_async != nullptr ) { // dir.read(encoding, bufferSize, req)
242252 AsyncCall (env, req_wrap_async, args, " readdir" , encoding,
243253 AfterDirRead, uv_fs_readdir, dir->dir ());
244- } else { // dir.read(encoding, undefined, ctx)
245- CHECK_EQ (argc, 3 );
254+ } else { // dir.read(encoding, bufferSize, undefined, ctx)
255+ CHECK_EQ (argc, 4 );
246256 FSReqWrapSync req_wrap_sync;
247257 FS_DIR_SYNC_TRACE_BEGIN (readdir);
248- int err = SyncCall (env, args[2 ], &req_wrap_sync, " readdir" , uv_fs_readdir,
258+ int err = SyncCall (env, args[3 ], &req_wrap_sync, " readdir" , uv_fs_readdir,
249259 dir->dir ());
250260 FS_DIR_SYNC_TRACE_END (readdir);
251261 if (err < 0 ) {
0 commit comments