123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979 |
- using System;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Runtime.CompilerServices;
- 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.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 MruPropertyCache2<PropertyDescriptor> _intrinsicProperties;
- private MruPropertyCache2<PropertyDescriptor> _properties;
-
- private readonly string _class;
- protected readonly Engine _engine;
-
- public ObjectInstance(Engine engine) : this(engine, "Object")
- {
- _engine = engine;
- }
-
- protected ObjectInstance(Engine engine, in string objectClass) : base(Types.Object)
- {
- _engine = engine;
- _class = objectClass;
- }
- public Engine Engine => _engine;
- protected bool TryGetIntrinsicValue(JsSymbol symbol, out JsValue value)
- {
- if (_intrinsicProperties != null && _intrinsicProperties.TryGetValue(symbol.AsSymbol(), out var descriptor))
- {
- value = descriptor.Value;
- return true;
- }
- if (ReferenceEquals(Prototype, null))
- {
- value = Undefined;
- return false;
- }
- return Prototype.TryGetIntrinsicValue(symbol, out value);
- }
- public void SetIntrinsicValue(string name, JsValue value, bool writable, bool enumerable, bool configurable)
- {
- SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
- }
- protected void SetIntrinsicValue(JsSymbol symbol, JsValue value, bool writable, bool enumerable, bool configurable)
- {
- if (_intrinsicProperties == null)
- {
- _intrinsicProperties = new MruPropertyCache2<PropertyDescriptor>();
- }
- _intrinsicProperties[symbol.AsSymbol()] = new PropertyDescriptor(value, writable, enumerable, configurable);
- }
- /// <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 ref readonly string Class => ref _class;
- public virtual IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
- {
- EnsureInitialized();
- if (_properties != null)
- {
- foreach (var pair in _properties.GetEnumerator())
- {
- yield return pair;
- }
- }
- }
- protected virtual void AddProperty(string propertyName, PropertyDescriptor descriptor)
- {
- if (_properties == null)
- {
- _properties = new MruPropertyCache2<PropertyDescriptor>();
- }
- _properties.Add(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) ?? false;
- }
- 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)
- {
- if (desc == PropertyDescriptor.Undefined)
- {
- return Undefined;
- }
- if (desc.IsDataDescriptor())
- {
- var val = desc.Value;
- return val ?? 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(this, 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();
- if (_properties != null && _properties.TryGetValue(propertyName, out var x))
- {
- return x;
- }
- return PropertyDescriptor.Undefined;
- }
- protected internal virtual void SetOwnProperty(string propertyName, PropertyDescriptor desc)
- {
- EnsureInitialized();
- if (_properties == null)
- {
- _properties = new MruPropertyCache2<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;
- }
- if (ReferenceEquals(Prototype, null))
- {
- return PropertyDescriptor.Undefined;
- }
- return Prototype.GetProperty(propertyName);
- }
- 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)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- 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;
- }
- else
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- }
- /// <summary>
- /// Hint is a String. Returns a default value for the
- /// object.
- /// </summary>
- /// <param name="hint"></param>
- /// <returns></returns>
- public JsValue DefaultValue(Types hint)
- {
- EnsureInitialized();
- if (hint == Types.String || (hint == Types.None && Class == "Date"))
- {
- var toString = Get("toString").TryCast<ICallable>();
- if (toString != null)
- {
- var str = toString.Call(this, Arguments.Empty);
- if (str.IsPrimitive())
- {
- return str;
- }
- }
- var valueOf = Get("valueOf").TryCast<ICallable>();
- if (valueOf != null)
- {
- var val = valueOf.Call(this, Arguments.Empty);
- if (val.IsPrimitive())
- {
- return val;
- }
- }
- throw new JavaScriptException(Engine.TypeError);
- }
- if (hint == Types.Number || hint == Types.None)
- {
- var valueOf = Get("valueOf").TryCast<ICallable>();
- if (valueOf != null)
- {
- var val = valueOf.Call(this, Arguments.Empty);
- if (val.IsPrimitive())
- {
- return val;
- }
- }
- var toString = Get("toString").TryCast<ICallable>();
- if (toString != null)
- {
- var str = toString.Call(this, Arguments.Empty);
- if (str.IsPrimitive())
- {
- return str;
- }
- }
- throw new JavaScriptException(Engine.TypeError);
- }
- 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)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- else
- {
- if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
- {
- PropertyDescriptor propertyDescriptor;
- if (desc.Configurable && desc.Enumerable && desc.Writable)
- {
- propertyDescriptor = new PropertyDescriptor(descValue ?? Undefined, PropertyFlag.ConfigurableEnumerableWritable);
- }
- else if (!desc.Configurable && !desc.Enumerable && !desc.Writable)
- {
- 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.ConfigurableSet &&
- !current.EnumerableSet &&
- !current.WritableSet &&
- 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)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- if (desc.EnumerableSet && (desc.Enumerable != current.Enumerable))
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- }
- if (!desc.IsGenericDescriptor())
- {
- if (current.IsDataDescriptor() != desc.IsDataDescriptor())
- {
- if (!current.Configurable)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- 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)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- if (!current.Writable)
- {
- if (!ReferenceEquals(descValue, null) && !ExpressionInterpreter.SameValue(descValue, currentValue))
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- 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)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- 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.AsStringWithoutTypeCheck();
- }
- 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 ((JsNumber) 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 Dictionary<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)
- {
- uint GetLength()
- {
- var desc = GetProperty("length");
- var descValue = desc.Value;
- if (desc.IsDataDescriptor() && !ReferenceEquals(descValue, null))
- {
- return TypeConverter.ToUint32(descValue);
- }
- var getter = desc.Get ?? Undefined;
- if (getter.IsUndefined())
- {
- return 0;
- }
- // if getter is not undefined it must be ICallable
- return TypeConverter.ToUint32(((ICallable) getter).Call(this, Arguments.Empty));
- }
-
- bool TryGetValue(uint idx, out JsValue jsValue)
- {
- var property = TypeConverter.ToString(idx);
- var kPresent = HasProperty(property);
- jsValue = kPresent ? Get(property) : Undefined;
- return kPresent;
- }
- var len = GetLength();
- if (len == 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);
- for (uint k = 0; k < len; k++)
- {
- if (TryGetValue(k, out var kvalue))
- {
- args[0] = kvalue;
- args[1] = k;
- args[2] = this;
- 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;
- }
- throw new JavaScriptException(Engine.TypeError, "Argument must be callable");
- }
- 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;
- }
- internal void Clear()
- {
- _intrinsicProperties?.Clear();
- _properties?.Clear();
- }
- }
- }
|