@@ -462,48 +462,17 @@ URLPattern::URLPatternOptions::FromJsObject(Environment* env,
462
462
// by returning std::nullopt.
463
463
return std::nullopt;
464
464
}
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
- }
497
465
498
- MaybeLocal<Value> URLPattern::Search () const {
499
- auto context = env ()->context ();
500
- return ToV8Value (context, url_pattern_.get_search ());
466
+ return options;
501
467
}
502
468
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
507
476
508
477
bool URLPattern::HasRegExpGroups () const {
509
478
return url_pattern_.has_regexp_groups ();
@@ -616,77 +585,17 @@ void URLPattern::Test(const FunctionCallbackInfo<Value>& args) {
616
585
args.GetReturnValue ().Set (url_pattern->Test (env, input, baseURL_opt));
617
586
}
618
587
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
+ } \
688
596
}
689
- }
597
+ URL_PATTERN_COMPONENTS (URL_PATTERN_COMPONENT_GETTERS)
598
+ #undef URL_PATTERN_COMPONENT_GETTERS
690
599
691
600
void URLPattern::HasRegexpGroups (const FunctionCallbackInfo<Value>& info) {
692
601
URLPattern* url_pattern;
@@ -696,14 +605,10 @@ void URLPattern::HasRegexpGroups(const FunctionCallbackInfo<Value>& info) {
696
605
697
606
static void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
698
607
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
707
612
registry->Register (URLPattern::HasRegexpGroups);
708
613
registry->Register (URLPattern::Exec);
709
614
registry->Register (URLPattern::Test);
@@ -726,61 +631,18 @@ static void Initialize(Local<Object> target,
726
631
auto signature = Signature::New (isolate, ctor_tmpl);
727
632
728
633
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);
763
634
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>(), \
783
642
attributes);
643
+ URL_PATTERN_COMPONENTS (URL_PATTERN_COMPONENT_GETTERS)
644
+ #undef URL_PATTERN_COMPONENT_GETTERS
645
+ #undef ENV_GETTER
784
646
785
647
prototype_template->SetAccessorProperty (
786
648
env->has_regexp_groups_string (),
0 commit comments