src: simplify JS Array creation
Use `ToV8Value()` where appropriate to reduce duplication and avoid extra `Array::Set()` calls. PR-URL: https://github.com/nodejs/node/pull/25288 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
112ee2ab07
commit
0ff1eb83d9
30
src/node.cc
30
src/node.cc
@ -889,28 +889,15 @@ void SetupProcessObject(Environment* env,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// process.argv
|
// process.argv
|
||||||
Local<Array> arguments = Array::New(env->isolate(), args.size());
|
|
||||||
for (size_t i = 0; i < args.size(); ++i) {
|
|
||||||
arguments->Set(env->context(), i,
|
|
||||||
String::NewFromUtf8(env->isolate(), args[i].c_str(),
|
|
||||||
NewStringType::kNormal).ToLocalChecked())
|
|
||||||
.FromJust();
|
|
||||||
}
|
|
||||||
process->Set(env->context(),
|
process->Set(env->context(),
|
||||||
FIXED_ONE_BYTE_STRING(env->isolate(), "argv"),
|
FIXED_ONE_BYTE_STRING(env->isolate(), "argv"),
|
||||||
arguments).FromJust();
|
ToV8Value(env->context(), args).ToLocalChecked()).FromJust();
|
||||||
|
|
||||||
// process.execArgv
|
// process.execArgv
|
||||||
Local<Array> exec_arguments = Array::New(env->isolate(), exec_args.size());
|
|
||||||
for (size_t i = 0; i < exec_args.size(); ++i) {
|
|
||||||
exec_arguments->Set(env->context(), i,
|
|
||||||
String::NewFromUtf8(env->isolate(), exec_args[i].c_str(),
|
|
||||||
NewStringType::kNormal).ToLocalChecked())
|
|
||||||
.FromJust();
|
|
||||||
}
|
|
||||||
process->Set(env->context(),
|
process->Set(env->context(),
|
||||||
FIXED_ONE_BYTE_STRING(env->isolate(), "execArgv"),
|
FIXED_ONE_BYTE_STRING(env->isolate(), "execArgv"),
|
||||||
exec_arguments).FromJust();
|
ToV8Value(env->context(), exec_args)
|
||||||
|
.ToLocalChecked()).FromJust();
|
||||||
|
|
||||||
// create process.env
|
// create process.env
|
||||||
process
|
process
|
||||||
@ -960,17 +947,10 @@ void SetupProcessObject(Environment* env,
|
|||||||
const std::vector<std::string>& preload_modules =
|
const std::vector<std::string>& preload_modules =
|
||||||
env->options()->preload_modules;
|
env->options()->preload_modules;
|
||||||
if (!preload_modules.empty()) {
|
if (!preload_modules.empty()) {
|
||||||
Local<Array> array = Array::New(env->isolate());
|
|
||||||
for (unsigned int i = 0; i < preload_modules.size(); ++i) {
|
|
||||||
Local<String> module = String::NewFromUtf8(env->isolate(),
|
|
||||||
preload_modules[i].c_str(),
|
|
||||||
NewStringType::kNormal)
|
|
||||||
.ToLocalChecked();
|
|
||||||
array->Set(env->context(), i, module).FromJust();
|
|
||||||
}
|
|
||||||
READONLY_PROPERTY(process,
|
READONLY_PROPERTY(process,
|
||||||
"_preload_modules",
|
"_preload_modules",
|
||||||
array);
|
ToV8Value(env->context(), preload_modules)
|
||||||
|
.ToLocalChecked());
|
||||||
}
|
}
|
||||||
|
|
||||||
// --no-deprecation
|
// --no-deprecation
|
||||||
|
@ -15,9 +15,9 @@ using v8::Array;
|
|||||||
using v8::Context;
|
using v8::Context;
|
||||||
using v8::Function;
|
using v8::Function;
|
||||||
using v8::FunctionCallbackInfo;
|
using v8::FunctionCallbackInfo;
|
||||||
using v8::Integer;
|
|
||||||
using v8::Isolate;
|
using v8::Isolate;
|
||||||
using v8::Local;
|
using v8::Local;
|
||||||
|
using v8::MaybeLocal;
|
||||||
using v8::Object;
|
using v8::Object;
|
||||||
using v8::String;
|
using v8::String;
|
||||||
using v8::Uint32;
|
using v8::Uint32;
|
||||||
@ -253,36 +253,21 @@ static void GetGroups(const FunctionCallbackInfo<Value>& args) {
|
|||||||
Environment* env = Environment::GetCurrent(args);
|
Environment* env = Environment::GetCurrent(args);
|
||||||
|
|
||||||
int ngroups = getgroups(0, nullptr);
|
int ngroups = getgroups(0, nullptr);
|
||||||
|
|
||||||
if (ngroups == -1) return env->ThrowErrnoException(errno, "getgroups");
|
if (ngroups == -1) return env->ThrowErrnoException(errno, "getgroups");
|
||||||
|
|
||||||
gid_t* groups = new gid_t[ngroups];
|
std::vector<gid_t> groups(ngroups);
|
||||||
|
|
||||||
ngroups = getgroups(ngroups, groups);
|
ngroups = getgroups(ngroups, groups.data());
|
||||||
|
if (ngroups == -1)
|
||||||
if (ngroups == -1) {
|
|
||||||
delete[] groups;
|
|
||||||
return env->ThrowErrnoException(errno, "getgroups");
|
return env->ThrowErrnoException(errno, "getgroups");
|
||||||
}
|
|
||||||
|
|
||||||
Local<Array> groups_list = Array::New(env->isolate(), ngroups);
|
groups.resize(ngroups);
|
||||||
bool seen_egid = false;
|
|
||||||
gid_t egid = getegid();
|
gid_t egid = getegid();
|
||||||
|
if (std::find(groups.begin(), groups.end(), egid) == groups.end())
|
||||||
for (int i = 0; i < ngroups; i++) {
|
groups.push_back(egid);
|
||||||
groups_list->Set(env->context(), i, Integer::New(env->isolate(), groups[i]))
|
MaybeLocal<Value> array = ToV8Value(env->context(), groups);
|
||||||
.FromJust();
|
if (!array.IsEmpty())
|
||||||
if (groups[i] == egid) seen_egid = true;
|
args.GetReturnValue().Set(array.ToLocalChecked());
|
||||||
}
|
|
||||||
|
|
||||||
delete[] groups;
|
|
||||||
|
|
||||||
if (seen_egid == false)
|
|
||||||
groups_list
|
|
||||||
->Set(env->context(), ngroups, Integer::New(env->isolate(), egid))
|
|
||||||
.FromJust();
|
|
||||||
|
|
||||||
args.GetReturnValue().Set(groups_list);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SetGroups(const FunctionCallbackInfo<Value>& args) {
|
static void SetGroups(const FunctionCallbackInfo<Value>& args) {
|
||||||
|
@ -250,22 +250,12 @@ struct CryptoErrorVector : public std::vector<std::string> {
|
|||||||
CHECK(!exception_v.IsEmpty());
|
CHECK(!exception_v.IsEmpty());
|
||||||
|
|
||||||
if (!empty()) {
|
if (!empty()) {
|
||||||
Local<Array> array = Array::New(env->isolate(), size());
|
|
||||||
CHECK(!array.IsEmpty());
|
|
||||||
|
|
||||||
for (const std::string& string : *this) {
|
|
||||||
const size_t index = &string - &front();
|
|
||||||
Local<String> value =
|
|
||||||
String::NewFromUtf8(env->isolate(), string.data(),
|
|
||||||
NewStringType::kNormal, string.size())
|
|
||||||
.ToLocalChecked();
|
|
||||||
array->Set(env->context(), index, value).FromJust();
|
|
||||||
}
|
|
||||||
|
|
||||||
CHECK(exception_v->IsObject());
|
CHECK(exception_v->IsObject());
|
||||||
Local<Object> exception = exception_v.As<Object>();
|
Local<Object> exception = exception_v.As<Object>();
|
||||||
exception->Set(env->context(),
|
exception->Set(env->context(),
|
||||||
env->openssl_error_stack(), array).FromJust();
|
env->openssl_error_stack(),
|
||||||
|
ToV8Value(env->context(), *this).ToLocalChecked())
|
||||||
|
.FromJust();
|
||||||
}
|
}
|
||||||
|
|
||||||
return exception_v;
|
return exception_v;
|
||||||
|
@ -1190,15 +1190,6 @@ inline std::vector<std::string> FromJSStringArray(Environment* env,
|
|||||||
return vec;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Local<Array> ToJSStringArray(Environment* env,
|
|
||||||
const std::vector<std::string>& vec) {
|
|
||||||
Isolate* isolate = env->isolate();
|
|
||||||
Local<Array> array = Array::New(isolate, vec.size());
|
|
||||||
for (size_t n = 0; n < vec.size(); n++)
|
|
||||||
array->Set(env->context(), n, Utf8String(isolate, vec[n])).FromJust();
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline url_data HarvestBase(Environment* env, Local<Object> base_obj) {
|
inline url_data HarvestBase(Environment* env, Local<Object> base_obj) {
|
||||||
url_data base;
|
url_data base;
|
||||||
Local<Context> context = env->context();
|
Local<Context> context = env->context();
|
||||||
@ -2119,7 +2110,7 @@ static inline void SetArgs(Environment* env,
|
|||||||
if (url.port > -1)
|
if (url.port > -1)
|
||||||
argv[ARG_PORT] = Integer::New(isolate, url.port);
|
argv[ARG_PORT] = Integer::New(isolate, url.port);
|
||||||
if (url.flags & URL_FLAGS_HAS_PATH)
|
if (url.flags & URL_FLAGS_HAS_PATH)
|
||||||
argv[ARG_PATH] = ToJSStringArray(env, url.path);
|
argv[ARG_PATH] = ToV8Value(env->context(), url.path).ToLocalChecked();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Parse(Environment* env,
|
static void Parse(Environment* env,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user