1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831 |
- using System.Linq;
- using Jint.Collections;
- using Jint.Native.Iterator;
- using Jint.Native.Map;
- using Jint.Native.Number;
- using Jint.Native.Object;
- using Jint.Native.Symbol;
- using Jint.Pooling;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Descriptors.Specialized;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Array
- {
- /// <summary>
- /// https://tc39.es/ecma262/#sec-properties-of-the-array-prototype-object
- /// </summary>
- public sealed class ArrayPrototype : ArrayInstance
- {
- private readonly Realm _realm;
- private readonly ArrayConstructor _constructor;
- internal ClrFunctionInstance? _originalIteratorFunction;
- internal ArrayPrototype(
- Engine engine,
- Realm realm,
- ArrayConstructor arrayConstructor,
- ObjectPrototype objectPrototype) : base(engine, InternalTypes.Object)
- {
- _prototype = objectPrototype;
- _length = new PropertyDescriptor(JsNumber.PositiveZero, PropertyFlag.Writable);
- _realm = realm;
- _constructor = arrayConstructor;
- }
- protected override void Initialize()
- {
- const PropertyFlag PropertyFlags = PropertyFlag.Writable | PropertyFlag.Configurable;
- var properties = new PropertyDictionary(40, checkExistingKeys: false)
- {
- ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
- ["at"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "at", At, 1, PropertyFlag.Configurable), PropertyFlags),
- ["concat"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "concat", Concat, 1, PropertyFlag.Configurable), PropertyFlags),
- ["copyWithin"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "copyWithin", CopyWithin, 2, PropertyFlag.Configurable), PropertyFlags),
- ["entries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "entries", Entries, 0, PropertyFlag.Configurable), PropertyFlags),
- ["every"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "every", Every, 1, PropertyFlag.Configurable), PropertyFlags),
- ["fill"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "fill", Fill, 1, PropertyFlag.Configurable), PropertyFlags),
- ["filter"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "filter", Filter, 1, PropertyFlag.Configurable), PropertyFlags),
- ["find"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "find", Find, 1, PropertyFlag.Configurable), PropertyFlags),
- ["findIndex"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "findIndex", FindIndex, 1, PropertyFlag.Configurable), PropertyFlags),
- ["findLast"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "findLast", FindLast, 1, PropertyFlag.Configurable), PropertyFlags),
- ["findLastIndex"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "findLastIndex", FindLastIndex, 1, PropertyFlag.Configurable), PropertyFlags),
- ["flat"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "flat", Flat, 0, PropertyFlag.Configurable), PropertyFlags),
- ["flatMap"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "flatMap", FlatMap, 1, PropertyFlag.Configurable), PropertyFlags),
- ["forEach"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "forEach", ForEach, 1, PropertyFlag.Configurable), PropertyFlags),
- ["group"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "group", Group, 1, PropertyFlag.Configurable), PropertyFlags),
- ["groupToMap"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "groupToMap", GroupToMap, 1, PropertyFlag.Configurable), PropertyFlags),
- ["includes"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "includes", Includes, 1, PropertyFlag.Configurable), PropertyFlags),
- ["indexOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "indexOf", IndexOf, 1, PropertyFlag.Configurable), PropertyFlags),
- ["join"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "join", Join, 1, PropertyFlag.Configurable), PropertyFlags),
- ["keys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keys", Keys, 0, PropertyFlag.Configurable), PropertyFlags),
- ["lastIndexOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "lastIndexOf", LastIndexOf, 1, PropertyFlag.Configurable), PropertyFlags),
- ["map"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "map", Map, 1, PropertyFlag.Configurable), PropertyFlags),
- ["pop"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "pop", Pop, 0, PropertyFlag.Configurable), PropertyFlags),
- ["push"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "push", Push, 1, PropertyFlag.Configurable), PropertyFlags),
- ["reduce"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "reduce", Reduce, 1, PropertyFlag.Configurable), PropertyFlags),
- ["reduceRight"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "reduceRight", ReduceRight, 1, PropertyFlag.Configurable), PropertyFlags),
- ["reverse"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "reverse", Reverse, 0, PropertyFlag.Configurable), PropertyFlags),
- ["shift"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "shift", Shift, 0, PropertyFlag.Configurable), PropertyFlags),
- ["slice"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "slice", Slice, 2, PropertyFlag.Configurable), PropertyFlags),
- ["some"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "some", Some, 1, PropertyFlag.Configurable), PropertyFlags),
- ["sort"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "sort", Sort, 1, PropertyFlag.Configurable), PropertyFlags),
- ["splice"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "splice", Splice, 2, PropertyFlag.Configurable), PropertyFlags),
- ["toLocaleString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toLocaleString", ToLocaleString, 0, PropertyFlag.Configurable), PropertyFlags),
- ["toReversed"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toReversed", ToReversed, 0, PropertyFlag.Configurable), PropertyFlags),
- ["toSorted"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toSorted", ToSorted, 1, PropertyFlag.Configurable), PropertyFlags),
- ["toSpliced"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toSpliced", ToSpliced, 2, PropertyFlag.Configurable), PropertyFlags),
- ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToString, 0, PropertyFlag.Configurable), PropertyFlags),
- ["unshift"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "unshift", Unshift, 1, PropertyFlag.Configurable), PropertyFlags),
- ["values"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "values", Values, 0, PropertyFlag.Configurable), PropertyFlags),
- ["with"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "with", With, 2, PropertyFlag.Configurable), PropertyFlags),
- };
- SetProperties(properties);
- _originalIteratorFunction = new ClrFunctionInstance(Engine, "iterator", Values, 1);
- var symbols = new SymbolDictionary(2)
- {
- [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(_originalIteratorFunction, PropertyFlags),
- [GlobalSymbolRegistry.Unscopables] = new LazyPropertyDescriptor(_engine, static state =>
- {
- var unscopables = new JsObject((Engine) state!)
- {
- _prototype = null
- };
- unscopables.FastSetDataProperty("at", JsBoolean.True);
- unscopables.FastSetDataProperty("copyWithin", JsBoolean.True);
- unscopables.FastSetDataProperty("entries", JsBoolean.True);
- unscopables.FastSetDataProperty("fill", JsBoolean.True);
- unscopables.FastSetDataProperty("find", JsBoolean.True);
- unscopables.FastSetDataProperty("findIndex", JsBoolean.True);
- unscopables.FastSetDataProperty("findLast", JsBoolean.True);
- unscopables.FastSetDataProperty("findLastIndex", JsBoolean.True);
- unscopables.FastSetDataProperty("flat", JsBoolean.True);
- unscopables.FastSetDataProperty("flatMap", JsBoolean.True);
- unscopables.FastSetDataProperty("group", JsBoolean.True);
- unscopables.FastSetDataProperty("groupBy", JsBoolean.True);
- unscopables.FastSetDataProperty("groupByToMap", JsBoolean.True);
- unscopables.FastSetDataProperty("groupToMap", JsBoolean.True);
- unscopables.FastSetDataProperty("includes", JsBoolean.True);
- unscopables.FastSetDataProperty("keys", JsBoolean.True);
- unscopables.FastSetDataProperty("toReversed", JsBoolean.True);
- unscopables.FastSetDataProperty("toSorted", JsBoolean.True);
- unscopables.FastSetDataProperty("toSpliced", JsBoolean.True);
- unscopables.FastSetDataProperty("values", JsBoolean.True);
- return unscopables;
- }, PropertyFlag.Configurable)
- };
- SetSymbols(symbols);
- }
- private ObjectInstance Keys(JsValue thisObj, JsValue[] arguments)
- {
- if (thisObj is ObjectInstance oi && oi.IsArrayLike)
- {
- return _realm.Intrinsics.ArrayIteratorPrototype.Construct(oi, ArrayIteratorType.Key);
- }
- ExceptionHelper.ThrowTypeError(_realm, "cannot construct iterator");
- return null;
- }
- internal ObjectInstance Values(JsValue thisObj, JsValue[] arguments)
- {
- if (thisObj is ObjectInstance oi && oi.IsArrayLike)
- {
- return _realm.Intrinsics.ArrayIteratorPrototype.Construct(oi, ArrayIteratorType.Value);
- }
- ExceptionHelper.ThrowTypeError(_realm, "cannot construct iterator");
- return null;
- }
- private ObjectInstance With(JsValue thisObj, JsValue[] arguments)
- {
- var o = ArrayOperations.For(TypeConverter.ToObject(_realm, thisObj));
- var len = o.GetLongLength();
- var relativeIndex = TypeConverter.ToIntegerOrInfinity(arguments.At(0));
- var value = arguments.At(1);
- long actualIndex;
- if (relativeIndex >= 0)
- {
- actualIndex = (long) relativeIndex;
- }
- else
- {
- actualIndex = (long) (len + relativeIndex);
- }
- if (actualIndex >= (long) len || actualIndex < 0)
- {
- ExceptionHelper.ThrowRangeError(_realm, "Invalid start index");
- }
- var a = CreateBackingArray(len);
- ulong k = 0;
- while (k < len)
- {
- a[k] = k == (ulong) actualIndex ? value : o.Get(k);
- k++;
- }
- return new JsArray(_engine, a);
- }
- private ObjectInstance Entries(JsValue thisObj, JsValue[] arguments)
- {
- if (thisObj is ObjectInstance oi && oi.IsArrayLike)
- {
- return _realm.Intrinsics.ArrayIteratorPrototype.Construct(oi, ArrayIteratorType.KeyAndValue);
- }
- ExceptionHelper.ThrowTypeError(_realm, "cannot construct iterator");
- return null;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.fill
- /// </summary>
- private JsValue Fill(JsValue thisObj, JsValue[] arguments)
- {
- var value = arguments.At(0);
- var start = arguments.At(1);
- var end = arguments.At(2);
- var o = TypeConverter.ToObject(_realm, thisObj);
- var operations = ArrayOperations.For(o);
- var length = operations.GetLongLength();
- var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
- ulong k;
- if (double.IsNegativeInfinity(relativeStart))
- {
- k = 0;
- }
- else if (relativeStart < 0)
- {
- k = (ulong) System.Math.Max(length + relativeStart, 0);
- }
- else
- {
- k = (ulong) System.Math.Min(relativeStart, length);
- }
- var relativeEnd = end.IsUndefined() ? length : TypeConverter.ToIntegerOrInfinity(end);
- ulong final;
- if (double.IsNegativeInfinity(relativeEnd))
- {
- final = 0;
- }
- else if (relativeEnd < 0)
- {
- final = (ulong) System.Math.Max(length + relativeEnd, 0);
- }
- else
- {
- final = (ulong) System.Math.Min(relativeEnd, length);
- }
- for (var i = k; i < final; ++i)
- {
- operations.Set(i, value, throwOnError: false);
- }
- return o;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.copywithin
- /// </summary>
- private JsValue CopyWithin(JsValue thisObj, JsValue[] arguments)
- {
- var o = TypeConverter.ToObject(_realm, thisObj);
- JsValue target = arguments.At(0);
- JsValue start = arguments.At(1);
- JsValue end = arguments.At(2);
- var operations = ArrayOperations.For(o);
- var len = operations.GetLongLength();
- var relativeTarget = TypeConverter.ToIntegerOrInfinity(target);
- var to = relativeTarget < 0 ?
- System.Math.Max(len + relativeTarget, 0) :
- System.Math.Min(relativeTarget, len);
- var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
- long from;
- if (double.IsNegativeInfinity(relativeStart))
- {
- from = 0;
- }
- else if (relativeStart < 0)
- {
- from = (long) System.Math.Max(len + relativeStart, 0);
- }
- else
- {
- from = (long) System.Math.Min(relativeStart, len);
- }
- var relativeEnd = end.IsUndefined() ? len : TypeConverter.ToIntegerOrInfinity(end);
- long final;
- if (double.IsNegativeInfinity(relativeEnd))
- {
- final = 0;
- }
- else if (relativeEnd < 0)
- {
- final = (long) System.Math.Max(len + relativeEnd, 0);
- }
- else
- {
- final = (long) System.Math.Min(relativeEnd, len);
- }
- var count = (long) System.Math.Min(final - from, len - to);
- long direction = 1;
- if (from < to && to < from + count)
- {
- direction = -1;
- from += count - 1;
- to += count - 1;
- }
- while (count > 0)
- {
- var fromPresent = operations.HasProperty((ulong) from);
- if (fromPresent)
- {
- var fromValue = operations.Get((ulong) from);
- operations.Set((ulong) to, fromValue, updateLength: true, throwOnError: true);
- }
- else
- {
- operations.DeletePropertyOrThrow((ulong) to);
- }
- from += direction;
- to += direction;
- count--;
- }
- return o;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.lastindexof
- /// </summary>
- private JsValue LastIndexOf(JsValue thisObj, JsValue[] arguments)
- {
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLongLength();
- if (len == 0)
- {
- return JsNumber.IntegerNegativeOne;
- }
- var n = arguments.Length > 1
- ? TypeConverter.ToInteger(arguments[1])
- : len - 1;
- double k;
- if (n >= 0)
- {
- k = System.Math.Min(n, len - 1); // min
- }
- else
- {
- k = len - System.Math.Abs(n);
- }
- if (k < 0 || k > ArrayOperations.MaxArrayLikeLength)
- {
- return JsNumber.IntegerNegativeOne;
- }
- var searchElement = arguments.At(0);
- var i = (ulong) k;
- for (; ; i--)
- {
- var kPresent = o.HasProperty(i);
- if (kPresent)
- {
- var elementK = o.Get(i);
- if (elementK == searchElement)
- {
- return i;
- }
- }
- if (i == 0)
- {
- break;
- }
- }
- return JsNumber.IntegerNegativeOne;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.reduce
- /// </summary>
- private JsValue Reduce(JsValue thisObj, JsValue[] arguments)
- {
- var callbackfn = arguments.At(0);
- var initialValue = arguments.At(1);
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLength();
- var callable = GetCallable(callbackfn);
- if (len == 0 && arguments.Length < 2)
- {
- ExceptionHelper.ThrowTypeError(_realm);
- }
- var k = 0;
- JsValue accumulator = Undefined;
- if (arguments.Length > 1)
- {
- accumulator = initialValue;
- }
- else
- {
- var kPresent = false;
- while (kPresent == false && k < len)
- {
- if (kPresent = o.TryGetValue((uint) k, out var temp))
- {
- accumulator = temp;
- }
- k++;
- }
- if (kPresent == false)
- {
- ExceptionHelper.ThrowTypeError(_realm);
- }
- }
- var args = new JsValue[4];
- args[3] = o.Target;
- while (k < len)
- {
- var i = (uint) k;
- if (o.TryGetValue(i, out var kvalue))
- {
- args[0] = accumulator;
- args[1] = kvalue;
- args[2] = i;
- accumulator = callable.Call(Undefined, args);
- }
- k++;
- }
- return accumulator;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.filter
- /// </summary>
- private JsValue Filter(JsValue thisObj, JsValue[] arguments)
- {
- var callbackfn = arguments.At(0);
- var thisArg = arguments.At(1);
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLength();
- var callable = GetCallable(callbackfn);
- var a = _realm.Intrinsics.Array.ArraySpeciesCreate(TypeConverter.ToObject(_realm, thisObj), 0);
- var operations = ArrayOperations.For(a);
- uint to = 0;
- var args = _engine._jsValueArrayPool.RentArray(3);
- args[2] = o.Target;
- for (uint k = 0; k < len; k++)
- {
- if (o.TryGetValue(k, out var kvalue))
- {
- args[0] = kvalue;
- args[1] = k;
- var selected = callable.Call(thisArg, args);
- if (TypeConverter.ToBoolean(selected))
- {
- operations.CreateDataPropertyOrThrow(to, kvalue);
- to++;
- }
- }
- }
- operations.SetLength(to);
- _engine._jsValueArrayPool.ReturnArray(args);
- return a;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.map
- /// </summary>
- private JsValue Map(JsValue thisObj, JsValue[] arguments)
- {
- if (thisObj is JsArray { CanUseFastAccess: true } arrayInstance
- && !arrayInstance.HasOwnProperty(CommonProperties.Constructor))
- {
- return arrayInstance.Map(arguments);
- }
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLongLength();
- if (len > ArrayOperations.MaxArrayLength)
- {
- ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");;
- }
- var callbackfn = arguments.At(0);
- var thisArg = arguments.At(1);
- var callable = GetCallable(callbackfn);
- var a = ArrayOperations.For(_realm.Intrinsics.Array.ArraySpeciesCreate(TypeConverter.ToObject(_realm, thisObj), (uint) len));
- var args = _engine._jsValueArrayPool.RentArray(3);
- args[2] = o.Target;
- for (uint k = 0; k < len; k++)
- {
- if (o.TryGetValue(k, out var kvalue))
- {
- args[0] = kvalue;
- args[1] = k;
- var mappedValue = callable.Call(thisArg, args);
- a.CreateDataPropertyOrThrow(k, mappedValue);
- }
- }
- _engine._jsValueArrayPool.ReturnArray(args);
- return a.Target;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.flat
- /// </summary>
- private JsValue Flat(JsValue thisObj, JsValue[] arguments)
- {
- var O = TypeConverter.ToObject(_realm, thisObj);
- var operations = ArrayOperations.For(O);
- var sourceLen = operations.GetLength();
- double depthNum = 1;
- var depth = arguments.At(0);
- if (!depth.IsUndefined())
- {
- depthNum = TypeConverter.ToIntegerOrInfinity(depth);
- }
- if (depthNum < 0)
- {
- depthNum = 0;
- }
- var A = _realm.Intrinsics.Array.ArraySpeciesCreate(O, 0);
- FlattenIntoArray(A, O, sourceLen, 0, depthNum);
- return A;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.flatmap
- /// </summary>
- private JsValue FlatMap(JsValue thisObj, JsValue[] arguments)
- {
- var O = TypeConverter.ToObject(_realm, thisObj);
- var mapperFunction = arguments.At(0);
- var thisArg = arguments.At(1);
- var sourceLen = O.Length;
- if (!mapperFunction.IsCallable)
- {
- ExceptionHelper.ThrowTypeError(_realm, "flatMap mapper function is not callable");
- }
- var A = _realm.Intrinsics.Array.ArraySpeciesCreate(O, 0);
- FlattenIntoArray(A, O, sourceLen, 0, 1, (ICallable) mapperFunction, thisArg);
- return A;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-flattenintoarray
- /// </summary>
- private long FlattenIntoArray(
- ObjectInstance target,
- ObjectInstance source,
- uint sourceLen,
- long start,
- double depth,
- ICallable? mapperFunction = null,
- JsValue? thisArg = null)
- {
- var targetIndex = start;
- var sourceIndex = 0;
- var callArguments = System.Array.Empty<JsValue>();
- if (mapperFunction is not null)
- {
- callArguments = _engine._jsValueArrayPool.RentArray(3);
- callArguments[2] = source;
- }
- while (sourceIndex < sourceLen)
- {
- var P = TypeConverter.ToString(sourceIndex);
- var exists = source.HasProperty(P);
- if (exists)
- {
- var element = source.Get(P);
- if (mapperFunction is not null)
- {
- callArguments[0] = element;
- callArguments[1] = JsNumber.Create(sourceIndex);
- element = mapperFunction.Call(thisArg ?? Undefined, callArguments);
- }
- var shouldFlatten = false;
- if (depth > 0)
- {
- shouldFlatten = element.IsArray();
- }
- if (shouldFlatten)
- {
- var newDepth = double.IsPositiveInfinity(depth)
- ? depth
- : depth - 1;
- var objectInstance = (ObjectInstance) element;
- var elementLen = objectInstance.Length;
- targetIndex = FlattenIntoArray(target, objectInstance, elementLen, targetIndex, newDepth);
- }
- else
- {
- if (targetIndex >= NumberConstructor.MaxSafeInteger)
- {
- ExceptionHelper.ThrowTypeError(_realm);
- }
- target.CreateDataPropertyOrThrow(targetIndex, element);
- targetIndex += 1;
- }
- }
- sourceIndex++;
- }
- if (mapperFunction is not null)
- {
- _engine._jsValueArrayPool.ReturnArray(callArguments);
- }
- return targetIndex;
- }
- private JsValue ForEach(JsValue thisObj, JsValue[] arguments)
- {
- var callbackfn = arguments.At(0);
- var thisArg = arguments.At(1);
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLength();
- var callable = GetCallable(callbackfn);
- var args = _engine._jsValueArrayPool.RentArray(3);
- args[2] = o.Target;
- for (uint k = 0; k < len; k++)
- {
- if (o.TryGetValue(k, out var kvalue))
- {
- args[0] = kvalue;
- args[1] = k;
- callable.Call(thisArg, args);
- }
- }
- _engine._jsValueArrayPool.ReturnArray(args);
- return Undefined;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.includes
- /// </summary>
- private JsValue Includes(JsValue thisObj, JsValue[] arguments)
- {
- var o = ArrayOperations.For(_realm, thisObj);
- var len = (long) o.GetLongLength();
- if (len == 0)
- {
- return JsBoolean.False;
- }
- var searchElement = arguments.At(0);
- var fromIndex = arguments.At(1);
- long k = 0;
- var n = TypeConverter.ToIntegerOrInfinity(fromIndex);
- if (double.IsPositiveInfinity(n))
- {
- return JsBoolean.False;
- }
- else if (double.IsNegativeInfinity(n))
- {
- n = 0;
- }
- else if (n >= 0)
- {
- k = (long) n;
- }
- else
- {
- k = len + (long) n;
- if (k < 0)
- {
- k = 0;
- }
- }
- while (k < len)
- {
- var value = o.Get((ulong) k);
- if (SameValueZeroComparer.Equals(value, searchElement))
- {
- return true;
- }
- k++;
- }
- return false;
- }
- private JsValue Some(JsValue thisObj, JsValue[] arguments)
- {
- var target = TypeConverter.ToObject(_realm, thisObj);
- return target.FindWithCallback(arguments, out _, out _, false);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.every
- /// </summary>
- private JsValue Every(JsValue thisObj, JsValue[] arguments)
- {
- var o = ArrayOperations.For(_realm, thisObj);
- ulong len = o.GetLongLength();
- if (len == 0)
- {
- return JsBoolean.True;
- }
- var callbackfn = arguments.At(0);
- var thisArg = arguments.At(1);
- var callable = GetCallable(callbackfn);
- var args = _engine._jsValueArrayPool.RentArray(3);
- args[2] = o.Target;
- for (uint k = 0; k < len; k++)
- {
- if (o.TryGetValue(k, out var kvalue))
- {
- args[0] = kvalue;
- args[1] = k;
- var testResult = callable.Call(thisArg, args);
- if (false == TypeConverter.ToBoolean(testResult))
- {
- return JsBoolean.False;
- }
- }
- }
- _engine._jsValueArrayPool.ReturnArray(args);
- return JsBoolean.True;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.indexof
- /// </summary>
- private JsValue IndexOf(JsValue thisObj, JsValue[] arguments)
- {
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLongLength();
- if (len == 0)
- {
- return -1;
- }
- var startIndex = arguments.Length > 1
- ? TypeConverter.ToIntegerOrInfinity(arguments.At(1))
- : 0;
- if (startIndex > ArrayOperations.MaxArrayLikeLength)
- {
- return JsNumber.IntegerNegativeOne;
- }
- ulong k;
- if (startIndex < 0)
- {
- var abs = System.Math.Abs(startIndex);
- ulong temp = len - (uint) abs;
- if (abs > len || temp < 0)
- {
- temp = 0;
- }
- k = temp;
- }
- else
- {
- k = (ulong) startIndex;
- }
- if (k >= len)
- {
- return -1;
- }
- ulong smallestIndex = o.GetSmallestIndex(len);
- if (smallestIndex > k)
- {
- k = smallestIndex;
- }
- var searchElement = arguments.At(0);
- for (; k < len; k++)
- {
- var kPresent = o.HasProperty(k);
- if (kPresent)
- {
- var elementK = o.Get(k);
- if (elementK == searchElement)
- {
- return k;
- }
- }
- }
- return -1;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.find
- /// </summary>
- private JsValue Find(JsValue thisObj, JsValue[] arguments)
- {
- var target = TypeConverter.ToObject(_realm, thisObj);
- target.FindWithCallback(arguments, out _, out var value, visitUnassigned: true);
- return value;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.findindex
- /// </summary>
- private JsValue FindIndex(JsValue thisObj, JsValue[] arguments)
- {
- var target = TypeConverter.ToObject(_realm, thisObj);
- if (target.FindWithCallback(arguments, out var index, out _, visitUnassigned: true))
- {
- return index;
- }
- return -1;
- }
- private JsValue FindLast(JsValue thisObj, JsValue[] arguments)
- {
- var target = TypeConverter.ToObject(_realm, thisObj);
- target.FindWithCallback(arguments, out _, out var value, visitUnassigned: true, fromEnd: true);
- return value;
- }
- private JsValue FindLastIndex(JsValue thisObj, JsValue[] arguments)
- {
- var target = TypeConverter.ToObject(_realm, thisObj);
- if (target.FindWithCallback(arguments, out var index, out _, visitUnassigned: true, fromEnd: true))
- {
- return index;
- }
- return -1;
- }
- /// <summary>
- /// https://tc39.es/proposal-relative-indexing-method/#sec-array-prototype-additions
- /// </summary>
- private JsValue At(JsValue thisObj, JsValue[] arguments)
- {
- var target = TypeConverter.ToObject(_realm, thisObj);
- var len = target.Length;
- var relativeIndex = TypeConverter.ToInteger(arguments.At(0));
- ulong actualIndex;
- if (relativeIndex < 0)
- {
- actualIndex = (ulong) (len + relativeIndex);
- }
- else
- {
- actualIndex = (ulong) relativeIndex;
- }
- if (actualIndex < 0 || actualIndex >= len)
- {
- return Undefined;
- }
- return target.Get(actualIndex);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.splice
- /// </summary>
- private JsValue Splice(JsValue thisObj, JsValue[] arguments)
- {
- var start = arguments.At(0);
- var deleteCount = arguments.At(1);
- var obj = TypeConverter.ToObject(_realm, thisObj);
- var o = ArrayOperations.For(_realm, obj);
- var len = o.GetLongLength();
- var relativeStart = TypeConverter.ToInteger(start);
- ulong actualStart;
- if (relativeStart < 0)
- {
- actualStart = (ulong) System.Math.Max(len + relativeStart, 0);
- }
- else
- {
- actualStart = (ulong) System.Math.Min(relativeStart, len);
- }
- var items = System.Array.Empty<JsValue>();
- ulong insertCount;
- ulong actualDeleteCount;
- if (arguments.Length == 0)
- {
- insertCount = 0;
- actualDeleteCount = 0;
- }
- else if (arguments.Length == 1)
- {
- insertCount = 0;
- actualDeleteCount = len - actualStart;
- }
- else
- {
- insertCount = (ulong) (arguments.Length - 2);
- var dc = TypeConverter.ToInteger(deleteCount);
- actualDeleteCount = (ulong) System.Math.Min(System.Math.Max(dc,0), len - actualStart);
- items = System.Array.Empty<JsValue>();
- if (arguments.Length > 2)
- {
- items = new JsValue[arguments.Length - 2];
- System.Array.Copy(arguments, 2, items, 0, items.Length);
- }
- }
- if (len + insertCount - actualDeleteCount > ArrayOperations.MaxArrayLikeLength)
- {
- ExceptionHelper.ThrowTypeError(_realm, "Invalid array length");
- }
- var instance = _realm.Intrinsics.Array.ArraySpeciesCreate(obj, actualDeleteCount);
- var a = ArrayOperations.For(instance);
- for (uint k = 0; k < actualDeleteCount; k++)
- {
- var index = actualStart + k;
- if (o.HasProperty(index))
- {
- var fromValue = o.Get(index);
- a.CreateDataPropertyOrThrow(k, fromValue);
- }
- }
- a.SetLength((uint) actualDeleteCount);
- var length = len - actualDeleteCount + (uint) items.Length;
- o.EnsureCapacity(length);
- if ((ulong) items.Length < actualDeleteCount)
- {
- for (ulong k = actualStart; k < len - actualDeleteCount; k++)
- {
- var from = k + actualDeleteCount;
- var to = k + (ulong) items.Length;
- if (o.HasProperty(from))
- {
- var fromValue = o.Get(from);
- o.Set(to, fromValue, throwOnError: false);
- }
- else
- {
- o.DeletePropertyOrThrow(to);
- }
- }
- for (var k = len; k > len - actualDeleteCount + (ulong) items.Length; k--)
- {
- o.DeletePropertyOrThrow(k - 1);
- }
- }
- else if ((ulong) items.Length > actualDeleteCount)
- {
- for (var k = len - actualDeleteCount; k > actualStart; k--)
- {
- var from = k + actualDeleteCount - 1;
- var to = k + (ulong) items.Length - 1;
- if (o.HasProperty(from))
- {
- var fromValue = o.Get(from);
- o.Set(to, fromValue, throwOnError: true);
- }
- else
- {
- o.DeletePropertyOrThrow(to);
- }
- }
- }
- for (uint k = 0; k < items.Length; k++)
- {
- var e = items[k];
- o.Set(k + actualStart, e, throwOnError: true);
- }
- o.SetLength(length);
- return a.Target;
- }
- /// <summary>
- /// /https://tc39.es/ecma262/#sec-array.prototype.unshift
- /// </summary>
- private JsValue Unshift(JsValue thisObj, JsValue[] arguments)
- {
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLongLength();
- var argCount = (uint) arguments.Length;
- if (len + argCount > ArrayOperations.MaxArrayLikeLength)
- {
- ExceptionHelper.ThrowTypeError(_realm, "Invalid array length");
- }
- o.EnsureCapacity(len + argCount);
- var minIndex = o.GetSmallestIndex(len);
- for (var k = len; k > minIndex; k--)
- {
- var from = k - 1;
- var to = k + argCount - 1;
- if (o.TryGetValue(from, out var fromValue))
- {
- o.Set(to, fromValue, updateLength: false);
- }
- else
- {
- o.DeletePropertyOrThrow(to);
- }
- }
- for (uint j = 0; j < argCount; j++)
- {
- o.Set(j, arguments[j], updateLength: false);
- }
- o.SetLength(len + argCount);
- return len + argCount;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.sort
- /// </summary>
- private JsValue Sort(JsValue thisObj, JsValue[] arguments)
- {
- var objectInstance = TypeConverter.ToObject(_realm, thisObj);
- var obj = ArrayOperations.For(objectInstance);
- var compareFn = GetCompareFunction(arguments.At(0));
- var len = obj.GetLength();
- if (len <= 1)
- {
- return obj.Target;
- }
- var items = new List<JsValue>((int) System.Math.Min(10_000, obj.GetLength()));
- for (ulong k = 0; k < len; ++k)
- {
- if (obj.TryGetValue(k, out var kValue))
- {
- items.Add(kValue);
- }
- }
- var itemCount = items.Count;
- // don't eat inner exceptions
- try
- {
- var array = items.OrderBy(x => x, ArrayComparer.WithFunction(_engine, compareFn!)).ToArray();
- uint j;
- for (j = 0; j < itemCount; ++j)
- {
- var updateLength = j == itemCount - 1;
- obj.Set(j, array[j], updateLength: updateLength, throwOnError: true);
- }
- for (; j < len; ++j)
- {
- obj.DeletePropertyOrThrow(j);
- }
- }
- catch (InvalidOperationException e)
- {
- throw e.InnerException ?? e;
- }
- return obj.Target;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.slice
- /// </summary>
- private JsValue Slice(JsValue thisObj, JsValue[] arguments)
- {
- var start = arguments.At(0);
- var end = arguments.At(1);
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLongLength();
- var relativeStart = TypeConverter.ToInteger(start);
- ulong k;
- if (relativeStart < 0)
- {
- k = (ulong) System.Math.Max(len + relativeStart, 0);
- }
- else
- {
- k = (ulong) System.Math.Min(TypeConverter.ToInteger(start), len);
- }
- ulong final;
- if (end.IsUndefined())
- {
- final = (ulong) TypeConverter.ToNumber(len);
- }
- else
- {
- double relativeEnd = TypeConverter.ToInteger(end);
- if (relativeEnd < 0)
- {
- final = (ulong) System.Math.Max(len + relativeEnd, 0);
- }
- else
- {
- final = (ulong) System.Math.Min(relativeEnd, len);
- }
- }
- if (k < final && final - k > ArrayOperations.MaxArrayLength)
- {
- ExceptionHelper.ThrowRangeError(_realm, "Invalid array length");;
- }
- var length = (uint) System.Math.Max(0, (long) final - (long) k);
- var a = _realm.Intrinsics.Array.ArraySpeciesCreate(TypeConverter.ToObject(_realm, thisObj), length);
- if (thisObj is JsArray ai && a is JsArray a2)
- {
- a2.CopyValues(ai, (uint) k, 0, length);
- }
- else
- {
- // slower path
- var operations = ArrayOperations.For(a);
- for (uint n = 0; k < final; k++, n++)
- {
- if (o.TryGetValue(k, out var kValue))
- {
- operations.CreateDataPropertyOrThrow(n, kValue);
- }
- }
- }
- return a;
- }
- private JsValue Shift(JsValue thisObj, JsValue[] arg2)
- {
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLength();
- if (len == 0)
- {
- o.SetLength(0);
- return Undefined;
- }
- var first = o.Get(0);
- for (uint k = 1; k < len; k++)
- {
- var to = k - 1;
- if (o.TryGetValue(k, out var fromVal))
- {
- o.Set(to, fromVal, throwOnError: false);
- }
- else
- {
- o.DeletePropertyOrThrow(to);
- }
- }
- o.DeletePropertyOrThrow(len - 1);
- o.SetLength(len - 1);
- return first;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.reverse
- /// </summary>
- private JsValue Reverse(JsValue thisObj, JsValue[] arguments)
- {
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLongLength();
- var middle = (ulong) System.Math.Floor(len / 2.0);
- uint lower = 0;
- while (lower != middle)
- {
- var upper = len - lower - 1;
- var lowerExists = o.HasProperty(lower);
- var lowerValue = lowerExists ? o.Get(lower) : null;
- var upperExists = o.HasProperty(upper);
- var upperValue = upperExists ? o.Get(upper) : null;
- if (lowerExists && upperExists)
- {
- o.Set(lower, upperValue!, throwOnError: true);
- o.Set(upper, lowerValue!, throwOnError: true);
- }
- if (!lowerExists && upperExists)
- {
- o.Set(lower, upperValue!, throwOnError: true);
- o.DeletePropertyOrThrow(upper);
- }
- if (lowerExists && !upperExists)
- {
- o.DeletePropertyOrThrow(lower);
- o.Set(upper, lowerValue!, throwOnError: true);
- }
- lower++;
- }
- return o.Target;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.join
- /// </summary>
- private JsValue Join(JsValue thisObj, JsValue[] arguments)
- {
- var separator = arguments.At(0);
- var o = ArrayOperations.For(_realm, thisObj);
- var len = o.GetLength();
- var sep = TypeConverter.ToString(separator.IsUndefined() ? JsString.CommaString : separator);
- // as per the spec, this has to be called after ToString(separator)
- if (len == 0)
- {
- return JsString.Empty;
- }
- static string StringFromJsValue(JsValue value)
- {
- return value.IsNullOrUndefined()
- ? ""
- : TypeConverter.ToString(value);
- }
- var s = StringFromJsValue(o.Get(0));
- if (len == 1)
- {
- return s;
- }
- using var sb = StringBuilderPool.Rent();
- sb.Builder.Append(s);
- for (uint k = 1; k < len; k++)
- {
- if (sep != "")
- {
- sb.Builder.Append(sep);
- }
- sb.Builder.Append(StringFromJsValue(o.Get(k)));
- }
- return sb.ToString();
- }
- private JsValue ToLocaleString(JsValue thisObj, JsValue[] arguments)
- {
- var array = ArrayOperations.For(_realm, thisObj);
- var len = array.GetLength();
- const string Separator = ",";
- if (len == 0)
- {
- return JsString.Empty;
- }
- JsValue r;
- if (!array.TryGetValue(0, out var firstElement) || firstElement.IsNull() || firstElement.IsUndefined())
- {
- r = JsString.Empty;
- }
- else
- {
- r = Invoke(firstElement, "toLocaleString", System.Array.Empty<JsValue>());
- }
- for (uint k = 1; k < len; k++)
- {
- var s = r + Separator;
- if (!array.TryGetValue(k, out var nextElement) || nextElement.IsNull())
- {
- r = JsString.Empty;
- }
- else
- {
- r = Invoke(nextElement, "toLocaleString", System.Array.Empty<JsValue>());
- }
- r = s + r;
- }
- return r;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.concat
- /// </summary>
- private JsValue Concat(JsValue thisObj, JsValue[] arguments)
- {
- var o = TypeConverter.ToObject(_realm, thisObj);
- var items = new List<JsValue>(arguments.Length + 1) {o};
- items.AddRange(arguments);
- uint n = 0;
- var a = _realm.Intrinsics.Array.ArraySpeciesCreate(TypeConverter.ToObject(_realm, thisObj), 0);
- var aOperations = ArrayOperations.For(a);
- for (var i = 0; i < items.Count; i++)
- {
- var e = items[i];
- if (e is ObjectInstance { IsConcatSpreadable: true } oi)
- {
- if (e is JsArray eArray && a is JsArray a2)
- {
- a2.CopyValues(eArray, 0, n, eArray.GetLength());
- n += eArray.GetLength();
- }
- else
- {
- var operations = ArrayOperations.For(oi);
- var len = operations.GetLongLength();
- if (n + len > ArrayOperations.MaxArrayLikeLength)
- {
- ExceptionHelper.ThrowTypeError(_realm, "Invalid array length");
- }
- for (uint k = 0; k < len; k++)
- {
- operations.TryGetValue(k, out var subElement);
- aOperations.CreateDataPropertyOrThrow(n, subElement);
- n++;
- }
- }
- }
- else
- {
- aOperations.CreateDataPropertyOrThrow(n, e);
- n++;
- }
- }
- // this is not in the specs, but is necessary in case the last element of the last
- // array doesn't exist, and thus the length would not be incremented
- a.DefineOwnProperty(CommonProperties.Length, new PropertyDescriptor(n, PropertyFlag.OnlyWritable));
- return a;
- }
- internal JsValue ToString(JsValue thisObj, JsValue[] arguments)
- {
- var array = TypeConverter.ToObject(_realm, thisObj);
- Func<JsValue, JsValue[], JsValue> func;
- if (array.Get("join") is ICallable joinFunc)
- {
- func = joinFunc.Call;
- }
- else
- {
- func = _realm.Intrinsics.Object.PrototypeObject.ToObjectString;
- }
- return func(array, Arguments.Empty);
- }
- private JsValue ToReversed(JsValue thisObj, JsValue[] arguments)
- {
- var o = ArrayOperations.For(TypeConverter.ToObject(_realm, thisObj));
- var len = o.GetLongLength();
- if (len == 0)
- {
- return new JsArray(_engine);
- }
- var a = CreateBackingArray(len);
- ulong k = 0;
- while (k < len)
- {
- var from = len - k - 1;
- a[k++] = o.Get(from);
- }
- return new JsArray(_engine, a);
- }
- private JsValue ToSorted(JsValue thisObj, JsValue[] arguments)
- {
- var o = ArrayOperations.For(TypeConverter.ToObject(_realm, thisObj));
- var compareFn = GetCompareFunction(arguments.At(0));
- var len = o.GetLongLength();
- ValidateArrayLength(len);
- if (len == 0)
- {
- return new JsArray(_engine);
- }
- var array = o.GetAll(skipHoles: true);
- array = SortArray(array, compareFn);
- return new JsArray(_engine, array);
- }
- private JsValue ToSpliced(JsValue thisObj, JsValue[] arguments)
- {
- var start = arguments.At(0);
- var deleteCount = arguments.At(1);
- var o = ArrayOperations.For(_realm, TypeConverter.ToObject(_realm, thisObj));
- var len = o.GetLongLength();
- var relativeStart = TypeConverter.ToIntegerOrInfinity(start);
- ulong actualStart;
- if (double.IsNegativeInfinity(relativeStart))
- {
- actualStart = 0;
- }
- else if (relativeStart < 0)
- {
- actualStart = (ulong) System.Math.Max(len + relativeStart, 0);
- }
- else
- {
- actualStart = (ulong) System.Math.Min(relativeStart, len);
- }
- var items = System.Array.Empty<JsValue>();
- ulong insertCount;
- ulong actualDeleteCount;
- if (arguments.Length == 0)
- {
- insertCount = 0;
- actualDeleteCount = 0;
- }
- else if (arguments.Length == 1)
- {
- insertCount = 0;
- actualDeleteCount = len - actualStart;
- }
- else
- {
- insertCount = (ulong) (arguments.Length - 2);
- var dc = TypeConverter.ToIntegerOrInfinity(deleteCount);
- actualDeleteCount = (ulong) System.Math.Min(System.Math.Max(dc,0), len - actualStart);
- items = System.Array.Empty<JsValue>();
- if (arguments.Length > 2)
- {
- items = new JsValue[arguments.Length - 2];
- System.Array.Copy(arguments, 2, items, 0, items.Length);
- }
- }
- var newLen = len + insertCount - actualDeleteCount;
- if (newLen > ArrayOperations.MaxArrayLikeLength)
- {
- ExceptionHelper.ThrowTypeError(_realm, "Invalid input length");
- }
- ValidateArrayLength(newLen);
- var r = actualStart + actualDeleteCount;
- var a = new JsArray(_engine, (uint) newLen);
- uint i = 0;
- while (i < actualStart)
- {
- a.SetIndexValue(i, o.Get(i), updateLength: false);
- i++;
- }
- a.SetLength((uint) actualStart);
- foreach (var item in items)
- {
- a.SetIndexValue(i++, item, updateLength: false);
- }
- while (i < newLen)
- {
- var fromValue = o.Get(r);
- a.SetIndexValue(i, fromValue, updateLength: false);
- i++;
- r++;
- }
- a.SetLength(i);
- return a;
- }
- private JsValue[] SortArray(IEnumerable<JsValue> array, ICallable? compareFn)
- {
- var comparer = ArrayComparer.WithFunction(_engine, compareFn);
- try
- {
- return array.OrderBy(x => x, comparer).ToArray();
- }
- catch (InvalidOperationException e)
- {
- throw e.InnerException ?? e;
- }
- }
- private ICallable? GetCompareFunction(JsValue compareArg)
- {
- ICallable? compareFn = null;
- if (!compareArg.IsUndefined())
- {
- if (compareArg is not ICallable callable)
- {
- ExceptionHelper.ThrowTypeError(_realm, "The comparison function must be either a function or undefined");
- return null;
- }
- compareFn = callable;
- }
- return compareFn;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.reduceright
- /// </summary>
- private JsValue ReduceRight(JsValue thisObj, JsValue[] arguments)
- {
- var callbackfn = arguments.At(0);
- var initialValue = arguments.At(1);
- var o = ArrayOperations.For(TypeConverter.ToObject(_realm, thisObj));
- var len = o.GetLongLength();
- var callable = GetCallable(callbackfn);
- if (len == 0 && arguments.Length < 2)
- {
- ExceptionHelper.ThrowTypeError(_realm);
- }
- long k = (long) (len - 1);
- JsValue accumulator = Undefined;
- if (arguments.Length > 1)
- {
- accumulator = initialValue;
- }
- else
- {
- var kPresent = false;
- while (kPresent == false && k >= 0)
- {
- if ((kPresent = o.TryGetValue((ulong) k, out var temp)))
- {
- accumulator = temp;
- }
- k--;
- }
- if (kPresent == false)
- {
- ExceptionHelper.ThrowTypeError(_realm);
- }
- }
- var jsValues = new JsValue[4];
- jsValues[3] = o.Target;
- for (; k >= 0; k--)
- {
- if (o.TryGetValue((ulong) k, out var kvalue))
- {
- jsValues[0] = accumulator;
- jsValues[1] = kvalue;
- jsValues[2] = k;
- accumulator = callable.Call(Undefined, jsValues);
- }
- }
- return accumulator;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-array.prototype.push
- /// </summary>
- public JsValue Push(JsValue thisObject, JsValue[] arguments)
- {
- if (thisObject is JsArray { CanUseFastAccess: true } arrayInstance)
- {
- return arrayInstance.Push(arguments);
- }
- var o = ArrayOperations.For(TypeConverter.ToObject(_realm, thisObject));
- var n = o.GetLongLength();
- if (n + (ulong) arguments.Length > ArrayOperations.MaxArrayLikeLength)
- {
- ExceptionHelper.ThrowTypeError(_realm, "Invalid array length");
- }
- foreach (var a in arguments)
- {
- o.Set(n, a, true);
- n++;
- }
- o.SetLength(n);
- return n;
- }
- public JsValue Pop(JsValue thisObject, JsValue[] arguments)
- {
- var o = ArrayOperations.For(_realm, thisObject);
- ulong len = o.GetLongLength();
- if (len == 0)
- {
- o.SetLength(0);
- return Undefined;
- }
- len = len - 1;
- JsValue element = o.Get(len);
- o.DeletePropertyOrThrow(len);
- o.SetLength(len);
- return element;
- }
- /// <summary>
- /// https://tc39.es/proposal-array-grouping/#sec-array.prototype.group
- /// </summary>
- private JsValue Group(JsValue thisObject, JsValue[] arguments)
- {
- var grouping = BuildArrayGrouping(thisObject, arguments, mapMode: false);
- var obj = OrdinaryObjectCreate(_engine, null);
- foreach (var pair in grouping)
- {
- obj.FastSetProperty(pair.Key, new PropertyDescriptor(pair.Value, PropertyFlag.ConfigurableEnumerableWritable));
- }
- return obj;
- }
- /// <summary>
- /// https://tc39.es/proposal-array-grouping/#sec-array.prototype.grouptomap
- /// </summary>
- private JsValue GroupToMap(JsValue thisObject, JsValue[] arguments)
- {
- var grouping = BuildArrayGrouping(thisObject, arguments, mapMode: true);
- var map = (MapInstance) Construct(_realm.Intrinsics.Map);
- foreach (var pair in grouping)
- {
- map.MapSet(pair.Key, pair.Value);
- }
- return map;
- }
- private Dictionary<JsValue, JsArray> BuildArrayGrouping(JsValue thisObject, JsValue[] arguments, bool mapMode)
- {
- var o = ArrayOperations.For(_realm, thisObject);
- var len = o.GetLongLength();
- var callbackfn = arguments.At(0);
- var callable = GetCallable(callbackfn);
- var thisArg = arguments.At(1);
- var result = new Dictionary<JsValue, JsArray>();
- var args = _engine._jsValueArrayPool.RentArray(3);
- args[2] = o.Target;
- for (uint k = 0; k < len; k++)
- {
- var kValue = o.Get(k);
- args[0] = kValue;
- args[1] = k;
- var value = callable.Call(thisArg, args);
- JsValue key;
- if (mapMode)
- {
- key = (value as JsNumber)?.IsNegativeZero() == true ? JsNumber.PositiveZero : value;
- }
- else
- {
- key = TypeConverter.ToPropertyKey(value);
- }
- if (!result.TryGetValue(key, out var list))
- {
- result[key] = list = new JsArray(_engine);
- }
- list.SetIndexValue(list.GetLength(), kValue, updateLength: true);
- }
- _engine._jsValueArrayPool.ReturnArray(args);
- return result;
- }
- private object[] CreateBackingArray(ulong length)
- {
- ValidateArrayLength(length);
- return new object[length];
- }
- private void ValidateArrayLength(ulong length)
- {
- if (length > ArrayOperations.MaxArrayLength)
- {
- ExceptionHelper.ThrowRangeError(_engine.Realm, "Invalid array length " + length);
- }
- }
- internal sealed class ArrayComparer : IComparer<JsValue>
- {
- /// <summary>
- /// Default instance without any compare function.
- /// </summary>
- public static readonly ArrayComparer Default = new(null, null);
- public static ArrayComparer WithFunction(Engine engine, ICallable? compare)
- {
- return compare is null ? Default : new ArrayComparer(engine, compare);
- }
- private readonly Engine? _engine;
- private readonly ICallable? _compare;
- private readonly JsValue[] _comparableArray = new JsValue[2];
- private ArrayComparer(Engine? engine, ICallable? compare)
- {
- _engine = engine;
- _compare = compare;
- }
- public int Compare(JsValue? x, JsValue? y)
- {
- var xIsNull = ReferenceEquals(x, null);
- var yIsNull = ReferenceEquals(y, null);
- if (xIsNull)
- {
- if (yIsNull)
- {
- return 0;
- }
- return 1;
- }
- else
- {
- if (yIsNull)
- {
- return -1;
- }
- }
- var xUndefined = x!.IsUndefined();
- var yUndefined = y!.IsUndefined();
- if (xUndefined && yUndefined)
- {
- return 0;
- }
- if (xUndefined)
- {
- return 1;
- }
- if (yUndefined)
- {
- return -1;
- }
- if (_compare != null)
- {
- _engine!.RunBeforeExecuteStatementChecks(null);
- _comparableArray[0] = x!;
- _comparableArray[1] = y!;
- var s = TypeConverter.ToNumber(_compare.Call(Undefined, _comparableArray));
- if (s < 0)
- {
- return -1;
- }
- if (s > 0)
- {
- return 1;
- }
- return 0;
- }
- var xString = TypeConverter.ToString(x!);
- var yString = TypeConverter.ToString(y!);
- return string.CompareOrdinal(xString, yString);
- }
- }
- }
- }
|