Skip to content

Commit d5ac3e3

Browse files
JonasBanodejs-github-bot
authored andcommittedMar 16, 2025··
src: define urlpattern components using a macro
PR-URL: #57452 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 26c4851 commit d5ac3e3

File tree

2 files changed

+52
-187
lines changed

2 files changed

+52
-187
lines changed
 

‎src/node_url_pattern.cc

+32-170
Original file line numberDiff line numberDiff line change
@@ -462,48 +462,17 @@ URLPattern::URLPatternOptions::FromJsObject(Environment* env,
462462
// by returning std::nullopt.
463463
return std::nullopt;
464464
}
465-
return options;
466-
}
467-
468-
MaybeLocal<Value> URLPattern::Hash() const {
469-
auto context = env()->context();
470-
return ToV8Value(context, url_pattern_.get_hash());
471-
}
472-
473-
MaybeLocal<Value> URLPattern::Hostname() const {
474-
auto context = env()->context();
475-
return ToV8Value(context, url_pattern_.get_hostname());
476-
}
477-
478-
MaybeLocal<Value> URLPattern::Password() const {
479-
auto context = env()->context();
480-
return ToV8Value(context, url_pattern_.get_password());
481-
}
482-
483-
MaybeLocal<Value> URLPattern::Pathname() const {
484-
auto context = env()->context();
485-
return ToV8Value(context, url_pattern_.get_pathname());
486-
}
487-
488-
MaybeLocal<Value> URLPattern::Port() const {
489-
auto context = env()->context();
490-
return ToV8Value(context, url_pattern_.get_port());
491-
}
492-
493-
MaybeLocal<Value> URLPattern::Protocol() const {
494-
auto context = env()->context();
495-
return ToV8Value(context, url_pattern_.get_protocol());
496-
}
497465

498-
MaybeLocal<Value> URLPattern::Search() const {
499-
auto context = env()->context();
500-
return ToV8Value(context, url_pattern_.get_search());
466+
return options;
501467
}
502468

503-
MaybeLocal<Value> URLPattern::Username() const {
504-
auto context = env()->context();
505-
return ToV8Value(context, url_pattern_.get_username());
506-
}
469+
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
470+
MaybeLocal<Value> URLPattern::uppercase_name() const { \
471+
auto context = env()->context(); \
472+
return ToV8Value(context, url_pattern_.get_##lowercase_name()); \
473+
}
474+
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
475+
#undef URL_PATTERN_COMPONENT_GETTERS
507476

508477
bool URLPattern::HasRegExpGroups() const {
509478
return url_pattern_.has_regexp_groups();
@@ -616,77 +585,17 @@ void URLPattern::Test(const FunctionCallbackInfo<Value>& args) {
616585
args.GetReturnValue().Set(url_pattern->Test(env, input, baseURL_opt));
617586
}
618587

619-
void URLPattern::Protocol(const FunctionCallbackInfo<Value>& info) {
620-
URLPattern* url_pattern;
621-
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
622-
Local<Value> result;
623-
if (url_pattern->Protocol().ToLocal(&result)) {
624-
info.GetReturnValue().Set(result);
625-
}
626-
}
627-
628-
void URLPattern::Username(const FunctionCallbackInfo<Value>& info) {
629-
URLPattern* url_pattern;
630-
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
631-
Local<Value> result;
632-
if (url_pattern->Username().ToLocal(&result)) {
633-
info.GetReturnValue().Set(result);
634-
}
635-
}
636-
637-
void URLPattern::Password(const FunctionCallbackInfo<Value>& info) {
638-
URLPattern* url_pattern;
639-
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
640-
Local<Value> result;
641-
if (url_pattern->Password().ToLocal(&result)) {
642-
info.GetReturnValue().Set(result);
643-
}
644-
}
645-
646-
void URLPattern::Hostname(const FunctionCallbackInfo<Value>& info) {
647-
URLPattern* url_pattern;
648-
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
649-
Local<Value> result;
650-
if (url_pattern->Hostname().ToLocal(&result)) {
651-
info.GetReturnValue().Set(result);
652-
}
653-
}
654-
655-
void URLPattern::Port(const FunctionCallbackInfo<Value>& info) {
656-
URLPattern* url_pattern;
657-
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
658-
Local<Value> result;
659-
if (url_pattern->Port().ToLocal(&result)) {
660-
info.GetReturnValue().Set(result);
661-
}
662-
}
663-
664-
void URLPattern::Pathname(const FunctionCallbackInfo<Value>& info) {
665-
URLPattern* url_pattern;
666-
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
667-
Local<Value> result;
668-
if (url_pattern->Pathname().ToLocal(&result)) {
669-
info.GetReturnValue().Set(result);
670-
}
671-
}
672-
673-
void URLPattern::Search(const FunctionCallbackInfo<Value>& info) {
674-
URLPattern* url_pattern;
675-
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
676-
Local<Value> result;
677-
if (url_pattern->Search().ToLocal(&result)) {
678-
info.GetReturnValue().Set(result);
679-
}
680-
}
681-
682-
void URLPattern::Hash(const FunctionCallbackInfo<Value>& info) {
683-
URLPattern* url_pattern;
684-
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This());
685-
Local<Value> result;
686-
if (url_pattern->Hash().ToLocal(&result)) {
687-
info.GetReturnValue().Set(result);
588+
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
589+
void URLPattern::uppercase_name(const FunctionCallbackInfo<Value>& info) { \
590+
URLPattern* url_pattern; \
591+
ASSIGN_OR_RETURN_UNWRAP(&url_pattern, info.This()); \
592+
Local<Value> result; \
593+
if (url_pattern->uppercase_name().ToLocal(&result)) { \
594+
info.GetReturnValue().Set(result); \
595+
} \
688596
}
689-
}
597+
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
598+
#undef URL_PATTERN_COMPONENT_GETTERS
690599

691600
void URLPattern::HasRegexpGroups(const FunctionCallbackInfo<Value>& info) {
692601
URLPattern* url_pattern;
@@ -696,14 +605,10 @@ void URLPattern::HasRegexpGroups(const FunctionCallbackInfo<Value>& info) {
696605

697606
static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
698607
registry->Register(URLPattern::New);
699-
registry->Register(URLPattern::Protocol);
700-
registry->Register(URLPattern::Username);
701-
registry->Register(URLPattern::Password);
702-
registry->Register(URLPattern::Hostname);
703-
registry->Register(URLPattern::Port);
704-
registry->Register(URLPattern::Pathname);
705-
registry->Register(URLPattern::Search);
706-
registry->Register(URLPattern::Hash);
608+
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, _) \
609+
registry->Register(URLPattern::uppercase_name);
610+
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
611+
#undef URL_PATTERN_COMPONENT_GETTERS
707612
registry->Register(URLPattern::HasRegexpGroups);
708613
registry->Register(URLPattern::Exec);
709614
registry->Register(URLPattern::Test);
@@ -726,61 +631,18 @@ static void Initialize(Local<Object> target,
726631
auto signature = Signature::New(isolate, ctor_tmpl);
727632

728633
instance_template->SetInternalFieldCount(URLPattern::kInternalFieldCount);
729-
prototype_template->SetAccessorProperty(
730-
env->protocol_string(),
731-
FunctionTemplate::New(
732-
isolate, URLPattern::Protocol, Local<Value>(), signature),
733-
Local<FunctionTemplate>(),
734-
attributes);
735-
736-
prototype_template->SetAccessorProperty(
737-
env->username_string(),
738-
FunctionTemplate::New(
739-
isolate, URLPattern::Username, Local<Value>(), signature),
740-
Local<FunctionTemplate>(),
741-
attributes);
742-
743-
prototype_template->SetAccessorProperty(
744-
env->password_string(),
745-
FunctionTemplate::New(
746-
isolate, URLPattern::Password, Local<Value>(), signature),
747-
Local<FunctionTemplate>(),
748-
attributes);
749-
750-
prototype_template->SetAccessorProperty(
751-
env->hostname_string(),
752-
FunctionTemplate::New(
753-
isolate, URLPattern::Hostname, Local<Value>(), signature),
754-
Local<FunctionTemplate>(),
755-
attributes);
756-
757-
prototype_template->SetAccessorProperty(
758-
env->port_string(),
759-
FunctionTemplate::New(
760-
isolate, URLPattern::Port, Local<Value>(), signature),
761-
Local<FunctionTemplate>(),
762-
attributes);
763634

764-
prototype_template->SetAccessorProperty(
765-
env->pathname_string(),
766-
FunctionTemplate::New(
767-
isolate, URLPattern::Pathname, Local<Value>(), signature),
768-
Local<FunctionTemplate>(),
769-
attributes);
770-
771-
prototype_template->SetAccessorProperty(
772-
env->search_string(),
773-
FunctionTemplate::New(
774-
isolate, URLPattern::Search, Local<Value>(), signature),
775-
Local<FunctionTemplate>(),
776-
attributes);
777-
778-
prototype_template->SetAccessorProperty(
779-
env->hash_string(),
780-
FunctionTemplate::New(
781-
isolate, URLPattern::Hash, Local<Value>(), signature),
782-
Local<FunctionTemplate>(),
635+
#define ENV_GETTER(lowercase_name) env->lowercase_name##_string()
636+
#define URL_PATTERN_COMPONENT_GETTERS(uppercase_name, lowercase_name) \
637+
prototype_template->SetAccessorProperty( \
638+
ENV_GETTER(lowercase_name), \
639+
FunctionTemplate::New( \
640+
isolate, URLPattern::uppercase_name, Local<Value>(), signature), \
641+
Local<FunctionTemplate>(), \
783642
attributes);
643+
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
644+
#undef URL_PATTERN_COMPONENT_GETTERS
645+
#undef ENV_GETTER
784646

785647
prototype_template->SetAccessorProperty(
786648
env->has_regexp_groups_string(),

‎src/node_url_pattern.h

+20-17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414

1515
namespace node::url_pattern {
1616

17+
#define URL_PATTERN_COMPONENTS(V) \
18+
V(Protocol, protocol) \
19+
V(Username, username) \
20+
V(Password, password) \
21+
V(Hostname, hostname) \
22+
V(Port, port) \
23+
V(Pathname, pathname) \
24+
V(Search, search) \
25+
V(Hash, hash)
26+
1727
// By default, ada::url_pattern doesn't ship with any regex library.
1828
// Ada has a std::regex implementation, but it is considered unsafe and does
1929
// not have a fully compliant ecmascript syntax support. Therefore, Ada
@@ -42,15 +52,12 @@ class URLPattern : public BaseObject {
4252
// - Functions
4353
static void Exec(const v8::FunctionCallbackInfo<v8::Value>& info);
4454
static void Test(const v8::FunctionCallbackInfo<v8::Value>& info);
45-
// - Getters
46-
static void Hash(const v8::FunctionCallbackInfo<v8::Value>& info);
47-
static void Hostname(const v8::FunctionCallbackInfo<v8::Value>& info);
48-
static void Password(const v8::FunctionCallbackInfo<v8::Value>& info);
49-
static void Pathname(const v8::FunctionCallbackInfo<v8::Value>& info);
50-
static void Port(const v8::FunctionCallbackInfo<v8::Value>& info);
51-
static void Protocol(const v8::FunctionCallbackInfo<v8::Value>& info);
52-
static void Search(const v8::FunctionCallbackInfo<v8::Value>& info);
53-
static void Username(const v8::FunctionCallbackInfo<v8::Value>& info);
55+
// - Component Getters
56+
#define URL_PATTERN_COMPONENT_GETTERS(name, _) \
57+
static void name(const v8::FunctionCallbackInfo<v8::Value>& info);
58+
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
59+
#undef URL_PATTERN_COMPONENT_GETTERS
60+
// - Has Regexp Groups
5461
static void HasRegexpGroups(const v8::FunctionCallbackInfo<v8::Value>& info);
5562

5663
void MemoryInfo(MemoryTracker* tracker) const override;
@@ -86,14 +93,10 @@ class URLPattern : public BaseObject {
8693
private:
8794
ada::url_pattern<URLPatternRegexProvider> url_pattern_;
8895
// Getter methods
89-
v8::MaybeLocal<v8::Value> Hash() const;
90-
v8::MaybeLocal<v8::Value> Hostname() const;
91-
v8::MaybeLocal<v8::Value> Password() const;
92-
v8::MaybeLocal<v8::Value> Pathname() const;
93-
v8::MaybeLocal<v8::Value> Port() const;
94-
v8::MaybeLocal<v8::Value> Protocol() const;
95-
v8::MaybeLocal<v8::Value> Search() const;
96-
v8::MaybeLocal<v8::Value> Username() const;
96+
#define URL_PATTERN_COMPONENT_GETTERS(name, _) \
97+
v8::MaybeLocal<v8::Value> name() const;
98+
URL_PATTERN_COMPONENTS(URL_PATTERN_COMPONENT_GETTERS)
99+
#undef URL_PATTERN_COMPONENT_GETTERS
97100
bool HasRegExpGroups() const;
98101
// Public API
99102
v8::MaybeLocal<v8::Value> Exec(

0 commit comments

Comments
 (0)
Please sign in to comment.