@@ -616,6 +616,10 @@ void URLPattern::Test(const FunctionCallbackInfo<Value>& args) {
616
616
}
617
617
618
618
void URLPattern::Protocol (const FunctionCallbackInfo<Value>& info) {
619
+ auto env = Environment::GetCurrent (info);
620
+ if (!HasInstance (env, info.This ())) {
621
+ return THROW_ERR_INVALID_THIS (env);
622
+ }
619
623
URLPattern* url_pattern;
620
624
ASSIGN_OR_RETURN_UNWRAP (&url_pattern, info.This ());
621
625
Local<Value> result;
@@ -625,6 +629,10 @@ void URLPattern::Protocol(const FunctionCallbackInfo<Value>& info) {
625
629
}
626
630
627
631
void URLPattern::Username (const FunctionCallbackInfo<Value>& info) {
632
+ auto env = Environment::GetCurrent (info);
633
+ if (!HasInstance (env, info.This ())) {
634
+ return THROW_ERR_INVALID_THIS (env);
635
+ }
628
636
URLPattern* url_pattern;
629
637
ASSIGN_OR_RETURN_UNWRAP (&url_pattern, info.This ());
630
638
Local<Value> result;
@@ -634,6 +642,10 @@ void URLPattern::Username(const FunctionCallbackInfo<Value>& info) {
634
642
}
635
643
636
644
void URLPattern::Password (const FunctionCallbackInfo<Value>& info) {
645
+ auto env = Environment::GetCurrent (info);
646
+ if (!HasInstance (env, info.This ())) {
647
+ return THROW_ERR_INVALID_THIS (env);
648
+ }
637
649
URLPattern* url_pattern;
638
650
ASSIGN_OR_RETURN_UNWRAP (&url_pattern, info.This ());
639
651
Local<Value> result;
@@ -643,6 +655,10 @@ void URLPattern::Password(const FunctionCallbackInfo<Value>& info) {
643
655
}
644
656
645
657
void URLPattern::Hostname (const FunctionCallbackInfo<Value>& info) {
658
+ auto env = Environment::GetCurrent (info);
659
+ if (!HasInstance (env, info.This ())) {
660
+ return THROW_ERR_INVALID_THIS (env);
661
+ }
646
662
URLPattern* url_pattern;
647
663
ASSIGN_OR_RETURN_UNWRAP (&url_pattern, info.This ());
648
664
Local<Value> result;
@@ -652,6 +668,10 @@ void URLPattern::Hostname(const FunctionCallbackInfo<Value>& info) {
652
668
}
653
669
654
670
void URLPattern::Port (const FunctionCallbackInfo<Value>& info) {
671
+ auto env = Environment::GetCurrent (info);
672
+ if (!HasInstance (env, info.This ())) {
673
+ return THROW_ERR_INVALID_THIS (env);
674
+ }
655
675
URLPattern* url_pattern;
656
676
ASSIGN_OR_RETURN_UNWRAP (&url_pattern, info.This ());
657
677
Local<Value> result;
@@ -661,6 +681,10 @@ void URLPattern::Port(const FunctionCallbackInfo<Value>& info) {
661
681
}
662
682
663
683
void URLPattern::Pathname (const FunctionCallbackInfo<Value>& info) {
684
+ auto env = Environment::GetCurrent (info);
685
+ if (!HasInstance (env, info.This ())) {
686
+ return THROW_ERR_INVALID_THIS (env);
687
+ }
664
688
URLPattern* url_pattern;
665
689
ASSIGN_OR_RETURN_UNWRAP (&url_pattern, info.This ());
666
690
Local<Value> result;
@@ -670,6 +694,10 @@ void URLPattern::Pathname(const FunctionCallbackInfo<Value>& info) {
670
694
}
671
695
672
696
void URLPattern::Search (const FunctionCallbackInfo<Value>& info) {
697
+ auto env = Environment::GetCurrent (info);
698
+ if (!HasInstance (env, info.This ())) {
699
+ return THROW_ERR_INVALID_THIS (env);
700
+ }
673
701
URLPattern* url_pattern;
674
702
ASSIGN_OR_RETURN_UNWRAP (&url_pattern, info.This ());
675
703
Local<Value> result;
@@ -679,6 +707,10 @@ void URLPattern::Search(const FunctionCallbackInfo<Value>& info) {
679
707
}
680
708
681
709
void URLPattern::Hash (const FunctionCallbackInfo<Value>& info) {
710
+ auto env = Environment::GetCurrent (info);
711
+ if (!HasInstance (env, info.This ())) {
712
+ return THROW_ERR_INVALID_THIS (env);
713
+ }
682
714
URLPattern* url_pattern;
683
715
ASSIGN_OR_RETURN_UNWRAP (&url_pattern, info.This ());
684
716
Local<Value> result;
@@ -688,6 +720,10 @@ void URLPattern::Hash(const FunctionCallbackInfo<Value>& info) {
688
720
}
689
721
690
722
void URLPattern::HasRegexpGroups (const FunctionCallbackInfo<Value>& info) {
723
+ auto env = Environment::GetCurrent (info);
724
+ if (!HasInstance (env, info.This ())) {
725
+ return THROW_ERR_INVALID_THIS (env);
726
+ }
691
727
URLPattern* url_pattern;
692
728
ASSIGN_OR_RETURN_UNWRAP (&url_pattern, info.This ());
693
729
info.GetReturnValue ().Set (url_pattern->HasRegExpGroups ());
@@ -708,75 +744,87 @@ static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
708
744
registry->Register (URLPattern::Test);
709
745
}
710
746
747
+ Local<FunctionTemplate> URLPattern::GetConstructorTemplate (Environment* env) {
748
+ auto tmpl = env->urlpattern_constructor_template ();
749
+ if (tmpl.IsEmpty ()) {
750
+ auto attributes = static_cast <PropertyAttribute>(ReadOnly | DontDelete);
751
+ tmpl = NewFunctionTemplate (env->isolate (), URLPattern::New);
752
+ auto instance_template = tmpl->InstanceTemplate ();
753
+ auto prototype_template = tmpl->PrototypeTemplate ();
754
+
755
+ instance_template->SetInternalFieldCount (URLPattern::kInternalFieldCount );
756
+ prototype_template->SetAccessorProperty (
757
+ env->protocol_string (),
758
+ FunctionTemplate::New (env->isolate (), URLPattern::Protocol),
759
+ Local<FunctionTemplate>(),
760
+ attributes);
761
+
762
+ prototype_template->SetAccessorProperty (
763
+ env->username_string (),
764
+ FunctionTemplate::New (env->isolate (), URLPattern::Username),
765
+ Local<FunctionTemplate>(),
766
+ attributes);
767
+
768
+ prototype_template->SetAccessorProperty (
769
+ env->password_string (),
770
+ FunctionTemplate::New (env->isolate (), URLPattern::Password),
771
+ Local<FunctionTemplate>(),
772
+ attributes);
773
+
774
+ prototype_template->SetAccessorProperty (
775
+ env->hostname_string (),
776
+ FunctionTemplate::New (env->isolate (), URLPattern::Hostname),
777
+ Local<FunctionTemplate>(),
778
+ attributes);
779
+
780
+ prototype_template->SetAccessorProperty (
781
+ env->port_string (),
782
+ FunctionTemplate::New (env->isolate (), URLPattern::Port),
783
+ Local<FunctionTemplate>(),
784
+ attributes);
785
+
786
+ prototype_template->SetAccessorProperty (
787
+ env->pathname_string (),
788
+ FunctionTemplate::New (env->isolate (), URLPattern::Pathname),
789
+ Local<FunctionTemplate>(),
790
+ attributes);
791
+
792
+ prototype_template->SetAccessorProperty (
793
+ env->search_string (),
794
+ FunctionTemplate::New (env->isolate (), URLPattern::Search),
795
+ Local<FunctionTemplate>(),
796
+ attributes);
797
+
798
+ prototype_template->SetAccessorProperty (
799
+ env->hash_string (),
800
+ FunctionTemplate::New (env->isolate (), URLPattern::Hash),
801
+ Local<FunctionTemplate>(),
802
+ attributes);
803
+
804
+ prototype_template->SetAccessorProperty (
805
+ env->has_regexp_groups_string (),
806
+ FunctionTemplate::New (env->isolate (), URLPattern::HasRegexpGroups),
807
+ Local<FunctionTemplate>(),
808
+ attributes);
809
+
810
+ SetProtoMethodNoSideEffect (env->isolate (), tmpl, " exec" , URLPattern::Exec);
811
+ SetProtoMethodNoSideEffect (env->isolate (), tmpl, " test" , URLPattern::Test);
812
+ env->set_urlpattern_constructor_template (tmpl);
813
+ }
814
+ return tmpl;
815
+ }
816
+
817
+ bool URLPattern::HasInstance (Environment* env, Local<Value> value) {
818
+ return GetConstructorTemplate (env)->HasInstance (value);
819
+ }
820
+
711
821
static void Initialize (Local<Object> target,
712
822
Local<Value> unused,
713
823
Local<Context> context,
714
824
void * priv) {
715
825
Environment* env = Environment::GetCurrent (context);
716
- Isolate* isolate = env->isolate ();
717
- auto attributes = static_cast <PropertyAttribute>(ReadOnly | DontDelete);
718
- auto ctor_tmpl = NewFunctionTemplate (isolate, URLPattern::New);
719
- auto instance_template = ctor_tmpl->InstanceTemplate ();
720
- auto prototype_template = ctor_tmpl->PrototypeTemplate ();
721
- ctor_tmpl->SetClassName (FIXED_ONE_BYTE_STRING (isolate, " URLPattern" ));
722
-
723
- instance_template->SetInternalFieldCount (URLPattern::kInternalFieldCount );
724
- prototype_template->SetAccessorProperty (
725
- env->protocol_string (),
726
- FunctionTemplate::New (isolate, URLPattern::Protocol),
727
- Local<FunctionTemplate>(),
728
- attributes);
729
-
730
- prototype_template->SetAccessorProperty (
731
- env->username_string (),
732
- FunctionTemplate::New (isolate, URLPattern::Username),
733
- Local<FunctionTemplate>(),
734
- attributes);
735
-
736
- prototype_template->SetAccessorProperty (
737
- env->password_string (),
738
- FunctionTemplate::New (isolate, URLPattern::Password),
739
- Local<FunctionTemplate>(),
740
- attributes);
741
-
742
- prototype_template->SetAccessorProperty (
743
- env->hostname_string (),
744
- FunctionTemplate::New (isolate, URLPattern::Hostname),
745
- Local<FunctionTemplate>(),
746
- attributes);
747
-
748
- prototype_template->SetAccessorProperty (
749
- env->port_string (),
750
- FunctionTemplate::New (isolate, URLPattern::Port),
751
- Local<FunctionTemplate>(),
752
- attributes);
753
-
754
- prototype_template->SetAccessorProperty (
755
- env->pathname_string (),
756
- FunctionTemplate::New (isolate, URLPattern::Pathname),
757
- Local<FunctionTemplate>(),
758
- attributes);
759
-
760
- prototype_template->SetAccessorProperty (
761
- env->search_string (),
762
- FunctionTemplate::New (isolate, URLPattern::Search),
763
- Local<FunctionTemplate>(),
764
- attributes);
765
-
766
- prototype_template->SetAccessorProperty (
767
- env->hash_string (),
768
- FunctionTemplate::New (isolate, URLPattern::Hash),
769
- Local<FunctionTemplate>(),
770
- attributes);
771
-
772
- prototype_template->SetAccessorProperty (
773
- env->has_regexp_groups_string (),
774
- FunctionTemplate::New (isolate, URLPattern::HasRegexpGroups),
775
- Local<FunctionTemplate>(),
776
- attributes);
777
-
778
- SetProtoMethodNoSideEffect (isolate, ctor_tmpl, " exec" , URLPattern::Exec);
779
- SetProtoMethodNoSideEffect (isolate, ctor_tmpl, " test" , URLPattern::Test);
826
+ auto ctor_tmpl = URLPattern::GetConstructorTemplate (env);
827
+ CHECK (!ctor_tmpl.IsEmpty ());
780
828
SetConstructorFunction (context, target, " URLPattern" , ctor_tmpl);
781
829
}
782
830
0 commit comments