@@ -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,88 @@ 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
+ tmpl->SetClassName (FIXED_ONE_BYTE_STRING (env->isolate (), " URLPattern" ));
753
+ auto instance_template = tmpl->InstanceTemplate ();
754
+ auto prototype_template = tmpl->PrototypeTemplate ();
755
+
756
+ instance_template->SetInternalFieldCount (URLPattern::kInternalFieldCount );
757
+ prototype_template->SetAccessorProperty (
758
+ env->protocol_string (),
759
+ FunctionTemplate::New (env->isolate (), URLPattern::Protocol),
760
+ Local<FunctionTemplate>(),
761
+ attributes);
762
+
763
+ prototype_template->SetAccessorProperty (
764
+ env->username_string (),
765
+ FunctionTemplate::New (env->isolate (), URLPattern::Username),
766
+ Local<FunctionTemplate>(),
767
+ attributes);
768
+
769
+ prototype_template->SetAccessorProperty (
770
+ env->password_string (),
771
+ FunctionTemplate::New (env->isolate (), URLPattern::Password),
772
+ Local<FunctionTemplate>(),
773
+ attributes);
774
+
775
+ prototype_template->SetAccessorProperty (
776
+ env->hostname_string (),
777
+ FunctionTemplate::New (env->isolate (), URLPattern::Hostname),
778
+ Local<FunctionTemplate>(),
779
+ attributes);
780
+
781
+ prototype_template->SetAccessorProperty (
782
+ env->port_string (),
783
+ FunctionTemplate::New (env->isolate (), URLPattern::Port),
784
+ Local<FunctionTemplate>(),
785
+ attributes);
786
+
787
+ prototype_template->SetAccessorProperty (
788
+ env->pathname_string (),
789
+ FunctionTemplate::New (env->isolate (), URLPattern::Pathname),
790
+ Local<FunctionTemplate>(),
791
+ attributes);
792
+
793
+ prototype_template->SetAccessorProperty (
794
+ env->search_string (),
795
+ FunctionTemplate::New (env->isolate (), URLPattern::Search),
796
+ Local<FunctionTemplate>(),
797
+ attributes);
798
+
799
+ prototype_template->SetAccessorProperty (
800
+ env->hash_string (),
801
+ FunctionTemplate::New (env->isolate (), URLPattern::Hash),
802
+ Local<FunctionTemplate>(),
803
+ attributes);
804
+
805
+ prototype_template->SetAccessorProperty (
806
+ env->has_regexp_groups_string (),
807
+ FunctionTemplate::New (env->isolate (), URLPattern::HasRegexpGroups),
808
+ Local<FunctionTemplate>(),
809
+ attributes);
810
+
811
+ SetProtoMethodNoSideEffect (env->isolate (), tmpl, " exec" , URLPattern::Exec);
812
+ SetProtoMethodNoSideEffect (env->isolate (), tmpl, " test" , URLPattern::Test);
813
+ env->set_urlpattern_constructor_template (tmpl);
814
+ }
815
+ return tmpl;
816
+ }
817
+
818
+ bool URLPattern::HasInstance (Environment* env, Local<Value> value) {
819
+ return GetConstructorTemplate (env)->HasInstance (value);
820
+ }
821
+
711
822
static void Initialize (Local<Object> target,
712
823
Local<Value> unused,
713
824
Local<Context> context,
714
825
void * priv) {
715
826
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);
827
+ auto ctor_tmpl = URLPattern::GetConstructorTemplate (env);
828
+ CHECK (!ctor_tmpl.IsEmpty ());
780
829
SetConstructorFunction (context, target, " URLPattern" , ctor_tmpl);
781
830
}
782
831
0 commit comments