123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Dynamic;
- using System.Linq;
- 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.Interop;
- namespace Jint.Native.Object
- {
- public class ObjectInstance : JsValue, IEquatable<ObjectInstance>
- {
- private bool _initialized;
- private readonly ObjectClass _class;
- internal PropertyDictionary _properties;
- internal SymbolDictionary _symbols;
- internal ObjectInstance _prototype;
- protected readonly Engine _engine;
- public ObjectInstance(Engine engine) : this(engine, ObjectClass.Object)
- {
- }
- internal ObjectInstance(Engine engine, ObjectClass objectClass, InternalTypes type = InternalTypes.Object)
- : base(type)
- {
- _engine = engine;
- _class = objectClass;
- // if engine is ready, we can take default prototype for object
- _prototype = engine.Realm.Intrinsics?.Object?.PrototypeObject;
- Extensible = true;
- }
- public Engine Engine
- {
- [DebuggerStepThrough]
- get => _engine;
- }
- /// <summary>
- /// The prototype of this object.
- /// </summary>
- public ObjectInstance Prototype
- {
- [DebuggerStepThrough]
- get => GetPrototypeOf();
- }
- /// <summary>
- /// If true, own properties may be added to the
- /// object.
- /// </summary>
- public virtual bool Extensible { get; private set; }
- internal PropertyDictionary Properties
- {
- [DebuggerStepThrough]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => _properties;
- }
- /// <summary>
- /// A value indicating a specification defined classification of objects.
- /// </summary>
- internal ObjectClass Class
- {
- [DebuggerStepThrough]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => _class;
- }
- public JsValue this[JsValue property] => Get(property);
- /// <summary>
- /// https://tc39.es/ecma262/#sec-construct
- /// </summary>
- internal static ObjectInstance Construct(IConstructor f, JsValue[] argumentsList = null, IConstructor newTarget = null)
- {
- newTarget ??= f;
- argumentsList ??= System.Array.Empty<JsValue>();
- return f.Construct(argumentsList, (JsValue) newTarget);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-speciesconstructor
- /// </summary>
- internal static IConstructor SpeciesConstructor(ObjectInstance o, IConstructor defaultConstructor)
- {
- var c = o.Get(CommonProperties.Constructor);
- if (c.IsUndefined())
- {
- return defaultConstructor;
- }
- var oi = c as ObjectInstance;
- if (oi is null)
- {
- ExceptionHelper.ThrowTypeError(o._engine.Realm);
- }
- var s = oi.Get(GlobalSymbolRegistry.Species);
- if (s.IsNullOrUndefined())
- {
- return defaultConstructor;
- }
- if (s.IsConstructor)
- {
- return (IConstructor) s;
- }
- ExceptionHelper.ThrowTypeError(o._engine.Realm);
- return null;
- }
- internal void SetProperties(PropertyDictionary properties)
- {
- if (properties != null)
- {
- properties.CheckExistingKeys = true;
- }
- _properties = properties;
- }
- internal void SetSymbols(SymbolDictionary symbols)
- {
- _symbols = symbols;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal void SetProperty(JsValue property, PropertyDescriptor value)
- {
- if (property is JsString jsString)
- {
- SetProperty(jsString.ToString(), value);
- }
- else
- {
- SetPropertyUnlikely(property, value);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal void SetProperty(string property, PropertyDescriptor value)
- {
- Key key = property;
- SetProperty(key, value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal void SetProperty(Key property, PropertyDescriptor value)
- {
- _properties ??= new PropertyDictionary();
- _properties[property] = value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal void SetDataProperty(string property, JsValue value)
- {
- _properties ??= new PropertyDictionary();
- _properties[property] = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- private void SetPropertyUnlikely(JsValue property, PropertyDescriptor value)
- {
- var propertyKey = TypeConverter.ToPropertyKey(property);
- if (!property.IsSymbol())
- {
- _properties ??= new PropertyDictionary();
- _properties[TypeConverter.ToString(propertyKey)] = value;
- }
- else
- {
- _symbols ??= new SymbolDictionary();
- _symbols[(JsSymbol) propertyKey] = value;
- }
- }
- internal void ClearProperties()
- {
- _properties?.Clear();
- _symbols?.Clear();
- }
- public virtual IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
- {
- EnsureInitialized();
- if (_properties != null)
- {
- foreach (var pair in _properties)
- {
- yield return new KeyValuePair<JsValue, PropertyDescriptor>(new JsString(pair.Key), pair.Value);
- }
- }
- if (_symbols != null)
- {
- foreach (var pair in _symbols)
- {
- yield return new KeyValuePair<JsValue, PropertyDescriptor>(pair.Key, pair.Value);
- }
- }
- }
- public virtual List<JsValue> GetOwnPropertyKeys(Types types = Types.String | Types.Symbol)
- {
- EnsureInitialized();
- var propertyKeys = new List<JsValue>();
- if ((types & Types.String) != 0)
- {
- propertyKeys.AddRange(GetInitialOwnStringPropertyKeys());
- }
- var keys = new List<JsValue>(_properties?.Count ?? 0 + _symbols?.Count ?? 0 + propertyKeys.Count);
- List<JsValue> symbolKeys = null;
- if ((types & Types.String) != 0 && _properties != null)
- {
- foreach (var pair in _properties)
- {
- var isArrayIndex = ulong.TryParse(pair.Key, out var index) && index < ArrayOperations.MaxArrayLength;
- if (isArrayIndex)
- {
- keys.Add(JsString.Create(index));
- }
- else
- {
- propertyKeys.Add(new JsString(pair.Key));
- }
- }
- }
- keys.Sort((v1, v2) => TypeConverter.ToNumber(v1).CompareTo(TypeConverter.ToNumber(v2)));
- keys.AddRange(propertyKeys);
- if ((types & Types.Symbol) != 0 && _symbols != null)
- {
- foreach (var pair in _symbols)
- {
- symbolKeys ??= new List<JsValue>();
- symbolKeys.Add(pair.Key);
- }
- }
- if (symbolKeys != null)
- {
- keys.AddRange(symbolKeys);
- }
- return keys;
- }
- internal virtual IEnumerable<JsValue> GetInitialOwnStringPropertyKeys() => Enumerable.Empty<JsValue>();
- protected virtual void AddProperty(JsValue property, PropertyDescriptor descriptor)
- {
- SetProperty(property, descriptor);
- }
- protected virtual bool TryGetProperty(JsValue property, out PropertyDescriptor descriptor)
- {
- descriptor = null;
- var key = TypeConverter.ToPropertyKey(property);
- if (!key.IsSymbol())
- {
- return _properties?.TryGetValue(TypeConverter.ToString(key), out descriptor) == true;
- }
- return _symbols?.TryGetValue((JsSymbol) key, out descriptor) == true;
- }
- public override bool HasOwnProperty(JsValue property)
- {
- EnsureInitialized();
- var key = TypeConverter.ToPropertyKey(property);
- if (!key.IsSymbol())
- {
- return _properties?.ContainsKey(TypeConverter.ToString(key)) == true;
- }
- return _symbols?.ContainsKey((JsSymbol) key) == true;
- }
- public virtual void RemoveOwnProperty(JsValue property)
- {
- EnsureInitialized();
- var key = TypeConverter.ToPropertyKey(property);
- if (!key.IsSymbol())
- {
- _properties?.Remove(TypeConverter.ToString(key));
- return;
- }
- _symbols?.Remove((JsSymbol) key);
- }
- public override JsValue Get(JsValue property, JsValue receiver)
- {
- var desc = GetOwnProperty(property);
- if (desc != PropertyDescriptor.Undefined)
- {
- return UnwrapJsValue(desc, receiver);
- }
- return Prototype?.Get(property, receiver) ?? Undefined;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal JsValue UnwrapJsValue(PropertyDescriptor desc)
- {
- return UnwrapJsValue(desc, this);
- }
- internal static JsValue UnwrapJsValue(PropertyDescriptor desc, JsValue thisObject)
- {
- var value = (desc._flags & PropertyFlag.CustomJsValue) != 0
- ? desc.CustomValue
- : desc._value;
- // IsDataDescriptor inlined
- if ((desc._flags & (PropertyFlag.WritableSet | PropertyFlag.Writable)) != 0 || value is not null)
- {
- return value ?? Undefined;
- }
- return UnwrapFromGetter(desc, thisObject);
- }
- /// <summary>
- /// A rarer case.
- /// </summary>
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static JsValue UnwrapFromGetter(PropertyDescriptor desc, JsValue thisObject)
- {
- var getter = desc.Get ?? Undefined;
- if (getter.IsUndefined())
- {
- return Undefined;
- }
- var functionInstance = (FunctionInstance) getter;
- return functionInstance._engine.Call(functionInstance, thisObject, Arguments.Empty, expression: null);
- }
- /// <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>
- public virtual PropertyDescriptor GetOwnProperty(JsValue property)
- {
- EnsureInitialized();
- PropertyDescriptor descriptor = null;
- var key = TypeConverter.ToPropertyKey(property);
- if (!key.IsSymbol())
- {
- _properties?.TryGetValue(TypeConverter.ToString(key), out descriptor);
- }
- else
- {
- _symbols?.TryGetValue((JsSymbol) key, out descriptor);
- }
- return descriptor ?? PropertyDescriptor.Undefined;
- }
- protected internal virtual void SetOwnProperty(JsValue property, PropertyDescriptor desc)
- {
- EnsureInitialized();
- SetProperty(property, desc);
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
- /// </summary>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public PropertyDescriptor GetProperty(JsValue property)
- {
- var prop = GetOwnProperty(property);
- if (prop != PropertyDescriptor.Undefined)
- {
- return prop;
- }
- return Prototype?.GetProperty(property) ?? PropertyDescriptor.Undefined;
- }
- public bool TryGetValue(JsValue property, out JsValue value)
- {
- value = Undefined;
- var desc = GetOwnProperty(property);
- 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(property, out value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool Set(JsValue p, JsValue v, bool throwOnError)
- {
- if (!Set(p, v, this) && throwOnError)
- {
- ExceptionHelper.ThrowTypeError(_engine.Realm);
- }
- return true;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool Set(JsValue property, JsValue value)
- {
- return Set(property, value, this);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-ordinarysetwithowndescriptor
- /// </summary>
- public override bool Set(JsValue property, JsValue value, JsValue receiver)
- {
- var ownDesc = GetOwnProperty(property);
- if (ownDesc == PropertyDescriptor.Undefined)
- {
- var parent = GetPrototypeOf();
- if (parent is not null)
- {
- return parent.Set(property, value, receiver);
- }
- ownDesc = new PropertyDescriptor(Undefined, PropertyFlag.ConfigurableEnumerableWritable);
- }
- if (ownDesc.IsDataDescriptor())
- {
- if (!ownDesc.Writable)
- {
- return false;
- }
- if (receiver is not ObjectInstance oi)
- {
- return false;
- }
- var existingDescriptor = oi.GetOwnProperty(property);
- if (existingDescriptor != PropertyDescriptor.Undefined)
- {
- if (existingDescriptor.IsAccessorDescriptor())
- {
- return false;
- }
- if (!existingDescriptor.Writable)
- {
- return false;
- }
- var valueDesc = new PropertyDescriptor(value, PropertyFlag.None);
- return oi.DefineOwnProperty(property, valueDesc);
- }
- else
- {
- return oi.CreateDataProperty(property, value);
- }
- }
- if (ownDesc.Set is not FunctionInstance setter)
- {
- return false;
- }
- _engine.Call(setter, receiver, new[] { value }, expression: null);
- return true;
- }
- /// <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>
- public bool CanPut(JsValue property)
- {
- var desc = GetOwnProperty(property);
- 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(property);
- 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>
- /// https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-hasproperty-p
- /// </summary>
- public virtual bool HasProperty(JsValue property)
- {
- var hasOwn = GetOwnProperty(property);
- if (hasOwn != PropertyDescriptor.Undefined)
- {
- return true;
- }
- var parent = GetPrototypeOf();
- if (parent != null)
- {
- return parent.HasProperty(property);
- }
- return false;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-deletepropertyorthrow
- /// </summary>
- public bool DeletePropertyOrThrow(JsValue property)
- {
- if (!Delete(property))
- {
- ExceptionHelper.ThrowTypeError(_engine.Realm);
- }
- return true;
- }
- /// <summary>
- /// Removes the specified named own property
- /// from the object. The flag controls failure
- /// handling.
- /// </summary>
- public virtual bool Delete(JsValue property)
- {
- var desc = GetOwnProperty(property);
- if (desc == PropertyDescriptor.Undefined)
- {
- return true;
- }
- if (desc.Configurable)
- {
- RemoveOwnProperty(property);
- return true;
- }
- return false;
- }
- public bool DefinePropertyOrThrow(JsValue property, PropertyDescriptor desc)
- {
- if (!DefineOwnProperty(property, desc))
- {
- ExceptionHelper.ThrowTypeError(_engine.Realm, "Cannot redefine property: " + property);
- }
- return true;
- }
- /// <summary>
- /// Creates or alters the named own property to
- /// have the state described by a Property
- /// Descriptor. The flag controls failure handling.
- /// </summary>
- public virtual bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
- {
- var current = GetOwnProperty(property);
- var extensible = Extensible;
- if (current == desc)
- {
- return true;
- }
- return ValidateAndApplyPropertyDescriptor(this, property, extensible, desc, current);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-validateandapplypropertydescriptor
- /// </summary>
- protected static bool ValidateAndApplyPropertyDescriptor(ObjectInstance o, JsValue property, bool extensible, PropertyDescriptor desc, PropertyDescriptor current)
- {
- var descValue = desc.Value;
- if (current == PropertyDescriptor.Undefined)
- {
- if (!extensible)
- {
- return false;
- }
- if (o is object)
- {
- 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
- };
- }
- o.SetOwnProperty(property, propertyDescriptor);
- }
- else
- {
- var descriptor = new GetSetPropertyDescriptor(desc.Get, desc.Set, PropertyFlag.None)
- {
- Enumerable = desc.Enumerable,
- Configurable = desc.Configurable
- };
- o.SetOwnProperty(property, descriptor);
- }
- }
- return true;
- }
- // Step 3
- var currentGet = current.Get;
- var currentSet = current.Set;
- var currentValue = current.Value;
- // 4. If every field in Desc is absent, return true.
- 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) && SameValue(currentGet, descGet))) &&
- ((ReferenceEquals(currentSet, null) && ReferenceEquals(descSet, null)) || (!ReferenceEquals(currentSet, null) && !ReferenceEquals(descSet, null) && SameValue(currentSet, descSet))) &&
- ((ReferenceEquals(currentValue, null) && ReferenceEquals(descValue, null)) || (!ReferenceEquals(currentValue, null) && !ReferenceEquals(descValue, null) && currentValue == descValue))
- )
- {
- return true;
- }
- if (!current.Configurable)
- {
- if (desc.Configurable)
- {
- return false;
- }
- if (desc.EnumerableSet && (desc.Enumerable != current.Enumerable))
- {
- return false;
- }
- }
- if (!desc.IsGenericDescriptor())
- {
- if (current.IsDataDescriptor() != desc.IsDataDescriptor())
- {
- if (!current.Configurable)
- {
- return false;
- }
- if (o is object)
- {
- var flags = current.Flags & ~(PropertyFlag.Writable | PropertyFlag.WritableSet | PropertyFlag.CustomJsValue);
- if (current.IsDataDescriptor())
- {
- o.SetOwnProperty(property, current = new GetSetPropertyDescriptor(
- get: Undefined,
- set: Undefined,
- flags
- ));
- }
- else
- {
- o.SetOwnProperty(property, current = new PropertyDescriptor(
- value: Undefined,
- flags
- ));
- }
- }
- }
- else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
- {
- if (!current.Configurable)
- {
- if (!current.Writable && desc.Writable)
- {
- return false;
- }
- if (!current.Writable)
- {
- if (!ReferenceEquals(descValue, null) && !SameValue(descValue, currentValue))
- {
- return false;
- }
- }
- }
- }
- else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
- {
- if (!current.Configurable)
- {
- if ((!ReferenceEquals(descSet, null) && !SameValue(descSet, currentSet ?? Undefined))
- ||
- (!ReferenceEquals(descGet, null) && !SameValue(descGet, currentGet ?? Undefined)))
- {
- return false;
- }
- }
- }
- }
- if (o is object)
- {
- 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
- o.FastSetProperty(property, mutable);
- }
- }
- return true;
- }
- /// <summary>
- /// Optimized version of [[Put]] when the property is known to be undeclared already
- /// </summary>
- public void FastAddProperty(JsValue 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(JsValue name, PropertyDescriptor value)
- {
- SetOwnProperty(name, value);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- protected internal void EnsureInitialized()
- {
- if (_initialized)
- {
- return;
- }
- // we need to set flag eagerly to prevent wrong recursion
- _initialized = true;
- Initialize();
- }
- protected virtual void Initialize()
- {
- }
- public override object ToObject()
- {
- return ToObject(new ObjectTraverseStack(_engine));
- }
- private object ToObject(ObjectTraverseStack stack)
- {
- if (this is IObjectWrapper wrapper)
- {
- return wrapper.Target;
- }
- stack.Enter(this);
- object converted = null;
- switch (Class)
- {
- case ObjectClass.Array:
- if (this is ArrayInstance arrayInstance)
- {
- var result = new object[arrayInstance.Length];
- for (uint i = 0; i < result.Length; i++)
- {
- var value = arrayInstance[i];
- object valueToSet = null;
- if (!value.IsUndefined())
- {
- valueToSet = value is ObjectInstance oi
- ? oi.ToObject(stack)
- : value.ToObject();
- }
- result[i] = valueToSet;
- }
- converted = result;
- }
- break;
- case ObjectClass.String:
- if (this is StringInstance stringInstance)
- {
- converted = stringInstance.StringData.ToString();
- }
- break;
- case ObjectClass.Date:
- if (this is DateInstance dateInstance)
- {
- converted = dateInstance.ToDateTime();
- }
- break;
- case ObjectClass.Boolean:
- if (this is BooleanInstance booleanInstance)
- {
- converted = ((JsBoolean) booleanInstance.BooleanData)._value
- ? JsBoolean.BoxedTrue
- : JsBoolean.BoxedFalse;
- }
- break;
- case ObjectClass.Function:
- if (this is ICallable function)
- {
- converted = (Func<JsValue, JsValue[], JsValue>) function.Call;
- }
- break;
- case ObjectClass.Number:
- if (this is NumberInstance numberInstance)
- {
- converted = numberInstance.NumberData._value;
- }
- break;
- case ObjectClass.RegExp:
- if (this is RegExpInstance regeExpInstance)
- {
- converted = regeExpInstance.Value;
- }
- break;
- case ObjectClass.Arguments:
- case ObjectClass.Object:
- var o = _engine.Options.Interop.CreateClrObject(this);
- foreach (var p in GetOwnProperties())
- {
- if (!p.Value.Enumerable)
- {
- continue;
- }
- var key = p.Key.ToString();
- var propertyValue = Get(p.Key);
- var value = propertyValue is ObjectInstance oi
- ? oi.ToObject(stack)
- : propertyValue.ToObject();
- o.Add(key, value);
- }
- converted = o;
- break;
- default:
- converted = this;
- break;
- }
- stack.Exit();
- return converted;
- }
- /// <summary>
- /// Handles the generic find of (callback[, thisArg])
- /// </summary>
- internal virtual bool FindWithCallback(
- JsValue[] arguments,
- out uint index,
- out JsValue value,
- bool visitUnassigned,
- bool fromEnd = false)
- {
- long GetLength()
- {
- var desc = GetProperty(CommonProperties.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, ArrayOperations.MaxArrayLikeLength));
- }
- bool TryGetValue(uint idx, out JsValue jsValue)
- {
- var property = JsString.Create(idx);
- var kPresent = HasProperty(property);
- jsValue = kPresent ? Get(property, this) : 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;
- }
- internal ICallable GetCallable(JsValue source)
- {
- if (source is ICallable callable)
- {
- return callable;
- }
- ExceptionHelper.ThrowTypeError(_engine.Realm, "Argument must be callable");
- return null;
- }
- internal bool IsConcatSpreadable
- {
- get
- {
- var spreadable = Get(GlobalSymbolRegistry.IsConcatSpreadable, this);
- if (!spreadable.IsUndefined())
- {
- return TypeConverter.ToBoolean(spreadable);
- }
- return IsArray();
- }
- }
- public virtual bool IsArrayLike => TryGetValue(CommonProperties.Length, out var lengthValue)
- && lengthValue.IsNumber()
- && ((JsNumber) lengthValue)._value >= 0;
- // safe default
- internal virtual bool HasOriginalIterator => false;
- internal override bool IsIntegerIndexedArray => false;
- public virtual uint Length => (uint) TypeConverter.ToLength(Get(CommonProperties.Length));
- public virtual bool PreventExtensions()
- {
- Extensible = false;
- return true;
- }
- protected internal virtual ObjectInstance GetPrototypeOf()
- {
- return _prototype;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-ordinarysetprototypeof
- /// </summary>
- public virtual bool SetPrototypeOf(JsValue value)
- {
- if (!value.IsObject() && !value.IsNull())
- {
- ExceptionHelper.ThrowArgumentException();
- }
- var current = _prototype ?? Null;
- if (ReferenceEquals(value, current))
- {
- return true;
- }
- if (!Extensible)
- {
- return false;
- }
- if (value.IsNull())
- {
- _prototype = null;
- return true;
- }
- // validate chain
- var p = value as ObjectInstance;
- bool done = false;
- while (!done)
- {
- if (p is null)
- {
- done = true;
- }
- else if (ReferenceEquals(p, this))
- {
- return false;
- }
- else
- {
- p = p._prototype;
- }
- }
- _prototype = value as ObjectInstance;
- return true;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-setfunctionname
- /// </summary>
- internal void SetFunctionName(JsValue name, string prefix = null)
- {
- if (name is JsSymbol symbol)
- {
- name = symbol._value.IsUndefined()
- ? JsString.Empty
- : new JsString("[" + symbol._value + "]");
- }
- if (!string.IsNullOrWhiteSpace(prefix))
- {
- name = prefix + " " + name;
- }
- DefinePropertyOrThrow(CommonProperties.Name, new PropertyDescriptor(name, PropertyFlag.Configurable));
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-createmethodproperty
- /// </summary>
- internal virtual bool CreateMethodProperty(JsValue p, JsValue v)
- {
- var newDesc = new PropertyDescriptor(v, PropertyFlag.NonEnumerable);
- return DefineOwnProperty(p, newDesc);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-createdatapropertyorthrow
- /// </summary>
- internal bool CreateDataProperty(JsValue p, JsValue v)
- {
- var newDesc = new PropertyDescriptor(v, PropertyFlag.ConfigurableEnumerableWritable);
- return DefineOwnProperty(p, newDesc);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-createdataproperty
- /// </summary>
- internal bool CreateDataPropertyOrThrow(JsValue p, JsValue v)
- {
- if (!CreateDataProperty(p, v))
- {
- ExceptionHelper.ThrowTypeError(_engine.Realm);
- }
- return true;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal ICallable GetMethod(JsValue property)
- {
- return GetMethod(_engine.Realm, this, property);
- }
- internal static ICallable GetMethod(Realm realm, JsValue v, JsValue p)
- {
- var jsValue = v.Get(p);
- if (jsValue.IsNullOrUndefined())
- {
- return null;
- }
- var callable = jsValue as ICallable;
- if (callable is null)
- {
- ExceptionHelper.ThrowTypeError(realm, "Value returned for property '" + p + "' of object is not a function");
- }
- return callable;
- }
- internal void CopyDataProperties(
- ObjectInstance target,
- HashSet<JsValue> excludedItems)
- {
- var keys = GetOwnPropertyKeys();
- for (var i = 0; i < keys.Count; i++)
- {
- var key = keys[i];
- if (excludedItems == null || !excludedItems.Contains(key))
- {
- var desc = GetOwnProperty(key);
- if (desc.Enumerable)
- {
- target.CreateDataProperty(key, UnwrapJsValue(desc, this));
- }
- }
- }
- }
- internal ArrayInstance EnumerableOwnPropertyNames(EnumerableOwnPropertyNamesKind kind)
- {
- var ownKeys = GetOwnPropertyKeys(Types.String);
- var array = Engine.Realm.Intrinsics.Array.ArrayCreate((uint) ownKeys.Count);
- uint index = 0;
- for (var i = 0; i < ownKeys.Count; i++)
- {
- var property = ownKeys[i];
- if (!property.IsString())
- {
- continue;
- }
- var desc = GetOwnProperty(property);
- if (desc != PropertyDescriptor.Undefined && desc.Enumerable)
- {
- if (kind == EnumerableOwnPropertyNamesKind.Key)
- {
- array.SetIndexValue(index, property, updateLength: false);
- }
- else
- {
- var value = Get(property);
- if (kind == EnumerableOwnPropertyNamesKind.Value)
- {
- array.SetIndexValue(index, value, updateLength: false);
- }
- else
- {
- var objectInstance = _engine.Realm.Intrinsics.Array.ArrayCreate(2);
- objectInstance.SetIndexValue(0, property, updateLength: false);
- objectInstance.SetIndexValue(1, value, updateLength: false);
- array.SetIndexValue(index, objectInstance, updateLength: false);
- }
- }
- index++;
- }
- }
- array.SetLength(index);
- return array;
- }
- internal enum EnumerableOwnPropertyNamesKind
- {
- Key,
- Value,
- KeyValue
- }
- internal ObjectInstance AssertThisIsObjectInstance(JsValue value, string methodName)
- {
- var instance = value as ObjectInstance;
- if (instance is null)
- {
- ThrowIncompatibleReceiver(value, methodName);
- }
- return instance;
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- private void ThrowIncompatibleReceiver(JsValue value, string methodName)
- {
- ExceptionHelper.ThrowTypeError(_engine.Realm, $"Method {methodName} called on incompatible receiver {value}");
- }
- public override bool Equals(JsValue obj)
- {
- return Equals(obj as ObjectInstance);
- }
- public bool Equals(ObjectInstance other)
- {
- if (other is null)
- {
- return false;
- }
- if (ReferenceEquals(this, other))
- {
- return true;
- }
- return false;
- }
- public override string ToString()
- {
- return TypeConverter.ToString(this);
- }
- }
- }
|