Skip to content

Commit 0dbacb1

Browse files
addaleaxanonrig
authored andcommitted
src: use std::array for passing argv in node::url
Implements a review suggestion from 72e971e. Refs: nodejs#47001 (comment) PR-URL: nodejs#47035 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent b0f9629 commit 0dbacb1

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

src/node_url.cc

+22-26
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ using v8::Object;
2222
using v8::String;
2323
using v8::Value;
2424

25-
Local<String> Utf8String(Isolate* isolate, const std::string& str) {
26-
return String::NewFromUtf8(isolate,
27-
str.data(),
28-
NewStringType::kNormal,
29-
str.length()).ToLocalChecked();
30-
}
31-
3225
namespace url {
3326
namespace {
3427

@@ -45,20 +38,25 @@ enum url_update_action {
4538
kHref = 9,
4639
};
4740

48-
void SetArgs(Environment* env,
49-
Local<Value> (*argv)[10],
50-
const ada::result& url) {
41+
auto GetCallbackArgs(Environment* env, const ada::result& url) {
42+
Local<Context> context = env->context();
5143
Isolate* isolate = env->isolate();
52-
(*argv)[0] = Utf8String(isolate, url->get_href());
53-
(*argv)[1] = Utf8String(isolate, url->get_origin());
54-
(*argv)[2] = Utf8String(isolate, url->get_protocol());
55-
(*argv)[3] = Utf8String(isolate, url->get_hostname());
56-
(*argv)[4] = Utf8String(isolate, url->get_pathname());
57-
(*argv)[5] = Utf8String(isolate, url->get_search());
58-
(*argv)[6] = Utf8String(isolate, url->get_username());
59-
(*argv)[7] = Utf8String(isolate, url->get_password());
60-
(*argv)[8] = Utf8String(isolate, url->get_port());
61-
(*argv)[9] = Utf8String(isolate, url->get_hash());
44+
45+
auto js_string = [&](std::string_view sv) {
46+
return ToV8Value(context, sv, isolate).ToLocalChecked();
47+
};
48+
return std::array{
49+
js_string(url->get_href()),
50+
js_string(url->get_origin()),
51+
js_string(url->get_protocol()),
52+
js_string(url->get_hostname()),
53+
js_string(url->get_pathname()),
54+
js_string(url->get_search()),
55+
js_string(url->get_username()),
56+
js_string(url->get_password()),
57+
js_string(url->get_port()),
58+
js_string(url->get_hash()),
59+
};
6260
}
6361

6462
void Parse(const FunctionCallbackInfo<Value>& args) {
@@ -89,10 +87,9 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
8987
return args.GetReturnValue().Set(false);
9088
}
9189

92-
Local<Value> argv[10];
93-
SetArgs(env, &argv, out);
90+
auto argv = GetCallbackArgs(env, out);
9491
USE(success_callback_->Call(
95-
env->context(), args.This(), arraysize(argv), argv));
92+
env->context(), args.This(), argv.size(), argv.data()));
9693
args.GetReturnValue().Set(true);
9794
}
9895

@@ -223,10 +220,9 @@ void UpdateUrl(const FunctionCallbackInfo<Value>& args) {
223220
}
224221
}
225222

226-
Local<Value> argv[10];
227-
SetArgs(env, &argv, out);
223+
auto argv = GetCallbackArgs(env, out);
228224
USE(success_callback_->Call(
229-
env->context(), args.This(), arraysize(argv), argv));
225+
env->context(), args.This(), argv.size(), argv.data()));
230226
args.GetReturnValue().Set(result);
231227
}
232228

0 commit comments

Comments
 (0)