From f49b4fc7949672f8bf31cc03172c97edd862cb15 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 6 Nov 2018 18:35:01 +0800 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/24125 Refs: https://github.com/v8/v8/commit/0483e9a9abe77a73632fd85b9c0cd608efa9aa0d Reviewed-By: Anna Henningsen Reviewed-By: Yang Guo Reviewed-By: Gus Caplan Reviewed-By: Colin Ihrig Reviewed-By: Ben Noordhuis Reviewed-By: Refael Ackermann --- src/node_file.cc | 74 +++++++----------------------------------------- 1 file changed, 10 insertions(+), 64 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index 5b102269258e0b..35ca7d3514c2e5 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -571,10 +571,7 @@ void AfterScanDir(uv_fs_t* req) { Environment* env = req_wrap->env(); Local error; int r; - Local names = Array::New(env->isolate(), 0); - Local fn = env->push_values_to_array_function(); - Local name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX]; - size_t name_idx = 0; + std::vector> name_argv; for (int i = 0; ; i++) { uv_dirent_t ent; @@ -596,23 +593,11 @@ void AfterScanDir(uv_fs_t* req) { if (filename.IsEmpty()) return req_wrap->Reject(error); - name_argv[name_idx++] = filename.ToLocalChecked(); - - if (name_idx >= arraysize(name_argv)) { - MaybeLocal ret = fn->Call(env->context(), names, name_idx, - name_argv); - if (ret.IsEmpty()) { - return; - } - name_idx = 0; - } - } - - if (name_idx > 0) { - fn->Call(env->context(), names, name_idx, name_argv) - .ToLocalChecked(); + name_argv.push_back(filename.ToLocalChecked()); } + Local names = + Array::New(env->isolate(), name_argv.data(), name_argv.size()); req_wrap->Resolve(names); } @@ -1497,18 +1482,8 @@ static void ReadDir(const FunctionCallbackInfo& args) { CHECK_GE(req_wrap_sync.req.result, 0); int r; - Local names = Array::New(isolate, 0); - Local fn = env->push_values_to_array_function(); - Local name_v[NODE_PUSH_VAL_TO_ARRAY_MAX]; - size_t name_idx = 0; - - Local types; - Local type_v[NODE_PUSH_VAL_TO_ARRAY_MAX]; - size_t type_idx; - if (with_types) { - types = Array::New(isolate, 0); - type_idx = 0; - } + std::vector> name_v; + std::vector> type_v; for (int i = 0; ; i++) { uv_dirent_t ent; @@ -1537,47 +1512,18 @@ static void ReadDir(const FunctionCallbackInfo& args) { return; } - name_v[name_idx++] = filename.ToLocalChecked(); - - if (name_idx >= arraysize(name_v)) { - MaybeLocal ret = fn->Call(env->context(), names, name_idx, - name_v); - if (ret.IsEmpty()) { - return; - } - name_idx = 0; - } + name_v.push_back(filename.ToLocalChecked()); if (with_types) { - type_v[type_idx++] = Integer::New(isolate, ent.type); - - if (type_idx >= arraysize(type_v)) { - MaybeLocal ret = fn->Call(env->context(), types, type_idx, - type_v); - if (ret.IsEmpty()) { - return; - } - type_idx = 0; - } + type_v.push_back(Integer::New(isolate, ent.type)); } } - if (name_idx > 0) { - MaybeLocal ret = fn->Call(env->context(), names, name_idx, name_v); - if (ret.IsEmpty()) { - return; - } - } - - if (with_types && type_idx > 0) { - MaybeLocal ret = fn->Call(env->context(), types, type_idx, type_v); - if (ret.IsEmpty()) { - return; - } - } + Local names = Array::New(isolate, name_v.data(), name_v.size()); if (with_types) { Local result = Array::New(isolate, 2); + Local types = Array::New(isolate, type_v.data(), type_v.size()); result->Set(0, names); result->Set(1, types); args.GetReturnValue().Set(result);