1// Copyright 2021 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5namespace string { 6 7struct StringMatchFunctor { 8 macro FnSymbol(): Symbol { 9 return MatchSymbolConstant(); 10 } 11 macro CanCallFast(implicit context: Context)(maybeRegExp: HeapObject): bool { 12 return regexp::IsFastRegExpForMatch(maybeRegExp); 13 } 14 transitioning macro CallFast(implicit context: Context)( 15 regexp: FastJSRegExp, string: String): JSAny { 16 return regexp::RegExpMatchFast(regexp, string); 17 } 18} 19 20struct StringSearchFunctor { 21 macro FnSymbol(): Symbol { 22 return SearchSymbolConstant(); 23 } 24 macro CanCallFast(implicit context: Context)(maybeRegExp: HeapObject): bool { 25 return regexp::IsFastRegExpForSearch(maybeRegExp); 26 } 27 transitioning macro CallFast(implicit context: Context)( 28 regexp: FastJSRegExp, string: String): JSAny { 29 return regexp::RegExpSearchFast(regexp, string); 30 } 31} 32 33transitioning macro StringMatchSearch<F: type>( 34 implicit context: NativeContext, receiver: JSAny)( 35 regexp: JSAny, functor: F, methodName: constexpr string): JSAny { 36 // 1. Let O be ? RequireObjectCoercible(this value). 37 RequireObjectCoercible(receiver, methodName); 38 39 try { 40 // 3. Let string be ? ToString(O). 41 const string = Cast<String>(receiver) otherwise Slow; 42 const heapRegexp = Cast<HeapObject>(regexp) otherwise Slow; 43 if (!functor.CanCallFast(heapRegexp)) goto Slow; 44 45 return functor.CallFast(UnsafeCast<FastJSRegExp>(heapRegexp), string); 46 } label Slow deferred { 47 // 2. If regexp is neither undefined nor null, then 48 if (regexp != Undefined && regexp != Null) { 49 try { 50 // a. Let fn be ? GetMethod(regexp, @@match/@@search). 51 // b. If fn is not undefined, then 52 const fn = GetMethod(regexp, functor.FnSymbol()) 53 otherwise FnSymbolIsNullOrUndefined; 54 // i. Return ? Call(fn, regexp, « O »). 55 return Call(context, fn, regexp, receiver); 56 } label FnSymbolIsNullOrUndefined {} 57 } 58 59 // 3. Let string be ? ToString(O). 60 const string = ToString_Inline(receiver); 61 62 // 4. Let rx be ? RegExpCreate(regexp, undefined). 63 const rx = regexp::RegExpCreate(context, regexp, kEmptyString); 64 65 // 5. Return ? Invoke(rx, @@match/@@search, « string »). 66 const fn = GetProperty(rx, functor.FnSymbol()); 67 return Call(context, fn, rx, string); 68 } 69} 70 71// https://tc39.es/ecma262/#sec-string.prototype.match 72transitioning javascript builtin 73StringPrototypeMatch( 74 js-implicit context: NativeContext, receiver: JSAny)(regexp: JSAny): JSAny { 75 return StringMatchSearch( 76 regexp, StringMatchFunctor{}, 'String.prototype.match'); 77} 78 79// https://tc39.es/ecma262/#sec-string.prototype.search 80transitioning javascript builtin 81StringPrototypeSearch( 82 js-implicit context: NativeContext, receiver: JSAny)(regexp: JSAny): JSAny { 83 return StringMatchSearch( 84 regexp, StringSearchFunctor{}, 'String.prototype.search'); 85} 86} 87