From c77afbb335483e24d9600b2c870cbd229f2c4f02 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 13 Mar 2023 21:28:57 +0100 Subject: [PATCH] src: use std::array for passing argv in node::url MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements a review suggestion from 72e971ee2e7d1c974. Refs: https://github.com/nodejs/node/pull/47001#issuecomment-1462999332 PR-URL: https://github.com/nodejs/node/pull/47035 Reviewed-By: Darshan Sen Reviewed-By: Yagiz Nizipli Reviewed-By: Joyee Cheung Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Tobias Nießen --- src/node_url.cc | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/node_url.cc b/src/node_url.cc index b5ec6f04a7055e..97fb128b7eb2ff 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -22,13 +22,6 @@ using v8::Object; using v8::String; using v8::Value; -Local Utf8String(Isolate* isolate, const std::string& str) { - return String::NewFromUtf8(isolate, - str.data(), - NewStringType::kNormal, - str.length()).ToLocalChecked(); -} - namespace url { namespace { @@ -45,20 +38,25 @@ enum url_update_action { kHref = 9, }; -void SetArgs(Environment* env, - Local (*argv)[10], - const ada::result& url) { +auto GetCallbackArgs(Environment* env, const ada::result& url) { + Local 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& args) { @@ -89,10 +87,9 @@ void Parse(const FunctionCallbackInfo& args) { return args.GetReturnValue().Set(false); } - Local 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); } @@ -223,10 +220,9 @@ void UpdateUrl(const FunctionCallbackInfo& args) { } } - Local 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); }