RegExpPrototype.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. accumulatedResult = accumulatedResult +
  251. s.Substring(nextSourcePosition, position - nextSourcePosition) +
  252. replacement;
  253. nextSourcePosition = position + matchLength;
  254. }
  255. }
  256. if (nextSourcePosition >= lengthS)
  257. {
  258. return accumulatedResult;
  259. }
  260. return accumulatedResult + s.Substring(nextSourcePosition);
  261. }
  262. private static string CallFunctionalReplace(JsValue replacer, List<JsValue> replacerArgs)
  263. {
  264. var result = ((ICallable) replacer).Call(Undefined, replacerArgs.ToArray());
  265. return TypeConverter.ToString(result);
  266. }
  267. /// <summary>
  268. /// https://tc39.es/ecma262/#sec-getsubstitution
  269. /// </summary>
  270. internal static string GetSubstitution(
  271. string matched,
  272. string str,
  273. int position,
  274. string[] captures,
  275. JsValue namedCaptures,
  276. string replacement)
  277. {
  278. // If there is no pattern, replace the pattern as is.
  279. if (replacement.IndexOf('$') < 0)
  280. {
  281. return replacement;
  282. }
  283. // Patterns
  284. // $$ Inserts a "$".
  285. // $& Inserts the matched substring.
  286. // $` Inserts the portion of the string that precedes the matched substring.
  287. // $' Inserts the portion of the string that follows the matched substring.
  288. // $n or $nn Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.
  289. using var replacementBuilder = StringBuilderPool.Rent();
  290. var sb = replacementBuilder.Builder;
  291. for (var i = 0; i < replacement.Length; i++)
  292. {
  293. char c = replacement[i];
  294. if (c == '$' && i < replacement.Length - 1)
  295. {
  296. c = replacement[++i];
  297. switch (c)
  298. {
  299. case '$':
  300. sb.Append('$');
  301. break;
  302. case '&':
  303. sb.Append(matched);
  304. break;
  305. case '`':
  306. sb.Append(str.Substring(0, position));
  307. break;
  308. case '\'':
  309. sb.Append(str.Substring(position + matched.Length));
  310. break;
  311. case '<':
  312. var gtPos = replacement.IndexOf('>', i + 1);
  313. if (gtPos == -1 || namedCaptures.IsUndefined())
  314. {
  315. sb.Append('$');
  316. sb.Append(c);
  317. }
  318. else
  319. {
  320. var startIndex = i + 1;
  321. var groupName = replacement.Substring(startIndex, gtPos - startIndex);
  322. var capture = namedCaptures.Get(groupName);
  323. if (!capture.IsUndefined())
  324. {
  325. sb.Append(TypeConverter.ToString(capture));
  326. }
  327. i = gtPos;
  328. }
  329. break;
  330. default:
  331. {
  332. if (char.IsDigit(c))
  333. {
  334. int matchNumber1 = c - '0';
  335. // The match number can be one or two digits long.
  336. int matchNumber2 = 0;
  337. if (i < replacement.Length - 1 && char.IsDigit(replacement[i + 1]))
  338. {
  339. matchNumber2 = matchNumber1 * 10 + (replacement[i + 1] - '0');
  340. }
  341. // Try the two digit capture first.
  342. if (matchNumber2 > 0 && matchNumber2 <= captures.Length)
  343. {
  344. // Two digit capture replacement.
  345. sb.Append(TypeConverter.ToString(captures[matchNumber2 - 1]));
  346. i++;
  347. }
  348. else if (matchNumber1 > 0 && matchNumber1 <= captures.Length)
  349. {
  350. // Single digit capture replacement.
  351. sb.Append(TypeConverter.ToString(captures[matchNumber1 - 1]));
  352. }
  353. else
  354. {
  355. // Capture does not exist.
  356. sb.Append('$');
  357. i--;
  358. }
  359. }
  360. else
  361. {
  362. // Unknown replacement pattern.
  363. sb.Append('$');
  364. sb.Append(c);
  365. }
  366. break;
  367. }
  368. }
  369. }
  370. else
  371. {
  372. sb.Append(c);
  373. }
  374. }
  375. return replacementBuilder.ToString();
  376. }
  377. /// <summary>
  378. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@split
  379. /// </summary>
  380. private JsValue Split(JsValue thisObject, JsValue[] arguments)
  381. {
  382. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.split");
  383. var s = TypeConverter.ToString(arguments.At(0));
  384. var limit = arguments.At(1);
  385. var c = SpeciesConstructor(rx, _realm.Intrinsics.RegExp);
  386. var flags = TypeConverter.ToJsString(rx.Get(PropertyFlags));
  387. var unicodeMatching = flags.IndexOf('u') > -1;
  388. var newFlags = flags.IndexOf('y') > -1 ? flags : new JsString(flags.ToString() + 'y');
  389. var splitter = Construct(c, new JsValue[]
  390. {
  391. rx,
  392. newFlags
  393. });
  394. uint lengthA = 0;
  395. var lim = limit.IsUndefined() ? NumberConstructor.MaxSafeInteger : TypeConverter.ToUint32(limit);
  396. if (lim == 0)
  397. {
  398. return _realm.Intrinsics.Array.ArrayCreate(0);
  399. }
  400. if (s.Length == 0)
  401. {
  402. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  403. var z = RegExpExec(splitter, s);
  404. if (!z.IsNull())
  405. {
  406. return a;
  407. }
  408. a.SetIndexValue(0, s, updateLength: true);
  409. return a;
  410. }
  411. if (!unicodeMatching && rx is JsRegExp R && R.HasDefaultRegExpExec)
  412. {
  413. // we can take faster path
  414. if (R.Source == JsRegExp.regExpForMatchingAllCharacters)
  415. {
  416. // if empty string, just a string split
  417. return StringPrototype.SplitWithStringSeparator(_realm, "", s, (uint) s.Length);
  418. }
  419. var a = _realm.Intrinsics.Array.Construct(Arguments.Empty);
  420. int lastIndex = 0;
  421. uint index = 0;
  422. for (var match = R.Value.Match(s, 0); match.Success; match = match.NextMatch())
  423. {
  424. if (match.Length == 0 && (match.Index == 0 || match.Index == s.Length || match.Index == lastIndex))
  425. {
  426. continue;
  427. }
  428. // Add the match results to the array.
  429. a.SetIndexValue(index++, s.Substring(lastIndex, match.Index - lastIndex), updateLength: true);
  430. if (index >= lim)
  431. {
  432. return a;
  433. }
  434. lastIndex = match.Index + match.Length;
  435. var actualGroupCount = GetActualRegexGroupCount(R, match);
  436. for (int i = 1; i < actualGroupCount; i++)
  437. {
  438. var group = match.Groups[i];
  439. var item = Undefined;
  440. if (group.Captures.Count > 0)
  441. {
  442. item = match.Groups[i].Value;
  443. }
  444. a.SetIndexValue(index++, item, updateLength: true);
  445. if (index >= lim)
  446. {
  447. return a;
  448. }
  449. }
  450. }
  451. // Add the last part of the split
  452. a.SetIndexValue(index, s.Substring(lastIndex), updateLength: true);
  453. return a;
  454. }
  455. return SplitSlow(s, splitter, unicodeMatching, lengthA, lim);
  456. }
  457. private JsValue SplitSlow(string s, ObjectInstance splitter, bool unicodeMatching, uint lengthA, long lim)
  458. {
  459. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  460. ulong previousStringIndex = 0;
  461. ulong currentIndex = 0;
  462. while (currentIndex < (ulong) s.Length)
  463. {
  464. splitter.Set(JsRegExp.PropertyLastIndex, currentIndex, true);
  465. var z = RegExpExec(splitter, s);
  466. if (z.IsNull())
  467. {
  468. currentIndex = AdvanceStringIndex(s, currentIndex, unicodeMatching);
  469. continue;
  470. }
  471. var endIndex = TypeConverter.ToLength(splitter.Get(JsRegExp.PropertyLastIndex));
  472. endIndex = System.Math.Min(endIndex, (ulong) s.Length);
  473. if (endIndex == previousStringIndex)
  474. {
  475. currentIndex = AdvanceStringIndex(s, currentIndex, unicodeMatching);
  476. continue;
  477. }
  478. var t = s.Substring((int) previousStringIndex, (int) (currentIndex - previousStringIndex));
  479. a.SetIndexValue(lengthA, t, updateLength: true);
  480. lengthA++;
  481. if (lengthA == lim)
  482. {
  483. return a;
  484. }
  485. previousStringIndex = endIndex;
  486. var numberOfCaptures = (int) TypeConverter.ToLength(z.Get(CommonProperties.Length));
  487. numberOfCaptures = System.Math.Max(numberOfCaptures - 1, 0);
  488. var i = 1;
  489. while (i <= numberOfCaptures)
  490. {
  491. var nextCapture = z.Get(i);
  492. a.SetIndexValue(lengthA, nextCapture, updateLength: true);
  493. i++;
  494. lengthA++;
  495. if (lengthA == lim)
  496. {
  497. return a;
  498. }
  499. }
  500. currentIndex = previousStringIndex;
  501. }
  502. a.SetIndexValue(lengthA, s.Substring((int) previousStringIndex, s.Length - (int) previousStringIndex), updateLength: true);
  503. return a;
  504. }
  505. private JsValue Flags(JsValue thisObject, JsValue[] arguments)
  506. {
  507. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.flags");
  508. static string AddFlagIfPresent(JsValue o, JsValue p, char flag, string s)
  509. {
  510. return TypeConverter.ToBoolean(o.Get(p)) ? s + flag : s;
  511. }
  512. var result = AddFlagIfPresent(r, "hasIndices", 'd', "");
  513. result = AddFlagIfPresent(r, PropertyGlobal, 'g', result);
  514. result = AddFlagIfPresent(r, "ignoreCase", 'i', result);
  515. result = AddFlagIfPresent(r, "multiline", 'm', result);
  516. result = AddFlagIfPresent(r, "dotAll", 's', result);
  517. result = AddFlagIfPresent(r, "unicode", 'u', result);
  518. result = AddFlagIfPresent(r, "unicodeSets", 'v', result);
  519. result = AddFlagIfPresent(r, PropertySticky, 'y', result);
  520. return result;
  521. }
  522. private JsValue ToRegExpString(JsValue thisObject, JsValue[] arguments)
  523. {
  524. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.toString");
  525. var pattern = TypeConverter.ToString(r.Get(PropertySource));
  526. var flags = TypeConverter.ToString(r.Get(PropertyFlags));
  527. return "/" + pattern + "/" + flags;
  528. }
  529. private JsValue Test(JsValue thisObject, JsValue[] arguments)
  530. {
  531. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.test");
  532. var s = TypeConverter.ToString(arguments.At(0));
  533. // check couple fast paths
  534. if (r is JsRegExp R && !R.FullUnicode)
  535. {
  536. if (!R.Sticky && !R.Global)
  537. {
  538. R.Set(JsRegExp.PropertyLastIndex, 0, throwOnError: true);
  539. return R.Value.IsMatch(s);
  540. }
  541. var lastIndex = (int) TypeConverter.ToLength(R.Get(JsRegExp.PropertyLastIndex));
  542. if (lastIndex >= s.Length && s.Length > 0)
  543. {
  544. return JsBoolean.False;
  545. }
  546. var m = R.Value.Match(s, lastIndex);
  547. if (!m.Success || (R.Sticky && m.Index != lastIndex))
  548. {
  549. R.Set(JsRegExp.PropertyLastIndex, 0, throwOnError: true);
  550. return JsBoolean.False;
  551. }
  552. R.Set(JsRegExp.PropertyLastIndex, m.Index + m.Length, throwOnError: true);
  553. return JsBoolean.True;
  554. }
  555. var match = RegExpExec(r, s);
  556. return !match.IsNull();
  557. }
  558. /// <summary>
  559. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@search
  560. /// </summary>
  561. private JsValue Search(JsValue thisObject, JsValue[] arguments)
  562. {
  563. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.search");
  564. var s = TypeConverter.ToString(arguments.At(0));
  565. var previousLastIndex = rx.Get(JsRegExp.PropertyLastIndex);
  566. if (!SameValue(previousLastIndex, 0))
  567. {
  568. rx.Set(JsRegExp.PropertyLastIndex, 0, true);
  569. }
  570. var result = RegExpExec(rx, s);
  571. var currentLastIndex = rx.Get(JsRegExp.PropertyLastIndex);
  572. if (!SameValue(currentLastIndex, previousLastIndex))
  573. {
  574. rx.Set(JsRegExp.PropertyLastIndex, previousLastIndex, true);
  575. }
  576. if (result.IsNull())
  577. {
  578. return -1;
  579. }
  580. return result.Get(PropertyIndex);
  581. }
  582. /// <summary>
  583. /// https://tc39.es/ecma262/#sec-regexp.prototype-@@match
  584. /// </summary>
  585. private JsValue Match(JsValue thisObject, JsValue[] arguments)
  586. {
  587. var rx = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.match");
  588. var s = TypeConverter.ToString(arguments.At(0));
  589. var flags = TypeConverter.ToString(rx.Get(PropertyFlags));
  590. var global = flags.IndexOf('g') != -1;
  591. if (!global)
  592. {
  593. return RegExpExec(rx, s);
  594. }
  595. var fullUnicode = flags.IndexOf('u') != -1;
  596. rx.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero, true);
  597. if (!fullUnicode
  598. && rx is JsRegExp rei
  599. && rei.HasDefaultRegExpExec)
  600. {
  601. // fast path
  602. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  603. if (rei.Sticky)
  604. {
  605. var match = rei.Value.Match(s);
  606. if (!match.Success || match.Index != 0)
  607. {
  608. return Null;
  609. }
  610. a.SetIndexValue(0, match.Value, updateLength: false);
  611. uint li = 0;
  612. while (true)
  613. {
  614. match = match.NextMatch();
  615. if (!match.Success || match.Index != ++li)
  616. break;
  617. a.SetIndexValue(li, match.Value, updateLength: false);
  618. }
  619. a.SetLength(li);
  620. return a;
  621. }
  622. else
  623. {
  624. var matches = rei.Value.Matches(s);
  625. if (matches.Count == 0)
  626. {
  627. return Null;
  628. }
  629. a.EnsureCapacity((uint) matches.Count);
  630. a.SetLength((uint) matches.Count);
  631. for (var i = 0; i < matches.Count; i++)
  632. {
  633. a.SetIndexValue((uint) i, matches[i].Value, updateLength: false);
  634. }
  635. return a;
  636. }
  637. }
  638. return MatchSlow(rx, s, fullUnicode);
  639. }
  640. private JsValue MatchSlow(ObjectInstance rx, string s, bool fullUnicode)
  641. {
  642. var a = _realm.Intrinsics.Array.ArrayCreate(0);
  643. uint n = 0;
  644. while (true)
  645. {
  646. var result = RegExpExec(rx, s);
  647. if (result.IsNull())
  648. {
  649. a.SetLength(n);
  650. return n == 0 ? Null : a;
  651. }
  652. var matchStr = TypeConverter.ToString(result.Get(JsString.NumberZeroString));
  653. a.SetIndexValue(n, matchStr, updateLength: false);
  654. if (matchStr == "")
  655. {
  656. var thisIndex = TypeConverter.ToLength(rx.Get(JsRegExp.PropertyLastIndex));
  657. var nextIndex = AdvanceStringIndex(s, thisIndex, fullUnicode);
  658. rx.Set(JsRegExp.PropertyLastIndex, nextIndex, true);
  659. }
  660. n++;
  661. }
  662. }
  663. /// <summary>
  664. /// https://tc39.es/ecma262/#sec-regexp-prototype-matchall
  665. /// </summary>
  666. private JsValue MatchAll(JsValue thisObject, JsValue[] arguments)
  667. {
  668. var r = AssertThisIsObjectInstance(thisObject, "RegExp.prototype.matchAll");
  669. var s = TypeConverter.ToString(arguments.At(0));
  670. var c = SpeciesConstructor(r, _realm.Intrinsics.RegExp);
  671. var flags = TypeConverter.ToJsString(r.Get(PropertyFlags));
  672. var matcher = Construct(c, new JsValue[]
  673. {
  674. r,
  675. flags
  676. });
  677. var lastIndex = TypeConverter.ToLength(r.Get(JsRegExp.PropertyLastIndex));
  678. matcher.Set(JsRegExp.PropertyLastIndex, lastIndex, true);
  679. var global = flags.IndexOf('g') != -1;
  680. var fullUnicode = flags.IndexOf('u') != -1;
  681. return _realm.Intrinsics.RegExpStringIteratorPrototype.Construct(matcher, s, global, fullUnicode);
  682. }
  683. private static ulong AdvanceStringIndex(string s, ulong index, bool unicode)
  684. {
  685. if (!unicode || index + 1 >= (ulong) s.Length)
  686. {
  687. return index + 1;
  688. }
  689. var first = s[(int) index];
  690. if (first < 0xD800 || first > 0xDBFF)
  691. {
  692. return index + 1;
  693. }
  694. var second = s[(int) (index + 1)];
  695. if (second < 0xDC00 || second > 0xDFFF)
  696. {
  697. return index + 1;
  698. }
  699. return index + 2;
  700. }
  701. internal static JsValue RegExpExec(ObjectInstance r, string s)
  702. {
  703. var ri = r as JsRegExp;
  704. if ((ri is null || !ri.HasDefaultRegExpExec) && r.Get(PropertyExec) is ICallable callable)
  705. {
  706. var result = callable.Call(r, new JsValue[] { s });
  707. if (!result.IsNull() && !result.IsObject())
  708. {
  709. ExceptionHelper.ThrowTypeError(r.Engine.Realm);
  710. }
  711. return result;
  712. }
  713. if (ri is null)
  714. {
  715. ExceptionHelper.ThrowTypeError(r.Engine.Realm);
  716. }
  717. return RegExpBuiltinExec(ri, s);
  718. }
  719. internal bool HasDefaultExec => Get(PropertyExec) is ClrFunctionInstance functionInstance && functionInstance._func == _defaultExec;
  720. /// <summary>
  721. /// https://tc39.es/ecma262/#sec-regexpbuiltinexec
  722. /// </summary>
  723. private static JsValue RegExpBuiltinExec(JsRegExp R, string s)
  724. {
  725. var length = (ulong) s.Length;
  726. var lastIndex = TypeConverter.ToLength(R.Get(JsRegExp.PropertyLastIndex));
  727. var global = R.Global;
  728. var sticky = R.Sticky;
  729. if (!global && !sticky)
  730. {
  731. lastIndex = 0;
  732. }
  733. if (R.Source == JsRegExp.regExpForMatchingAllCharacters) // Reg Exp is really ""
  734. {
  735. if (lastIndex > (ulong) s.Length)
  736. {
  737. return Null;
  738. }
  739. // "aaa".match() => [ '', index: 0, input: 'aaa' ]
  740. var array = R.Engine.Realm.Intrinsics.Array.ArrayCreate(1);
  741. array.FastSetDataProperty(PropertyIndex._value, lastIndex);
  742. array.FastSetDataProperty(PropertyInput._value, s);
  743. array.SetIndexValue(0, JsString.Empty, updateLength: false);
  744. return array;
  745. }
  746. var matcher = R.Value;
  747. var fullUnicode = R.FullUnicode;
  748. var hasIndices = R.Indices;
  749. if (!global & !sticky && !fullUnicode && !hasIndices)
  750. {
  751. // we can the non-stateful fast path which is the common case
  752. var m = matcher.Match(s, (int) lastIndex);
  753. if (!m.Success)
  754. {
  755. return Null;
  756. }
  757. return CreateReturnValueArray(R, m, s, fullUnicode: false, hasIndices: false);
  758. }
  759. // the stateful version
  760. Match match;
  761. if (lastIndex > length)
  762. {
  763. R.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero, true);
  764. return Null;
  765. }
  766. var startAt = (int) lastIndex;
  767. while (true)
  768. {
  769. match = R.Value.Match(s, startAt);
  770. // The conversion of Unicode regex patterns to .NET Regex has some flaws:
  771. // when the pattern may match empty strings, the adapted Regex will return empty string matches
  772. // in the middle of surrogate pairs. As a best effort solution, we remove these fake positive matches.
  773. // (See also: https://github.com/sebastienros/esprima-dotnet/pull/364#issuecomment-1606045259)
  774. if (match.Success
  775. && fullUnicode
  776. && match.Length == 0
  777. && 0 < match.Index && match.Index < s.Length
  778. && char.IsHighSurrogate(s[match.Index - 1]) && char.IsLowSurrogate(s[match.Index]))
  779. {
  780. startAt++;
  781. continue;
  782. }
  783. break;
  784. }
  785. var success = match.Success && (!sticky || match.Index == (int) lastIndex);
  786. if (!success)
  787. {
  788. R.Set(JsRegExp.PropertyLastIndex, JsNumber.PositiveZero, true);
  789. return Null;
  790. }
  791. var e = match.Index + match.Length;
  792. // NOTE: Even in Unicode mode, we don't need to translate indices as .NET regexes always return code unit indices.
  793. if (global || sticky)
  794. {
  795. R.Set(JsRegExp.PropertyLastIndex, e, true);
  796. }
  797. return CreateReturnValueArray(R, match, s, fullUnicode, hasIndices);
  798. }
  799. private static JsArray CreateReturnValueArray(
  800. JsRegExp rei,
  801. Match match,
  802. string s,
  803. bool fullUnicode,
  804. bool hasIndices)
  805. {
  806. var engine = rei.Engine;
  807. var actualGroupCount = GetActualRegexGroupCount(rei, match);
  808. var array = engine.Realm.Intrinsics.Array.ArrayCreate((ulong) actualGroupCount);
  809. array.CreateDataProperty(PropertyIndex, match.Index);
  810. array.CreateDataProperty(PropertyInput, s);
  811. ObjectInstance? groups = null;
  812. List<string>? groupNames = null;
  813. var indices = hasIndices ? new List<JsNumber[]?>(actualGroupCount) : null;
  814. for (uint i = 0; i < actualGroupCount; i++)
  815. {
  816. var capture = match.Groups[(int) i];
  817. var capturedValue = Undefined;
  818. if (capture?.Success == true)
  819. {
  820. capturedValue = capture.Value;
  821. }
  822. if (hasIndices)
  823. {
  824. if (capture?.Success == true)
  825. {
  826. indices!.Add(new[] { JsNumber.Create(capture.Index), JsNumber.Create(capture.Index + capture.Length) });
  827. }
  828. else
  829. {
  830. indices!.Add(null);
  831. }
  832. }
  833. var groupName = GetRegexGroupName(rei, (int) i);
  834. if (!string.IsNullOrWhiteSpace(groupName))
  835. {
  836. groups ??= OrdinaryObjectCreate(engine, null);
  837. groups.CreateDataPropertyOrThrow(groupName, capturedValue);
  838. groupNames ??= new List<string>();
  839. groupNames.Add(groupName!);
  840. }
  841. array.SetIndexValue(i, capturedValue, updateLength: false);
  842. }
  843. array.CreateDataProperty(PropertyGroups, groups ?? Undefined);
  844. if (hasIndices)
  845. {
  846. var indicesArray = MakeMatchIndicesIndexPairArray(engine, s, indices!, groupNames, groupNames?.Count > 0);
  847. array.CreateDataPropertyOrThrow("indices", indicesArray);
  848. }
  849. return array;
  850. }
  851. /// <summary>
  852. /// https://tc39.es/ecma262/#sec-makematchindicesindexpairarray
  853. /// </summary>
  854. private static JsArray MakeMatchIndicesIndexPairArray(
  855. Engine engine,
  856. string s,
  857. List<JsNumber[]?> indices,
  858. List<string>? groupNames,
  859. bool hasGroups)
  860. {
  861. var n = indices.Count;
  862. var a = engine.Realm.Intrinsics.Array.Construct((uint) n);
  863. ObjectInstance? groups = null;
  864. if (hasGroups)
  865. {
  866. groups = OrdinaryObjectCreate(engine, null);
  867. }
  868. a.CreateDataPropertyOrThrow("groups", groups ?? Undefined);
  869. for (var i = 0; i < n; ++i)
  870. {
  871. var matchIndices = indices[i];
  872. var matchIndexPair = matchIndices is not null
  873. ? GetMatchIndexPair(engine, s, matchIndices)
  874. : Undefined;
  875. a.Push(matchIndexPair);
  876. if (i > 0 && !string.IsNullOrWhiteSpace(groupNames?[i - 1]))
  877. {
  878. groups!.CreateDataPropertyOrThrow(groupNames![i - 1], matchIndexPair);
  879. }
  880. }
  881. return a;
  882. }
  883. /// <summary>
  884. /// https://tc39.es/ecma262/#sec-getmatchindexpair
  885. /// </summary>
  886. private static JsValue GetMatchIndexPair(Engine engine, string s, JsNumber[] match)
  887. {
  888. return engine.Realm.Intrinsics.Array.CreateArrayFromList(match);
  889. }
  890. private static int GetActualRegexGroupCount(JsRegExp rei, Match match)
  891. {
  892. return rei.ParseResult.Success ? rei.ParseResult.ActualRegexGroupCount : match.Groups.Count;
  893. }
  894. private static string? GetRegexGroupName(JsRegExp rei, int index)
  895. {
  896. if (index == 0)
  897. {
  898. return null;
  899. }
  900. var regex = rei.Value;
  901. if (rei.ParseResult.Success)
  902. {
  903. return rei.ParseResult.GetRegexGroupName(index);
  904. }
  905. var groupNameFromNumber = regex.GroupNameFromNumber(index);
  906. if (groupNameFromNumber.Length == 1 && groupNameFromNumber[0] == 48 + index)
  907. {
  908. // regex defaults to index as group name when it's not a named group
  909. return null;
  910. }
  911. return groupNameFromNumber;
  912. }
  913. private JsValue Exec(JsValue thisObject, JsValue[] arguments)
  914. {
  915. var r = thisObject as JsRegExp;
  916. if (r is null)
  917. {
  918. ExceptionHelper.ThrowTypeError(_engine.Realm);
  919. }
  920. var s = TypeConverter.ToString(arguments.At(0));
  921. return RegExpBuiltinExec(r, s);
  922. }
  923. }
  924. }