Skip to content

Commit f49b4fc

Browse files
joyeecheungBridgeAR
authored andcommittedNov 13, 2018
fs: replace pushValueToArray with pure C++ API
Instead of calling into JS from C++ to push values into an array, use the new Array::New API that takes a pointer and a length directly. PR-URL: #24125 Refs: v8/v8@0483e9a Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yang Guo <yangguo@chromium.org> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 3e1c53f commit f49b4fc

File tree

1 file changed

+10
-64
lines changed

1 file changed

+10
-64
lines changed
 

‎src/node_file.cc

+10-64
Original file line numberDiff line numberDiff line change
@@ -571,10 +571,7 @@ void AfterScanDir(uv_fs_t* req) {
571571
Environment* env = req_wrap->env();
572572
Local<Value> error;
573573
int r;
574-
Local<Array> names = Array::New(env->isolate(), 0);
575-
Local<Function> fn = env->push_values_to_array_function();
576-
Local<Value> name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
577-
size_t name_idx = 0;
574+
std::vector<Local<Value>> name_argv;
578575

579576
for (int i = 0; ; i++) {
580577
uv_dirent_t ent;
@@ -596,23 +593,11 @@ void AfterScanDir(uv_fs_t* req) {
596593
if (filename.IsEmpty())
597594
return req_wrap->Reject(error);
598595

599-
name_argv[name_idx++] = filename.ToLocalChecked();
600-
601-
if (name_idx >= arraysize(name_argv)) {
602-
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx,
603-
name_argv);
604-
if (ret.IsEmpty()) {
605-
return;
606-
}
607-
name_idx = 0;
608-
}
609-
}
610-
611-
if (name_idx > 0) {
612-
fn->Call(env->context(), names, name_idx, name_argv)
613-
.ToLocalChecked();
596+
name_argv.push_back(filename.ToLocalChecked());
614597
}
615598

599+
Local<Array> names =
600+
Array::New(env->isolate(), name_argv.data(), name_argv.size());
616601
req_wrap->Resolve(names);
617602
}
618603

@@ -1497,18 +1482,8 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
14971482

14981483
CHECK_GE(req_wrap_sync.req.result, 0);
14991484
int r;
1500-
Local<Array> names = Array::New(isolate, 0);
1501-
Local<Function> fn = env->push_values_to_array_function();
1502-
Local<Value> name_v[NODE_PUSH_VAL_TO_ARRAY_MAX];
1503-
size_t name_idx = 0;
1504-
1505-
Local<Value> types;
1506-
Local<Value> type_v[NODE_PUSH_VAL_TO_ARRAY_MAX];
1507-
size_t type_idx;
1508-
if (with_types) {
1509-
types = Array::New(isolate, 0);
1510-
type_idx = 0;
1511-
}
1485+
std::vector<Local<Value>> name_v;
1486+
std::vector<Local<Value>> type_v;
15121487

15131488
for (int i = 0; ; i++) {
15141489
uv_dirent_t ent;
@@ -1537,47 +1512,18 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
15371512
return;
15381513
}
15391514

1540-
name_v[name_idx++] = filename.ToLocalChecked();
1541-
1542-
if (name_idx >= arraysize(name_v)) {
1543-
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx,
1544-
name_v);
1545-
if (ret.IsEmpty()) {
1546-
return;
1547-
}
1548-
name_idx = 0;
1549-
}
1515+
name_v.push_back(filename.ToLocalChecked());
15501516

15511517
if (with_types) {
1552-
type_v[type_idx++] = Integer::New(isolate, ent.type);
1553-
1554-
if (type_idx >= arraysize(type_v)) {
1555-
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx,
1556-
type_v);
1557-
if (ret.IsEmpty()) {
1558-
return;
1559-
}
1560-
type_idx = 0;
1561-
}
1518+
type_v.push_back(Integer::New(isolate, ent.type));
15621519
}
15631520
}
15641521

1565-
if (name_idx > 0) {
1566-
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, name_v);
1567-
if (ret.IsEmpty()) {
1568-
return;
1569-
}
1570-
}
1571-
1572-
if (with_types && type_idx > 0) {
1573-
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx, type_v);
1574-
if (ret.IsEmpty()) {
1575-
return;
1576-
}
1577-
}
15781522

1523+
Local<Array> names = Array::New(isolate, name_v.data(), name_v.size());
15791524
if (with_types) {
15801525
Local<Array> result = Array::New(isolate, 2);
1526+
Local<Value> types = Array::New(isolate, type_v.data(), type_v.size());
15811527
result->Set(0, names);
15821528
result->Set(1, types);
15831529
args.GetReturnValue().Set(result);

0 commit comments

Comments
 (0)