Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 40ef04c

Browse filesBrowse files
daeyeondanielleadams
authored andcommitted
src,fs: refactor duplicated code in fs.readdir
`AfterScanDirWithTypes` is almost same as `AfterScanDir` except for handling the `with file types` option. This merges the two functions into one. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: #43204 Reviewed-By: Feng Yu <F3n67u@outlook.com>
1 parent 0b909a4 commit 40ef04c
Copy full SHA for 40ef04c

File tree

Expand file treeCollapse file tree

2 files changed

+28
-57
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+28
-57
lines changed
Open diff view settings
Collapse file

‎src/node_file.cc‎

Copy file name to clipboardExpand all lines: src/node_file.cc
+25-57Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -777,43 +777,6 @@ void AfterScanDir(uv_fs_t* req) {
777777
FSReqBase* req_wrap = FSReqBase::from_req(req);
778778
FSReqAfterScope after(req_wrap, req);
779779

780-
if (!after.Proceed()) {
781-
return;
782-
}
783-
Environment* env = req_wrap->env();
784-
Local<Value> error;
785-
int r;
786-
std::vector<Local<Value>> name_v;
787-
788-
for (;;) {
789-
uv_dirent_t ent;
790-
791-
r = uv_fs_scandir_next(req, &ent);
792-
if (r == UV_EOF)
793-
break;
794-
if (r != 0) {
795-
return req_wrap->Reject(UVException(
796-
env->isolate(), r, nullptr, req_wrap->syscall(), req->path));
797-
}
798-
799-
MaybeLocal<Value> filename =
800-
StringBytes::Encode(env->isolate(),
801-
ent.name,
802-
req_wrap->encoding(),
803-
&error);
804-
if (filename.IsEmpty())
805-
return req_wrap->Reject(error);
806-
807-
name_v.push_back(filename.ToLocalChecked());
808-
}
809-
810-
req_wrap->Resolve(Array::New(env->isolate(), name_v.data(), name_v.size()));
811-
}
812-
813-
void AfterScanDirWithTypes(uv_fs_t* req) {
814-
FSReqBase* req_wrap = FSReqBase::from_req(req);
815-
FSReqAfterScope after(req_wrap, req);
816-
817780
if (!after.Proceed()) {
818781
return;
819782
}
@@ -826,6 +789,8 @@ void AfterScanDirWithTypes(uv_fs_t* req) {
826789
std::vector<Local<Value>> name_v;
827790
std::vector<Local<Value>> type_v;
828791

792+
const bool with_file_types = req_wrap->with_file_types();
793+
829794
for (;;) {
830795
uv_dirent_t ent;
831796

@@ -837,23 +802,23 @@ void AfterScanDirWithTypes(uv_fs_t* req) {
837802
UVException(isolate, r, nullptr, req_wrap->syscall(), req->path));
838803
}
839804

840-
MaybeLocal<Value> filename =
841-
StringBytes::Encode(isolate,
842-
ent.name,
843-
req_wrap->encoding(),
844-
&error);
845-
if (filename.IsEmpty())
805+
Local<Value> filename;
806+
if (!StringBytes::Encode(isolate, ent.name, req_wrap->encoding(), &error)
807+
.ToLocal(&filename)) {
846808
return req_wrap->Reject(error);
809+
}
810+
name_v.push_back(filename);
847811

848-
name_v.push_back(filename.ToLocalChecked());
849-
type_v.emplace_back(Integer::New(isolate, ent.type));
812+
if (with_file_types) type_v.emplace_back(Integer::New(isolate, ent.type));
850813
}
851814

852-
Local<Value> result[] = {
853-
Array::New(isolate, name_v.data(), name_v.size()),
854-
Array::New(isolate, type_v.data(), type_v.size())
855-
};
856-
req_wrap->Resolve(Array::New(isolate, result, arraysize(result)));
815+
if (with_file_types) {
816+
Local<Value> result[] = {Array::New(isolate, name_v.data(), name_v.size()),
817+
Array::New(isolate, type_v.data(), type_v.size())};
818+
req_wrap->Resolve(Array::New(isolate, result, arraysize(result)));
819+
} else {
820+
req_wrap->Resolve(Array::New(isolate, name_v.data(), name_v.size()));
821+
}
857822
}
858823

859824
void Access(const FunctionCallbackInfo<Value>& args) {
@@ -1650,13 +1615,16 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
16501615

16511616
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
16521617
if (req_wrap_async != nullptr) { // readdir(path, encoding, withTypes, req)
1653-
if (with_types) {
1654-
AsyncCall(env, req_wrap_async, args, "scandir", encoding,
1655-
AfterScanDirWithTypes, uv_fs_scandir, *path, 0 /*flags*/);
1656-
} else {
1657-
AsyncCall(env, req_wrap_async, args, "scandir", encoding,
1658-
AfterScanDir, uv_fs_scandir, *path, 0 /*flags*/);
1659-
}
1618+
req_wrap_async->set_with_file_types(with_types);
1619+
AsyncCall(env,
1620+
req_wrap_async,
1621+
args,
1622+
"scandir",
1623+
encoding,
1624+
AfterScanDir,
1625+
uv_fs_scandir,
1626+
*path,
1627+
0 /*flags*/);
16601628
} else { // readdir(path, encoding, withTypes, undefined, ctx)
16611629
CHECK_EQ(argc, 5);
16621630
FSReqWrapSync req_wrap_sync;
Collapse file

‎src/node_file.h‎

Copy file name to clipboardExpand all lines: src/node_file.h
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ class FSReqBase : public ReqWrap<uv_fs_t> {
8989
enum encoding encoding() const { return encoding_; }
9090
bool use_bigint() const { return use_bigint_; }
9191
bool is_plain_open() const { return is_plain_open_; }
92+
bool with_file_types() const { return with_file_types_; }
9293

9394
void set_is_plain_open(bool value) { is_plain_open_ = value; }
95+
void set_with_file_types(bool value) { with_file_types_ = value; }
9496

9597
FSContinuationData* continuation_data() const {
9698
return continuation_data_.get();
@@ -116,6 +118,7 @@ class FSReqBase : public ReqWrap<uv_fs_t> {
116118
bool has_data_ = false;
117119
bool use_bigint_ = false;
118120
bool is_plain_open_ = false;
121+
bool with_file_types_ = false;
119122
const char* syscall_ = nullptr;
120123

121124
BaseObjectPtr<BindingData> binding_data_;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.