RegExpPrototype.cs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using System.Text.RegularExpressions;
  3. using Jint.Collections;
  4. using Jint.Native.Number;
  5. using Jint.Native.Object;
  6. using Jint.Native.String;
  7. using Jint.Native.Symbol;
  8. using Jint.Pooling;
  9. using Jint.Runtime;
  10. using Jint.Runtime.Descriptors;
  11. using Jint.Runtime.Interop;
  12. namespace Jint.Native.RegExp
  13. {
  14. internal sealed class RegExpPrototype : Prototype
  15. {
  16. private static readonly JsString PropertyExec = new("exec");
  17. private static readonly JsString PropertyIndex = new("index");
  18. private static readonly JsString PropertyInput = new("input");
  19. private static readonly JsString PropertySticky = new("sticky");
  20. private static readonly JsString PropertyGlobal = new("global");
  21. internal static readonly JsString PropertySource = new("source");
  22. private static readonly JsString DefaultSource = new("(?:)");
  23. internal static readonly JsString PropertyFlags = new("flags");
  24. private static readonly JsString PropertyGroups = new("groups");
  25. private readonly RegExpConstructor _constructor;
  26. private readonly Func<JsValue, JsValue[], JsValue> _defaultExec;
  27. internal RegExpPrototype(
  28. Engine engine,
  29. Realm realm,
  30. RegExpConstructor constructor,
  31. ObjectPrototype objectPrototype) : base(engine, realm)
  32. {
  33. _defaultExec = Exec;
  34. _constructor = constructor;
  35. _prototype = objectPrototype;
  36. }
  37. protected override void Initialize()
  38. {
  39. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  40. GetSetPropertyDescriptor CreateGetAccessorDescriptor(string name, Func<JsRegExp, JsValue> valueExtractor, JsValue? protoValue = null)
  41. {
  42. return new GetSetPropertyDescriptor(
  43. get: new ClrFunctionInstance(Engine, name, (thisObj, arguments) =>
  44. {
  45. if (ReferenceEquals(thisObj, this))
  46. {
  47. return protoValue ?? Undefined;
  48. }
  49. var r = thisObj as JsRegExp;
  50. if (r is null)
  51. {
  52. ExceptionHelper.ThrowTypeError(_realm);
  53. }
  54. return valueExtractor(r);
  55. }, 0, lengthFlags),
  56. set: Undefined,
  57. flags: PropertyFlag.Configurable);
  58. }
  59. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  60. var properties = new PropertyDictionary(14, checkExistingKeys: false)
  61. {
  62. ["constructor"] = new PropertyDescriptor(_constructor, propertyFlags),
  63. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToRegExpString, 0, lengthFlags), propertyFlags),
  64. ["exec"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "exec", _defaultExec, 1, lengthFlags), propertyFlags),
  65. ["test"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "test", Test, 1, lengthFlags), propertyFlags),
  66. ["dotAll"] = CreateGetAccessorDescriptor("get dotAll", static r => r.DotAll),
  67. ["flags"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get flags", Flags, 0, lengthFlags), set: Undefined, flags: PropertyFlag.Configurable),
  68. ["global"] = CreateGetAccessorDescriptor("get global", static r => r.Global),
  69. ["hasIndices"] = CreateGetAccessorDescriptor("get hasIndices", static r => r.Indices),
  70. ["ignoreCase"] = CreateGetAccessorDescriptor("get ignoreCase", static r => r.IgnoreCase),
  71. ["multiline"] = CreateGetAccessorDescriptor("get multiline", static r => r.Multiline),
  72. ["source"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get source", Source, 0, lengthFlags), set: Undefined, flags: PropertyFlag.Configurable),
  73. ["sticky"] = CreateGetAccessorDescriptor("get sticky", static r => r.Sticky),
  74. ["unicode"] = CreateGetAccessorDescriptor("get unicode", static r => r.FullUnicode),
  75. ["unicodeSets"] = CreateGetAccessorDescriptor("get unicodeSets", static r => r.UnicodeSets)
  76. };
  77. SetProperties(properties);
  78. var symbols = new SymbolDictionary(5)
  79. {
  80. [GlobalSymbolRegistry.Match] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.match]", Match, 1, lengthFlags), propertyFlags),
  81. [GlobalSymbolRegistry.MatchAll] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.matchAll]", MatchAll, 1, lengthFlags), propertyFlags),
  82. [GlobalSymbolRegistry.Replace] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.replace]", Replace, 2, lengthFlags), propertyFlags),
  83. [GlobalSymbolRegistry.Search] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.search]", Search, 1, lengthFlags), propertyFlags),
  84. [GlobalSymbolRegistry.Split] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.split]", Split, 2, lengthFlags), propertyFlags)
  85. };
  86. SetSymbols(symbols);
  87. }
  88. /// <summary>
  89. /// https://tc39.es/ecma262/#sec-get-regexp.prototype.source
  90. /// </summary>
  91. private JsValue Source(JsValue thisObject, JsValue[] arguments)
  92. {
  93. if (ReferenceEquals(thisObject, this))
  94. {
  95. return DefaultSource;
  96. }
  97. var r = thisObject as JsRegExp;
  98. if (r is null)
  99. {
  100. ExceptionHelper.ThrowTypeError(_realm);
  101. }
  102. if (string.IsNullOrEmpty(r.Source))
  103. {
  104. return JsRegExp.regExpForMatchingAllCharacters;
  105. }
  106. return r.Source
  107. .Replace("\\/", "/") // ensure forward-slashes
  108. .Replace("/", "\\/") // then escape again
  109. .Replace("\n", "\\n");
  110. }
  111. /// <summary>
  112. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
  113. /// </summary>
  114. private JsValue Replace(JsValue thisObject, JsValue[] arguments)
  115. {
  116. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.replace");
  117. var s = TypeConverter.ToString(arguments.At(0));
  118. var lengthS = s.Length;
  119. var replaceValue = arguments.At(1);
  120. var functionalReplace = replaceValue is ICallable;
  121. // we need heavier logic if we have named captures
  122. var mayHaveNamedCaptures = false;
  123. if (!functionalReplace)
  124. {
  125. var value = TypeConverter.ToString(replaceValue);
  126. replaceValue = value;
  127. mayHaveNamedCaptures = value.Contains('$');
  128. }
  129. var flags = TypeConverter.ToString(rx.Get(PropertyFlags));
  130. var global = flags.Contains('g');
  131. var fullUnicode = false;
  132. if (global)
  133. {
  134. fullUnicode = flags.Contains('u');
  135. rx.Set(JsRegExp.PropertyLastIndex, 0, true);
  136. }
  137. // check if we can access fast path
  138. if (!fullUnicode
  139. && !mayHaveNamedCaptures
  140. && !TypeConverter.ToBoolean(rx.Get(PropertySticky))
  141. && rx is JsRegExp rei && rei.HasDefaultRegExpExec)
  142. {
  143. var count = global ? int.MaxValue : 1;
  144. string result;
  145. if (functionalReplace)
  146. {
  147. string Evaluator(Match match)
  148. {
  149. var actualGroupCount = GetActualRegexGroupCount(rei, match);
  150. var replacerArgs = new List<JsValue>(actualGroupCount + 2);
  151. replacerArgs.Add(match.Value);
  152. ObjectInstance? groups = null;
  153. for (var i = 1; i < actualGroupCount; i++)
  154. {
  155. var capture = match.Groups[i];
  156. replacerArgs.Add(capture.Success ? capture.Value : Undefined);
  157. var groupName = GetRegexGroupName(rei, i);
  158. if (!string.IsNullOrWhiteSpace(groupName))
  159. {
  160. groups ??= OrdinaryObjectCreate(_engine, null);
  161. groups.CreateDataPropertyOrThrow(groupName, capture.Success ? capture.Value : Undefined);
  162. }
  163. }
  164. replacerArgs.Add(match.Index);
  165. replacerArgs.Add(s);
  166. if (groups is not null)
  167. {
  168. replacerArgs.Add(groups);
  169. }
  170. return CallFunctionalReplace(replaceValue, replacerArgs);
  171. }
  172. result = rei.Value.Replace(s, Evaluator, count);
  173. }
  174. else
  175. {
  176. result = rei.Value.Replace(s, TypeConverter.ToString(replaceValue), count);
  177. }
  178. rx.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero);
  179. return result;
  180. }
  181. var results = new List<ObjectInstance>();
  182. while (true)
  183. {
  184. var result = RegExpExec(rx, s);
  185. if (result.IsNull())
  186. {
  187. break;
  188. }
  189. results.Add((ObjectInstance) result);
  190. if (!global)
  191. {
  192. break;
  193. }
  194. var matchStr = TypeConverter.ToString(result.Get(0));
  195. if (matchStr == "")
  196. {
  197. var thisIndex = TypeConverter.ToLength(rx.Get(JsRegExp.PropertyLastIndex));
  198. var nextIndex = AdvanceStringIndex(s, thisIndex, fullUnicode);
  199. rx.Set(JsRegExp.PropertyLastIndex, nextIndex);
  200. }
  201. }
  202. var accumulatedResult = "";
  203. var nextSourcePosition = 0;
  204. var captures = new List<string>();
  205. for (var i = 0; i < results.Count; i++)
  206. {
  207. var result = results[i];
  208. var nCaptures = (int) result.Length;
  209. nCaptures = System.Math.Max(nCaptures - 1, 0);
  210. var matched = TypeConverter.ToString(result.Get(0));
  211. var matchLength = matched.Length;
  212. var position = (int) TypeConverter.ToInteger(result.Get(PropertyIndex));
  213. position = System.Math.Max(System.Math.Min(position, lengthS), 0);
  214. uint n = 1;
  215. captures.Clear();
  216. while (n <= nCaptures)
  217. {
  218. var capN = result.Get(n);
  219. var value = !capN.IsUndefined() ? TypeConverter.ToString(capN) : "";
  220. captures.Add(value);
  221. n++;
  222. }
  223. var namedCaptures = result.Get(PropertyGroups);
  224. string replacement;
  225. if (functionalReplace)
  226. {
  227. var replacerArgs = new List<JsValue>();
  228. replacerArgs.Add(matched);
  229. foreach (var capture in captures)
  230. {
  231. replacerArgs.Add(capture);
  232. }
  233. replacerArgs.Add(position);
  234. replacerArgs.Add(s);
  235. if (!namedCaptures.IsUndefined())
  236. {
  237. replacerArgs.Add(namedCaptures);
  238. }
  239. replacement = CallFunctionalReplace(replaceValue, replacerArgs);
  240. }
  241. else
  242. {
  243. if (!namedCaptures.IsUndefined())
  244. {
  245. namedCaptures = TypeConverter.ToObject(_realm, namedCaptures);
  246. }
  247. replacement = GetSubstitution(matched, s, position, captures.ToArray(), namedCaptures, TypeConverter.ToString(replaceValue));
  248. }
  249. if (position >= nextSourcePosition)
  250. {
  251. #pragma warning disable CA1845
  252. accumulatedResult = accumulatedResult +
  253. s.Substring(nextSourcePosition, position - nextSourcePosition) +
  254. replacement;
  255. #pragma warning restore CA1845
  256. nextSourcePosition = position + matchLength;
  257. }
  258. }
  259. if (nextSourcePosition >= lengthS)
  260. {
  261. return accumulatedResult;
  262. }
  263. #pragma warning disable CA1845
  264. return accumulatedResult + s.Substring(nextSourcePosition);
  265. #pragma warning restore CA1845
  266. }
  267. private static string CallFunctionalReplace(JsValue replacer, List<JsValue> replacerArgs)
  268. {
  269. var result = ((ICallable) replacer).Call(Undefined, replacerArgs.ToArray());
  270. return TypeConverter.ToString(result);
  271. }
  272. /// <summary>
  273. /// https://tc39.es/ecma262/#sec-getsubstitution
  274. /// </summary>
  275. internal static string GetSubstitution(
  276. string matched,
  277. string str,
  278. int position,
  279. string[] captures,
  280. JsValue namedCaptures,
  281. string replacement)
  282. {
  283. // If there is no pattern, replace the pattern as is.
  284. if (!replacement.Contains('$'))
  285. {
  286. return replacement;
  287. }
  288. // Patterns
  289. // $$ Inserts a "$".
  290. // $& Inserts the matched substring.
  291. // $` Inserts the portion of the string that precedes the matched substring.
  292. // $' Inserts the portion of the string that follows the matched substring.
  293. // $n or $nn Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.
  294. using var replacementBuilder = StringBuilderPool.Rent();
  295. var sb = replacementBuilder.Builder;
  296. for (var i = 0; i < replacement.Length; i++)
  297. {
  298. char c = replacement[i];
  299. if (c == '$' && i < replacement.Length - 1)
  300. {
  301. c = replacement[++i];
  302. switch (c)
  303. {
  304. case '$':
  305. sb.Append('$');
  306. break;
  307. case '&':
  308. sb.Append(matched);
  309. break;
  310. #pragma warning disable CA1846
  311. case '`':
  312. sb.Append(str.Substring(0, position));
  313. break;
  314. case '\'':
  315. sb.Append(str.Substring(position + matched.Length));
  316. break;
  317. #pragma warning restore CA1846
  318. case '<':
  319. var gtPos = replacement.IndexOf('>', i + 1);
  320. if (gtPos == -1 || namedCaptures.IsUndefined())
  321. {
  322. sb.Append('$');
  323. sb.Append(c);
  324. }
  325. else
  326. {
  327. var startIndex = i + 1;
  328. var groupName = replacement.Substring(startIndex, gtPos - startIndex);
  329. var capture = namedCaptures.Get(groupName);
  330. if (!capture.IsUndefined())
  331. {
  332. sb.Append(TypeConverter.ToString(capture));
  333. }
  334. i = gtPos;
  335. }
  336. break;
  337. default:
  338. {
  339. if (char.IsDigit(c))
  340. {
  341. int matchNumber1 = c - '0';
  342. // The match number can be one or two digits long.
  343. int matchNumber2 = 0;
  344. if (i < replacement.Length - 1 && char.IsDigit(replacement[i + 1]))
  345. {
  346. matchNumber2 = matchNumber1 * 10 + (replacement[i + 1] - '0');
  347. }
  348. // Try the two digit capture first.
  349. if (matchNumber2 > 0 && matchNumber2 <= captures.Length)
  350. {
  351. // Two digit capture replacement.
  352. sb.Append(TypeConverter.ToString(captures[matchNumber2 - 1]));
  353. i++;
  354. }
  355. else if (matchNumber1 > 0 && matchNumber1 <= captures.Length)
  356. {
  357. // Single digit capture replacement.
  358. sb.Append(TypeConverter.ToString(captures[matchNumber1 - 1]));
  359. }
  360. else
  361. {
  362. // Capture does not exist.
  363. sb.Append('$');
  364. i--;
  365. }
  366. }
  367. else
  368. {
  369. // Unknown replacement pattern.
  370. sb.Append('$');
  371. sb.Append(c);
  372. }
  373. break;
  374. }
  375. }
  376. }
  377. else
  378. {
  379. sb.Append(c);
  380. }
  381. }
  382. return replacementBuilder.ToString();
  383. }
  384. /// <summary>
  385. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@split
  386. /// </summary>
  387. private JsValue Split(JsValue thisObject, JsValue[] arguments)
  388. {
  389. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.split");
  390. var s = TypeConverter.ToString(arguments.At(0));
  391. var limit = arguments.At(1);
  392. var c = SpeciesConstructor(rx, _realm.Intrinsics.RegExp);
  393. var flags = TypeConverter.ToJsString(rx.Get(PropertyFlags));
  394. var unicodeMatching = flags.Contains('u');
  395. var newFlags = flags.Contains('y') ? flags : new JsString(flags.ToString() + 'y');
  396. var splitter = Construct(c, new JsValue[]
  397. {
  398. rx,
  399. newFlags
  400. });
  401. uint lengthA = 0;
  402. var lim = limit.IsUndefined() ? NumberConstructor.MaxSafeInteger : TypeConverter.ToUint32(limit);
  403. if (lim == 0)
  404. {
  405. return _realm.Intrinsics.Array.ArrayCreate(0);
  406. }
  407. if (s.Length == 0)
  408. {
  409. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  410. var z = RegExpExec(splitter, s);
  411. if (!z.IsNull())
  412. {
  413. return a;
  414. }
  415. a.SetIndexValue(0, s, updateLength: true);
  416. return a;
  417. }
  418. if (!unicodeMatching && rx is JsRegExp R && R.HasDefaultRegExpExec)
  419. {
  420. // we can take faster path
  421. if (string.Equals(R.Source, JsRegExp.regExpForMatchingAllCharacters, StringComparison.Ordinal))
  422. {
  423. // if empty string, just a string split
  424. return StringPrototype.SplitWithStringSeparator(_realm, "", s, (uint) s.Length);
  425. }
  426. var a = _realm.Intrinsics.Array.Construct(Arguments.Empty);
  427. int lastIndex = 0;
  428. uint index = 0;
  429. for (var match = R.Value.Match(s, 0); match.Success; match = match.NextMatch())
  430. {
  431. if (match.Length == 0 && (match.Index == 0 || match.Index == s.Length || match.Index == lastIndex))
  432. {
  433. continue;
  434. }
  435. // Add the match results to the array.
  436. a.SetIndexValue(index++, s.Substring(lastIndex, match.Index - lastIndex), updateLength: true);
  437. if (index >= lim)
  438. {
  439. return a;
  440. }
  441. lastIndex = match.Index + match.Length;
  442. var actualGroupCount = GetActualRegexGroupCount(R, match);
  443. for (int i = 1; i < actualGroupCount; i++)
  444. {
  445. var group = match.Groups[i];
  446. var item = Undefined;
  447. if (group.Captures.Count > 0)
  448. {
  449. item = match.Groups[i].Value;
  450. }
  451. a.SetIndexValue(index++, item, updateLength: true);
  452. if (index >= lim)
  453. {
  454. return a;
  455. }
  456. }
  457. }
  458. // Add the last part of the split
  459. a.SetIndexValue(index, s.Substring(lastIndex), updateLength: true);
  460. return a;
  461. }
  462. return SplitSlow(s, splitter, unicodeMatching, lengthA, lim);
  463. }
  464. private JsArray SplitSlow(string s, ObjectInstance splitter, bool unicodeMatching, uint lengthA, long lim)
  465. {
  466. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  467. ulong previousStringIndex = 0;
  468. ulong currentIndex = 0;
  469. while (currentIndex < (ulong) s.Length)
  470. {
  471. splitter.Set(JsRegExp.PropertyLastIndex, currentIndex, true);
  472. var z = RegExpExec(splitter, s);
  473. if (z.IsNull())
  474. {
  475. currentIndex = AdvanceStringIndex(s, currentIndex, unicodeMatching);
  476. continue;
  477. }
  478. var endIndex = TypeConverter.ToLength(splitter.Get(JsRegExp.PropertyLastIndex));
  479. endIndex = System.Math.Min(endIndex, (ulong) s.Length);
  480. if (endIndex == previousStringIndex)
  481. {
  482. currentIndex = AdvanceStringIndex(s, currentIndex, unicodeMatching);
  483. continue;
  484. }
  485. var t = s.Substring((int) previousStringIndex, (int) (currentIndex - previousStringIndex));
  486. a.SetIndexValue(lengthA, t, updateLength: true);
  487. lengthA++;
  488. if (lengthA == lim)
  489. {
  490. return a;
  491. }
  492. previousStringIndex = endIndex;
  493. var numberOfCaptures = (int) TypeConverter.ToLength(z.Get(CommonProperties.Length));
  494. numberOfCaptures = System.Math.Max(numberOfCaptures - 1, 0);
  495. var i = 1;
  496. while (i <= numberOfCaptures)
  497. {
  498. var nextCapture = z.Get(i);
  499. a.SetIndexValue(lengthA, nextCapture, updateLength: true);
  500. i++;
  501. lengthA++;
  502. if (lengthA == lim)
  503. {
  504. return a;
  505. }
  506. }
  507. currentIndex = previousStringIndex;
  508. }
  509. a.SetIndexValue(lengthA, s.Substring((int) previousStringIndex, s.Length - (int) previousStringIndex), updateLength: true);
  510. return a;
  511. }
  512. private JsValue Flags(JsValue thisObject, JsValue[] arguments)
  513. {
  514. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.flags");
  515. static string AddFlagIfPresent(JsValue o, JsValue p, char flag, string s)
  516. {
  517. return TypeConverter.ToBoolean(o.Get(p)) ? s + flag : s;
  518. }
  519. var result = AddFlagIfPresent(r, "hasIndices", 'd', "");
  520. result = AddFlagIfPresent(r, PropertyGlobal, 'g', result);
  521. result = AddFlagIfPresent(r, "ignoreCase", 'i', result);
  522. result = AddFlagIfPresent(r, "multiline", 'm', result);
  523. result = AddFlagIfPresent(r, "dotAll", 's', result);
  524. result = AddFlagIfPresent(r, "unicode", 'u', result);
  525. result = AddFlagIfPresent(r, "unicodeSets", 'v', result);
  526. result = AddFlagIfPresent(r, PropertySticky, 'y', result);
  527. return result;
  528. }
  529. private JsValue ToRegExpString(JsValue thisObject, JsValue[] arguments)
  530. {
  531. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.toString");
  532. var pattern = TypeConverter.ToString(r.Get(PropertySource));
  533. var flags = TypeConverter.ToString(r.Get(PropertyFlags));
  534. return "/" + pattern + "/" + flags;
  535. }
  536. private JsValue Test(JsValue thisObject, JsValue[] arguments)
  537. {
  538. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.test");
  539. var s = TypeConverter.ToString(arguments.At(0));
  540. // check couple fast paths
  541. if (r is JsRegExp R && !R.FullUnicode)
  542. {
  543. if (!R.Sticky && !R.Global)
  544. {
  545. R.Set(JsRegExp.PropertyLastIndex, 0, throwOnError: true);
  546. return R.Value.IsMatch(s);
  547. }
  548. var lastIndex = (int) TypeConverter.ToLength(R.Get(JsRegExp.PropertyLastIndex));
  549. if (lastIndex >= s.Length && s.Length > 0)
  550. {
  551. return JsBoolean.False;
  552. }
  553. var m = R.Value.Match(s, lastIndex);
  554. if (!m.Success || (R.Sticky && m.Index != lastIndex))
  555. {
  556. R.Set(JsRegExp.PropertyLastIndex, 0, throwOnError: true);
  557. return JsBoolean.False;
  558. }
  559. R.Set(JsRegExp.PropertyLastIndex, m.Index + m.Length, throwOnError: true);
  560. return JsBoolean.True;
  561. }
  562. var match = RegExpExec(r, s);
  563. return !match.IsNull();
  564. }
  565. /// <summary>
  566. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@search
  567. /// </summary>
  568. private JsValue Search(JsValue thisObject, JsValue[] arguments)
  569. {
  570. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.search");
  571. var s = TypeConverter.ToString(arguments.At(0));
  572. var previousLastIndex = rx.Get(JsRegExp.PropertyLastIndex);
  573. if (!SameValue(previousLastIndex, 0))
  574. {
  575. rx.Set(JsRegExp.PropertyLastIndex, 0, true);
  576. }
  577. var result = RegExpExec(rx, s);
  578. var currentLastIndex = rx.Get(JsRegExp.PropertyLastIndex);
  579. if (!SameValue(currentLastIndex, previousLastIndex))
  580. {
  581. rx.Set(JsRegExp.PropertyLastIndex, previousLastIndex, true);
  582. }
  583. if (result.IsNull())
  584. {
  585. return -1;
  586. }
  587. return result.Get(PropertyIndex);
  588. }
  589. /// <summary>
  590. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@match
  591. /// </summary>
  592. private JsValue Match(JsValue thisObject, JsValue[] arguments)
  593. {
  594. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.match");
  595. var s = TypeConverter.ToString(arguments.At(0));
  596. var flags = TypeConverter.ToString(rx.Get(PropertyFlags));
  597. var global = flags.Contains('g');
  598. if (!global)
  599. {
  600. return RegExpExec(rx, s);
  601. }
  602. var fullUnicode = flags.Contains('u');
  603. rx.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero, true);
  604. if (!fullUnicode
  605. && rx is JsRegExp rei
  606. && rei.HasDefaultRegExpExec)
  607. {
  608. // fast path
  609. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  610. if (rei.Sticky)
  611. {
  612. var match = rei.Value.Match(s);
  613. if (!match.Success || match.Index != 0)
  614. {
  615. return Null;
  616. }
  617. a.SetIndexValue(0, match.Value, updateLength: false);
  618. uint li = 0;
  619. while (true)
  620. {
  621. match = match.NextMatch();
  622. if (!match.Success || match.Index != ++li)
  623. break;
  624. a.SetIndexValue(li, match.Value, updateLength: false);
  625. }
  626. a.SetLength(li);
  627. return a;
  628. }
  629. else
  630. {
  631. var matches = rei.Value.Matches(s);
  632. if (matches.Count == 0)
  633. {
  634. return Null;
  635. }
  636. a.EnsureCapacity((uint) matches.Count);
  637. a.SetLength((uint) matches.Count);
  638. for (var i = 0; i < matches.Count; i++)
  639. {
  640. a.SetIndexValue((uint) i, matches[i].Value, updateLength: false);
  641. }
  642. return a;
  643. }
  644. }
  645. return MatchSlow(rx, s, fullUnicode);
  646. }
  647. private JsValue MatchSlow(ObjectInstance rx, string s, bool fullUnicode)
  648. {
  649. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  650. uint n = 0;
  651. while (true)
  652. {
  653. var result = RegExpExec(rx, s);
  654. if (result.IsNull())
  655. {
  656. a.SetLength(n);
  657. return n == 0 ? Null : a;
  658. }
  659. var matchStr = TypeConverter.ToString(result.Get(JsString.NumberZeroString));
  660. a.SetIndexValue(n, matchStr, updateLength: false);
  661. if (matchStr == "")
  662. {
  663. var thisIndex = TypeConverter.ToLength(rx.Get(JsRegExp.PropertyLastIndex));
  664. var nextIndex = AdvanceStringIndex(s, thisIndex, fullUnicode);
  665. rx.Set(JsRegExp.PropertyLastIndex, nextIndex, true);
  666. }
  667. n++;
  668. }
  669. }
  670. /// <summary>
  671. /// https://tc39.es/ecma262/#sec-regexp-prototype-matchall
  672. /// </summary>
  673. private JsValue MatchAll(JsValue thisObject, JsValue[] arguments)
  674. {
  675. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.matchAll");
  676. var s = TypeConverter.ToString(arguments.At(0));
  677. var c = SpeciesConstructor(r, _realm.Intrinsics.RegExp);
  678. var flags = TypeConverter.ToJsString(r.Get(PropertyFlags));
  679. var matcher = Construct(c, new JsValue[]
  680. {
  681. r,
  682. flags
  683. });
  684. var lastIndex = TypeConverter.ToLength(r.Get(JsRegExp.PropertyLastIndex));
  685. matcher.Set(JsRegExp.PropertyLastIndex, lastIndex, true);
  686. var global = flags.Contains('g');
  687. var fullUnicode = flags.Contains('u');
  688. return _realm.Intrinsics.RegExpStringIteratorPrototype.Construct(matcher, s, global, fullUnicode);
  689. }
  690. private static ulong AdvanceStringIndex(string s, ulong index, bool unicode)
  691. {
  692. if (!unicode || index + 1 >= (ulong) s.Length)
  693. {
  694. return index + 1;
  695. }
  696. var first = s[(int) index];
  697. if (first < 0xD800 || first > 0xDBFF)
  698. {
  699. return index + 1;
  700. }
  701. var second = s[(int) (index + 1)];
  702. if (second < 0xDC00 || second > 0xDFFF)
  703. {
  704. return index + 1;
  705. }
  706. return index + 2;
  707. }
  708. internal static JsValue RegExpExec(ObjectInstance r, string s)
  709. {
  710. var ri = r as JsRegExp;
  711. if ((ri is null || !ri.HasDefaultRegExpExec) && r.Get(PropertyExec) is ICallable callable)
  712. {
  713. var result = callable.Call(r, new JsValue[] { s });
  714. if (!result.IsNull() && !result.IsObject())
  715. {
  716. ExceptionHelper.ThrowTypeError(r.Engine.Realm);
  717. }
  718. return result;
  719. }
  720. if (ri is null)
  721. {
  722. ExceptionHelper.ThrowTypeError(r.Engine.Realm);
  723. }
  724. return RegExpBuiltinExec(ri, s);
  725. }
  726. internal bool HasDefaultExec => Get(PropertyExec) is ClrFunctionInstance functionInstance && functionInstance._func == _defaultExec;
  727. /// <summary>
  728. /// https://tc39.es/ecma262/#sec-regexpbuiltinexec
  729. /// </summary>
  730. private static JsValue RegExpBuiltinExec(JsRegExp R, string s)
  731. {
  732. var length = (ulong) s.Length;
  733. var lastIndex = TypeConverter.ToLength(R.Get(JsRegExp.PropertyLastIndex));
  734. var global = R.Global;
  735. var sticky = R.Sticky;
  736. if (!global && !sticky)
  737. {
  738. lastIndex = 0;
  739. }
  740. if (string.Equals(R.Source, JsRegExp.regExpForMatchingAllCharacters, StringComparison.Ordinal)) // Reg Exp is really ""
  741. {
  742. if (lastIndex > (ulong) s.Length)
  743. {
  744. return Null;
  745. }
  746. // "aaa".match() => [ '', index: 0, input: 'aaa' ]
  747. var array = R.Engine.Realm.Intrinsics.Array.ArrayCreate(1);
  748. array.FastSetDataProperty(PropertyIndex._value, lastIndex);
  749. array.FastSetDataProperty(PropertyInput._value, s);
  750. array.SetIndexValue(0, JsString.Empty, updateLength: false);
  751. return array;
  752. }
  753. var matcher = R.Value;
  754. var fullUnicode = R.FullUnicode;
  755. var hasIndices = R.Indices;
  756. if (!global & !sticky && !fullUnicode && !hasIndices)
  757. {
  758. // we can the non-stateful fast path which is the common case
  759. var m = matcher.Match(s, (int) lastIndex);
  760. if (!m.Success)
  761. {
  762. return Null;
  763. }
  764. return CreateReturnValueArray(R, m, s, fullUnicode: false, hasIndices: false);
  765. }
  766. // the stateful version
  767. Match match;
  768. if (lastIndex > length)
  769. {
  770. R.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero, true);
  771. return Null;
  772. }
  773. var startAt = (int) lastIndex;
  774. while (true)
  775. {
  776. match = R.Value.Match(s, startAt);
  777. // The conversion of Unicode regex patterns to .NET Regex has some flaws:
  778. // when the pattern may match empty strings, the adapted Regex will return empty string matches
  779. // in the middle of surrogate pairs. As a best effort solution, we remove these fake positive matches.
  780. // (See also: https://github.com/sebastienros/esprima-dotnet/pull/364#issuecomment-1606045259)
  781. if (match.Success
  782. && fullUnicode
  783. && match.Length == 0
  784. && 0 < match.Index && match.Index < s.Length
  785. && char.IsHighSurrogate(s[match.Index - 1]) && char.IsLowSurrogate(s[match.Index]))
  786. {
  787. startAt++;
  788. continue;
  789. }
  790. break;
  791. }
  792. var success = match.Success && (!sticky || match.Index == (int) lastIndex);
  793. if (!success)
  794. {
  795. R.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero, true);
  796. return Null;
  797. }
  798. var e = match.Index + match.Length;
  799. // NOTE: Even in Unicode mode, we don't need to translate indices as .NET regexes always return code unit indices.
  800. if (global || sticky)
  801. {
  802. R.Set(JsRegExp.PropertyLastIndex, e, true);
  803. }
  804. return CreateReturnValueArray(R, match, s, fullUnicode, hasIndices);
  805. }
  806. private static JsArray CreateReturnValueArray(
  807. JsRegExp rei,
  808. Match match,
  809. string s,
  810. bool fullUnicode,
  811. bool hasIndices)
  812. {
  813. var engine = rei.Engine;
  814. var actualGroupCount = GetActualRegexGroupCount(rei, match);
  815. var array = engine.Realm.Intrinsics.Array.ArrayCreate((ulong) actualGroupCount);
  816. array.CreateDataProperty(PropertyIndex, match.Index);
  817. array.CreateDataProperty(PropertyInput, s);
  818. ObjectInstance? groups = null;
  819. List<string>? groupNames = null;
  820. var indices = hasIndices ? new List<JsNumber[]?>(actualGroupCount) : null;
  821. for (uint i = 0; i < actualGroupCount; i++)
  822. {
  823. var capture = match.Groups[(int) i];
  824. var capturedValue = Undefined;
  825. if (capture?.Success == true)
  826. {
  827. capturedValue = capture.Value;
  828. }
  829. if (hasIndices)
  830. {
  831. if (capture?.Success == true)
  832. {
  833. indices!.Add(new[] { JsNumber.Create(capture.Index), JsNumber.Create(capture.Index + capture.Length) });
  834. }
  835. else
  836. {
  837. indices!.Add(null);
  838. }
  839. }
  840. var groupName = GetRegexGroupName(rei, (int) i);
  841. if (!string.IsNullOrWhiteSpace(groupName))
  842. {
  843. groups ??= OrdinaryObjectCreate(engine, null);
  844. groups.CreateDataPropertyOrThrow(groupName, capturedValue);
  845. groupNames ??= new List<string>();
  846. groupNames.Add(groupName!);
  847. }
  848. array.SetIndexValue(i, capturedValue, updateLength: false);
  849. }
  850. array.CreateDataProperty(PropertyGroups, groups ?? Undefined);
  851. if (hasIndices)
  852. {
  853. var indicesArray = MakeMatchIndicesIndexPairArray(engine, s, indices!, groupNames, groupNames?.Count > 0);
  854. array.CreateDataPropertyOrThrow("indices", indicesArray);
  855. }
  856. return array;
  857. }
  858. /// <summary>
  859. /// https://tc39.es/ecma262/#sec-makematchindicesindexpairarray
  860. /// </summary>
  861. private static JsArray MakeMatchIndicesIndexPairArray(
  862. Engine engine,
  863. string s,
  864. List<JsNumber[]?> indices,
  865. List<string>? groupNames,
  866. bool hasGroups)
  867. {
  868. var n = indices.Count;
  869. var a = engine.Realm.Intrinsics.Array.Construct((uint) n);
  870. ObjectInstance? groups = null;
  871. if (hasGroups)
  872. {
  873. groups = OrdinaryObjectCreate(engine, null);
  874. }
  875. a.CreateDataPropertyOrThrow("groups", groups ?? Undefined);
  876. for (var i = 0; i < n; ++i)
  877. {
  878. var matchIndices = indices[i];
  879. var matchIndexPair = matchIndices is not null
  880. ? GetMatchIndexPair(engine, s, matchIndices)
  881. : Undefined;
  882. a.Push(matchIndexPair);
  883. if (i > 0 && !string.IsNullOrWhiteSpace(groupNames?[i - 1]))
  884. {
  885. groups!.CreateDataPropertyOrThrow(groupNames![i - 1], matchIndexPair);
  886. }
  887. }
  888. return a;
  889. }
  890. /// <summary>
  891. /// https://tc39.es/ecma262/#sec-getmatchindexpair
  892. /// </summary>
  893. private static JsValue GetMatchIndexPair(Engine engine, string s, JsNumber[] match)
  894. {
  895. return engine.Realm.Intrinsics.Array.CreateArrayFromList(match);
  896. }
  897. private static int GetActualRegexGroupCount(JsRegExp rei, Match match)
  898. {
  899. return rei.ParseResult.Success ? rei.ParseResult.ActualRegexGroupCount : match.Groups.Count;
  900. }
  901. private static string? GetRegexGroupName(JsRegExp rei, int index)
  902. {
  903. if (index == 0)
  904. {
  905. return null;
  906. }
  907. var regex = rei.Value;
  908. if (rei.ParseResult.Success)
  909. {
  910. return rei.ParseResult.GetRegexGroupName(index);
  911. }
  912. var groupNameFromNumber = regex.GroupNameFromNumber(index);
  913. if (groupNameFromNumber.Length == 1 && groupNameFromNumber[0] == 48 + index)
  914. {
  915. // regex defaults to index as group name when it's not a named group
  916. return null;
  917. }
  918. return groupNameFromNumber;
  919. }
  920. private JsValue Exec(JsValue thisObject, JsValue[] arguments)
  921. {
  922. var r = thisObject as JsRegExp;
  923. if (r is null)
  924. {
  925. ExceptionHelper.ThrowTypeError(_engine.Realm);
  926. }
  927. var s = TypeConverter.ToString(arguments.At(0));
  928. return RegExpBuiltinExec(r, s);
  929. }
  930. }
  931. }