12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250 |
- #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
- using System.Globalization;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Text;
- using Jint.Native.Json;
- using Jint.Native.Object;
- using Jint.Native.RegExp;
- using Jint.Native.Symbol;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Descriptors.Specialized;
- using Jint.Runtime.Interop;
- namespace Jint.Native.String;
- /// <summary>
- /// https://tc39.es/ecma262/#sec-properties-of-the-string-prototype-object
- /// </summary>
- internal sealed class StringPrototype : StringInstance
- {
- private readonly Realm _realm;
- private readonly StringConstructor _constructor;
- internal ClrFunction? _originalIteratorFunction;
- internal StringPrototype(
- Engine engine,
- Realm realm,
- StringConstructor constructor,
- ObjectPrototype objectPrototype)
- : base(engine, JsString.Empty)
- {
- _prototype = objectPrototype;
- _length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
- _realm = realm;
- _constructor = constructor;
- }
- protected override void Initialize()
- {
- const PropertyFlag lengthFlags = PropertyFlag.Configurable;
- const PropertyFlag propertyFlags = lengthFlags | PropertyFlag.Writable;
- var trimStart = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "trimStart", prototype.TrimStart, 0, lengthFlags), propertyFlags);
- var trimEnd = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "trimEnd", prototype.TrimEnd, 0, lengthFlags), propertyFlags);
- var properties = new PropertyDictionary(37, checkExistingKeys: false)
- {
- ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
- ["toString"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toString", prototype.ToStringString, 0, lengthFlags), propertyFlags),
- ["valueOf"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "valueOf", prototype.ValueOf, 0, lengthFlags), propertyFlags),
- ["charAt"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "charAt", prototype.CharAt, 1, lengthFlags), propertyFlags),
- ["charCodeAt"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "charCodeAt", prototype.CharCodeAt, 1, lengthFlags), propertyFlags),
- ["codePointAt"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "codePointAt", prototype.CodePointAt, 1, lengthFlags), propertyFlags),
- ["concat"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "concat", prototype.Concat, 1, lengthFlags), propertyFlags),
- ["indexOf"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "indexOf", prototype.IndexOf, 1, lengthFlags), propertyFlags),
- ["endsWith"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "endsWith", prototype.EndsWith, 1, lengthFlags), propertyFlags),
- ["startsWith"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "startsWith", prototype.StartsWith, 1, lengthFlags), propertyFlags),
- ["lastIndexOf"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "lastIndexOf", prototype.LastIndexOf, 1, lengthFlags), propertyFlags),
- ["localeCompare"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "localeCompare", prototype.LocaleCompare, 1, lengthFlags), propertyFlags),
- ["match"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "match", prototype.Match, 1, lengthFlags), propertyFlags),
- ["matchAll"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "matchAll", prototype.MatchAll, 1, lengthFlags), propertyFlags),
- ["replace"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "replace", prototype.Replace, 2, lengthFlags), propertyFlags),
- ["replaceAll"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "replaceAll", prototype.ReplaceAll, 2, lengthFlags), propertyFlags),
- ["search"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "search", prototype.Search, 1, lengthFlags), propertyFlags),
- ["slice"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "slice", prototype.Slice, 2, lengthFlags), propertyFlags),
- ["split"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "split", prototype.Split, 2, lengthFlags), propertyFlags),
- ["substr"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "substr", Substr, 2), propertyFlags),
- ["substring"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "substring", prototype.Substring, 2, lengthFlags), propertyFlags),
- ["toLowerCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toLowerCase", prototype.ToLowerCase, 0, lengthFlags), propertyFlags),
- ["toLocaleLowerCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toLocaleLowerCase", prototype.ToLocaleLowerCase, 0, lengthFlags), propertyFlags),
- ["toUpperCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toUpperCase", prototype.ToUpperCase, 0, lengthFlags), propertyFlags),
- ["toLocaleUpperCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toLocaleUpperCase", prototype.ToLocaleUpperCase, 0, lengthFlags), propertyFlags),
- ["trim"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "trim", prototype.Trim, 0, lengthFlags), propertyFlags),
- ["trimStart"] = trimStart,
- ["trimEnd"] = trimEnd,
- ["trimLeft"] = trimStart,
- ["trimRight"] = trimEnd,
- ["padStart"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "padStart", prototype.PadStart, 1, lengthFlags), propertyFlags),
- ["padEnd"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "padEnd", prototype.PadEnd, 1, lengthFlags), propertyFlags),
- ["includes"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "includes", prototype.Includes, 1, lengthFlags), propertyFlags),
- ["normalize"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "normalize", prototype.Normalize, 0, lengthFlags), propertyFlags),
- ["repeat"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "repeat", prototype.Repeat, 1, lengthFlags), propertyFlags),
- ["at"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "at", prototype.At, 1, lengthFlags), propertyFlags),
- ["isWellFormed"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "isWellFormed", prototype.IsWellFormed, 0, lengthFlags), propertyFlags),
- ["toWellFormed"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toWellFormed", prototype.ToWellFormed, 0, lengthFlags), propertyFlags),
- };
- SetProperties(properties);
- _originalIteratorFunction = new ClrFunction(_engine, "[Symbol.iterator]", Iterator, 0, lengthFlags);
- var symbols = new SymbolDictionary(1)
- {
- [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(_originalIteratorFunction, propertyFlags)
- };
- SetSymbols(symbols);
- }
- internal override bool HasOriginalIterator => ReferenceEquals(Get(GlobalSymbolRegistry.Iterator), _originalIteratorFunction);
- private ObjectInstance Iterator(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(_engine, thisObject);
- var str = TypeConverter.ToString(thisObject);
- return _realm.Intrinsics.StringIteratorPrototype.Construct(str);
- }
- private JsValue ToStringString(JsValue thisObject, JsCallArguments arguments)
- {
- if (thisObject.IsString())
- {
- return thisObject;
- }
- var s = TypeConverter.ToObject(_realm, thisObject) as StringInstance;
- if (s is null)
- {
- Throw.TypeError(_realm);
- }
- return s.StringData;
- }
- // http://msdn.microsoft.com/en-us/library/system.char.iswhitespace(v=vs.110).aspx
- // http://en.wikipedia.org/wiki/Byte_order_mark
- const char BOM_CHAR = '\uFEFF';
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static bool IsWhiteSpaceEx(char c)
- {
- return char.IsWhiteSpace(c) || c == BOM_CHAR;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static string TrimEndEx(string s)
- {
- if (s.Length == 0)
- return string.Empty;
- if (!IsWhiteSpaceEx(s[s.Length - 1]))
- return s;
- return TrimEnd(s);
- }
- private static string TrimEnd(string s)
- {
- var i = s.Length - 1;
- while (i >= 0)
- {
- if (IsWhiteSpaceEx(s[i]))
- i--;
- else
- break;
- }
- return i >= 0 ? s.Substring(0, i + 1) : string.Empty;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static string TrimStartEx(string s)
- {
- if (s.Length == 0)
- return string.Empty;
- if (!IsWhiteSpaceEx(s[0]))
- return s;
- return TrimStart(s);
- }
- private static string TrimStart(string s)
- {
- var i = 0;
- while (i < s.Length)
- {
- if (IsWhiteSpaceEx(s[i]))
- i++;
- else
- break;
- }
- return i >= s.Length ? string.Empty : s.Substring(i);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static string TrimEx(string s)
- {
- return TrimEndEx(TrimStartEx(s));
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.trim
- /// </summary>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private JsValue Trim(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToJsString(thisObject);
- if (s.Length == 0 || (!IsWhiteSpaceEx(s[0]) && !IsWhiteSpaceEx(s[s.Length - 1])))
- {
- return s;
- }
- return TrimEx(s.ToString());
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.trimstart
- /// </summary>
- private JsValue TrimStart(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToJsString(thisObject);
- if (s.Length == 0 || !IsWhiteSpaceEx(s[0]))
- {
- return s;
- }
- return TrimStartEx(s.ToString());
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.trimend
- /// </summary>
- private JsValue TrimEnd(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToJsString(thisObject);
- if (s.Length == 0 || !IsWhiteSpaceEx(s[s.Length - 1]))
- {
- return s;
- }
- return TrimEndEx(s.ToString());
- }
- private JsValue ToLocaleUpperCase(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(_engine, thisObject);
- var s = TypeConverter.ToString(thisObject);
- var culture = CultureInfo.InvariantCulture;
- if (arguments.Length > 0 && arguments[0].IsString())
- {
- try
- {
- var cultureArgument = arguments[0].ToString();
- culture = CultureInfo.GetCultureInfo(cultureArgument);
- }
- catch (CultureNotFoundException)
- {
- Throw.RangeError(_realm, "Incorrect culture information provided");
- }
- }
- if (string.Equals("lt", culture.Name, StringComparison.OrdinalIgnoreCase))
- {
- s = StringInlHelper.LithuanianStringProcessor(s);
- #if NET462
- // Code specific to .NET Framework 4.6.2.
- // For no good reason this verison does not upper case these characters correctly.
- return new JsString(s.ToUpper(culture)
- .Replace("ϳ", "Ϳ")
- .Replace("ʝ", "Ʝ"));
- #endif
- }
- return new JsString(s.ToUpper(culture));
- }
- private JsValue ToUpperCase(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(_engine, thisObject);
- var s = TypeConverter.ToString(thisObject);
- return new JsString(s.ToUpperInvariant());
- }
- private JsValue ToLocaleLowerCase(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(_engine, thisObject);
- var s = TypeConverter.ToString(thisObject);
- return new JsString(s.ToLower(CultureInfo.InvariantCulture));
- }
- private JsValue ToLowerCase(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(_engine, thisObject);
- var s = TypeConverter.ToString(thisObject);
- return s.ToLowerInvariant();
- }
- private static int ToIntegerSupportInfinity(JsValue numberVal)
- {
- return numberVal._type == InternalTypes.Integer
- ? numberVal.AsInteger()
- : ToIntegerSupportInfinityUnlikely(numberVal);
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static int ToIntegerSupportInfinityUnlikely(JsValue numberVal)
- {
- var doubleVal = TypeConverter.ToInteger(numberVal);
- int intVal;
- if (double.IsPositiveInfinity(doubleVal))
- intVal = int.MaxValue;
- else if (double.IsNegativeInfinity(doubleVal))
- intVal = int.MinValue;
- else
- intVal = (int) doubleVal;
- return intVal;
- }
- private JsValue Substring(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToString(thisObject);
- var start = TypeConverter.ToNumber(arguments.At(0));
- var end = TypeConverter.ToNumber(arguments.At(1));
- if (double.IsNaN(start) || start < 0)
- {
- start = 0;
- }
- if (double.IsNaN(end) || end < 0)
- {
- end = 0;
- }
- var len = s.Length;
- var intStart = ToIntegerSupportInfinity(start);
- var intEnd = arguments.At(1).IsUndefined() ? len : ToIntegerSupportInfinity(end);
- var finalStart = System.Math.Min(len, System.Math.Max(intStart, 0));
- var finalEnd = System.Math.Min(len, System.Math.Max(intEnd, 0));
- // Swap value if finalStart < finalEnd
- var from = System.Math.Min(finalStart, finalEnd);
- var to = System.Math.Max(finalStart, finalEnd);
- var length = to - from;
- if (length == 0)
- {
- return JsString.Empty;
- }
- if (length == 1)
- {
- return JsString.Create(s[from]);
- }
- return new JsString(s.Substring(from, length));
- }
- private static JsValue Substr(JsValue thisObject, JsCallArguments arguments)
- {
- var s = TypeConverter.ToString(thisObject);
- var start = TypeConverter.ToInteger(arguments.At(0));
- var length = arguments.At(1).IsUndefined()
- ? double.PositiveInfinity
- : TypeConverter.ToInteger(arguments.At(1));
- start = start >= 0 ? start : System.Math.Max(s.Length + start, 0);
- length = System.Math.Min(System.Math.Max(length, 0), s.Length - start);
- if (length <= 0)
- {
- return JsString.Empty;
- }
- var startIndex = TypeConverter.ToInt32(start);
- var l = TypeConverter.ToInt32(length);
- if (l == 1)
- {
- return TypeConverter.ToString(s[startIndex]);
- }
- return s.Substring(startIndex, l);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.split
- /// </summary>
- private JsValue Split(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var separator = arguments.At(0);
- var limit = arguments.At(1);
- // fast path for empty regexp
- if (separator is JsRegExp R && string.Equals(R.Source, JsRegExp.regExpForMatchingAllCharacters, StringComparison.Ordinal))
- {
- separator = JsString.Empty;
- }
- if (separator is ObjectInstance oi)
- {
- var splitter = GetMethod(_realm, oi, GlobalSymbolRegistry.Split);
- if (splitter != null)
- {
- return splitter.Call(separator, thisObject, limit);
- }
- }
- var s = TypeConverter.ToString(thisObject);
- // Coerce into a number, true will become 1
- var lim = limit.IsUndefined() ? uint.MaxValue : TypeConverter.ToUint32(limit);
- if (separator.IsNull())
- {
- separator = "null";
- }
- else if (!separator.IsUndefined())
- {
- if (!separator.IsRegExp())
- {
- separator = TypeConverter.ToJsString(separator); // Coerce into a string, for an object call toString()
- }
- }
- if (lim == 0)
- {
- return _realm.Intrinsics.Array.ArrayCreate(0);
- }
- if (separator.IsUndefined())
- {
- var arrayInstance = _realm.Intrinsics.Array.ArrayCreate(1);
- arrayInstance.SetIndexValue(0, s, updateLength: false);
- return arrayInstance;
- }
- return SplitWithStringSeparator(_realm, separator, s, lim);
- }
- internal static JsValue SplitWithStringSeparator(Realm realm, JsValue separator, string s, uint lim)
- {
- var segments = StringExecutionContext.Current.SplitSegmentList;
- segments.Clear();
- var sep = TypeConverter.ToString(separator);
- if (sep == string.Empty)
- {
- if (s.Length > segments.Capacity)
- {
- segments.Capacity = s.Length;
- }
- for (var i = 0; i < s.Length; i++)
- {
- segments.Add(TypeConverter.ToString(s[i]));
- }
- }
- else
- {
- var array = StringExecutionContext.Current.SplitArray1;
- array[0] = sep;
- segments.AddRange(s.Split(array, StringSplitOptions.None));
- }
- var length = (uint) System.Math.Min(segments.Count, lim);
- var a = realm.Intrinsics.Array.ArrayCreate(length);
- for (int i = 0; i < length; i++)
- {
- a.SetIndexValue((uint) i, segments[i], updateLength: false);
- }
- a.SetLength(length);
- return a;
- }
- /// <summary>
- /// https://tc39.es/proposal-relative-indexing-method/#sec-string-prototype-additions
- /// </summary>
- private JsValue At(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(_engine, thisObject);
- var start = arguments.At(0);
- var o = thisObject.ToString();
- long len = o.Length;
- var relativeIndex = TypeConverter.ToInteger(start);
- int k;
- if (relativeIndex < 0)
- {
- k = (int) (len + relativeIndex);
- }
- else
- {
- k = (int) relativeIndex;
- }
- if (k < 0 || k >= len)
- {
- return Undefined;
- }
- return o[k];
- }
- private JsValue Slice(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var start = TypeConverter.ToNumber(arguments.At(0));
- if (double.IsNegativeInfinity(start))
- {
- start = 0;
- }
- if (double.IsPositiveInfinity(start))
- {
- return JsString.Empty;
- }
- var s = TypeConverter.ToJsString(thisObject);
- var end = TypeConverter.ToNumber(arguments.At(1));
- if (double.IsPositiveInfinity(end))
- {
- end = s.Length;
- }
- var len = s.Length;
- var intStart = (int) start;
- var intEnd = arguments.At(1).IsUndefined() ? len : (int) TypeConverter.ToInteger(end);
- var from = intStart < 0 ? System.Math.Max(len + intStart, 0) : System.Math.Min(intStart, len);
- var to = intEnd < 0 ? System.Math.Max(len + intEnd, 0) : System.Math.Min(intEnd, len);
- var span = System.Math.Max(to - from, 0);
- if (span == 0)
- {
- return JsString.Empty;
- }
- if (span == 1)
- {
- return JsString.Create(s[from]);
- }
- return s.Substring(from, span);
- }
- private JsValue Search(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var regex = arguments.At(0);
- if (regex is ObjectInstance oi)
- {
- var searcher = GetMethod(_realm, oi, GlobalSymbolRegistry.Search);
- if (searcher != null)
- {
- return searcher.Call(regex, thisObject);
- }
- }
- var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct([regex]);
- var s = TypeConverter.ToJsString(thisObject);
- return _engine.Invoke(rx, GlobalSymbolRegistry.Search, [s]);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.replace
- /// </summary>
- private JsValue Replace(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var searchValue = arguments.At(0);
- var replaceValue = arguments.At(1);
- if (!searchValue.IsNullOrUndefined())
- {
- var replacer = GetMethod(_realm, searchValue, GlobalSymbolRegistry.Replace);
- if (replacer != null)
- {
- return replacer.Call(searchValue, thisObject, replaceValue);
- }
- }
- var thisString = TypeConverter.ToJsString(thisObject);
- var searchString = TypeConverter.ToString(searchValue);
- var functionalReplace = replaceValue is ICallable;
- if (!functionalReplace)
- {
- replaceValue = TypeConverter.ToJsString(replaceValue);
- }
- var position = thisString.IndexOf(searchString);
- if (position < 0)
- {
- return thisString;
- }
- string replStr;
- if (functionalReplace)
- {
- var replValue = ((ICallable) replaceValue).Call(Undefined, searchString, position, thisString);
- replStr = TypeConverter.ToString(replValue);
- }
- else
- {
- var captures = System.Array.Empty<string>();
- replStr = RegExpPrototype.GetSubstitution(searchString, thisString.ToString(), position, captures, Undefined, TypeConverter.ToString(replaceValue));
- }
- var tailPos = position + searchString.Length;
- var newString = thisString.Substring(0, position) + replStr + thisString.Substring(tailPos);
- return newString;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.replaceall
- /// </summary>
- private JsValue ReplaceAll(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var searchValue = arguments.At(0);
- var replaceValue = arguments.At(1);
- if (!searchValue.IsNullOrUndefined())
- {
- if (searchValue.IsRegExp())
- {
- var flags = searchValue.Get(RegExpPrototype.PropertyFlags);
- TypeConverter.RequireObjectCoercible(_engine, flags);
- if (!TypeConverter.ToString(flags).Contains('g'))
- {
- Throw.TypeError(_realm, "String.prototype.replaceAll called with a non-global RegExp argument");
- }
- }
- var replacer = GetMethod(_realm, searchValue, GlobalSymbolRegistry.Replace);
- if (replacer != null)
- {
- return replacer.Call(searchValue, thisObject, replaceValue);
- }
- }
- var thisString = TypeConverter.ToString(thisObject);
- var searchString = TypeConverter.ToString(searchValue);
- var functionalReplace = replaceValue is ICallable;
- if (!functionalReplace)
- {
- replaceValue = TypeConverter.ToJsString(replaceValue);
- // check fast case
- var newValue = replaceValue.ToString();
- if (!newValue.Contains('$') && searchString.Length > 0)
- {
- // just plain old string replace
- return thisString.Replace(searchString, newValue);
- }
- }
- // https://tc39.es/ecma262/#sec-stringindexof
- static int StringIndexOf(string s, string search, int fromIndex)
- {
- if (search.Length == 0 && fromIndex <= s.Length)
- {
- return fromIndex;
- }
- return fromIndex < s.Length
- ? s.IndexOf(search, fromIndex, StringComparison.Ordinal)
- : -1;
- }
- var searchLength = searchString.Length;
- var advanceBy = System.Math.Max(1, searchLength);
- var endOfLastMatch = 0;
- using var result = new ValueStringBuilder();
- var position = StringIndexOf(thisString, searchString, 0);
- while (position != -1)
- {
- string replacement;
- var preserved = thisString.Substring(endOfLastMatch, position - endOfLastMatch);
- if (functionalReplace)
- {
- var replValue = ((ICallable) replaceValue).Call(Undefined, searchString, position, thisString);
- replacement = TypeConverter.ToString(replValue);
- }
- else
- {
- var captures = System.Array.Empty<string>();
- replacement = RegExpPrototype.GetSubstitution(searchString, thisString, position, captures, Undefined, TypeConverter.ToString(replaceValue));
- }
- result.Append(preserved);
- result.Append(replacement);
- endOfLastMatch = position + searchLength;
- position = StringIndexOf(thisString, searchString, position + advanceBy);
- }
- if (endOfLastMatch < thisString.Length)
- {
- result.Append(thisString.AsSpan(endOfLastMatch));
- }
- return result.ToString();
- }
- private JsValue Match(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var regex = arguments.At(0);
- if (regex is ObjectInstance oi)
- {
- var matcher = GetMethod(_realm, oi, GlobalSymbolRegistry.Match);
- if (matcher != null)
- {
- return matcher.Call(regex, thisObject);
- }
- }
- var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct([regex]);
- var s = TypeConverter.ToJsString(thisObject);
- return _engine.Invoke(rx, GlobalSymbolRegistry.Match, [s]);
- }
- private JsValue MatchAll(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(_engine, thisObject);
- var regex = arguments.At(0);
- if (!regex.IsNullOrUndefined())
- {
- if (regex.IsRegExp())
- {
- var flags = regex.Get(RegExpPrototype.PropertyFlags);
- TypeConverter.RequireObjectCoercible(_engine, flags);
- if (!TypeConverter.ToString(flags).Contains('g'))
- {
- Throw.TypeError(_realm);
- }
- }
- var matcher = GetMethod(_realm, regex, GlobalSymbolRegistry.MatchAll);
- if (matcher != null)
- {
- return matcher.Call(regex, thisObject);
- }
- }
- var s = TypeConverter.ToJsString(thisObject);
- var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct([regex, "g"]);
- return _engine.Invoke(rx, GlobalSymbolRegistry.MatchAll, [s]);
- }
- private JsValue LocaleCompare(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToString(thisObject);
- var that = TypeConverter.ToString(arguments.At(0));
- var culture = Engine.Options.Culture;
- if (arguments.Length > 1 && arguments[1].IsString())
- {
- culture = CultureInfo.GetCultureInfo(arguments.At(1).AsString());
- }
- return culture.CompareInfo.Compare(s.Normalize(NormalizationForm.FormKD), that.Normalize(NormalizationForm.FormKD));
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.lastindexof
- /// </summary>
- private JsValue LastIndexOf(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var jsString = TypeConverter.ToJsString(thisObject);
- var searchStr = TypeConverter.ToString(arguments.At(0));
- double numPos = double.NaN;
- if (arguments.Length > 1 && !arguments[1].IsUndefined())
- {
- numPos = TypeConverter.ToNumber(arguments[1]);
- }
- var pos = double.IsNaN(numPos) ? double.PositiveInfinity : TypeConverter.ToInteger(numPos);
- var len = jsString.Length;
- var start = (int) System.Math.Min(System.Math.Max(pos, 0), len);
- var searchLen = searchStr.Length;
- if (searchLen > len)
- {
- return JsNumber.IntegerNegativeOne;
- }
- var s = jsString.ToString();
- var i = start;
- bool found;
- do
- {
- found = true;
- var j = 0;
- while (found && j < searchLen)
- {
- if (i + searchLen > len || s[i + j] != searchStr[j])
- {
- found = false;
- }
- else
- {
- j++;
- }
- }
- if (!found)
- {
- i--;
- }
- } while (!found && i >= 0);
- return i;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.indexof
- /// </summary>
- private JsValue IndexOf(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToJsString(thisObject);
- var searchStr = TypeConverter.ToString(arguments.At(0));
- double pos = 0;
- if (arguments.Length > 1 && !arguments[1].IsUndefined())
- {
- pos = TypeConverter.ToInteger(arguments[1]);
- }
- if (pos > s.Length)
- {
- pos = s.Length;
- }
- if (pos < 0)
- {
- pos = 0;
- }
- return s.IndexOf(searchStr, (int) pos);
- }
- private JsValue Concat(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- if (thisObject is not JsString jsString)
- {
- jsString = new JsString.ConcatenatedString(TypeConverter.ToString(thisObject));
- }
- else
- {
- jsString = jsString.EnsureCapacity(0);
- }
- foreach (var argument in arguments)
- {
- jsString = jsString.Append(argument);
- }
- return jsString;
- }
- private JsValue CharCodeAt(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- JsValue pos = arguments.Length > 0 ? arguments[0] : 0;
- var s = TypeConverter.ToJsString(thisObject);
- var position = (int) TypeConverter.ToInteger(pos);
- if (position < 0 || position >= s.Length)
- {
- return JsNumber.DoubleNaN;
- }
- return (long) s[position];
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.codepointat
- /// </summary>
- private JsValue CodePointAt(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- JsValue pos = arguments.Length > 0 ? arguments[0] : 0;
- var s = TypeConverter.ToString(thisObject);
- var position = (int) TypeConverter.ToInteger(pos);
- if (position < 0 || position >= s.Length)
- {
- return Undefined;
- }
- return CodePointAt(s, position).CodePoint;
- }
- [StructLayout(LayoutKind.Auto)]
- private readonly record struct CodePointResult(int CodePoint, int CodeUnitCount, bool IsUnpairedSurrogate);
- private static CodePointResult CodePointAt(string s, int position)
- {
- var size = s.Length;
- var first = s.CharCodeAt(position);
- var cp = s.CharCodeAt(position);
- var firstIsLeading = char.IsHighSurrogate(first);
- var firstIsTrailing = char.IsLowSurrogate(first);
- if (!firstIsLeading && !firstIsTrailing)
- {
- return new CodePointResult(cp, 1, false);
- }
- if (firstIsTrailing || position + 1 == size)
- {
- return new CodePointResult(cp, 1, true);
- }
- var second = s.CharCodeAt(position + 1);
- if (!char.IsLowSurrogate(second))
- {
- return new CodePointResult(cp, 1, true);
- }
- return new CodePointResult(char.ConvertToUtf32(first, second), 2, false);
- }
- private JsValue CharAt(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToJsString(thisObject);
- var position = TypeConverter.ToInteger(arguments.At(0));
- var size = s.Length;
- if (position >= size || position < 0)
- {
- return JsString.Empty;
- }
- return JsString.Create(s[(int) position]);
- }
- private JsValue ValueOf(JsValue thisObject, JsCallArguments arguments)
- {
- if (thisObject is StringInstance si)
- {
- return si.StringData;
- }
- if (thisObject is JsString)
- {
- return thisObject;
- }
- Throw.TypeError(_realm);
- return Undefined;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.padstart
- /// </summary>
- private JsValue PadStart(JsValue thisObject, JsCallArguments arguments)
- {
- return StringPad(thisObject, arguments, true);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.padend
- /// </summary>
- private JsValue PadEnd(JsValue thisObject, JsCallArguments arguments)
- {
- return StringPad(thisObject, arguments, false);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-stringpad
- /// </summary>
- private JsValue StringPad(JsValue thisObject, JsCallArguments arguments, bool padStart)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToJsString(thisObject);
- var targetLength = TypeConverter.ToInt32(arguments.At(0));
- var padStringValue = arguments.At(1);
- var padString = padStringValue.IsUndefined()
- ? " "
- : TypeConverter.ToString(padStringValue);
- if (s.Length > targetLength || padString.Length == 0)
- {
- return s;
- }
- targetLength -= s.Length;
- if (targetLength > padString.Length)
- {
- padString = string.Join("", System.Linq.Enumerable.Repeat(padString, (targetLength / padString.Length) + 1));
- }
- return padStart
- ? $"{padString.Substring(0, targetLength)}{s}"
- : $"{s}{padString.Substring(0, targetLength)}";
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.startswith
- /// </summary>
- private JsValue StartsWith(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToJsString(thisObject);
- var searchString = arguments.At(0);
- if (ReferenceEquals(searchString, Null))
- {
- searchString = "null";
- }
- else
- {
- if (searchString.IsRegExp())
- {
- Throw.TypeError(_realm);
- }
- }
- var searchStr = TypeConverter.ToString(searchString);
- var pos = TypeConverter.ToInt32(arguments.At(1));
- var len = s.Length;
- var start = System.Math.Min(System.Math.Max(pos, 0), len);
- return s.StartsWith(searchStr, start);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.endswith
- /// </summary>
- private JsValue EndsWith(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToJsString(thisObject);
- var searchString = arguments.At(0);
- if (ReferenceEquals(searchString, Null))
- {
- searchString = "null";
- }
- else
- {
- if (searchString.IsRegExp())
- {
- Throw.TypeError(_realm);
- }
- }
- var searchStr = TypeConverter.ToString(searchString);
- var len = s.Length;
- var pos = TypeConverter.ToInt32(arguments.At(1, len));
- var end = System.Math.Min(System.Math.Max(pos, 0), len);
- return s.EndsWith(searchStr, end);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.includes
- /// </summary>
- private JsValue Includes(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToJsString(thisObject);
- var searchString = arguments.At(0);
- if (searchString.IsRegExp())
- {
- Throw.TypeError(_realm, "First argument to String.prototype.includes must not be a regular expression");
- }
- var searchStr = TypeConverter.ToString(searchString);
- double pos = 0;
- if (arguments.Length > 1 && !arguments[1].IsUndefined())
- {
- pos = TypeConverter.ToInteger(arguments[1]);
- }
- if (searchStr.Length == 0)
- {
- return JsBoolean.True;
- }
- if (pos < 0)
- {
- pos = 0;
- }
- return s.IndexOf(searchStr, (int) pos) > -1;
- }
- private JsValue Normalize(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var str = TypeConverter.ToString(thisObject);
- var param = arguments.At(0);
- var form = "NFC";
- if (!param.IsUndefined())
- {
- form = TypeConverter.ToString(param);
- }
- var nf = NormalizationForm.FormC;
- switch (form)
- {
- case "NFC":
- nf = NormalizationForm.FormC;
- break;
- case "NFD":
- nf = NormalizationForm.FormD;
- break;
- case "NFKC":
- nf = NormalizationForm.FormKC;
- break;
- case "NFKD":
- nf = NormalizationForm.FormKD;
- break;
- default:
- Throw.RangeError(
- _realm,
- "The normalization form should be one of NFC, NFD, NFKC, NFKD.");
- break;
- }
- return str.Normalize(nf);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-string.prototype.repeat
- /// </summary>
- private JsValue Repeat(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(Engine, thisObject);
- var s = TypeConverter.ToString(thisObject);
- var count = arguments.At(0);
- var n = TypeConverter.ToIntegerOrInfinity(count);
- if (n < 0 || double.IsPositiveInfinity(n))
- {
- Throw.RangeError(_realm, "Invalid count value");
- }
- if (n == 0 || s.Length == 0)
- {
- return JsString.Empty;
- }
- if (s.Length == 1)
- {
- return new string(s[0], (int) n);
- }
- var sb = new ValueStringBuilder((int) (n * s.Length));
- for (var i = 0; i < n; ++i)
- {
- sb.Append(s);
- }
- return sb.ToString();
- }
- private JsValue IsWellFormed(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(_engine, thisObject);
- var s = TypeConverter.ToString(thisObject);
- return IsStringWellFormedUnicode(s);
- }
- private JsValue ToWellFormed(JsValue thisObject, JsCallArguments arguments)
- {
- TypeConverter.RequireObjectCoercible(_engine, thisObject);
- var s = TypeConverter.ToString(thisObject);
- var strLen = s.Length;
- var k = 0;
- var result = new ValueStringBuilder();
- while (k < strLen)
- {
- var cp = CodePointAt(s, k);
- if (cp.IsUnpairedSurrogate)
- {
- // \uFFFD
- result.Append('�');
- }
- else
- {
- result.Append(s.AsSpan(k, cp.CodeUnitCount));
- }
- k += cp.CodeUnitCount;
- }
- return result.ToString();
- }
- private static bool IsStringWellFormedUnicode(string s)
- {
- for (var i = 0; i < s.Length; ++i)
- {
- var isSurrogate = (s.CharCodeAt(i) & 0xF800) == 0xD800;
- if (!isSurrogate)
- {
- continue;
- }
- var isLeadingSurrogate = s.CharCodeAt(i) < 0xDC00;
- if (!isLeadingSurrogate)
- {
- return false; // unpaired trailing surrogate
- }
- var isFollowedByTrailingSurrogate = i + 1 < s.Length && (s.CharCodeAt(i + 1) & 0xFC00) == 0xDC00;
- if (!isFollowedByTrailingSurrogate)
- {
- return false; // unpaired leading surrogate
- }
- ++i;
- }
- return true;
- }
- }
|