RegExpPrototype.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using Jint.Collections;
  5. using Jint.Native.Number;
  6. using Jint.Native.Object;
  7. using Jint.Native.String;
  8. using Jint.Native.Symbol;
  9. using Jint.Runtime;
  10. using Jint.Runtime.Descriptors;
  11. using Jint.Runtime.Interop;
  12. namespace Jint.Native.RegExp;
  13. internal sealed class RegExpPrototype : Prototype
  14. {
  15. private static readonly JsString PropertyExec = new("exec");
  16. private static readonly JsString PropertyIndex = new("index");
  17. private static readonly JsString PropertyInput = new("input");
  18. private static readonly JsString PropertySticky = new("sticky");
  19. private static readonly JsString PropertyGlobal = new("global");
  20. internal static readonly JsString PropertySource = new("source");
  21. private static readonly JsString DefaultSource = new("(?:)");
  22. internal static readonly JsString PropertyFlags = new("flags");
  23. private static readonly JsString PropertyGroups = new("groups");
  24. private static readonly JsString PropertyIgnoreCase = new("ignoreCase");
  25. private static readonly JsString PropertyMultiline = new("multiline");
  26. private static readonly JsString PropertyDotAll = new("dotAll");
  27. private static readonly JsString PropertyUnicode = new("unicode");
  28. private static readonly JsString PropertyUnicodeSets = new("unicodeSets");
  29. private readonly RegExpConstructor _constructor;
  30. private readonly JsCallDelegate _defaultExec;
  31. internal RegExpPrototype(
  32. Engine engine,
  33. Realm realm,
  34. RegExpConstructor constructor,
  35. ObjectPrototype objectPrototype) : base(engine, realm)
  36. {
  37. _defaultExec = Exec;
  38. _constructor = constructor;
  39. _prototype = objectPrototype;
  40. }
  41. protected override void Initialize()
  42. {
  43. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  44. GetSetPropertyDescriptor CreateGetAccessorDescriptor(string name, Func<JsRegExp, JsValue> valueExtractor, JsValue? protoValue = null)
  45. {
  46. return new GetSetPropertyDescriptor(
  47. get: new ClrFunction(Engine, name, (thisObj, arguments) =>
  48. {
  49. if (ReferenceEquals(thisObj, this))
  50. {
  51. return protoValue ?? Undefined;
  52. }
  53. var r = thisObj as JsRegExp;
  54. if (r is null)
  55. {
  56. ExceptionHelper.ThrowTypeError(_realm);
  57. }
  58. return valueExtractor(r);
  59. }, 0, lengthFlags),
  60. set: Undefined,
  61. flags: PropertyFlag.Configurable);
  62. }
  63. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  64. var properties = new PropertyDictionary(14, checkExistingKeys: false)
  65. {
  66. ["constructor"] = new PropertyDescriptor(_constructor, propertyFlags),
  67. ["toString"] = new PropertyDescriptor(new ClrFunction(Engine, "toString", ToRegExpString, 0, lengthFlags), propertyFlags),
  68. ["exec"] = new PropertyDescriptor(new ClrFunction(Engine, "exec", _defaultExec, 1, lengthFlags), propertyFlags),
  69. ["test"] = new PropertyDescriptor(new ClrFunction(Engine, "test", Test, 1, lengthFlags), propertyFlags),
  70. ["dotAll"] = CreateGetAccessorDescriptor("get dotAll", static r => r.DotAll),
  71. ["flags"] = new GetSetPropertyDescriptor(get: new ClrFunction(Engine, "get flags", Flags, 0, lengthFlags), set: Undefined, flags: PropertyFlag.Configurable),
  72. ["global"] = CreateGetAccessorDescriptor("get global", static r => r.Global),
  73. ["hasIndices"] = CreateGetAccessorDescriptor("get hasIndices", static r => r.Indices),
  74. ["ignoreCase"] = CreateGetAccessorDescriptor("get ignoreCase", static r => r.IgnoreCase),
  75. ["multiline"] = CreateGetAccessorDescriptor("get multiline", static r => r.Multiline),
  76. ["source"] = new GetSetPropertyDescriptor(get: new ClrFunction(Engine, "get source", Source, 0, lengthFlags), set: Undefined, flags: PropertyFlag.Configurable),
  77. ["sticky"] = CreateGetAccessorDescriptor("get sticky", static r => r.Sticky),
  78. ["unicode"] = CreateGetAccessorDescriptor("get unicode", static r => r.FullUnicode),
  79. ["unicodeSets"] = CreateGetAccessorDescriptor("get unicodeSets", static r => r.UnicodeSets)
  80. };
  81. SetProperties(properties);
  82. var symbols = new SymbolDictionary(5)
  83. {
  84. [GlobalSymbolRegistry.Match] = new PropertyDescriptor(new ClrFunction(Engine, "[Symbol.match]", Match, 1, lengthFlags), propertyFlags),
  85. [GlobalSymbolRegistry.MatchAll] = new PropertyDescriptor(new ClrFunction(Engine, "[Symbol.matchAll]", MatchAll, 1, lengthFlags), propertyFlags),
  86. [GlobalSymbolRegistry.Replace] = new PropertyDescriptor(new ClrFunction(Engine, "[Symbol.replace]", Replace, 2, lengthFlags), propertyFlags),
  87. [GlobalSymbolRegistry.Search] = new PropertyDescriptor(new ClrFunction(Engine, "[Symbol.search]", Search, 1, lengthFlags), propertyFlags),
  88. [GlobalSymbolRegistry.Split] = new PropertyDescriptor(new ClrFunction(Engine, "[Symbol.split]", Split, 2, lengthFlags), propertyFlags)
  89. };
  90. SetSymbols(symbols);
  91. }
  92. /// <summary>
  93. /// https://tc39.es/ecma262/#sec-get-regexp.prototype.source
  94. /// </summary>
  95. private JsValue Source(JsValue thisObject, JsCallArguments arguments)
  96. {
  97. if (ReferenceEquals(thisObject, this))
  98. {
  99. return DefaultSource;
  100. }
  101. var r = thisObject as JsRegExp;
  102. if (r is null)
  103. {
  104. ExceptionHelper.ThrowTypeError(_realm);
  105. }
  106. if (string.IsNullOrEmpty(r.Source))
  107. {
  108. return JsRegExp.regExpForMatchingAllCharacters;
  109. }
  110. return r.Source
  111. .Replace("\\/", "/") // ensure forward-slashes
  112. .Replace("/", "\\/") // then escape again
  113. .Replace("\n", "\\n");
  114. }
  115. /// <summary>
  116. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
  117. /// </summary>
  118. private JsValue Replace(JsValue thisObject, JsCallArguments arguments)
  119. {
  120. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.replace");
  121. var s = TypeConverter.ToString(arguments.At(0));
  122. var lengthS = s.Length;
  123. var replaceValue = arguments.At(1);
  124. var functionalReplace = replaceValue is ICallable;
  125. // we need heavier logic if we have named captures
  126. var mayHaveNamedCaptures = false;
  127. if (!functionalReplace)
  128. {
  129. var value = TypeConverter.ToString(replaceValue);
  130. replaceValue = value;
  131. mayHaveNamedCaptures = value.Contains('$');
  132. }
  133. var flags = TypeConverter.ToString(rx.Get(PropertyFlags));
  134. var global = flags.Contains('g');
  135. var fullUnicode = false;
  136. if (global)
  137. {
  138. fullUnicode = flags.Contains('u');
  139. rx.Set(JsRegExp.PropertyLastIndex, 0, true);
  140. }
  141. // check if we can access fast path
  142. if (!fullUnicode
  143. && !mayHaveNamedCaptures
  144. && !TypeConverter.ToBoolean(rx.Get(PropertySticky))
  145. && rx is JsRegExp rei && rei.HasDefaultRegExpExec)
  146. {
  147. var count = global ? int.MaxValue : 1;
  148. string result;
  149. if (functionalReplace)
  150. {
  151. string Evaluator(Match match)
  152. {
  153. var actualGroupCount = GetActualRegexGroupCount(rei, match);
  154. var replacerArgs = new List<JsValue>(actualGroupCount + 2);
  155. replacerArgs.Add(match.Value);
  156. ObjectInstance? groups = null;
  157. for (var i = 1; i < actualGroupCount; i++)
  158. {
  159. var capture = match.Groups[i];
  160. replacerArgs.Add(capture.Success ? capture.Value : Undefined);
  161. var groupName = GetRegexGroupName(rei, i);
  162. if (!string.IsNullOrWhiteSpace(groupName))
  163. {
  164. groups ??= OrdinaryObjectCreate(_engine, null);
  165. groups.CreateDataPropertyOrThrow(groupName, capture.Success ? capture.Value : Undefined);
  166. }
  167. }
  168. replacerArgs.Add(match.Index);
  169. replacerArgs.Add(s);
  170. if (groups is not null)
  171. {
  172. replacerArgs.Add(groups);
  173. }
  174. return CallFunctionalReplace(replaceValue, replacerArgs);
  175. }
  176. result = rei.Value.Replace(s, Evaluator, count);
  177. }
  178. else
  179. {
  180. result = rei.Value.Replace(s, TypeConverter.ToString(replaceValue), count);
  181. }
  182. rx.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero);
  183. return result;
  184. }
  185. var results = new List<ObjectInstance>();
  186. while (true)
  187. {
  188. var result = RegExpExec(rx, s);
  189. if (result.IsNull())
  190. {
  191. break;
  192. }
  193. results.Add((ObjectInstance) result);
  194. if (!global)
  195. {
  196. break;
  197. }
  198. var matchStr = TypeConverter.ToString(result.Get(0));
  199. if (matchStr == "")
  200. {
  201. var thisIndex = TypeConverter.ToLength(rx.Get(JsRegExp.PropertyLastIndex));
  202. var nextIndex = AdvanceStringIndex(s, thisIndex, fullUnicode);
  203. rx.Set(JsRegExp.PropertyLastIndex, nextIndex);
  204. }
  205. }
  206. var accumulatedResult = "";
  207. var nextSourcePosition = 0;
  208. var captures = new List<string>();
  209. for (var i = 0; i < results.Count; i++)
  210. {
  211. var result = results[i];
  212. var nCaptures = (int) result.GetLength();
  213. nCaptures = System.Math.Max(nCaptures - 1, 0);
  214. var matched = TypeConverter.ToString(result.Get(0));
  215. var matchLength = matched.Length;
  216. var position = (int) TypeConverter.ToInteger(result.Get(PropertyIndex));
  217. position = System.Math.Max(System.Math.Min(position, lengthS), 0);
  218. uint n = 1;
  219. captures.Clear();
  220. while (n <= nCaptures)
  221. {
  222. var capN = result.Get(n);
  223. var value = !capN.IsUndefined() ? TypeConverter.ToString(capN) : "";
  224. captures.Add(value);
  225. n++;
  226. }
  227. var namedCaptures = result.Get(PropertyGroups);
  228. string replacement;
  229. if (functionalReplace)
  230. {
  231. var replacerArgs = new List<JsValue>();
  232. replacerArgs.Add(matched);
  233. foreach (var capture in captures)
  234. {
  235. replacerArgs.Add(capture);
  236. }
  237. replacerArgs.Add(position);
  238. replacerArgs.Add(s);
  239. if (!namedCaptures.IsUndefined())
  240. {
  241. replacerArgs.Add(namedCaptures);
  242. }
  243. replacement = CallFunctionalReplace(replaceValue, replacerArgs);
  244. }
  245. else
  246. {
  247. if (!namedCaptures.IsUndefined())
  248. {
  249. namedCaptures = TypeConverter.ToObject(_realm, namedCaptures);
  250. }
  251. replacement = GetSubstitution(matched, s, position, captures.ToArray(), namedCaptures, TypeConverter.ToString(replaceValue));
  252. }
  253. if (position >= nextSourcePosition)
  254. {
  255. #pragma warning disable CA1845
  256. accumulatedResult = accumulatedResult +
  257. s.Substring(nextSourcePosition, position - nextSourcePosition) +
  258. replacement;
  259. #pragma warning restore CA1845
  260. nextSourcePosition = position + matchLength;
  261. }
  262. }
  263. if (nextSourcePosition >= lengthS)
  264. {
  265. return accumulatedResult;
  266. }
  267. #pragma warning disable CA1845
  268. return accumulatedResult + s.Substring(nextSourcePosition);
  269. #pragma warning restore CA1845
  270. }
  271. private static string CallFunctionalReplace(JsValue replacer, List<JsValue> replacerArgs)
  272. {
  273. var result = ((ICallable) replacer).Call(Undefined, replacerArgs.ToArray());
  274. return TypeConverter.ToString(result);
  275. }
  276. /// <summary>
  277. /// https://tc39.es/ecma262/#sec-getsubstitution
  278. /// </summary>
  279. internal static string GetSubstitution(
  280. string matched,
  281. string str,
  282. int position,
  283. string[] captures,
  284. JsValue namedCaptures,
  285. string replacement)
  286. {
  287. // If there is no pattern, replace the pattern as is.
  288. if (!replacement.Contains('$'))
  289. {
  290. return replacement;
  291. }
  292. // Patterns
  293. // $$ Inserts a "$".
  294. // $& Inserts the matched substring.
  295. // $` Inserts the portion of the string that precedes the matched substring.
  296. // $' Inserts the portion of the string that follows the matched substring.
  297. // $n or $nn Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.
  298. using var sb = new ValueStringBuilder(stackalloc char[128]);
  299. for (var i = 0; i < replacement.Length; i++)
  300. {
  301. char c = replacement[i];
  302. if (c == '$' && i < replacement.Length - 1)
  303. {
  304. c = replacement[++i];
  305. switch (c)
  306. {
  307. case '$':
  308. sb.Append('$');
  309. break;
  310. case '&':
  311. sb.Append(matched);
  312. break;
  313. case '`':
  314. sb.Append(str.AsSpan(0, position));
  315. break;
  316. case '\'':
  317. sb.Append(str.AsSpan(position + matched.Length));
  318. break;
  319. case '<':
  320. var gtPos = replacement.IndexOf('>', i + 1);
  321. if (gtPos == -1 || namedCaptures.IsUndefined())
  322. {
  323. sb.Append('$');
  324. sb.Append(c);
  325. }
  326. else
  327. {
  328. var startIndex = i + 1;
  329. var groupName = replacement.Substring(startIndex, gtPos - startIndex);
  330. var capture = namedCaptures.Get(groupName);
  331. if (!capture.IsUndefined())
  332. {
  333. sb.Append(TypeConverter.ToString(capture));
  334. }
  335. i = gtPos;
  336. }
  337. break;
  338. default:
  339. {
  340. if (char.IsDigit(c))
  341. {
  342. int matchNumber1 = c - '0';
  343. // The match number can be one or two digits long.
  344. int matchNumber2 = 0;
  345. if (i < replacement.Length - 1 && char.IsDigit(replacement[i + 1]))
  346. {
  347. matchNumber2 = matchNumber1 * 10 + (replacement[i + 1] - '0');
  348. }
  349. // Try the two digit capture first.
  350. if (matchNumber2 > 0 && matchNumber2 <= captures.Length)
  351. {
  352. // Two digit capture replacement.
  353. sb.Append(TypeConverter.ToString(captures[matchNumber2 - 1]));
  354. i++;
  355. }
  356. else if (matchNumber1 > 0 && matchNumber1 <= captures.Length)
  357. {
  358. // Single digit capture replacement.
  359. sb.Append(TypeConverter.ToString(captures[matchNumber1 - 1]));
  360. }
  361. else
  362. {
  363. // Capture does not exist.
  364. sb.Append('$');
  365. i--;
  366. }
  367. }
  368. else
  369. {
  370. // Unknown replacement pattern.
  371. sb.Append('$');
  372. sb.Append(c);
  373. }
  374. break;
  375. }
  376. }
  377. }
  378. else
  379. {
  380. sb.Append(c);
  381. }
  382. }
  383. return sb.ToString();
  384. }
  385. /// <summary>
  386. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@split
  387. /// </summary>
  388. private JsValue Split(JsValue thisObject, JsCallArguments arguments)
  389. {
  390. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.split");
  391. var s = TypeConverter.ToString(arguments.At(0));
  392. var limit = arguments.At(1);
  393. var c = SpeciesConstructor(rx, _realm.Intrinsics.RegExp);
  394. var flags = TypeConverter.ToJsString(rx.Get(PropertyFlags));
  395. var unicodeMatching = flags.Contains('u');
  396. var newFlags = flags.Contains('y') ? flags : new JsString(flags.ToString() + 'y');
  397. var splitter = Construct(c, [
  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, JsCallArguments 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, PropertyIgnoreCase, 'i', result);
  522. result = AddFlagIfPresent(r, PropertyMultiline, 'm', result);
  523. result = AddFlagIfPresent(r, PropertyDotAll, 's', result);
  524. result = AddFlagIfPresent(r, PropertyUnicode, 'u', result);
  525. result = AddFlagIfPresent(r, PropertyUnicodeSets, 'v', result);
  526. result = AddFlagIfPresent(r, PropertySticky, 'y', result);
  527. return result;
  528. }
  529. private JsValue ToRegExpString(JsValue thisObject, JsCallArguments 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, JsCallArguments 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, JsCallArguments 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, JsCallArguments 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, JsCallArguments 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, [
  680. r,
  681. flags
  682. ]);
  683. var lastIndex = TypeConverter.ToLength(r.Get(JsRegExp.PropertyLastIndex));
  684. matcher.Set(JsRegExp.PropertyLastIndex, lastIndex, true);
  685. var global = flags.Contains('g');
  686. var fullUnicode = flags.Contains('u');
  687. return _realm.Intrinsics.RegExpStringIteratorPrototype.Construct(matcher, s, global, fullUnicode);
  688. }
  689. private static ulong AdvanceStringIndex(string s, ulong index, bool unicode)
  690. {
  691. if (!unicode || index + 1 >= (ulong) s.Length)
  692. {
  693. return index + 1;
  694. }
  695. var first = s[(int) index];
  696. if (first < 0xD800 || first > 0xDBFF)
  697. {
  698. return index + 1;
  699. }
  700. var second = s[(int) (index + 1)];
  701. if (second < 0xDC00 || second > 0xDFFF)
  702. {
  703. return index + 1;
  704. }
  705. return index + 2;
  706. }
  707. internal static JsValue RegExpExec(ObjectInstance r, string s)
  708. {
  709. var ri = r as JsRegExp;
  710. if ((ri is null || !ri.HasDefaultRegExpExec) && r.Get(PropertyExec) is ICallable callable)
  711. {
  712. var result = callable.Call(r, s);
  713. if (!result.IsNull() && !result.IsObject())
  714. {
  715. ExceptionHelper.ThrowTypeError(r.Engine.Realm);
  716. }
  717. return result;
  718. }
  719. if (ri is null)
  720. {
  721. ExceptionHelper.ThrowTypeError(r.Engine.Realm);
  722. }
  723. return RegExpBuiltinExec(ri, s);
  724. }
  725. internal bool HasDefaultExec => Get(PropertyExec) is ClrFunction functionInstance && functionInstance._func == _defaultExec;
  726. /// <summary>
  727. /// https://tc39.es/ecma262/#sec-regexpbuiltinexec
  728. /// </summary>
  729. private static JsValue RegExpBuiltinExec(JsRegExp R, string s)
  730. {
  731. var length = (ulong) s.Length;
  732. var lastIndex = TypeConverter.ToLength(R.Get(JsRegExp.PropertyLastIndex));
  733. var global = R.Global;
  734. var sticky = R.Sticky;
  735. if (!global && !sticky)
  736. {
  737. lastIndex = 0;
  738. }
  739. if (string.Equals(R.Source, JsRegExp.regExpForMatchingAllCharacters, StringComparison.Ordinal)) // Reg Exp is really ""
  740. {
  741. if (lastIndex > (ulong) s.Length)
  742. {
  743. return Null;
  744. }
  745. // "aaa".match() => [ '', index: 0, input: 'aaa' ]
  746. var array = R.Engine.Realm.Intrinsics.Array.ArrayCreate(1);
  747. array.FastSetDataProperty(PropertyIndex._value, lastIndex);
  748. array.FastSetDataProperty(PropertyInput._value, s);
  749. array.SetIndexValue(0, JsString.Empty, updateLength: false);
  750. return array;
  751. }
  752. var matcher = R.Value;
  753. var fullUnicode = R.FullUnicode;
  754. var hasIndices = R.Indices;
  755. if (!global && !sticky && !fullUnicode && !hasIndices)
  756. {
  757. // we can the non-stateful fast path which is the common case
  758. var m = matcher.Match(s, (int) lastIndex);
  759. if (!m.Success)
  760. {
  761. return Null;
  762. }
  763. return CreateReturnValueArray(R, m, s, fullUnicode: false, hasIndices: false);
  764. }
  765. // the stateful version
  766. Match match;
  767. if (lastIndex > length)
  768. {
  769. R.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero, true);
  770. return Null;
  771. }
  772. var startAt = (int) lastIndex;
  773. while (true)
  774. {
  775. match = R.Value.Match(s, startAt);
  776. // The conversion of Unicode regex patterns to .NET Regex has some flaws:
  777. // when the pattern may match empty strings, the adapted Regex will return empty string matches
  778. // in the middle of surrogate pairs. As a best effort solution, we remove these fake positive matches.
  779. // (See also: https://github.com/sebastienros/esprima-dotnet/pull/364#issuecomment-1606045259)
  780. if (match.Success
  781. && fullUnicode
  782. && match.Length == 0
  783. && 0 < match.Index && match.Index < s.Length
  784. && char.IsHighSurrogate(s[match.Index - 1]) && char.IsLowSurrogate(s[match.Index]))
  785. {
  786. startAt++;
  787. continue;
  788. }
  789. break;
  790. }
  791. var success = match.Success && (!sticky || match.Index == (int) lastIndex);
  792. if (!success)
  793. {
  794. R.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero, true);
  795. return Null;
  796. }
  797. var e = match.Index + match.Length;
  798. // NOTE: Even in Unicode mode, we don't need to translate indices as .NET regexes always return code unit indices.
  799. if (global || sticky)
  800. {
  801. R.Set(JsRegExp.PropertyLastIndex, e, true);
  802. }
  803. return CreateReturnValueArray(R, match, s, fullUnicode, hasIndices);
  804. }
  805. private static JsArray CreateReturnValueArray(
  806. JsRegExp rei,
  807. Match match,
  808. string s,
  809. bool fullUnicode,
  810. bool hasIndices)
  811. {
  812. var engine = rei.Engine;
  813. var actualGroupCount = GetActualRegexGroupCount(rei, match);
  814. var array = engine.Realm.Intrinsics.Array.ArrayCreate((ulong) actualGroupCount);
  815. array.CreateDataProperty(PropertyIndex, match.Index);
  816. array.CreateDataProperty(PropertyInput, s);
  817. ObjectInstance? groups = null;
  818. List<string>? groupNames = null;
  819. var indices = hasIndices ? new List<JsNumber[]?>(actualGroupCount) : null;
  820. for (uint i = 0; i < actualGroupCount; i++)
  821. {
  822. var capture = match.Groups[(int) i];
  823. var capturedValue = Undefined;
  824. if (capture?.Success == true)
  825. {
  826. capturedValue = capture.Value;
  827. }
  828. if (hasIndices)
  829. {
  830. if (capture?.Success == true)
  831. {
  832. indices!.Add([JsNumber.Create(capture.Index), JsNumber.Create(capture.Index + capture.Length)]);
  833. }
  834. else
  835. {
  836. indices!.Add(null);
  837. }
  838. }
  839. var groupName = GetRegexGroupName(rei, (int) i);
  840. if (!string.IsNullOrWhiteSpace(groupName))
  841. {
  842. groups ??= OrdinaryObjectCreate(engine, null);
  843. groups.CreateDataPropertyOrThrow(groupName, capturedValue);
  844. groupNames ??= [];
  845. groupNames.Add(groupName!);
  846. }
  847. array.SetIndexValue(i, capturedValue, updateLength: false);
  848. }
  849. array.CreateDataProperty(PropertyGroups, groups ?? Undefined);
  850. if (hasIndices)
  851. {
  852. var indicesArray = MakeMatchIndicesIndexPairArray(engine, s, indices!, groupNames, groupNames?.Count > 0);
  853. array.CreateDataPropertyOrThrow("indices", indicesArray);
  854. }
  855. return array;
  856. }
  857. /// <summary>
  858. /// https://tc39.es/ecma262/#sec-makematchindicesindexpairarray
  859. /// </summary>
  860. private static JsArray MakeMatchIndicesIndexPairArray(
  861. Engine engine,
  862. string s,
  863. List<JsNumber[]?> indices,
  864. List<string>? groupNames,
  865. bool hasGroups)
  866. {
  867. var n = indices.Count;
  868. var a = engine.Realm.Intrinsics.Array.Construct((uint) n);
  869. ObjectInstance? groups = null;
  870. if (hasGroups)
  871. {
  872. groups = OrdinaryObjectCreate(engine, null);
  873. }
  874. a.CreateDataPropertyOrThrow("groups", groups ?? Undefined);
  875. for (var i = 0; i < n; ++i)
  876. {
  877. var matchIndices = indices[i];
  878. var matchIndexPair = matchIndices is not null
  879. ? GetMatchIndexPair(engine, s, matchIndices)
  880. : Undefined;
  881. a.Push(matchIndexPair);
  882. if (i > 0 && !string.IsNullOrWhiteSpace(groupNames?[i - 1]))
  883. {
  884. groups!.CreateDataPropertyOrThrow(groupNames![i - 1], matchIndexPair);
  885. }
  886. }
  887. return a;
  888. }
  889. /// <summary>
  890. /// https://tc39.es/ecma262/#sec-getmatchindexpair
  891. /// </summary>
  892. private static JsValue GetMatchIndexPair(Engine engine, string s, JsNumber[] match)
  893. {
  894. return engine.Realm.Intrinsics.Array.CreateArrayFromList(match);
  895. }
  896. private static int GetActualRegexGroupCount(JsRegExp rei, Match match)
  897. {
  898. return rei.ParseResult.Success ? rei.ParseResult.ActualRegexGroupCount : match.Groups.Count;
  899. }
  900. private static string? GetRegexGroupName(JsRegExp rei, int index)
  901. {
  902. if (index == 0)
  903. {
  904. return null;
  905. }
  906. var regex = rei.Value;
  907. if (rei.ParseResult.Success)
  908. {
  909. return rei.ParseResult.GetRegexGroupName(index);
  910. }
  911. var groupNameFromNumber = regex.GroupNameFromNumber(index);
  912. if (groupNameFromNumber.Length == 1 && groupNameFromNumber[0] == 48 + index)
  913. {
  914. // regex defaults to index as group name when it's not a named group
  915. return null;
  916. }
  917. return groupNameFromNumber;
  918. }
  919. private JsValue Exec(JsValue thisObject, JsCallArguments arguments)
  920. {
  921. var r = thisObject as JsRegExp;
  922. if (r is null)
  923. {
  924. ExceptionHelper.ThrowTypeError(_engine.Realm);
  925. }
  926. var s = TypeConverter.ToString(arguments.At(0));
  927. return RegExpBuiltinExec(r, s);
  928. }
  929. }