From d5ac3e3803f5e0038c8b34b889bd7adb29b46f7d Mon Sep 17 00:00:00 2001 From: JonasBa Date: Thu, 13 Mar 2025 21:16:13 -0400 Subject: [PATCH] src: define urlpattern components using a macro PR-URL: https://github.com/nodejs/node/pull/57452 Reviewed-By: Yagiz Nizipli Reviewed-By: Darshan Sen Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Anna Henningsen --- src/node_url_pattern.cc | 202 +++++++--------------------------------- src/node_url_pattern.h | 37 ++++---- 2 files changed, 52 insertions(+), 187 deletions(-) diff --git a/src/node_url_pattern.cc b/src/node_url_pattern.cc index 3a7ab636264..6b62d516c43 100644 --- a/src/node_url_pattern.cc +++ b/src/node_url_pattern.cc @@ -462,48 +462,17 @@ URLPattern::URLPatternOptions::FromJsObject(Environment* env, // by returning std::nullopt. return std::nullopt; } + return options; } -MaybeLocal URLPattern::Hash() const { - auto context = env()->context(); - return ToV8Value(context, url_pattern_.get_hash()); -} - -MaybeLocal URLPattern::Hostname() const { - auto context = env()->context(); - return ToV8Value(context, url_pattern_.get_hostname()); -} - -MaybeLocal URLPattern::Password() const { - auto context = env()->context(); - return ToV8Value(context, url_pattern_.get_password()); -} - -MaybeLocal URLPattern::Pathname() const { - auto context = env()->context(); - return ToV8Value(context, url_pattern_.get_pathname()); -} - -MaybeLocal URLPattern::Port() const { - auto context = env()->context(); - return ToV8Value(context, url_pattern_.get_port()); -} - -MaybeLocal URLPattern::Protocol() const { - auto context = env()->context(); - return ToV8Value(context, url_pattern_.get_protocol()); -} - -MaybeLocal URLPattern::Search() const { - auto context = env()->context(); - return ToV8Value(context, url_pattern_.get_search()); -} - -MaybeLocal URLPattern::Username() const { - auto context = env()->context(); - return ToV8Value(context, url_pattern_.get_username()); -} +#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \ + MaybeLocal URLPattern::uppercase_name() const { \ + auto context = env()->context(); \ + return ToV8Value(context, url_pattern_.get_##lowercase_name()); \ + } +URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS) +#undef URL_PATTERN_COMPONENT_GETTERS bool URLPattern::HasRegExpGroups() const { return url_pattern_.has_regexp_groups(); @@ -616,77 +585,17 @@ void URLPattern::Test(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(url_pattern->Test(env, input, baseURL_opt)); } -void URLPattern::Protocol(const FunctionCallbackInfo& info) { - URLPattern* url_pattern; - ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); - Local result; - if (url_pattern->Protocol().ToLocal(&result)) { - info.GetReturnValue().Set(result); +#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \ + void URLPattern::uppercase_name(const FunctionCallbackInfo& info) { \ + URLPattern* url_pattern; \ + ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); \ + Local result; \ + if (url_pattern->uppercase_name().ToLocal(&result)) { \ + info.GetReturnValue().Set(result); \ + } \ } -} - -void URLPattern::Username(const FunctionCallbackInfo& info) { - URLPattern* url_pattern; - ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); - Local result; - if (url_pattern->Username().ToLocal(&result)) { - info.GetReturnValue().Set(result); - } -} - -void URLPattern::Password(const FunctionCallbackInfo& info) { - URLPattern* url_pattern; - ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); - Local result; - if (url_pattern->Password().ToLocal(&result)) { - info.GetReturnValue().Set(result); - } -} - -void URLPattern::Hostname(const FunctionCallbackInfo& info) { - URLPattern* url_pattern; - ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); - Local result; - if (url_pattern->Hostname().ToLocal(&result)) { - info.GetReturnValue().Set(result); - } -} - -void URLPattern::Port(const FunctionCallbackInfo& info) { - URLPattern* url_pattern; - ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); - Local result; - if (url_pattern->Port().ToLocal(&result)) { - info.GetReturnValue().Set(result); - } -} - -void URLPattern::Pathname(const FunctionCallbackInfo& info) { - URLPattern* url_pattern; - ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); - Local result; - if (url_pattern->Pathname().ToLocal(&result)) { - info.GetReturnValue().Set(result); - } -} - -void URLPattern::Search(const FunctionCallbackInfo& info) { - URLPattern* url_pattern; - ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); - Local result; - if (url_pattern->Search().ToLocal(&result)) { - info.GetReturnValue().Set(result); - } -} - -void URLPattern::Hash(const FunctionCallbackInfo& info) { - URLPattern* url_pattern; - ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); - Local result; - if (url_pattern->Hash().ToLocal(&result)) { - info.GetReturnValue().Set(result); - } -} +URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS) +#undef URL_PATTERN_COMPONENT_GETTERS void URLPattern::HasRegexpGroups(const FunctionCallbackInfo& info) { URLPattern* url_pattern; @@ -696,14 +605,10 @@ void URLPattern::HasRegexpGroups(const FunctionCallbackInfo& info) { static void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(URLPattern::New); - registry->Register(URLPattern::Protocol); - registry->Register(URLPattern::Username); - registry->Register(URLPattern::Password); - registry->Register(URLPattern::Hostname); - registry->Register(URLPattern::Port); - registry->Register(URLPattern::Pathname); - registry->Register(URLPattern::Search); - registry->Register(URLPattern::Hash); +#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, _) \ + registry->Register(URLPattern::uppercase_name); + URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS) +#undef URL_PATTERN_COMPONENT_GETTERS registry->Register(URLPattern::HasRegexpGroups); registry->Register(URLPattern::Exec); registry->Register(URLPattern::Test); @@ -726,61 +631,18 @@ static void Initialize(Local target, auto signature = Signature::New(isolate, ctor_tmpl); instance_template->SetInternalFieldCount(URLPattern::kInternalFieldCount); - prototype_template->SetAccessorProperty( - env->protocol_string(), - FunctionTemplate::New( - isolate, URLPattern::Protocol, Local(), signature), - Local(), - attributes); - prototype_template->SetAccessorProperty( - env->username_string(), - FunctionTemplate::New( - isolate, URLPattern::Username, Local(), signature), - Local(), - attributes); - - prototype_template->SetAccessorProperty( - env->password_string(), - FunctionTemplate::New( - isolate, URLPattern::Password, Local(), signature), - Local(), - attributes); - - prototype_template->SetAccessorProperty( - env->hostname_string(), - FunctionTemplate::New( - isolate, URLPattern::Hostname, Local(), signature), - Local(), - attributes); - - prototype_template->SetAccessorProperty( - env->port_string(), - FunctionTemplate::New( - isolate, URLPattern::Port, Local(), signature), - Local(), - attributes); - - prototype_template->SetAccessorProperty( - env->pathname_string(), - FunctionTemplate::New( - isolate, URLPattern::Pathname, Local(), signature), - Local(), - attributes); - - prototype_template->SetAccessorProperty( - env->search_string(), - FunctionTemplate::New( - isolate, URLPattern::Search, Local(), signature), - Local(), - attributes); - - prototype_template->SetAccessorProperty( - env->hash_string(), - FunctionTemplate::New( - isolate, URLPattern::Hash, Local(), signature), - Local(), +#define ENV_GETTER(lowercase_name) env->lowercase_name##_string() +#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \ + prototype_template->SetAccessorProperty( \ + ENV_GETTER(lowercase_name), \ + FunctionTemplate::New( \ + isolate, URLPattern::uppercase_name, Local(), signature), \ + Local(), \ attributes); + URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS) +#undef URL_PATTERN_COMPONENT_GETTERS +#undef ENV_GETTER prototype_template->SetAccessorProperty( env->has_regexp_groups_string(), diff --git a/src/node_url_pattern.h b/src/node_url_pattern.h index bfb0cd00fa8..e9209c742e5 100644 --- a/src/node_url_pattern.h +++ b/src/node_url_pattern.h @@ -14,6 +14,16 @@ namespace node::url_pattern { +#define URL_PATTERN_COMPONENTS(V) \ + V(Protocol, protocol) \ + V(Username, username) \ + V(Password, password) \ + V(Hostname, hostname) \ + V(Port, port) \ + V(Pathname, pathname) \ + V(Search, search) \ + V(Hash, hash) + // By default, ada::url_pattern doesn't ship with any regex library. // Ada has a std::regex implementation, but it is considered unsafe and does // not have a fully compliant ecmascript syntax support. Therefore, Ada @@ -42,15 +52,12 @@ class URLPattern : public BaseObject { // - Functions static void Exec(const v8::FunctionCallbackInfo& info); static void Test(const v8::FunctionCallbackInfo& info); - // - Getters - static void Hash(const v8::FunctionCallbackInfo& info); - static void Hostname(const v8::FunctionCallbackInfo& info); - static void Password(const v8::FunctionCallbackInfo& info); - static void Pathname(const v8::FunctionCallbackInfo& info); - static void Port(const v8::FunctionCallbackInfo& info); - static void Protocol(const v8::FunctionCallbackInfo& info); - static void Search(const v8::FunctionCallbackInfo& info); - static void Username(const v8::FunctionCallbackInfo& info); + // - Component Getters +#define URL_PATTERN_COMPONENT_GETTERS(name, _) \ + static void name(const v8::FunctionCallbackInfo& info); + URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS) +#undef URL_PATTERN_COMPONENT_GETTERS + // - Has Regexp Groups static void HasRegexpGroups(const v8::FunctionCallbackInfo& info); void MemoryInfo(MemoryTracker* tracker) const override; @@ -86,14 +93,10 @@ class URLPattern : public BaseObject { private: ada::url_pattern url_pattern_; // Getter methods - v8::MaybeLocal Hash() const; - v8::MaybeLocal Hostname() const; - v8::MaybeLocal Password() const; - v8::MaybeLocal Pathname() const; - v8::MaybeLocal Port() const; - v8::MaybeLocal Protocol() const; - v8::MaybeLocal Search() const; - v8::MaybeLocal Username() const; +#define URL_PATTERN_COMPONENT_GETTERS(name, _) \ + v8::MaybeLocal name() const; + URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS) +#undef URL_PATTERN_COMPONENT_GETTERS bool HasRegExpGroups() const; // Public API v8::MaybeLocal Exec(