Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: use std::array for passing argv in node::url #47035

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ using v8::Object;
using v8::String;
using v8::Value;

Local<String> Utf8String(Isolate* isolate, const std::string& str) {
return String::NewFromUtf8(isolate,
str.data(),
NewStringType::kNormal,
str.length()).ToLocalChecked();
}

namespace url {
namespace {

Expand All @@ -45,20 +38,25 @@ enum url_update_action {
kHref = 9,
};

void SetArgs(Environment* env,
Local<Value> (*argv)[10],
const ada::result& url) {
auto GetCallbackArgs(Environment* env, const ada::result& url) {
Local<Context> context = env->context();
Isolate* isolate = env->isolate();
(*argv)[0] = Utf8String(isolate, url->get_href());
(*argv)[1] = Utf8String(isolate, url->get_origin());
(*argv)[2] = Utf8String(isolate, url->get_protocol());
(*argv)[3] = Utf8String(isolate, url->get_hostname());
(*argv)[4] = Utf8String(isolate, url->get_pathname());
(*argv)[5] = Utf8String(isolate, url->get_search());
(*argv)[6] = Utf8String(isolate, url->get_username());
(*argv)[7] = Utf8String(isolate, url->get_password());
(*argv)[8] = Utf8String(isolate, url->get_port());
(*argv)[9] = Utf8String(isolate, url->get_hash());

auto js_string = [&](std::string_view sv) {
return ToV8Value(context, sv, isolate).ToLocalChecked();
};
return std::array{
js_string(url->get_href()),
js_string(url->get_origin()),
js_string(url->get_protocol()),
js_string(url->get_hostname()),
js_string(url->get_pathname()),
js_string(url->get_search()),
js_string(url->get_username()),
js_string(url->get_password()),
js_string(url->get_port()),
js_string(url->get_hash()),
};
}

void Parse(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -89,10 +87,9 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().Set(false);
}

Local<Value> argv[10];
SetArgs(env, &argv, out);
auto argv = GetCallbackArgs(env, out);
USE(success_callback_->Call(
env->context(), args.This(), arraysize(argv), argv));
env->context(), args.This(), argv.size(), argv.data()));
args.GetReturnValue().Set(true);
}

Expand Down Expand Up @@ -223,10 +220,9 @@ void UpdateUrl(const FunctionCallbackInfo<Value>& args) {
}
}

Local<Value> argv[10];
SetArgs(env, &argv, out);
auto argv = GetCallbackArgs(env, out);
USE(success_callback_->Call(
env->context(), args.This(), arraysize(argv), argv));
env->context(), args.This(), argv.size(), argv.data()));
args.GetReturnValue().Set(result);
}

Expand Down