RegExpPrototype.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. using System.Text.RegularExpressions;
  2. using Jint.Collections;
  3. using Jint.Native.Number;
  4. using Jint.Native.Object;
  5. using Jint.Native.String;
  6. using Jint.Native.Symbol;
  7. using Jint.Pooling;
  8. using Jint.Runtime;
  9. using Jint.Runtime.Descriptors;
  10. using Jint.Runtime.Interop;
  11. namespace Jint.Native.RegExp
  12. {
  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 readonly RegExpConstructor _constructor;
  25. private readonly Func<JsValue, JsValue[], JsValue> _defaultExec;
  26. internal RegExpPrototype(
  27. Engine engine,
  28. Realm realm,
  29. RegExpConstructor constructor,
  30. ObjectPrototype objectPrototype) : base(engine, realm)
  31. {
  32. _defaultExec = Exec;
  33. _constructor = constructor;
  34. _prototype = objectPrototype;
  35. }
  36. protected override void Initialize()
  37. {
  38. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  39. GetSetPropertyDescriptor CreateGetAccessorDescriptor(string name, Func<JsRegExp, JsValue> valueExtractor, JsValue? protoValue = null)
  40. {
  41. return new GetSetPropertyDescriptor(
  42. get: new ClrFunctionInstance(Engine, name, (thisObj, arguments) =>
  43. {
  44. if (ReferenceEquals(thisObj, this))
  45. {
  46. return protoValue ?? Undefined;
  47. }
  48. var r = thisObj as JsRegExp;
  49. if (r is null)
  50. {
  51. ExceptionHelper.ThrowTypeError(_realm);
  52. }
  53. return valueExtractor(r);
  54. }, 0, lengthFlags),
  55. set: Undefined,
  56. flags: PropertyFlag.Configurable);
  57. }
  58. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  59. var properties = new PropertyDictionary(14, checkExistingKeys: false)
  60. {
  61. ["constructor"] = new PropertyDescriptor(_constructor, propertyFlags),
  62. ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToRegExpString, 0, lengthFlags), propertyFlags),
  63. ["exec"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "exec", _defaultExec, 1, lengthFlags), propertyFlags),
  64. ["test"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "test", Test, 1, lengthFlags), propertyFlags),
  65. ["dotAll"] = CreateGetAccessorDescriptor("get dotAll", static r => r.DotAll),
  66. ["flags"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get flags", Flags, 0, lengthFlags), set: Undefined, flags: PropertyFlag.Configurable),
  67. ["global"] = CreateGetAccessorDescriptor("get global", static r => r.Global),
  68. ["hasIndices"] = CreateGetAccessorDescriptor("get hasIndices", static r => r.Indices),
  69. ["ignoreCase"] = CreateGetAccessorDescriptor("get ignoreCase", static r => r.IgnoreCase),
  70. ["multiline"] = CreateGetAccessorDescriptor("get multiline", static r => r.Multiline),
  71. ["source"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get source", Source, 0, lengthFlags), set: Undefined, flags: PropertyFlag.Configurable),
  72. ["sticky"] = CreateGetAccessorDescriptor("get sticky", static r => r.Sticky),
  73. ["unicode"] = CreateGetAccessorDescriptor("get unicode", static r => r.FullUnicode),
  74. ["unicodeSets"] = CreateGetAccessorDescriptor("get unicodeSets", static r => r.UnicodeSets)
  75. };
  76. SetProperties(properties);
  77. var symbols = new SymbolDictionary(5)
  78. {
  79. [GlobalSymbolRegistry.Match] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.match]", Match, 1, lengthFlags), propertyFlags),
  80. [GlobalSymbolRegistry.MatchAll] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.matchAll]", MatchAll, 1, lengthFlags), propertyFlags),
  81. [GlobalSymbolRegistry.Replace] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.replace]", Replace, 2, lengthFlags), propertyFlags),
  82. [GlobalSymbolRegistry.Search] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.search]", Search, 1, lengthFlags), propertyFlags),
  83. [GlobalSymbolRegistry.Split] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "[Symbol.split]", Split, 2, lengthFlags), propertyFlags)
  84. };
  85. SetSymbols(symbols);
  86. }
  87. /// <summary>
  88. /// https://tc39.es/ecma262/#sec-get-regexp.prototype.source
  89. /// </summary>
  90. private JsValue Source(JsValue thisObject, JsValue[] arguments)
  91. {
  92. if (ReferenceEquals(thisObject, this))
  93. {
  94. return DefaultSource;
  95. }
  96. var r = thisObject as JsRegExp;
  97. if (r is null)
  98. {
  99. ExceptionHelper.ThrowTypeError(_realm);
  100. }
  101. if (string.IsNullOrEmpty(r.Source))
  102. {
  103. return JsRegExp.regExpForMatchingAllCharacters;
  104. }
  105. return r.Source
  106. .Replace("\\/", "/") // ensure forward-slashes
  107. .Replace("/", "\\/") // then escape again
  108. .Replace("\n", "\\n");
  109. }
  110. /// <summary>
  111. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
  112. /// </summary>
  113. private JsValue Replace(JsValue thisObject, JsValue[] arguments)
  114. {
  115. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.replace");
  116. var s = TypeConverter.ToString(arguments.At(0));
  117. var lengthS = s.Length;
  118. var replaceValue = arguments.At(1);
  119. var functionalReplace = replaceValue is ICallable;
  120. // we need heavier logic if we have named captures
  121. var mayHaveNamedCaptures = false;
  122. if (!functionalReplace)
  123. {
  124. var value = TypeConverter.ToString(replaceValue);
  125. replaceValue = value;
  126. mayHaveNamedCaptures = value.IndexOf('$') != -1;
  127. }
  128. var flags = TypeConverter.ToString(rx.Get(PropertyFlags));
  129. var global = flags.IndexOf('g') != -1;
  130. var fullUnicode = false;
  131. if (global)
  132. {
  133. fullUnicode = flags.IndexOf('u') != -1;
  134. rx.Set(JsRegExp.PropertyLastIndex, 0, true);
  135. }
  136. // check if we can access fast path
  137. if (!fullUnicode
  138. && !mayHaveNamedCaptures
  139. && !TypeConverter.ToBoolean(rx.Get(PropertySticky))
  140. && rx is JsRegExp rei && rei.HasDefaultRegExpExec)
  141. {
  142. var count = global ? int.MaxValue : 1;
  143. string result;
  144. if (functionalReplace)
  145. {
  146. string Evaluator(Match match)
  147. {
  148. var actualGroupCount = GetActualRegexGroupCount(rei, match);
  149. var replacerArgs = new List<JsValue>(actualGroupCount + 2);
  150. replacerArgs.Add(match.Value);
  151. ObjectInstance? groups = null;
  152. for (var i = 1; i < actualGroupCount; i++)
  153. {
  154. var capture = match.Groups[i];
  155. replacerArgs.Add(capture.Success ? capture.Value : Undefined);
  156. var groupName = GetRegexGroupName(rei, i);
  157. if (!string.IsNullOrWhiteSpace(groupName))
  158. {
  159. groups ??= OrdinaryObjectCreate(_engine, null);
  160. groups.CreateDataPropertyOrThrow(groupName, capture.Success ? capture.Value : Undefined);
  161. }
  162. }
  163. replacerArgs.Add(match.Index);
  164. replacerArgs.Add(s);
  165. if (groups is not null)
  166. {
  167. replacerArgs.Add(groups);
  168. }
  169. return CallFunctionalReplace(replaceValue, replacerArgs);
  170. }
  171. result = rei.Value.Replace(s, Evaluator, count);
  172. }
  173. else
  174. {
  175. result = rei.Value.Replace(s, TypeConverter.ToString(replaceValue), count);
  176. }
  177. rx.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero);
  178. return result;
  179. }
  180. var results = new List<ObjectInstance>();
  181. while (true)
  182. {
  183. var result = RegExpExec(rx, s);
  184. if (result.IsNull())
  185. {
  186. break;
  187. }
  188. results.Add((ObjectInstance) result);
  189. if (!global)
  190. {
  191. break;
  192. }
  193. var matchStr = TypeConverter.ToString(result.Get(0));
  194. if (matchStr == "")
  195. {
  196. var thisIndex = TypeConverter.ToLength(rx.Get(JsRegExp.PropertyLastIndex));
  197. var nextIndex = AdvanceStringIndex(s, thisIndex, fullUnicode);
  198. rx.Set(JsRegExp.PropertyLastIndex, nextIndex);
  199. }
  200. }
  201. var accumulatedResult = "";
  202. var nextSourcePosition = 0;
  203. var captures = new List<string>();
  204. for (var i = 0; i < results.Count; i++)
  205. {
  206. var result = results[i];
  207. var nCaptures = (int) result.Length;
  208. nCaptures = System.Math.Max(nCaptures - 1, 0);
  209. var matched = TypeConverter.ToString(result.Get(0));
  210. var matchLength = matched.Length;
  211. var position = (int) TypeConverter.ToInteger(result.Get(PropertyIndex));
  212. position = System.Math.Max(System.Math.Min(position, lengthS), 0);
  213. uint n = 1;
  214. captures.Clear();
  215. while (n <= nCaptures)
  216. {
  217. var capN = result.Get(n);
  218. var value = !capN.IsUndefined() ? TypeConverter.ToString(capN) : "";
  219. captures.Add(value);
  220. n++;
  221. }
  222. var namedCaptures = result.Get(PropertyGroups);
  223. string replacement;
  224. if (functionalReplace)
  225. {
  226. var replacerArgs = new List<JsValue>();
  227. replacerArgs.Add(matched);
  228. foreach (var capture in captures)
  229. {
  230. replacerArgs.Add(capture);
  231. }
  232. replacerArgs.Add(position);
  233. replacerArgs.Add(s);
  234. if (!namedCaptures.IsUndefined())
  235. {
  236. replacerArgs.Add(namedCaptures);
  237. }
  238. replacement = CallFunctionalReplace(replaceValue, replacerArgs);
  239. }
  240. else
  241. {
  242. if (!namedCaptures.IsUndefined())
  243. {
  244. namedCaptures = TypeConverter.ToObject(_realm, namedCaptures);
  245. }
  246. replacement = GetSubstitution(matched, s, position, captures.ToArray(), namedCaptures, TypeConverter.ToString(replaceValue));
  247. }
  248. if (position >= nextSourcePosition)
  249. {
  250. #pragma warning disable CA1845
  251. accumulatedResult = accumulatedResult +
  252. s.Substring(nextSourcePosition, position - nextSourcePosition) +
  253. replacement;
  254. #pragma warning restore CA1845
  255. nextSourcePosition = position + matchLength;
  256. }
  257. }
  258. if (nextSourcePosition >= lengthS)
  259. {
  260. return accumulatedResult;
  261. }
  262. #pragma warning disable CA1845
  263. return accumulatedResult + s.Substring(nextSourcePosition);
  264. #pragma warning restore CA1845
  265. }
  266. private static string CallFunctionalReplace(JsValue replacer, List<JsValue> replacerArgs)
  267. {
  268. var result = ((ICallable) replacer).Call(Undefined, replacerArgs.ToArray());
  269. return TypeConverter.ToString(result);
  270. }
  271. /// <summary>
  272. /// https://tc39.es/ecma262/#sec-getsubstitution
  273. /// </summary>
  274. internal static string GetSubstitution(
  275. string matched,
  276. string str,
  277. int position,
  278. string[] captures,
  279. JsValue namedCaptures,
  280. string replacement)
  281. {
  282. // If there is no pattern, replace the pattern as is.
  283. if (replacement.IndexOf('$') < 0)
  284. {
  285. return replacement;
  286. }
  287. // Patterns
  288. // $$ Inserts a "$".
  289. // $& Inserts the matched substring.
  290. // $` Inserts the portion of the string that precedes the matched substring.
  291. // $' Inserts the portion of the string that follows the matched substring.
  292. // $n or $nn Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.
  293. using var replacementBuilder = StringBuilderPool.Rent();
  294. var sb = replacementBuilder.Builder;
  295. for (var i = 0; i < replacement.Length; i++)
  296. {
  297. char c = replacement[i];
  298. if (c == '$' && i < replacement.Length - 1)
  299. {
  300. c = replacement[++i];
  301. switch (c)
  302. {
  303. case '$':
  304. sb.Append('$');
  305. break;
  306. case '&':
  307. sb.Append(matched);
  308. break;
  309. #pragma warning disable CA1846
  310. case '`':
  311. sb.Append(str.Substring(0, position));
  312. break;
  313. case '\'':
  314. sb.Append(str.Substring(position + matched.Length));
  315. break;
  316. #pragma warning restore CA1846
  317. case '<':
  318. var gtPos = replacement.IndexOf('>', i + 1);
  319. if (gtPos == -1 || namedCaptures.IsUndefined())
  320. {
  321. sb.Append('$');
  322. sb.Append(c);
  323. }
  324. else
  325. {
  326. var startIndex = i + 1;
  327. var groupName = replacement.Substring(startIndex, gtPos - startIndex);
  328. var capture = namedCaptures.Get(groupName);
  329. if (!capture.IsUndefined())
  330. {
  331. sb.Append(TypeConverter.ToString(capture));
  332. }
  333. i = gtPos;
  334. }
  335. break;
  336. default:
  337. {
  338. if (char.IsDigit(c))
  339. {
  340. int matchNumber1 = c - '0';
  341. // The match number can be one or two digits long.
  342. int matchNumber2 = 0;
  343. if (i < replacement.Length - 1 && char.IsDigit(replacement[i + 1]))
  344. {
  345. matchNumber2 = matchNumber1 * 10 + (replacement[i + 1] - '0');
  346. }
  347. // Try the two digit capture first.
  348. if (matchNumber2 > 0 && matchNumber2 <= captures.Length)
  349. {
  350. // Two digit capture replacement.
  351. sb.Append(TypeConverter.ToString(captures[matchNumber2 - 1]));
  352. i++;
  353. }
  354. else if (matchNumber1 > 0 && matchNumber1 <= captures.Length)
  355. {
  356. // Single digit capture replacement.
  357. sb.Append(TypeConverter.ToString(captures[matchNumber1 - 1]));
  358. }
  359. else
  360. {
  361. // Capture does not exist.
  362. sb.Append('$');
  363. i--;
  364. }
  365. }
  366. else
  367. {
  368. // Unknown replacement pattern.
  369. sb.Append('$');
  370. sb.Append(c);
  371. }
  372. break;
  373. }
  374. }
  375. }
  376. else
  377. {
  378. sb.Append(c);
  379. }
  380. }
  381. return replacementBuilder.ToString();
  382. }
  383. /// <summary>
  384. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@split
  385. /// </summary>
  386. private JsValue Split(JsValue thisObject, JsValue[] arguments)
  387. {
  388. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.split");
  389. var s = TypeConverter.ToString(arguments.At(0));
  390. var limit = arguments.At(1);
  391. var c = SpeciesConstructor(rx, _realm.Intrinsics.RegExp);
  392. var flags = TypeConverter.ToJsString(rx.Get(PropertyFlags));
  393. var unicodeMatching = flags.IndexOf('u') > -1;
  394. var newFlags = flags.IndexOf('y') > -1 ? flags : new JsString(flags.ToString() + 'y');
  395. var splitter = Construct(c, new JsValue[]
  396. {
  397. rx,
  398. newFlags
  399. });
  400. uint lengthA = 0;
  401. var lim = limit.IsUndefined() ? NumberConstructor.MaxSafeInteger : TypeConverter.ToUint32(limit);
  402. if (lim == 0)
  403. {
  404. return _realm.Intrinsics.Array.ArrayCreate(0);
  405. }
  406. if (s.Length == 0)
  407. {
  408. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  409. var z = RegExpExec(splitter, s);
  410. if (!z.IsNull())
  411. {
  412. return a;
  413. }
  414. a.SetIndexValue(0, s, updateLength: true);
  415. return a;
  416. }
  417. if (!unicodeMatching && rx is JsRegExp R && R.HasDefaultRegExpExec)
  418. {
  419. // we can take faster path
  420. if (string.Equals(R.Source, JsRegExp.regExpForMatchingAllCharacters, StringComparison.Ordinal))
  421. {
  422. // if empty string, just a string split
  423. return StringPrototype.SplitWithStringSeparator(_realm, "", s, (uint) s.Length);
  424. }
  425. var a = _realm.Intrinsics.Array.Construct(Arguments.Empty);
  426. int lastIndex = 0;
  427. uint index = 0;
  428. for (var match = R.Value.Match(s, 0); match.Success; match = match.NextMatch())
  429. {
  430. if (match.Length == 0 && (match.Index == 0 || match.Index == s.Length || match.Index == lastIndex))
  431. {
  432. continue;
  433. }
  434. // Add the match results to the array.
  435. a.SetIndexValue(index++, s.Substring(lastIndex, match.Index - lastIndex), updateLength: true);
  436. if (index >= lim)
  437. {
  438. return a;
  439. }
  440. lastIndex = match.Index + match.Length;
  441. var actualGroupCount = GetActualRegexGroupCount(R, match);
  442. for (int i = 1; i < actualGroupCount; i++)
  443. {
  444. var group = match.Groups[i];
  445. var item = Undefined;
  446. if (group.Captures.Count > 0)
  447. {
  448. item = match.Groups[i].Value;
  449. }
  450. a.SetIndexValue(index++, item, updateLength: true);
  451. if (index >= lim)
  452. {
  453. return a;
  454. }
  455. }
  456. }
  457. // Add the last part of the split
  458. a.SetIndexValue(index, s.Substring(lastIndex), updateLength: true);
  459. return a;
  460. }
  461. return SplitSlow(s, splitter, unicodeMatching, lengthA, lim);
  462. }
  463. private JsValue SplitSlow(string s, ObjectInstance splitter, bool unicodeMatching, uint lengthA, long lim)
  464. {
  465. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  466. ulong previousStringIndex = 0;
  467. ulong currentIndex = 0;
  468. while (currentIndex < (ulong) s.Length)
  469. {
  470. splitter.Set(JsRegExp.PropertyLastIndex, currentIndex, true);
  471. var z = RegExpExec(splitter, s);
  472. if (z.IsNull())
  473. {
  474. currentIndex = AdvanceStringIndex(s, currentIndex, unicodeMatching);
  475. continue;
  476. }
  477. var endIndex = TypeConverter.ToLength(splitter.Get(JsRegExp.PropertyLastIndex));
  478. endIndex = System.Math.Min(endIndex, (ulong) s.Length);
  479. if (endIndex == previousStringIndex)
  480. {
  481. currentIndex = AdvanceStringIndex(s, currentIndex, unicodeMatching);
  482. continue;
  483. }
  484. var t = s.Substring((int) previousStringIndex, (int) (currentIndex - previousStringIndex));
  485. a.SetIndexValue(lengthA, t, updateLength: true);
  486. lengthA++;
  487. if (lengthA == lim)
  488. {
  489. return a;
  490. }
  491. previousStringIndex = endIndex;
  492. var numberOfCaptures = (int) TypeConverter.ToLength(z.Get(CommonProperties.Length));
  493. numberOfCaptures = System.Math.Max(numberOfCaptures - 1, 0);
  494. var i = 1;
  495. while (i <= numberOfCaptures)
  496. {
  497. var nextCapture = z.Get(i);
  498. a.SetIndexValue(lengthA, nextCapture, updateLength: true);
  499. i++;
  500. lengthA++;
  501. if (lengthA == lim)
  502. {
  503. return a;
  504. }
  505. }
  506. currentIndex = previousStringIndex;
  507. }
  508. a.SetIndexValue(lengthA, s.Substring((int) previousStringIndex, s.Length - (int) previousStringIndex), updateLength: true);
  509. return a;
  510. }
  511. private JsValue Flags(JsValue thisObject, JsValue[] arguments)
  512. {
  513. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.flags");
  514. static string AddFlagIfPresent(JsValue o, JsValue p, char flag, string s)
  515. {
  516. return TypeConverter.ToBoolean(o.Get(p)) ? s + flag : s;
  517. }
  518. var result = AddFlagIfPresent(r, "hasIndices", 'd', "");
  519. result = AddFlagIfPresent(r, PropertyGlobal, 'g', result);
  520. result = AddFlagIfPresent(r, "ignoreCase", 'i', result);
  521. result = AddFlagIfPresent(r, "multiline", 'm', result);
  522. result = AddFlagIfPresent(r, "dotAll", 's', result);
  523. result = AddFlagIfPresent(r, "unicode", 'u', result);
  524. result = AddFlagIfPresent(r, "unicodeSets", 'v', result);
  525. result = AddFlagIfPresent(r, PropertySticky, 'y', result);
  526. return result;
  527. }
  528. private JsValue ToRegExpString(JsValue thisObject, JsValue[] arguments)
  529. {
  530. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.toString");
  531. var pattern = TypeConverter.ToString(r.Get(PropertySource));
  532. var flags = TypeConverter.ToString(r.Get(PropertyFlags));
  533. return "/" + pattern + "/" + flags;
  534. }
  535. private JsValue Test(JsValue thisObject, JsValue[] arguments)
  536. {
  537. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.test");
  538. var s = TypeConverter.ToString(arguments.At(0));
  539. // check couple fast paths
  540. if (r is JsRegExp R && !R.FullUnicode)
  541. {
  542. if (!R.Sticky && !R.Global)
  543. {
  544. R.Set(JsRegExp.PropertyLastIndex, 0, throwOnError: true);
  545. return R.Value.IsMatch(s);
  546. }
  547. var lastIndex = (int) TypeConverter.ToLength(R.Get(JsRegExp.PropertyLastIndex));
  548. if (lastIndex >= s.Length && s.Length > 0)
  549. {
  550. return JsBoolean.False;
  551. }
  552. var m = R.Value.Match(s, lastIndex);
  553. if (!m.Success || (R.Sticky && m.Index != lastIndex))
  554. {
  555. R.Set(JsRegExp.PropertyLastIndex, 0, throwOnError: true);
  556. return JsBoolean.False;
  557. }
  558. R.Set(JsRegExp.PropertyLastIndex, m.Index + m.Length, throwOnError: true);
  559. return JsBoolean.True;
  560. }
  561. var match = RegExpExec(r, s);
  562. return !match.IsNull();
  563. }
  564. /// <summary>
  565. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@search
  566. /// </summary>
  567. private JsValue Search(JsValue thisObject, JsValue[] arguments)
  568. {
  569. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.search");
  570. var s = TypeConverter.ToString(arguments.At(0));
  571. var previousLastIndex = rx.Get(JsRegExp.PropertyLastIndex);
  572. if (!SameValue(previousLastIndex, 0))
  573. {
  574. rx.Set(JsRegExp.PropertyLastIndex, 0, true);
  575. }
  576. var result = RegExpExec(rx, s);
  577. var currentLastIndex = rx.Get(JsRegExp.PropertyLastIndex);
  578. if (!SameValue(currentLastIndex, previousLastIndex))
  579. {
  580. rx.Set(JsRegExp.PropertyLastIndex, previousLastIndex, true);
  581. }
  582. if (result.IsNull())
  583. {
  584. return -1;
  585. }
  586. return result.Get(PropertyIndex);
  587. }
  588. /// <summary>
  589. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@match
  590. /// </summary>
  591. private JsValue Match(JsValue thisObject, JsValue[] arguments)
  592. {
  593. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.match");
  594. var s = TypeConverter.ToString(arguments.At(0));
  595. var flags = TypeConverter.ToString(rx.Get(PropertyFlags));
  596. var global = flags.IndexOf('g') != -1;
  597. if (!global)
  598. {
  599. return RegExpExec(rx, s);
  600. }
  601. var fullUnicode = flags.IndexOf('u') != -1;
  602. rx.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero, true);
  603. if (!fullUnicode
  604. && rx is JsRegExp rei
  605. && rei.HasDefaultRegExpExec)
  606. {
  607. // fast path
  608. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  609. if (rei.Sticky)
  610. {
  611. var match = rei.Value.Match(s);
  612. if (!match.Success || match.Index != 0)
  613. {
  614. return Null;
  615. }
  616. a.SetIndexValue(0, match.Value, updateLength: false);
  617. uint li = 0;
  618. while (true)
  619. {
  620. match = match.NextMatch();
  621. if (!match.Success || match.Index != ++li)
  622. break;
  623. a.SetIndexValue(li, match.Value, updateLength: false);
  624. }
  625. a.SetLength(li);
  626. return a;
  627. }
  628. else
  629. {
  630. var matches = rei.Value.Matches(s);
  631. if (matches.Count == 0)
  632. {
  633. return Null;
  634. }
  635. a.EnsureCapacity((uint) matches.Count);
  636. a.SetLength((uint) matches.Count);
  637. for (var i = 0; i < matches.Count; i++)
  638. {
  639. a.SetIndexValue((uint) i, matches[i].Value, updateLength: false);
  640. }
  641. return a;
  642. }
  643. }
  644. return MatchSlow(rx, s, fullUnicode);
  645. }
  646. private JsValue MatchSlow(ObjectInstance rx, string s, bool fullUnicode)
  647. {
  648. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  649. uint n = 0;
  650. while (true)
  651. {
  652. var result = RegExpExec(rx, s);
  653. if (result.IsNull())
  654. {
  655. a.SetLength(n);
  656. return n == 0 ? Null : a;
  657. }
  658. var matchStr = TypeConverter.ToString(result.Get(JsString.NumberZeroString));
  659. a.SetIndexValue(n, matchStr, updateLength: false);
  660. if (matchStr == "")
  661. {
  662. var thisIndex = TypeConverter.ToLength(rx.Get(JsRegExp.PropertyLastIndex));
  663. var nextIndex = AdvanceStringIndex(s, thisIndex, fullUnicode);
  664. rx.Set(JsRegExp.PropertyLastIndex, nextIndex, true);
  665. }
  666. n++;
  667. }
  668. }
  669. /// <summary>
  670. /// https://tc39.es/ecma262/#sec-regexp-prototype-matchall
  671. /// </summary>
  672. private JsValue MatchAll(JsValue thisObject, JsValue[] arguments)
  673. {
  674. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.matchAll");
  675. var s = TypeConverter.ToString(arguments.At(0));
  676. var c = SpeciesConstructor(r, _realm.Intrinsics.RegExp);
  677. var flags = TypeConverter.ToJsString(r.Get(PropertyFlags));
  678. var matcher = Construct(c, new JsValue[]
  679. {
  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.IndexOf('g') != -1;
  686. var fullUnicode = flags.IndexOf('u') != -1;
  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, new JsValue[] { 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 ClrFunctionInstance 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(new[] { 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 ??= new List<string>();
  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, JsValue[] 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. }
  930. }