Skip to content

Commit 43535f5

Browse files
committed
src: simplify JS Array creation
Use `ToV8Value()` where appropriate to reduce duplication and avoid extra `Array::Set()` calls. PR-URL: #25288 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6b5c962 commit 43535f5

File tree

4 files changed

+19
-73
lines changed

4 files changed

+19
-73
lines changed

src/node.cc

+5-25
Original file line numberDiff line numberDiff line change
@@ -938,28 +938,15 @@ void SetupProcessObject(Environment* env,
938938
#endif
939939

940940
// process.argv
941-
Local<Array> arguments = Array::New(env->isolate(), args.size());
942-
for (size_t i = 0; i < args.size(); ++i) {
943-
arguments->Set(env->context(), i,
944-
String::NewFromUtf8(env->isolate(), args[i].c_str(),
945-
NewStringType::kNormal).ToLocalChecked())
946-
.FromJust();
947-
}
948941
process->Set(env->context(),
949942
FIXED_ONE_BYTE_STRING(env->isolate(), "argv"),
950-
arguments).FromJust();
943+
ToV8Value(env->context(), args).ToLocalChecked()).FromJust();
951944

952945
// process.execArgv
953-
Local<Array> exec_arguments = Array::New(env->isolate(), exec_args.size());
954-
for (size_t i = 0; i < exec_args.size(); ++i) {
955-
exec_arguments->Set(env->context(), i,
956-
String::NewFromUtf8(env->isolate(), exec_args[i].c_str(),
957-
NewStringType::kNormal).ToLocalChecked())
958-
.FromJust();
959-
}
960946
process->Set(env->context(),
961947
FIXED_ONE_BYTE_STRING(env->isolate(), "execArgv"),
962-
exec_arguments).FromJust();
948+
ToV8Value(env->context(), exec_args)
949+
.ToLocalChecked()).FromJust();
963950

964951
// create process.env
965952
process
@@ -1010,17 +997,10 @@ void SetupProcessObject(Environment* env,
1010997
const std::vector<std::string>& preload_modules =
1011998
env->options()->preload_modules;
1012999
if (!preload_modules.empty()) {
1013-
Local<Array> array = Array::New(env->isolate());
1014-
for (unsigned int i = 0; i < preload_modules.size(); ++i) {
1015-
Local<String> module = String::NewFromUtf8(env->isolate(),
1016-
preload_modules[i].c_str(),
1017-
NewStringType::kNormal)
1018-
.ToLocalChecked();
1019-
array->Set(env->context(), i, module).FromJust();
1020-
}
10211000
READONLY_PROPERTY(process,
10221001
"_preload_modules",
1023-
array);
1002+
ToV8Value(env->context(), preload_modules)
1003+
.ToLocalChecked());
10241004
}
10251005

10261006
// --no-deprecation

src/node_credentials.cc

+10-25
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ using v8::Array;
1515
using v8::Context;
1616
using v8::Function;
1717
using v8::FunctionCallbackInfo;
18-
using v8::Integer;
1918
using v8::Isolate;
2019
using v8::Local;
20+
using v8::MaybeLocal;
2121
using v8::Object;
2222
using v8::String;
2323
using v8::Uint32;
@@ -253,36 +253,21 @@ static void GetGroups(const FunctionCallbackInfo<Value>& args) {
253253
Environment* env = Environment::GetCurrent(args);
254254

255255
int ngroups = getgroups(0, nullptr);
256-
257256
if (ngroups == -1) return env->ThrowErrnoException(errno, "getgroups");
258257

259-
gid_t* groups = new gid_t[ngroups];
260-
261-
ngroups = getgroups(ngroups, groups);
258+
std::vector<gid_t> groups(ngroups);
262259

263-
if (ngroups == -1) {
264-
delete[] groups;
260+
ngroups = getgroups(ngroups, groups.data());
261+
if (ngroups == -1)
265262
return env->ThrowErrnoException(errno, "getgroups");
266-
}
267263

268-
Local<Array> groups_list = Array::New(env->isolate(), ngroups);
269-
bool seen_egid = false;
264+
groups.resize(ngroups);
270265
gid_t egid = getegid();
271-
272-
for (int i = 0; i < ngroups; i++) {
273-
groups_list->Set(env->context(), i, Integer::New(env->isolate(), groups[i]))
274-
.FromJust();
275-
if (groups[i] == egid) seen_egid = true;
276-
}
277-
278-
delete[] groups;
279-
280-
if (seen_egid == false)
281-
groups_list
282-
->Set(env->context(), ngroups, Integer::New(env->isolate(), egid))
283-
.FromJust();
284-
285-
args.GetReturnValue().Set(groups_list);
266+
if (std::find(groups.begin(), groups.end(), egid) == groups.end())
267+
groups.push_back(egid);
268+
MaybeLocal<Value> array = ToV8Value(env->context(), groups);
269+
if (!array.IsEmpty())
270+
args.GetReturnValue().Set(array.ToLocalChecked());
286271
}
287272

288273
static void SetGroups(const FunctionCallbackInfo<Value>& args) {

src/node_crypto.cc

+3-13
Original file line numberDiff line numberDiff line change
@@ -248,22 +248,12 @@ struct CryptoErrorVector : public std::vector<std::string> {
248248
CHECK(!exception_v.IsEmpty());
249249

250250
if (!empty()) {
251-
Local<Array> array = Array::New(env->isolate(), size());
252-
CHECK(!array.IsEmpty());
253-
254-
for (const std::string& string : *this) {
255-
const size_t index = &string - &front();
256-
Local<String> value =
257-
String::NewFromUtf8(env->isolate(), string.data(),
258-
NewStringType::kNormal, string.size())
259-
.ToLocalChecked();
260-
array->Set(env->context(), index, value).FromJust();
261-
}
262-
263251
CHECK(exception_v->IsObject());
264252
Local<Object> exception = exception_v.As<Object>();
265253
exception->Set(env->context(),
266-
env->openssl_error_stack(), array).FromJust();
254+
env->openssl_error_stack(),
255+
ToV8Value(env->context(), *this).ToLocalChecked())
256+
.FromJust();
267257
}
268258

269259
return exception_v;

src/node_url.cc

+1-10
Original file line numberDiff line numberDiff line change
@@ -1190,15 +1190,6 @@ inline std::vector<std::string> FromJSStringArray(Environment* env,
11901190
return vec;
11911191
}
11921192

1193-
inline Local<Array> ToJSStringArray(Environment* env,
1194-
const std::vector<std::string>& vec) {
1195-
Isolate* isolate = env->isolate();
1196-
Local<Array> array = Array::New(isolate, vec.size());
1197-
for (size_t n = 0; n < vec.size(); n++)
1198-
array->Set(env->context(), n, Utf8String(isolate, vec[n])).FromJust();
1199-
return array;
1200-
}
1201-
12021193
inline url_data HarvestBase(Environment* env, Local<Object> base_obj) {
12031194
url_data base;
12041195
Local<Context> context = env->context();
@@ -2119,7 +2110,7 @@ static inline void SetArgs(Environment* env,
21192110
if (url.port > -1)
21202111
argv[ARG_PORT] = Integer::New(isolate, url.port);
21212112
if (url.flags & URL_FLAGS_HAS_PATH)
2122-
argv[ARG_PATH] = ToJSStringArray(env, url.path);
2113+
argv[ARG_PATH] = ToV8Value(env->context(), url.path).ToLocalChecked();
21232114
}
21242115

21252116
static void Parse(Environment* env,

0 commit comments

Comments
 (0)