StringPrototype.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using System.Globalization;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using Jint.Collections;
  7. using Jint.Native.Json;
  8. using Jint.Native.Object;
  9. using Jint.Native.RegExp;
  10. using Jint.Native.Symbol;
  11. using Jint.Runtime;
  12. using Jint.Runtime.Descriptors;
  13. using Jint.Runtime.Descriptors.Specialized;
  14. using Jint.Runtime.Interop;
  15. namespace Jint.Native.String
  16. {
  17. /// <summary>
  18. /// https://tc39.es/ecma262/#sec-properties-of-the-string-prototype-object
  19. /// </summary>
  20. internal sealed class StringPrototype : StringInstance
  21. {
  22. private readonly Realm _realm;
  23. private readonly StringConstructor _constructor;
  24. internal ClrFunction? _originalIteratorFunction;
  25. internal StringPrototype(
  26. Engine engine,
  27. Realm realm,
  28. StringConstructor constructor,
  29. ObjectPrototype objectPrototype)
  30. : base(engine, JsString.Empty)
  31. {
  32. _prototype = objectPrototype;
  33. _length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
  34. _realm = realm;
  35. _constructor = constructor;
  36. }
  37. protected override void Initialize()
  38. {
  39. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  40. const PropertyFlag propertyFlags = lengthFlags | PropertyFlag.Writable;
  41. var trimStart = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "trimStart", prototype.TrimStart, 0, lengthFlags), propertyFlags);
  42. var trimEnd = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "trimEnd", prototype.TrimEnd, 0, lengthFlags), propertyFlags);
  43. var properties = new PropertyDictionary(37, checkExistingKeys: false)
  44. {
  45. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  46. ["toString"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toString", prototype.ToStringString, 0, lengthFlags), propertyFlags),
  47. ["valueOf"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "valueOf", prototype.ValueOf, 0, lengthFlags), propertyFlags),
  48. ["charAt"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "charAt", prototype.CharAt, 1, lengthFlags), propertyFlags),
  49. ["charCodeAt"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "charCodeAt", prototype.CharCodeAt, 1, lengthFlags), propertyFlags),
  50. ["codePointAt"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "codePointAt", prototype.CodePointAt, 1, lengthFlags), propertyFlags),
  51. ["concat"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "concat", prototype.Concat, 1, lengthFlags), propertyFlags),
  52. ["indexOf"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "indexOf", prototype.IndexOf, 1, lengthFlags), propertyFlags),
  53. ["endsWith"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "endsWith", prototype.EndsWith, 1, lengthFlags), propertyFlags),
  54. ["startsWith"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "startsWith", prototype.StartsWith, 1, lengthFlags), propertyFlags),
  55. ["lastIndexOf"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "lastIndexOf", prototype.LastIndexOf, 1, lengthFlags), propertyFlags),
  56. ["localeCompare"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "localeCompare", prototype.LocaleCompare, 1, lengthFlags), propertyFlags),
  57. ["match"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "match", prototype.Match, 1, lengthFlags), propertyFlags),
  58. ["matchAll"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "matchAll", prototype.MatchAll, 1, lengthFlags), propertyFlags),
  59. ["replace"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "replace", prototype.Replace, 2, lengthFlags), propertyFlags),
  60. ["replaceAll"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "replaceAll", prototype.ReplaceAll, 2, lengthFlags), propertyFlags),
  61. ["search"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "search", prototype.Search, 1, lengthFlags), propertyFlags),
  62. ["slice"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "slice", prototype.Slice, 2, lengthFlags), propertyFlags),
  63. ["split"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "split", prototype.Split, 2, lengthFlags), propertyFlags),
  64. ["substr"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "substr", Substr, 2), propertyFlags),
  65. ["substring"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "substring", prototype.Substring, 2, lengthFlags), propertyFlags),
  66. ["toLowerCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toLowerCase", prototype.ToLowerCase, 0, lengthFlags), propertyFlags),
  67. ["toLocaleLowerCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toLocaleLowerCase", prototype.ToLocaleLowerCase, 0, lengthFlags), propertyFlags),
  68. ["toUpperCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toUpperCase", prototype.ToUpperCase, 0, lengthFlags), propertyFlags),
  69. ["toLocaleUpperCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toLocaleUpperCase", prototype.ToLocaleUpperCase, 0, lengthFlags), propertyFlags),
  70. ["trim"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "trim", prototype.Trim, 0, lengthFlags), propertyFlags),
  71. ["trimStart"] = trimStart,
  72. ["trimEnd"] = trimEnd,
  73. ["trimLeft"] = trimStart,
  74. ["trimRight"] = trimEnd,
  75. ["padStart"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "padStart", prototype.PadStart, 1, lengthFlags), propertyFlags),
  76. ["padEnd"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "padEnd", prototype.PadEnd, 1, lengthFlags), propertyFlags),
  77. ["includes"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "includes", prototype.Includes, 1, lengthFlags), propertyFlags),
  78. ["normalize"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "normalize", prototype.Normalize, 0, lengthFlags), propertyFlags),
  79. ["repeat"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "repeat", prototype.Repeat, 1, lengthFlags), propertyFlags),
  80. ["at"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "at", prototype.At, 1, lengthFlags), propertyFlags),
  81. ["isWellFormed"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "isWellFormed", prototype.IsWellFormed, 0, lengthFlags), propertyFlags),
  82. ["toWellFormed"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toWellFormed", prototype.ToWellFormed, 0, lengthFlags), propertyFlags),
  83. };
  84. SetProperties(properties);
  85. _originalIteratorFunction = new ClrFunction(_engine, "[Symbol.iterator]", Iterator, 0, lengthFlags);
  86. var symbols = new SymbolDictionary(1)
  87. {
  88. [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(_originalIteratorFunction, propertyFlags)
  89. };
  90. SetSymbols(symbols);
  91. }
  92. internal override bool HasOriginalIterator => ReferenceEquals(Get(GlobalSymbolRegistry.Iterator), _originalIteratorFunction);
  93. private ObjectInstance Iterator(JsValue thisObject, JsValue[] arguments)
  94. {
  95. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  96. var str = TypeConverter.ToString(thisObject);
  97. return _realm.Intrinsics.StringIteratorPrototype.Construct(str);
  98. }
  99. private JsValue ToStringString(JsValue thisObject, JsValue[] arguments)
  100. {
  101. if (thisObject.IsString())
  102. {
  103. return thisObject;
  104. }
  105. var s = TypeConverter.ToObject(_realm, thisObject) as StringInstance;
  106. if (s is null)
  107. {
  108. ExceptionHelper.ThrowTypeError(_realm);
  109. }
  110. return s.StringData;
  111. }
  112. // http://msdn.microsoft.com/en-us/library/system.char.iswhitespace(v=vs.110).aspx
  113. // http://en.wikipedia.org/wiki/Byte_order_mark
  114. const char BOM_CHAR = '\uFEFF';
  115. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  116. private static bool IsWhiteSpaceEx(char c)
  117. {
  118. return char.IsWhiteSpace(c) || c == BOM_CHAR;
  119. }
  120. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  121. private static string TrimEndEx(string s)
  122. {
  123. if (s.Length == 0)
  124. return string.Empty;
  125. if (!IsWhiteSpaceEx(s[s.Length - 1]))
  126. return s;
  127. return TrimEnd(s);
  128. }
  129. private static string TrimEnd(string s)
  130. {
  131. var i = s.Length - 1;
  132. while (i >= 0)
  133. {
  134. if (IsWhiteSpaceEx(s[i]))
  135. i--;
  136. else
  137. break;
  138. }
  139. return i >= 0 ? s.Substring(0, i + 1) : string.Empty;
  140. }
  141. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  142. internal static string TrimStartEx(string s)
  143. {
  144. if (s.Length == 0)
  145. return string.Empty;
  146. if (!IsWhiteSpaceEx(s[0]))
  147. return s;
  148. return TrimStart(s);
  149. }
  150. private static string TrimStart(string s)
  151. {
  152. var i = 0;
  153. while (i < s.Length)
  154. {
  155. if (IsWhiteSpaceEx(s[i]))
  156. i++;
  157. else
  158. break;
  159. }
  160. return i >= s.Length ? string.Empty : s.Substring(i);
  161. }
  162. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  163. internal static string TrimEx(string s)
  164. {
  165. return TrimEndEx(TrimStartEx(s));
  166. }
  167. /// <summary>
  168. /// https://tc39.es/ecma262/#sec-string.prototype.trim
  169. /// </summary>
  170. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  171. private JsValue Trim(JsValue thisObject, JsValue[] arguments)
  172. {
  173. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  174. var s = TypeConverter.ToJsString(thisObject);
  175. if (s.Length == 0 || (!IsWhiteSpaceEx(s[0]) && !IsWhiteSpaceEx(s[s.Length - 1])))
  176. {
  177. return s;
  178. }
  179. return TrimEx(s.ToString());
  180. }
  181. /// <summary>
  182. /// https://tc39.es/ecma262/#sec-string.prototype.trimstart
  183. /// </summary>
  184. private JsValue TrimStart(JsValue thisObject, JsValue[] arguments)
  185. {
  186. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  187. var s = TypeConverter.ToJsString(thisObject);
  188. if (s.Length == 0 || !IsWhiteSpaceEx(s[0]))
  189. {
  190. return s;
  191. }
  192. return TrimStartEx(s.ToString());
  193. }
  194. /// <summary>
  195. /// https://tc39.es/ecma262/#sec-string.prototype.trimend
  196. /// </summary>
  197. private JsValue TrimEnd(JsValue thisObject, JsValue[] arguments)
  198. {
  199. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  200. var s = TypeConverter.ToJsString(thisObject);
  201. if (s.Length == 0 || !IsWhiteSpaceEx(s[s.Length - 1]))
  202. {
  203. return s;
  204. }
  205. return TrimEndEx(s.ToString());
  206. }
  207. private JsValue ToLocaleUpperCase(JsValue thisObject, JsValue[] arguments)
  208. {
  209. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  210. var s = TypeConverter.ToString(thisObject);
  211. var culture = CultureInfo.InvariantCulture;
  212. if (arguments.Length > 0 && arguments[0].IsString())
  213. {
  214. try
  215. {
  216. var cultureArgument = arguments[0].ToString();
  217. culture = CultureInfo.GetCultureInfo(cultureArgument);
  218. }
  219. catch (CultureNotFoundException)
  220. {
  221. ExceptionHelper.ThrowRangeError(_realm, "Incorrect culture information provided");
  222. }
  223. }
  224. if (string.Equals("lt", culture.Name, StringComparison.OrdinalIgnoreCase))
  225. {
  226. s = StringInlHelper.LithuanianStringProcessor(s);
  227. #if NET462
  228. // Code specific to .NET Framework 4.6.2.
  229. // For no good reason this verison does not upper case these characters correctly.
  230. return new JsString(s.ToUpper(culture)
  231. .Replace("ϳ", "Ϳ")
  232. .Replace("ʝ", "Ʝ"));
  233. #endif
  234. }
  235. return new JsString(s.ToUpper(culture));
  236. }
  237. private JsValue ToUpperCase(JsValue thisObject, JsValue[] arguments)
  238. {
  239. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  240. var s = TypeConverter.ToString(thisObject);
  241. return new JsString(s.ToUpperInvariant());
  242. }
  243. private JsValue ToLocaleLowerCase(JsValue thisObject, JsValue[] arguments)
  244. {
  245. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  246. var s = TypeConverter.ToString(thisObject);
  247. return new JsString(s.ToLower(CultureInfo.InvariantCulture));
  248. }
  249. private JsValue ToLowerCase(JsValue thisObject, JsValue[] arguments)
  250. {
  251. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  252. var s = TypeConverter.ToString(thisObject);
  253. return s.ToLowerInvariant();
  254. }
  255. private static int ToIntegerSupportInfinity(JsValue numberVal)
  256. {
  257. return numberVal._type == InternalTypes.Integer
  258. ? numberVal.AsInteger()
  259. : ToIntegerSupportInfinityUnlikely(numberVal);
  260. }
  261. [MethodImpl(MethodImplOptions.NoInlining)]
  262. private static int ToIntegerSupportInfinityUnlikely(JsValue numberVal)
  263. {
  264. var doubleVal = TypeConverter.ToInteger(numberVal);
  265. int intVal;
  266. if (double.IsPositiveInfinity(doubleVal))
  267. intVal = int.MaxValue;
  268. else if (double.IsNegativeInfinity(doubleVal))
  269. intVal = int.MinValue;
  270. else
  271. intVal = (int) doubleVal;
  272. return intVal;
  273. }
  274. private JsValue Substring(JsValue thisObject, JsValue[] arguments)
  275. {
  276. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  277. var s = TypeConverter.ToString(thisObject);
  278. var start = TypeConverter.ToNumber(arguments.At(0));
  279. var end = TypeConverter.ToNumber(arguments.At(1));
  280. if (double.IsNaN(start) || start < 0)
  281. {
  282. start = 0;
  283. }
  284. if (double.IsNaN(end) || end < 0)
  285. {
  286. end = 0;
  287. }
  288. var len = s.Length;
  289. var intStart = ToIntegerSupportInfinity(start);
  290. var intEnd = arguments.At(1).IsUndefined() ? len : ToIntegerSupportInfinity(end);
  291. var finalStart = System.Math.Min(len, System.Math.Max(intStart, 0));
  292. var finalEnd = System.Math.Min(len, System.Math.Max(intEnd, 0));
  293. // Swap value if finalStart < finalEnd
  294. var from = System.Math.Min(finalStart, finalEnd);
  295. var to = System.Math.Max(finalStart, finalEnd);
  296. var length = to - from;
  297. if (length == 0)
  298. {
  299. return JsString.Empty;
  300. }
  301. if (length == 1)
  302. {
  303. return JsString.Create(s[from]);
  304. }
  305. return new JsString(s.Substring(from, length));
  306. }
  307. private static JsValue Substr(JsValue thisObject, JsValue[] arguments)
  308. {
  309. var s = TypeConverter.ToString(thisObject);
  310. var start = TypeConverter.ToInteger(arguments.At(0));
  311. var length = arguments.At(1).IsUndefined()
  312. ? double.PositiveInfinity
  313. : TypeConverter.ToInteger(arguments.At(1));
  314. start = start >= 0 ? start : System.Math.Max(s.Length + start, 0);
  315. length = System.Math.Min(System.Math.Max(length, 0), s.Length - start);
  316. if (length <= 0)
  317. {
  318. return JsString.Empty;
  319. }
  320. var startIndex = TypeConverter.ToInt32(start);
  321. var l = TypeConverter.ToInt32(length);
  322. if (l == 1)
  323. {
  324. return TypeConverter.ToString(s[startIndex]);
  325. }
  326. return s.Substring(startIndex, l);
  327. }
  328. /// <summary>
  329. /// https://tc39.es/ecma262/#sec-string.prototype.split
  330. /// </summary>
  331. private JsValue Split(JsValue thisObject, JsValue[] arguments)
  332. {
  333. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  334. var separator = arguments.At(0);
  335. var limit = arguments.At(1);
  336. // fast path for empty regexp
  337. if (separator is JsRegExp R && string.Equals(R.Source, JsRegExp.regExpForMatchingAllCharacters, StringComparison.Ordinal))
  338. {
  339. separator = JsString.Empty;
  340. }
  341. if (separator is ObjectInstance oi)
  342. {
  343. var splitter = GetMethod(_realm, oi, GlobalSymbolRegistry.Split);
  344. if (splitter != null)
  345. {
  346. return splitter.Call(separator, new[] { thisObject, limit });
  347. }
  348. }
  349. var s = TypeConverter.ToString(thisObject);
  350. // Coerce into a number, true will become 1
  351. var lim = limit.IsUndefined() ? uint.MaxValue : TypeConverter.ToUint32(limit);
  352. if (separator.IsNull())
  353. {
  354. separator = "null";
  355. }
  356. else if (!separator.IsUndefined())
  357. {
  358. if (!separator.IsRegExp())
  359. {
  360. separator = TypeConverter.ToJsString(separator); // Coerce into a string, for an object call toString()
  361. }
  362. }
  363. if (lim == 0)
  364. {
  365. return _realm.Intrinsics.Array.ArrayCreate(0);
  366. }
  367. if (separator.IsUndefined())
  368. {
  369. var arrayInstance = _realm.Intrinsics.Array.ArrayCreate(1);
  370. arrayInstance.SetIndexValue(0, s, updateLength: false);
  371. return arrayInstance;
  372. }
  373. return SplitWithStringSeparator(_realm, separator, s, lim);
  374. }
  375. internal static JsValue SplitWithStringSeparator(Realm realm, JsValue separator, string s, uint lim)
  376. {
  377. var segments = StringExecutionContext.Current.SplitSegmentList;
  378. segments.Clear();
  379. var sep = TypeConverter.ToString(separator);
  380. if (sep == string.Empty)
  381. {
  382. if (s.Length > segments.Capacity)
  383. {
  384. segments.Capacity = s.Length;
  385. }
  386. for (var i = 0; i < s.Length; i++)
  387. {
  388. segments.Add(TypeConverter.ToString(s[i]));
  389. }
  390. }
  391. else
  392. {
  393. var array = StringExecutionContext.Current.SplitArray1;
  394. array[0] = sep;
  395. segments.AddRange(s.Split(array, StringSplitOptions.None));
  396. }
  397. var length = (uint) System.Math.Min(segments.Count, lim);
  398. var a = realm.Intrinsics.Array.ArrayCreate(length);
  399. for (int i = 0; i < length; i++)
  400. {
  401. a.SetIndexValue((uint) i, segments[i], updateLength: false);
  402. }
  403. a.SetLength(length);
  404. return a;
  405. }
  406. /// <summary>
  407. /// https://tc39.es/proposal-relative-indexing-method/#sec-string-prototype-additions
  408. /// </summary>
  409. private JsValue At(JsValue thisObject, JsValue[] arguments)
  410. {
  411. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  412. var start = arguments.At(0);
  413. var o = thisObject.ToString();
  414. long len = o.Length;
  415. var relativeIndex = TypeConverter.ToInteger(start);
  416. int k;
  417. if (relativeIndex < 0)
  418. {
  419. k = (int) (len + relativeIndex);
  420. }
  421. else
  422. {
  423. k = (int) relativeIndex;
  424. }
  425. if (k < 0 || k >= len)
  426. {
  427. return Undefined;
  428. }
  429. return o[k];
  430. }
  431. private JsValue Slice(JsValue thisObject, JsValue[] arguments)
  432. {
  433. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  434. var start = TypeConverter.ToNumber(arguments.At(0));
  435. if (double.IsNegativeInfinity(start))
  436. {
  437. start = 0;
  438. }
  439. if (double.IsPositiveInfinity(start))
  440. {
  441. return JsString.Empty;
  442. }
  443. var s = TypeConverter.ToJsString(thisObject);
  444. var end = TypeConverter.ToNumber(arguments.At(1));
  445. if (double.IsPositiveInfinity(end))
  446. {
  447. end = s.Length;
  448. }
  449. var len = s.Length;
  450. var intStart = (int) start;
  451. var intEnd = arguments.At(1).IsUndefined() ? len : (int) TypeConverter.ToInteger(end);
  452. var from = intStart < 0 ? System.Math.Max(len + intStart, 0) : System.Math.Min(intStart, len);
  453. var to = intEnd < 0 ? System.Math.Max(len + intEnd, 0) : System.Math.Min(intEnd, len);
  454. var span = System.Math.Max(to - from, 0);
  455. if (span == 0)
  456. {
  457. return JsString.Empty;
  458. }
  459. if (span == 1)
  460. {
  461. return JsString.Create(s[from]);
  462. }
  463. return s.Substring(from, span);
  464. }
  465. private JsValue Search(JsValue thisObject, JsValue[] arguments)
  466. {
  467. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  468. var regex = arguments.At(0);
  469. if (regex is ObjectInstance oi)
  470. {
  471. var searcher = GetMethod(_realm, oi, GlobalSymbolRegistry.Search);
  472. if (searcher != null)
  473. {
  474. return searcher.Call(regex, new[] { thisObject });
  475. }
  476. }
  477. var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct(new[] {regex});
  478. var s = TypeConverter.ToJsString(thisObject);
  479. return _engine.Invoke(rx, GlobalSymbolRegistry.Search, new JsValue[] { s });
  480. }
  481. /// <summary>
  482. /// https://tc39.es/ecma262/#sec-string.prototype.replace
  483. /// </summary>
  484. private JsValue Replace(JsValue thisObject, JsValue[] arguments)
  485. {
  486. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  487. var searchValue = arguments.At(0);
  488. var replaceValue = arguments.At(1);
  489. if (!searchValue.IsNullOrUndefined())
  490. {
  491. var replacer = GetMethod(_realm, searchValue, GlobalSymbolRegistry.Replace);
  492. if (replacer != null)
  493. {
  494. return replacer.Call(searchValue, thisObject, replaceValue);
  495. }
  496. }
  497. var thisString = TypeConverter.ToJsString(thisObject);
  498. var searchString = TypeConverter.ToString(searchValue);
  499. var functionalReplace = replaceValue is ICallable;
  500. if (!functionalReplace)
  501. {
  502. replaceValue = TypeConverter.ToJsString(replaceValue);
  503. }
  504. var position = thisString.IndexOf(searchString);
  505. if (position < 0)
  506. {
  507. return thisString;
  508. }
  509. string replStr;
  510. if (functionalReplace)
  511. {
  512. var replValue = ((ICallable) replaceValue).Call(Undefined, searchString, position, thisString);
  513. replStr = TypeConverter.ToString(replValue);
  514. }
  515. else
  516. {
  517. var captures = System.Array.Empty<string>();
  518. replStr = RegExpPrototype.GetSubstitution(searchString, thisString.ToString(), position, captures, Undefined, TypeConverter.ToString(replaceValue));
  519. }
  520. var tailPos = position + searchString.Length;
  521. var newString = thisString.Substring(0, position) + replStr + thisString.Substring(tailPos);
  522. return newString;
  523. }
  524. /// <summary>
  525. /// https://tc39.es/ecma262/#sec-string.prototype.replaceall
  526. /// </summary>
  527. private JsValue ReplaceAll(JsValue thisObject, JsValue[] arguments)
  528. {
  529. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  530. var searchValue = arguments.At(0);
  531. var replaceValue = arguments.At(1);
  532. if (!searchValue.IsNullOrUndefined())
  533. {
  534. if (searchValue.IsRegExp())
  535. {
  536. var flags = searchValue.Get(RegExpPrototype.PropertyFlags);
  537. TypeConverter.CheckObjectCoercible(_engine, flags);
  538. if (!TypeConverter.ToString(flags).Contains('g'))
  539. {
  540. ExceptionHelper.ThrowTypeError(_realm, "String.prototype.replaceAll called with a non-global RegExp argument");
  541. }
  542. }
  543. var replacer = GetMethod(_realm, searchValue, GlobalSymbolRegistry.Replace);
  544. if (replacer != null)
  545. {
  546. return replacer.Call(searchValue, thisObject, replaceValue);
  547. }
  548. }
  549. var thisString = TypeConverter.ToString(thisObject);
  550. var searchString = TypeConverter.ToString(searchValue);
  551. var functionalReplace = replaceValue is ICallable;
  552. if (!functionalReplace)
  553. {
  554. replaceValue = TypeConverter.ToJsString(replaceValue);
  555. // check fast case
  556. var newValue = replaceValue.ToString();
  557. if (!newValue.Contains('$') && searchString.Length > 0)
  558. {
  559. // just plain old string replace
  560. return thisString.Replace(searchString, newValue);
  561. }
  562. }
  563. // https://tc39.es/ecma262/#sec-stringindexof
  564. static int StringIndexOf(string s, string search, int fromIndex)
  565. {
  566. if (search.Length == 0 && fromIndex <= s.Length)
  567. {
  568. return fromIndex;
  569. }
  570. return fromIndex < s.Length
  571. ? s.IndexOf(search, fromIndex, StringComparison.Ordinal)
  572. : -1;
  573. }
  574. var searchLength = searchString.Length;
  575. var advanceBy = System.Math.Max(1, searchLength);
  576. var endOfLastMatch = 0;
  577. using var result = new ValueStringBuilder();
  578. var position = StringIndexOf(thisString, searchString, 0);
  579. while (position != -1)
  580. {
  581. string replacement;
  582. var preserved = thisString.Substring(endOfLastMatch, position - endOfLastMatch);
  583. if (functionalReplace)
  584. {
  585. var replValue = ((ICallable) replaceValue).Call(Undefined, searchString, position, thisString);
  586. replacement = TypeConverter.ToString(replValue);
  587. }
  588. else
  589. {
  590. var captures = System.Array.Empty<string>();
  591. replacement = RegExpPrototype.GetSubstitution(searchString, thisString, position, captures, Undefined, TypeConverter.ToString(replaceValue));
  592. }
  593. result.Append(preserved);
  594. result.Append(replacement);
  595. endOfLastMatch = position + searchLength;
  596. position = StringIndexOf(thisString, searchString, position + advanceBy);
  597. }
  598. if (endOfLastMatch < thisString.Length)
  599. {
  600. #if NETFRAMEWORK
  601. result.Append(thisString.AsSpan(endOfLastMatch));
  602. #else
  603. result.Append(thisString[endOfLastMatch..]);
  604. #endif
  605. }
  606. return result.ToString();
  607. }
  608. private JsValue Match(JsValue thisObject, JsValue[] arguments)
  609. {
  610. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  611. var regex = arguments.At(0);
  612. if (regex is ObjectInstance oi)
  613. {
  614. var matcher = GetMethod(_realm, oi, GlobalSymbolRegistry.Match);
  615. if (matcher != null)
  616. {
  617. return matcher.Call(regex, new[] { thisObject });
  618. }
  619. }
  620. var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct(new[] {regex});
  621. var s = TypeConverter.ToJsString(thisObject);
  622. return _engine.Invoke(rx, GlobalSymbolRegistry.Match, new JsValue[] { s });
  623. }
  624. private JsValue MatchAll(JsValue thisObject, JsValue[] arguments)
  625. {
  626. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  627. var regex = arguments.At(0);
  628. if (!regex.IsNullOrUndefined())
  629. {
  630. if (regex.IsRegExp())
  631. {
  632. var flags = regex.Get(RegExpPrototype.PropertyFlags);
  633. TypeConverter.CheckObjectCoercible(_engine, flags);
  634. if (!TypeConverter.ToString(flags).Contains('g'))
  635. {
  636. ExceptionHelper.ThrowTypeError(_realm);
  637. }
  638. }
  639. var matcher = GetMethod(_realm, (ObjectInstance) regex, GlobalSymbolRegistry.MatchAll);
  640. if (matcher != null)
  641. {
  642. return matcher.Call(regex, new[] { thisObject });
  643. }
  644. }
  645. var s = TypeConverter.ToJsString(thisObject);
  646. var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct(new[] { regex, "g" });
  647. return _engine.Invoke(rx, GlobalSymbolRegistry.MatchAll, new JsValue[] { s });
  648. }
  649. private JsValue LocaleCompare(JsValue thisObject, JsValue[] arguments)
  650. {
  651. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  652. var s = TypeConverter.ToString(thisObject);
  653. var that = TypeConverter.ToString(arguments.At(0));
  654. var culture = Engine.Options.Culture;
  655. if (arguments.Length > 1 && arguments[1].IsString())
  656. {
  657. culture = CultureInfo.GetCultureInfo(arguments.At(1).AsString());
  658. }
  659. return culture.CompareInfo.Compare(s.Normalize(NormalizationForm.FormKD), that.Normalize(NormalizationForm.FormKD));
  660. }
  661. /// <summary>
  662. /// https://tc39.es/ecma262/#sec-string.prototype.lastindexof
  663. /// </summary>
  664. private JsValue LastIndexOf(JsValue thisObject, JsValue[] arguments)
  665. {
  666. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  667. var jsString = TypeConverter.ToJsString(thisObject);
  668. var searchStr = TypeConverter.ToString(arguments.At(0));
  669. double numPos = double.NaN;
  670. if (arguments.Length > 1 && !arguments[1].IsUndefined())
  671. {
  672. numPos = TypeConverter.ToNumber(arguments[1]);
  673. }
  674. var pos = double.IsNaN(numPos) ? double.PositiveInfinity : TypeConverter.ToInteger(numPos);
  675. var len = jsString.Length;
  676. var start = (int)System.Math.Min(System.Math.Max(pos, 0), len);
  677. var searchLen = searchStr.Length;
  678. if (searchLen > len)
  679. {
  680. return JsNumber.IntegerNegativeOne;
  681. }
  682. var s = jsString.ToString();
  683. var i = start;
  684. bool found;
  685. do
  686. {
  687. found = true;
  688. var j = 0;
  689. while (found && j < searchLen)
  690. {
  691. if (i + searchLen > len || s[i + j] != searchStr[j])
  692. {
  693. found = false;
  694. }
  695. else
  696. {
  697. j++;
  698. }
  699. }
  700. if (!found)
  701. {
  702. i--;
  703. }
  704. } while (!found && i >= 0);
  705. return i;
  706. }
  707. /// <summary>
  708. /// https://tc39.es/ecma262/#sec-string.prototype.indexof
  709. /// </summary>
  710. private JsValue IndexOf(JsValue thisObject, JsValue[] arguments)
  711. {
  712. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  713. var s = TypeConverter.ToJsString(thisObject);
  714. var searchStr = TypeConverter.ToString(arguments.At(0));
  715. double pos = 0;
  716. if (arguments.Length > 1 && !arguments[1].IsUndefined())
  717. {
  718. pos = TypeConverter.ToInteger(arguments[1]);
  719. }
  720. if (pos > s.Length)
  721. {
  722. pos = s.Length;
  723. }
  724. if (pos < 0)
  725. {
  726. pos = 0;
  727. }
  728. return s.IndexOf(searchStr, (int) pos);
  729. }
  730. private JsValue Concat(JsValue thisObject, JsValue[] arguments)
  731. {
  732. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  733. if (thisObject is not JsString jsString)
  734. {
  735. jsString = new JsString.ConcatenatedString(TypeConverter.ToString(thisObject));
  736. }
  737. else
  738. {
  739. jsString = jsString.EnsureCapacity(0);
  740. }
  741. foreach (var argument in arguments)
  742. {
  743. jsString = jsString.Append(argument);
  744. }
  745. return jsString;
  746. }
  747. private JsValue CharCodeAt(JsValue thisObject, JsValue[] arguments)
  748. {
  749. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  750. JsValue pos = arguments.Length > 0 ? arguments[0] : 0;
  751. var s = TypeConverter.ToJsString(thisObject);
  752. var position = (int) TypeConverter.ToInteger(pos);
  753. if (position < 0 || position >= s.Length)
  754. {
  755. return JsNumber.DoubleNaN;
  756. }
  757. return (long) s[position];
  758. }
  759. /// <summary>
  760. /// https://tc39.es/ecma262/#sec-string.prototype.codepointat
  761. /// </summary>
  762. private JsValue CodePointAt(JsValue thisObject, JsValue[] arguments)
  763. {
  764. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  765. JsValue pos = arguments.Length > 0 ? arguments[0] : 0;
  766. var s = TypeConverter.ToString(thisObject);
  767. var position = (int)TypeConverter.ToInteger(pos);
  768. if (position < 0 || position >= s.Length)
  769. {
  770. return Undefined;
  771. }
  772. return CodePointAt(s, position).CodePoint;
  773. }
  774. [StructLayout(LayoutKind.Auto)]
  775. private readonly record struct CodePointResult(int CodePoint, int CodeUnitCount, bool IsUnpairedSurrogate);
  776. private static CodePointResult CodePointAt(string s, int position)
  777. {
  778. var size = s.Length;
  779. var first = s.CharCodeAt(position);
  780. var cp = s.CharCodeAt(position);
  781. var firstIsLeading = char.IsHighSurrogate(first);
  782. var firstIsTrailing = char.IsLowSurrogate(first);
  783. if (!firstIsLeading && !firstIsTrailing)
  784. {
  785. return new CodePointResult(cp, 1, false);
  786. }
  787. if (firstIsTrailing || position + 1 == size)
  788. {
  789. return new CodePointResult(cp, 1, true);
  790. }
  791. var second = s.CharCodeAt(position + 1);
  792. if (!char.IsLowSurrogate(second))
  793. {
  794. return new CodePointResult(cp, 1, true);
  795. }
  796. return new CodePointResult(char.ConvertToUtf32(first, second), 2, false);
  797. }
  798. private JsValue CharAt(JsValue thisObject, JsValue[] arguments)
  799. {
  800. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  801. var s = TypeConverter.ToJsString(thisObject);
  802. var position = TypeConverter.ToInteger(arguments.At(0));
  803. var size = s.Length;
  804. if (position >= size || position < 0)
  805. {
  806. return JsString.Empty;
  807. }
  808. return JsString.Create(s[(int) position]);
  809. }
  810. private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
  811. {
  812. if (thisObject is StringInstance si)
  813. {
  814. return si.StringData;
  815. }
  816. if (thisObject is JsString)
  817. {
  818. return thisObject;
  819. }
  820. ExceptionHelper.ThrowTypeError(_realm);
  821. return Undefined;
  822. }
  823. /// <summary>
  824. /// https://tc39.es/ecma262/#sec-string.prototype.padstart
  825. /// </summary>
  826. private JsValue PadStart(JsValue thisObject, JsValue[] arguments)
  827. {
  828. return StringPad(thisObject, arguments, true);
  829. }
  830. /// <summary>
  831. /// https://tc39.es/ecma262/#sec-string.prototype.padend
  832. /// </summary>
  833. private JsValue PadEnd(JsValue thisObject, JsValue[] arguments)
  834. {
  835. return StringPad(thisObject, arguments, false);
  836. }
  837. /// <summary>
  838. /// https://tc39.es/ecma262/#sec-stringpad
  839. /// </summary>
  840. private JsValue StringPad(JsValue thisObject, JsValue[] arguments, bool padStart)
  841. {
  842. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  843. var s = TypeConverter.ToJsString(thisObject);
  844. var targetLength = TypeConverter.ToInt32(arguments.At(0));
  845. var padStringValue = arguments.At(1);
  846. var padString = padStringValue.IsUndefined()
  847. ? " "
  848. : TypeConverter.ToString(padStringValue);
  849. if (s.Length > targetLength || padString.Length == 0)
  850. {
  851. return s;
  852. }
  853. targetLength -= s.Length;
  854. if (targetLength > padString.Length)
  855. {
  856. padString = string.Join("", System.Linq.Enumerable.Repeat(padString, (targetLength / padString.Length) + 1));
  857. }
  858. return padStart
  859. ? $"{padString.Substring(0, targetLength)}{s}"
  860. : $"{s}{padString.Substring(0, targetLength)}";
  861. }
  862. /// <summary>
  863. /// https://tc39.es/ecma262/#sec-string.prototype.startswith
  864. /// </summary>
  865. private JsValue StartsWith(JsValue thisObject, JsValue[] arguments)
  866. {
  867. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  868. var s = TypeConverter.ToJsString(thisObject);
  869. var searchString = arguments.At(0);
  870. if (ReferenceEquals(searchString, Null))
  871. {
  872. searchString = "null";
  873. }
  874. else
  875. {
  876. if (searchString.IsRegExp())
  877. {
  878. ExceptionHelper.ThrowTypeError(_realm);
  879. }
  880. }
  881. var searchStr = TypeConverter.ToString(searchString);
  882. var pos = TypeConverter.ToInt32(arguments.At(1));
  883. var len = s.Length;
  884. var start = System.Math.Min(System.Math.Max(pos, 0), len);
  885. return s.StartsWith(searchStr, start);
  886. }
  887. /// <summary>
  888. /// https://tc39.es/ecma262/#sec-string.prototype.endswith
  889. /// </summary>
  890. private JsValue EndsWith(JsValue thisObject, JsValue[] arguments)
  891. {
  892. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  893. var s = TypeConverter.ToJsString(thisObject);
  894. var searchString = arguments.At(0);
  895. if (ReferenceEquals(searchString, Null))
  896. {
  897. searchString = "null";
  898. }
  899. else
  900. {
  901. if (searchString.IsRegExp())
  902. {
  903. ExceptionHelper.ThrowTypeError(_realm);
  904. }
  905. }
  906. var searchStr = TypeConverter.ToString(searchString);
  907. var len = s.Length;
  908. var pos = TypeConverter.ToInt32(arguments.At(1, len));
  909. var end = System.Math.Min(System.Math.Max(pos, 0), len);
  910. return s.EndsWith(searchStr, end);
  911. }
  912. /// <summary>
  913. /// https://tc39.es/ecma262/#sec-string.prototype.includes
  914. /// </summary>
  915. private JsValue Includes(JsValue thisObject, JsValue[] arguments)
  916. {
  917. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  918. var s = TypeConverter.ToJsString(thisObject);
  919. var searchString = arguments.At(0);
  920. if (searchString.IsRegExp())
  921. {
  922. ExceptionHelper.ThrowTypeError(_realm, "First argument to String.prototype.includes must not be a regular expression");
  923. }
  924. var searchStr = TypeConverter.ToString(searchString);
  925. double pos = 0;
  926. if (arguments.Length > 1 && !arguments[1].IsUndefined())
  927. {
  928. pos = TypeConverter.ToInteger(arguments[1]);
  929. }
  930. if (searchStr.Length == 0)
  931. {
  932. return JsBoolean.True;
  933. }
  934. if (pos < 0)
  935. {
  936. pos = 0;
  937. }
  938. return s.IndexOf(searchStr, (int) pos) > -1;
  939. }
  940. private JsValue Normalize(JsValue thisObject, JsValue[] arguments)
  941. {
  942. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  943. var str = TypeConverter.ToString(thisObject);
  944. var param = arguments.At(0);
  945. var form = "NFC";
  946. if (!param.IsUndefined())
  947. {
  948. form = TypeConverter.ToString(param);
  949. }
  950. var nf = NormalizationForm.FormC;
  951. switch (form)
  952. {
  953. case "NFC":
  954. nf = NormalizationForm.FormC;
  955. break;
  956. case "NFD":
  957. nf = NormalizationForm.FormD;
  958. break;
  959. case "NFKC":
  960. nf = NormalizationForm.FormKC;
  961. break;
  962. case "NFKD":
  963. nf = NormalizationForm.FormKD;
  964. break;
  965. default:
  966. ExceptionHelper.ThrowRangeError(
  967. _realm,
  968. "The normalization form should be one of NFC, NFD, NFKC, NFKD.");
  969. break;
  970. }
  971. return str.Normalize(nf);
  972. }
  973. /// <summary>
  974. /// https://tc39.es/ecma262/#sec-string.prototype.repeat
  975. /// </summary>
  976. private JsValue Repeat(JsValue thisObject, JsValue[] arguments)
  977. {
  978. TypeConverter.CheckObjectCoercible(Engine, thisObject);
  979. var s = TypeConverter.ToString(thisObject);
  980. var count = arguments.At(0);
  981. var n = TypeConverter.ToIntegerOrInfinity(count);
  982. if (n < 0 || double.IsPositiveInfinity(n))
  983. {
  984. ExceptionHelper.ThrowRangeError(_realm, "Invalid count value");
  985. }
  986. if (n == 0 || s.Length == 0)
  987. {
  988. return JsString.Empty;
  989. }
  990. if (s.Length == 1)
  991. {
  992. return new string(s[0], (int) n);
  993. }
  994. var sb = new ValueStringBuilder((int) (n * s.Length));
  995. for (var i = 0; i < n; ++i)
  996. {
  997. sb.Append(s);
  998. }
  999. return sb.ToString();
  1000. }
  1001. private JsValue IsWellFormed(JsValue thisObject, JsValue[] arguments)
  1002. {
  1003. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  1004. var s = TypeConverter.ToString(thisObject);
  1005. return IsStringWellFormedUnicode(s);
  1006. }
  1007. private JsValue ToWellFormed(JsValue thisObject, JsValue[] arguments)
  1008. {
  1009. TypeConverter.CheckObjectCoercible(_engine, thisObject);
  1010. var s = TypeConverter.ToString(thisObject);
  1011. var strLen = s.Length;
  1012. var k = 0;
  1013. var result = new ValueStringBuilder();
  1014. while (k < strLen)
  1015. {
  1016. var cp = CodePointAt(s, k);
  1017. if (cp.IsUnpairedSurrogate)
  1018. {
  1019. // \uFFFD
  1020. result.Append('�');
  1021. }
  1022. else
  1023. {
  1024. result.Append(s.AsSpan(k, cp.CodeUnitCount));
  1025. }
  1026. k += cp.CodeUnitCount;
  1027. }
  1028. return result.ToString();
  1029. }
  1030. private static bool IsStringWellFormedUnicode(string s)
  1031. {
  1032. for (var i = 0; i < s.Length; ++i)
  1033. {
  1034. var isSurrogate = (s.CharCodeAt(i) & 0xF800) == 0xD800;
  1035. if (!isSurrogate)
  1036. {
  1037. continue;
  1038. }
  1039. var isLeadingSurrogate = s.CharCodeAt(i) < 0xDC00;
  1040. if (!isLeadingSurrogate)
  1041. {
  1042. return false; // unpaired trailing surrogate
  1043. }
  1044. var isFollowedByTrailingSurrogate = i + 1 < s.Length && (s.CharCodeAt(i + 1) & 0xFC00) == 0xDC00;
  1045. if (!isFollowedByTrailingSurrogate)
  1046. {
  1047. return false; // unpaired leading surrogate
  1048. }
  1049. ++i;
  1050. }
  1051. return true;
  1052. }
  1053. }
  1054. }