12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007 |
- using System;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Runtime.CompilerServices;
- using Jint.Collections;
- using Jint.Native.Array;
- using Jint.Native.Boolean;
- using Jint.Native.Date;
- using Jint.Native.Function;
- using Jint.Native.Number;
- using Jint.Native.RegExp;
- using Jint.Native.String;
- using Jint.Native.Symbol;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Descriptors.Specialized;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Object
- {
- public class ObjectInstance : JsValue, IEquatable<ObjectInstance>
- {
- private static readonly string ToPrimitiveSymbolName = GlobalSymbolRegistry.ToPrimitive._value;
- internal StringDictionarySlim<PropertyDescriptor> _properties;
- private readonly string _class;
- protected readonly Engine _engine;
- public ObjectInstance(Engine engine) : this(engine, "Object")
- {
- }
- protected ObjectInstance(Engine engine, string objectClass) : base(Types.Object)
- {
- _engine = engine;
- _class = objectClass;
- }
- public Engine Engine => _engine;
- /// <summary>
- /// The prototype of this object.
- /// </summary>
- public ObjectInstance Prototype { get; set; }
- /// <summary>
- /// If true, own properties may be added to the
- /// object.
- /// </summary>
- public bool Extensible { get; set; }
- /// <summary>
- /// A String value indicating a specification defined
- /// classification of objects.
- /// </summary>
- public string Class => _class;
- public virtual IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
- {
- EnsureInitialized();
- if (_properties != null)
- {
- foreach (var pair in _properties)
- {
- yield return pair;
- }
- }
- }
- protected virtual void AddProperty(string propertyName, PropertyDescriptor descriptor)
- {
- if (_properties == null)
- {
- _properties = new StringDictionarySlim<PropertyDescriptor>();
- }
- _properties[propertyName] = descriptor;
- }
- protected virtual bool TryGetProperty(string propertyName, out PropertyDescriptor descriptor)
- {
- if (_properties == null)
- {
- descriptor = null;
- return false;
- }
- return _properties.TryGetValue(propertyName, out descriptor);
- }
- public virtual bool HasOwnProperty(string propertyName)
- {
- EnsureInitialized();
- return _properties?.ContainsKey(propertyName) == true;
- }
- public virtual void RemoveOwnProperty(string propertyName)
- {
- EnsureInitialized();
- _properties?.Remove(propertyName);
- }
- /// <summary>
- /// Returns the value of the named property.
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public virtual JsValue Get(string propertyName)
- {
- var desc = GetProperty(propertyName);
- return UnwrapJsValue(desc);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal JsValue UnwrapJsValue(PropertyDescriptor desc)
- {
- return UnwrapJsValue(desc, this);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static JsValue UnwrapJsValue(PropertyDescriptor desc, JsValue thisObject)
- {
- if (desc == PropertyDescriptor.Undefined)
- {
- return Undefined;
- }
- var value = (desc._flags & PropertyFlag.CustomJsValue) != 0
- ? desc.CustomValue
- : desc._value;
- // IsDataDescriptor inlined
- if ((desc._flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != 0
- || !ReferenceEquals(value, null))
- {
- return value ?? Undefined;
- }
- var getter = desc.Get ?? Undefined;
- if (getter.IsUndefined())
- {
- return Undefined;
- }
- // if getter is not undefined it must be ICallable
- var callable = getter.TryCast<ICallable>();
- return callable.Call(thisObject, Arguments.Empty);
- }
- /// <summary>
- /// Returns the Property Descriptor of the named
- /// own property of this object, or undefined if
- /// absent.
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public virtual PropertyDescriptor GetOwnProperty(string propertyName)
- {
- EnsureInitialized();
- PropertyDescriptor descriptor = null;
- _properties?.TryGetValue(propertyName, out descriptor);
- return descriptor ?? PropertyDescriptor.Undefined;
- }
- protected internal virtual void SetOwnProperty(string propertyName, PropertyDescriptor desc)
- {
- EnsureInitialized();
- if (_properties == null)
- {
- _properties = new StringDictionarySlim<PropertyDescriptor>();
- }
- _properties[propertyName] = desc;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public PropertyDescriptor GetProperty(string propertyName)
- {
- var prop = GetOwnProperty(propertyName);
- if (prop != PropertyDescriptor.Undefined)
- {
- return prop;
- }
- return Prototype?.GetProperty(propertyName) ?? PropertyDescriptor.Undefined;
- }
- public bool TryGetValue(string propertyName, out JsValue value)
- {
- value = Undefined;
- var desc = GetOwnProperty(propertyName);
- if (desc != null && desc != PropertyDescriptor.Undefined)
- {
- if (desc == PropertyDescriptor.Undefined)
- {
- return false;
- }
- var descValue = desc.Value;
- if (desc.WritableSet && !ReferenceEquals(descValue, null))
- {
- value = descValue;
- return true;
- }
- var getter = desc.Get ?? Undefined;
- if (getter.IsUndefined())
- {
- value = Undefined;
- return false;
- }
- // if getter is not undefined it must be ICallable
- var callable = getter.TryCast<ICallable>();
- value = callable.Call(this, Arguments.Empty);
- return true;
- }
- if (ReferenceEquals(Prototype, null))
- {
- return false;
- }
- return Prototype.TryGetValue(propertyName, out value);
- }
- /// <summary>
- /// Sets the specified named property to the value
- /// of the second parameter. The flag controls
- /// failure handling.
- /// </summary>
- /// <param name="propertyName"></param>
- /// <param name="value"></param>
- /// <param name="throwOnError"></param>
- public virtual void Put(string propertyName, JsValue value, bool throwOnError)
- {
- if (!CanPut(propertyName))
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return;
- }
- var ownDesc = GetOwnProperty(propertyName);
- if (ownDesc.IsDataDescriptor())
- {
- ownDesc.Value = value;
- return;
- // as per specification
- // var valueDesc = new PropertyDescriptor(value: value, writable: null, enumerable: null, configurable: null);
- // DefineOwnProperty(propertyName, valueDesc, throwOnError);
- // return;
- }
- // property is an accessor or inherited
- var desc = GetProperty(propertyName);
- if (desc.IsAccessorDescriptor())
- {
- var setter = desc.Set.TryCast<ICallable>();
- setter.Call(this, new[] {value});
- }
- else
- {
- var newDesc = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
- DefineOwnProperty(propertyName, newDesc, throwOnError);
- }
- }
- /// <summary>
- /// Returns a Boolean value indicating whether a
- /// [[Put]] operation with PropertyName can be
- /// performed.
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public bool CanPut(string propertyName)
- {
- var desc = GetOwnProperty(propertyName);
- if (desc != PropertyDescriptor.Undefined)
- {
- if (desc.IsAccessorDescriptor())
- {
- var set = desc.Set;
- if (ReferenceEquals(set, null) || set.IsUndefined())
- {
- return false;
- }
- return true;
- }
- return desc.Writable;
- }
- if (ReferenceEquals(Prototype, null))
- {
- return Extensible;
- }
- var inherited = Prototype.GetProperty(propertyName);
- if (inherited == PropertyDescriptor.Undefined)
- {
- return Extensible;
- }
- if (inherited.IsAccessorDescriptor())
- {
- var set = inherited.Set;
- if (ReferenceEquals(set, null) || set.IsUndefined())
- {
- return false;
- }
- return true;
- }
- if (!Extensible)
- {
- return false;
- }
- return inherited.Writable;
- }
- /// <summary>
- /// Returns a Boolean value indicating whether the
- /// object already has a property with the given
- /// name.
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool HasProperty(string propertyName)
- {
- return GetProperty(propertyName) != PropertyDescriptor.Undefined;
- }
- /// <summary>
- /// Removes the specified named own property
- /// from the object. The flag controls failure
- /// handling.
- /// </summary>
- /// <param name="propertyName"></param>
- /// <param name="throwOnError"></param>
- /// <returns></returns>
- public virtual bool Delete(string propertyName, bool throwOnError)
- {
- var desc = GetOwnProperty(propertyName);
- if (desc == PropertyDescriptor.Undefined)
- {
- return true;
- }
- if (desc.Configurable)
- {
- RemoveOwnProperty(propertyName);
- return true;
- }
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return false;
- }
- /// <summary>
- /// Hint is a String. Returns a default value for the object.
- /// </summary>
- public JsValue DefaultValue(Types hint)
- {
- EnsureInitialized();
- if (hint == Types.String || (hint == Types.None && Class == "Date"))
- {
- var jsValue = Get(ToPrimitiveSymbolName);
- if (!jsValue.IsNullOrUndefined())
- {
- if (jsValue is ICallable toPrimitive)
- {
- var str = toPrimitive.Call(this, Arguments.Empty);
- if (str.IsPrimitive())
- {
- return str;
- }
- if (str.IsObject())
- {
- return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Cannot convert object to primitive value");
- }
- }
- const string message = "'Value returned for property 'Symbol(Symbol.toPrimitive)' of object is not a function";
- return ExceptionHelper.ThrowTypeError<JsValue>(_engine, message);
- }
- if (Get("toString") is ICallable toString)
- {
- var str = toString.Call(this, Arguments.Empty);
- if (str.IsPrimitive())
- {
- return str;
- }
- }
- if (Get("valueOf") is ICallable valueOf)
- {
- var val = valueOf.Call(this, Arguments.Empty);
- if (val.IsPrimitive())
- {
- return val;
- }
- }
- ExceptionHelper.ThrowTypeError(Engine);
- }
- if (hint == Types.Number || hint == Types.None)
- {
- var jsValue = Get(ToPrimitiveSymbolName);
- if (!jsValue.IsNullOrUndefined())
- {
- if (jsValue is ICallable toPrimitive)
- {
- var val = toPrimitive.Call(this, Arguments.Empty);
- if (val.IsPrimitive())
- {
- return val;
- }
- if (val.IsObject())
- {
- return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Cannot convert object to primitive value");
- }
- }
- const string message = "'Value returned for property 'Symbol(Symbol.toPrimitive)' of object is not a function";
- return ExceptionHelper.ThrowTypeError<JsValue>(_engine, message);
- }
- if (Get("valueOf") is ICallable valueOf)
- {
- var val = valueOf.Call(this, Arguments.Empty);
- if (val.IsPrimitive())
- {
- return val;
- }
- }
- if (Get("toString") is ICallable toString)
- {
- var str = toString.Call(this, Arguments.Empty);
- if (str.IsPrimitive())
- {
- return str;
- }
- }
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return ToString();
- }
- /// <summary>
- /// Creates or alters the named own property to
- /// have the state described by a Property
- /// Descriptor. The flag controls failure handling.
- /// </summary>
- /// <param name="propertyName"></param>
- /// <param name="desc"></param>
- /// <param name="throwOnError"></param>
- /// <returns></returns>
- public virtual bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
- {
- var current = GetOwnProperty(propertyName);
- if (current == desc)
- {
- return true;
- }
- var descValue = desc.Value;
- if (current == PropertyDescriptor.Undefined)
- {
- if (!Extensible)
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return false;
- }
- else
- {
- if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
- {
- PropertyDescriptor propertyDescriptor;
- if ((desc._flags & PropertyFlag.ConfigurableEnumerableWritable) == PropertyFlag.ConfigurableEnumerableWritable)
- {
- propertyDescriptor = new PropertyDescriptor(descValue ?? Undefined, PropertyFlag.ConfigurableEnumerableWritable);
- }
- else if ((desc._flags & PropertyFlag.ConfigurableEnumerableWritable) == 0)
- {
- propertyDescriptor = new PropertyDescriptor(descValue ?? Undefined, PropertyFlag.AllForbidden);
- }
- else
- {
- propertyDescriptor = new PropertyDescriptor(desc)
- {
- Value = descValue ?? Undefined
- };
- }
- SetOwnProperty(propertyName, propertyDescriptor);
- }
- else
- {
- SetOwnProperty(propertyName, new GetSetPropertyDescriptor(desc));
- }
- }
- return true;
- }
- // Step 5
- var currentGet = current.Get;
- var currentSet = current.Set;
- var currentValue = current.Value;
- if ((current._flags & PropertyFlag.ConfigurableSet | PropertyFlag.EnumerableSet | PropertyFlag.WritableSet) == 0 &&
- ReferenceEquals(currentGet, null) &&
- ReferenceEquals(currentSet, null) &&
- ReferenceEquals(currentValue, null))
- {
- return true;
- }
- // Step 6
- var descGet = desc.Get;
- var descSet = desc.Set;
- if (
- current.Configurable == desc.Configurable && current.ConfigurableSet == desc.ConfigurableSet &&
- current.Writable == desc.Writable && current.WritableSet == desc.WritableSet &&
- current.Enumerable == desc.Enumerable && current.EnumerableSet == desc.EnumerableSet &&
- ((ReferenceEquals(currentGet, null) && ReferenceEquals(descGet, null)) || (!ReferenceEquals(currentGet, null) && !ReferenceEquals(descGet, null) && ExpressionInterpreter.SameValue(currentGet, descGet))) &&
- ((ReferenceEquals(currentSet, null) && ReferenceEquals(descSet, null)) || (!ReferenceEquals(currentSet, null) && !ReferenceEquals(descSet, null) && ExpressionInterpreter.SameValue(currentSet, descSet))) &&
- ((ReferenceEquals(currentValue, null) && ReferenceEquals(descValue, null)) || (!ReferenceEquals(currentValue, null) && !ReferenceEquals(descValue, null) && ExpressionInterpreter.StrictlyEqual(currentValue, descValue)))
- )
- {
- return true;
- }
- if (!current.Configurable)
- {
- if (desc.Configurable)
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return false;
- }
- if (desc.EnumerableSet && (desc.Enumerable != current.Enumerable))
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return false;
- }
- }
- if (!desc.IsGenericDescriptor())
- {
- if (current.IsDataDescriptor() != desc.IsDataDescriptor())
- {
- if (!current.Configurable)
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return false;
- }
- if (current.IsDataDescriptor())
- {
- var flags = current.Flags & ~(PropertyFlag.Writable | PropertyFlag.WritableSet);
- SetOwnProperty(propertyName, current = new GetSetPropertyDescriptor(
- get: JsValue.Undefined,
- set: JsValue.Undefined,
- flags
- ));
- }
- else
- {
- var flags = current.Flags & ~(PropertyFlag.Writable | PropertyFlag.WritableSet);
- SetOwnProperty(propertyName, current = new PropertyDescriptor(
- value: JsValue.Undefined,
- flags
- ));
- }
- }
- else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
- {
- if (!current.Configurable)
- {
- if (!current.Writable && desc.Writable)
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return false;
- }
- if (!current.Writable)
- {
- if (!ReferenceEquals(descValue, null) && !ExpressionInterpreter.SameValue(descValue, currentValue))
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return false;
- }
- }
- }
- }
- else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
- {
- if (!current.Configurable)
- {
- if ((!ReferenceEquals(descSet, null) && !ExpressionInterpreter.SameValue(descSet, currentSet ?? Undefined))
- ||
- (!ReferenceEquals(descGet, null) && !ExpressionInterpreter.SameValue(descGet, currentGet ?? Undefined)))
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return false;
- }
- }
- }
- }
- if (!ReferenceEquals(descValue, null))
- {
- current.Value = descValue;
- }
- if (desc.WritableSet)
- {
- current.Writable = desc.Writable;
- }
- if (desc.EnumerableSet)
- {
- current.Enumerable = desc.Enumerable;
- }
- if (desc.ConfigurableSet)
- {
- current.Configurable = desc.Configurable;
- }
- PropertyDescriptor mutable = null;
- if (!ReferenceEquals(descGet, null))
- {
- mutable = new GetSetPropertyDescriptor(mutable ?? current);
- ((GetSetPropertyDescriptor) mutable).SetGet(descGet);
- }
- if (!ReferenceEquals(descSet, null))
- {
- mutable = new GetSetPropertyDescriptor(mutable ?? current);
- ((GetSetPropertyDescriptor) mutable).SetSet(descSet);
- }
- if (mutable != null)
- {
- // replace old with new type that supports get and set
- FastSetProperty(propertyName, mutable);
- }
- return true;
- }
- /// <summary>
- /// Optimized version of [[Put]] when the property is known to be undeclared already
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <param name="writable"></param>
- /// <param name="configurable"></param>
- /// <param name="enumerable"></param>
- public void FastAddProperty(string name, JsValue value, bool writable, bool enumerable, bool configurable)
- {
- SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
- }
- /// <summary>
- /// Optimized version of [[Put]] when the property is known to be already declared
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- public void FastSetProperty(string name, PropertyDescriptor value)
- {
- SetOwnProperty(name, value);
- }
- protected virtual void EnsureInitialized()
- {
- }
- public override string ToString()
- {
- return TypeConverter.ToString(this);
- }
- public override object ToObject()
- {
- if (this is IObjectWrapper wrapper)
- {
- return wrapper.Target;
- }
- switch (Class)
- {
- case "Array":
- if (this is ArrayInstance arrayInstance)
- {
- var len = TypeConverter.ToInt32(arrayInstance.Get("length"));
- var result = new object[len];
- for (var k = 0; k < len; k++)
- {
- var pk = TypeConverter.ToString(k);
- var kpresent = arrayInstance.HasProperty(pk);
- if (kpresent)
- {
- var kvalue = arrayInstance.Get(pk);
- result[k] = kvalue.ToObject();
- }
- else
- {
- result[k] = null;
- }
- }
- return result;
- }
- break;
- case "String":
- if (this is StringInstance stringInstance)
- {
- return stringInstance.PrimitiveValue.ToString();
- }
- break;
- case "Date":
- if (this is DateInstance dateInstance)
- {
- return dateInstance.ToDateTime();
- }
- break;
- case "Boolean":
- if (this is BooleanInstance booleanInstance)
- {
- return ((JsBoolean) booleanInstance.PrimitiveValue)._value
- ? JsBoolean.BoxedTrue
- : JsBoolean.BoxedFalse;
- }
- break;
- case "Function":
- if (this is FunctionInstance function)
- {
- return (Func<JsValue, JsValue[], JsValue>) function.Call;
- }
- break;
- case "Number":
- if (this is NumberInstance numberInstance)
- {
- return numberInstance.NumberData._value;
- }
- break;
- case "RegExp":
- if (this is RegExpInstance regeExpInstance)
- {
- return regeExpInstance.Value;
- }
- break;
- case "Arguments":
- case "Object":
- #if __IOS__
- IDictionary<string, object> o = new DictionarySlim<string, object>();
- #else
- IDictionary<string, object> o = new ExpandoObject();
- #endif
- foreach (var p in GetOwnProperties())
- {
- if (!p.Value.Enumerable)
- {
- continue;
- }
- o.Add(p.Key, Get(p.Key).ToObject());
- }
- return o;
- }
- return this;
- }
- /// <summary>
- /// Handles the generic find of (callback[, thisArg])
- /// </summary>
- internal virtual bool FindWithCallback(
- JsValue[] arguments,
- out uint index,
- out JsValue value,
- bool visitUnassigned)
- {
- long GetLength()
- {
- var desc = GetProperty("length");
- var descValue = desc.Value;
- double len;
- if (desc.IsDataDescriptor() && !ReferenceEquals(descValue, null))
- {
- len = TypeConverter.ToNumber(descValue);
- }
- else
- {
- var getter = desc.Get ?? Undefined;
- if (getter.IsUndefined())
- {
- len = 0;
- }
- else
- {
- // if getter is not undefined it must be ICallable
- len = TypeConverter.ToNumber(((ICallable) getter).Call(this, Arguments.Empty));
- }
- }
- return (long) System.Math.Max(
- 0,
- System.Math.Min(len, ArrayPrototype.ArrayOperations.MaxArrayLikeLength));
- }
- bool TryGetValue(uint idx, out JsValue jsValue)
- {
- var property = TypeConverter.ToString(idx);
- var kPresent = HasProperty(property);
- jsValue = kPresent ? Get(property) : Undefined;
- return kPresent;
- }
- if (GetLength() == 0)
- {
- index = 0;
- value = Undefined;
- return false;
- }
-
- var callbackfn = arguments.At(0);
- var thisArg = arguments.At(1);
- var callable = GetCallable(callbackfn);
- var args = _engine._jsValueArrayPool.RentArray(3);
- args[2] = this;
- var length = GetLength();
- for (uint k = 0; k < length; k++)
- {
- if (TryGetValue(k, out var kvalue) || visitUnassigned)
- {
- args[0] = kvalue;
- args[1] = k;
- var testResult = callable.Call(thisArg, args);
- if (TypeConverter.ToBoolean(testResult))
- {
- index = k;
- value = kvalue;
- return true;
- }
- }
- }
- _engine._jsValueArrayPool.ReturnArray(args);
- index = 0;
- value = Undefined;
- return false;
- }
- protected ICallable GetCallable(JsValue source)
- {
- if (source is ICallable callable)
- {
- return callable;
- }
- ExceptionHelper.ThrowTypeError(_engine, "Argument must be callable");
- return null;
- }
- internal virtual bool IsConcatSpreadable => TryGetIsConcatSpreadable(out var isConcatSpreadable) && isConcatSpreadable;
- internal virtual bool IsArrayLike => TryGetValue("length", out var lengthValue)
- && lengthValue.IsNumber()
- && ((JsNumber) lengthValue)._value >= 0;
- protected bool TryGetIsConcatSpreadable(out bool isConcatSpreadable)
- {
- isConcatSpreadable = false;
- if (TryGetValue(GlobalSymbolRegistry.IsConcatSpreadable._value, out var isConcatSpreadableValue)
- && !ReferenceEquals(isConcatSpreadableValue, null)
- && !isConcatSpreadableValue.IsUndefined())
- {
- isConcatSpreadable = TypeConverter.ToBoolean(isConcatSpreadableValue);
- return true;
- }
- return false;
- }
- public override bool Equals(JsValue obj)
- {
- if (ReferenceEquals(null, obj))
- {
- return false;
- }
- if (!(obj is ObjectInstance s))
- {
- return false;
- }
- return Equals(s);
- }
- public bool Equals(ObjectInstance other)
- {
- if (ReferenceEquals(null, other))
- {
- return false;
- }
- if (ReferenceEquals(this, other))
- {
- return true;
- }
- return false;
- }
- }
- }
|