@@ -123,24 +123,20 @@ const nsAtom* URLInfo::HostAtom() const {
123
123
return mHostAtom ;
124
124
}
125
125
126
- const nsString & URLInfo::FilePath () const {
126
+ const nsCString & URLInfo::FilePath () const {
127
127
if (mFilePath .IsEmpty ()) {
128
- nsCString path;
129
128
nsCOMPtr<nsIURL> url = do_QueryInterface (mURI );
130
- if (url && NS_SUCCEEDED(url->GetFilePath (path))) {
131
- AppendUTF8toUTF16 (path, mFilePath );
132
- } else {
129
+ if (!url || NS_FAILED(url->GetFilePath (mFilePath ))) {
133
130
mFilePath = Path ();
134
131
}
135
132
}
136
133
return mFilePath ;
137
134
}
138
135
139
- const nsString & URLInfo::Path () const {
136
+ const nsCString & URLInfo::Path () const {
140
137
if (mPath .IsEmpty ()) {
141
- nsCString path;
142
- if (NS_SUCCEEDED(URINoRef ()->GetPathQueryRef (path))) {
143
- AppendUTF8toUTF16 (path, mPath );
138
+ if (NS_FAILED(URINoRef ()->GetPathQueryRef (mPath ))) {
139
+ mPath .Truncate ();
144
140
}
145
141
}
146
142
return mPath ;
@@ -359,14 +355,13 @@ void MatchPattern::Init(JSContext* aCx, const nsAString& aPattern,
359
355
return ;
360
356
}
361
357
362
- auto path = tail;
358
+ NS_ConvertUTF16toUTF8 path ( tail) ;
363
359
if (path.IsEmpty ()) {
364
360
aRv.Throw (NS_ERROR_INVALID_ARG);
365
361
return ;
366
362
}
367
363
368
- mPath = new MatchGlob (this );
369
- mPath ->Init (aCx, path, false , aRv);
364
+ mPath = new MatchGlob (this , path, false , aRv);
370
365
}
371
366
372
367
bool MatchPattern::MatchesDomain (const nsACString& aDomain) const {
@@ -622,27 +617,26 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE(MatchPatternSet)
622
617
* MatchGlob
623
618
*****************************************************************************/
624
619
625
- MatchGlob::~MatchGlob () { mozilla::DropJSObjects ( this ); }
620
+ MatchGlob::~MatchGlob () = default ;
626
621
627
622
/* static */
628
623
already_AddRefed<MatchGlob> MatchGlob::Constructor (dom::GlobalObject& aGlobal,
629
- const nsAString & aGlob,
624
+ const nsACString & aGlob,
630
625
bool aAllowQuestion,
631
626
ErrorResult& aRv) {
632
- RefPtr<MatchGlob> glob = new MatchGlob (aGlobal. GetAsSupports ());
633
- glob-> Init (aGlobal.Context (), aGlob, aAllowQuestion, aRv);
627
+ RefPtr<MatchGlob> glob =
628
+ new MatchGlob (aGlobal.GetAsSupports (), aGlob, aAllowQuestion, aRv);
634
629
if (aRv.Failed ()) {
635
630
return nullptr ;
636
631
}
637
632
return glob.forget ();
638
633
}
639
634
640
- void MatchGlob::Init (JSContext* aCx, const nsAString& aGlob,
641
- bool aAllowQuestion, ErrorResult& aRv) {
642
- mGlob = aGlob;
643
-
635
+ MatchGlob::MatchGlob (nsISupports* aParent, const nsACString& aGlob,
636
+ bool aAllowQuestion, ErrorResult& aRv)
637
+ : mParent (aParent), mGlob (aGlob) {
644
638
// Check for a literal match with no glob metacharacters.
645
- auto index = mGlob .FindCharInSet (aAllowQuestion ? u " *?" : u " *" );
639
+ auto index = mGlob .FindCharInSet (aAllowQuestion ? " *?" : " *" );
646
640
if (index < 0 ) {
647
641
mPathLiteral = mGlob ;
648
642
return ;
@@ -659,7 +653,7 @@ void MatchGlob::Init(JSContext* aCx, const nsAString& aGlob,
659
653
// Fall back to the regexp slow path.
660
654
constexpr auto metaChars = " .+*?^${}()|[]\\ " _ns;
661
655
662
- nsAutoString escaped;
656
+ nsAutoCString escaped;
663
657
escaped.Append (' ^' );
664
658
665
659
// For any continuous string of * (and ? if aAllowQuestion) wildcards, only
@@ -688,37 +682,15 @@ void MatchGlob::Init(JSContext* aCx, const nsAString& aGlob,
688
682
689
683
escaped.Append (' $' );
690
684
691
- // TODO: Switch to the Rust regexp crate, when Rust integration is easier.
692
- // It uses a much more efficient, linear time matching algorithm, and
693
- // doesn't require special casing for the literal and prefix cases.
694
- mRegExp = JS::NewUCRegExpObject (aCx, escaped.get (), escaped.Length (), 0 );
695
- if (mRegExp ) {
696
- mozilla::HoldJSObjects (this );
697
- } else {
698
- aRv.NoteJSContextException (aCx);
685
+ mRegExp = RustRegex (escaped);
686
+ if (!mRegExp ) {
687
+ aRv.ThrowTypeError (" failed to compile regex for glob" );
699
688
}
700
689
}
701
690
702
- bool MatchGlob::Matches (const nsAString & aString) const {
691
+ bool MatchGlob::Matches (const nsACString & aString) const {
703
692
if (mRegExp ) {
704
- AutoJSAPI jsapi;
705
- jsapi.Init ();
706
- JSContext* cx = jsapi.cx ();
707
-
708
- JSAutoRealm ar (cx, mRegExp );
709
-
710
- JS::Rooted<JSObject*> regexp (cx, mRegExp );
711
- JS::Rooted<JS::Value> result (cx);
712
-
713
- nsString input (aString);
714
-
715
- size_t index = 0 ;
716
- if (!JS::ExecuteRegExpNoStatics (cx, regexp, input.BeginWriting (),
717
- aString.Length (), &index , true , &result)) {
718
- return false ;
719
- }
720
-
721
- return result.isBoolean () && result.toBoolean ();
693
+ return mRegExp .IsMatch (aString);
722
694
}
723
695
724
696
if (mIsPrefix ) {
@@ -733,22 +705,7 @@ JSObject* MatchGlob::WrapObject(JSContext* aCx,
733
705
return MatchGlob_Binding::Wrap (aCx, this , aGivenProto);
734
706
}
735
707
736
- NS_IMPL_CYCLE_COLLECTION_CLASS (MatchGlob)
737
-
738
- NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN (MatchGlob)
739
- NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
740
- NS_IMPL_CYCLE_COLLECTION_UNLINK (mParent )
741
- tmp->mRegExp = nullptr ;
742
- NS_IMPL_CYCLE_COLLECTION_UNLINK_END
743
-
744
- NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN (MatchGlob)
745
- NS_IMPL_CYCLE_COLLECTION_TRAVERSE (mParent )
746
- NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
747
-
748
- NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN (MatchGlob)
749
- NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
750
- NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK (mRegExp )
751
- NS_IMPL_CYCLE_COLLECTION_TRACE_END
708
+ NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE (MatchGlob, mParent )
752
709
753
710
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION (MatchGlob)
754
711
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
@@ -762,7 +719,7 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE(MatchGlob)
762
719
* MatchGlobSet
763
720
*****************************************************************************/
764
721
765
- bool MatchGlobSet::Matches (const nsAString & aValue) const {
722
+ bool MatchGlobSet::Matches (const nsACString & aValue) const {
766
723
for (auto & glob : *this ) {
767
724
if (glob->Matches (aValue)) {
768
725
return true ;
0 commit comments