12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.Contracts;
- using System.Dynamic;
- 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 const string PropertyNamePrototype = "prototype";
- private const string PropertyNameConstructor = "constructor";
- private const string PropertyNameLength = "length";
- private Dictionary<string, IPropertyDescriptor> _intrinsicProperties;
- private MruPropertyCache2<string, IPropertyDescriptor> _properties;
- private IPropertyDescriptor _prototype;
- private IPropertyDescriptor _constructor;
- private IPropertyDescriptor _length;
- public ObjectInstance(Engine engine)
- {
- Engine = engine;
- }
- public Engine Engine { get; set; }
- protected bool TryGetIntrinsicValue(JsSymbol symbol, out JsValue value)
- {
- IPropertyDescriptor descriptor;
- if (_intrinsicProperties != null && _intrinsicProperties.TryGetValue(symbol.AsSymbol(), out descriptor))
- {
- value = descriptor.Value;
- return true;
- }
- if (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 Dictionary<string, IPropertyDescriptor>();
- }
- _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 virtual string Class => "Object";
- public virtual IEnumerable<KeyValuePair<string, IPropertyDescriptor>> GetOwnProperties()
- {
- EnsureInitialized();
- if (_prototype != null)
- {
- yield return new KeyValuePair<string, IPropertyDescriptor>(PropertyNamePrototype, _prototype);
- }
- if (_constructor != null)
- {
- yield return new KeyValuePair<string, IPropertyDescriptor>(PropertyNameConstructor, _constructor);
- }
- if (_length != null)
- {
- yield return new KeyValuePair<string, IPropertyDescriptor>(PropertyNameLength, _length);
- }
- if (_properties != null)
- {
- foreach (var pair in _properties.GetEnumerator())
- {
- yield return pair;
- }
- }
- }
- protected void AddProperty(string propertyName, IPropertyDescriptor descriptor)
- {
- if (propertyName == PropertyNamePrototype)
- {
- _prototype = descriptor;
- return;
- }
- if (propertyName == PropertyNameConstructor)
- {
- _constructor = descriptor;
- return;
- }
- if (propertyName == PropertyNameLength)
- {
- _length = descriptor;
- return;
- }
- if (_properties == null)
- {
- _properties = new MruPropertyCache2<string, IPropertyDescriptor>();
- }
- _properties.Add(propertyName, descriptor);
- }
- protected bool TryGetProperty(string propertyName, out IPropertyDescriptor descriptor)
- {
- if (propertyName == PropertyNamePrototype)
- {
- descriptor = _prototype;
- return _prototype != null;
- }
- if (propertyName == PropertyNameConstructor)
- {
- descriptor = _constructor;
- return _constructor != null;
- }
- if (propertyName == PropertyNameLength)
- {
- descriptor = _length;
- return _length != null;
- }
- if (_properties == null)
- {
- descriptor = null;
- return false;
- }
- return _properties.TryGetValue(propertyName, out descriptor);
- }
- public virtual bool HasOwnProperty(string propertyName)
- {
- EnsureInitialized();
- if (propertyName == PropertyNamePrototype)
- {
- return _prototype != null;
- }
- if (propertyName == PropertyNameConstructor)
- {
- return _constructor != null;
- }
- if (propertyName == PropertyNameLength)
- {
- return _length != null;
- }
- return _properties?.ContainsKey(propertyName) ?? false;
- }
- public virtual void RemoveOwnProperty(string propertyName)
- {
- EnsureInitialized();
- if (propertyName == PropertyNamePrototype)
- {
- _prototype = null;
- }
- if (propertyName == PropertyNameConstructor)
- {
- _constructor = null;
- }
- if (propertyName == PropertyNameLength)
- {
- _length = null;
- }
- _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);
- if (desc == PropertyDescriptor.Undefined)
- {
- return Undefined;
- }
- if (desc.IsDataDescriptor())
- {
- var val = desc.Value;
- return val != null ? val : Undefined;
- }
- var getter = desc.Get != null ? desc.Get : Undefined;
- if (getter.IsUndefined())
- {
- return JsValue.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 IPropertyDescriptor GetOwnProperty(string propertyName)
- {
- EnsureInitialized();
- if (propertyName == PropertyNamePrototype)
- {
- return _prototype ?? PropertyDescriptor.Undefined;
- }
- if (propertyName == PropertyNameConstructor)
- {
- return _constructor ?? PropertyDescriptor.Undefined;
- }
- if (propertyName == PropertyNameLength)
- {
- return _length ?? PropertyDescriptor.Undefined;
- }
- if (_properties != null && _properties.TryGetValue(propertyName, out var x))
- {
- return x;
- }
- return PropertyDescriptor.Undefined;
- }
- protected internal virtual void SetOwnProperty(string propertyName, IPropertyDescriptor desc)
- {
- EnsureInitialized();
- if (propertyName == PropertyNamePrototype)
- {
- _prototype = desc;
- return;
- }
- if (propertyName == PropertyNameConstructor)
- {
- _constructor = desc;
- return;
- }
- if (propertyName == PropertyNameLength)
- {
- _length = desc;
- return;
- }
- if (_properties == null)
- {
- _properties = new MruPropertyCache2<string, IPropertyDescriptor>();
- }
- _properties[propertyName] = desc;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public IPropertyDescriptor GetProperty(string propertyName)
- {
- var prop = GetOwnProperty(propertyName);
- if (prop != PropertyDescriptor.Undefined)
- {
- return prop;
- }
- if (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;
- }
- if (desc.IsDataDescriptor() && desc.Value != null)
- {
- value = desc.Value;
- return true;
- }
- var getter = desc.Get != null ? 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 (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 ConfigurableEnumerableWritablePropertyDescriptor(value);
- 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())
- {
- if (desc.Set == null || desc.Set.IsUndefined())
- {
- return false;
- }
- return true;
- }
- return desc.Writable.HasValue && desc.Writable.Value;
- }
- if (Prototype == null)
- {
- return Extensible;
- }
- var inherited = Prototype.GetProperty(propertyName);
- if (inherited == PropertyDescriptor.Undefined)
- {
- return Extensible;
- }
- if (inherited.IsAccessorDescriptor())
- {
- if (inherited.Set == null || inherited.Set.IsUndefined())
- {
- return false;
- }
- return true;
- }
- if (!Extensible)
- {
- return false;
- }
- else
- {
- return inherited.Writable.HasValue && inherited.Writable.Value;
- }
- }
- /// <summary>
- /// Returns a Boolean value indicating whether the
- /// object already has a property with the given
- /// name.
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- 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.HasValue && desc.Configurable.Value)
- {
- 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, IPropertyDescriptor desc, bool throwOnError)
- {
- var current = GetOwnProperty(propertyName);
- if (current == desc)
- {
- return true;
- }
- if (current == PropertyDescriptor.Undefined)
- {
- if (!Extensible)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- else
- {
- if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
- {
- IPropertyDescriptor propertyDescriptor;
- if (desc.Configurable.GetValueOrDefault() && desc.Enumerable.GetValueOrDefault() && desc.Writable.GetValueOrDefault())
- {
- propertyDescriptor = new ConfigurableEnumerableWritablePropertyDescriptor(desc.Value != null ? desc.Value : Undefined);
- }
- else if (!desc.Configurable.GetValueOrDefault() && !desc.Enumerable.GetValueOrDefault() && !desc.Writable.GetValueOrDefault())
- {
- propertyDescriptor = new AllForbiddenPropertyDescriptor(desc.Value != null ? desc.Value : Undefined);
- }
- else
- {
- propertyDescriptor = new PropertyDescriptor(desc)
- {
- Value = desc.Value != null ? desc.Value : Undefined,
- Writable = desc.Writable.HasValue ? desc.Writable.Value : false,
- Enumerable = desc.Enumerable.HasValue ? desc.Enumerable.Value : false,
- Configurable = desc.Configurable.HasValue ? desc.Configurable.Value : false
- };
- }
- SetOwnProperty(propertyName, propertyDescriptor);
- }
- else
- {
- SetOwnProperty(propertyName, new PropertyDescriptor(desc)
- {
- Get = desc.Get,
- Set = desc.Set,
- Enumerable = desc.Enumerable.HasValue ? desc.Enumerable : false,
- Configurable = desc.Configurable.HasValue ? desc.Configurable : false,
- });
- }
- }
- return true;
- }
- // Step 5
- if (!current.Configurable.HasValue &&
- !current.Enumerable.HasValue &&
- !current.Writable.HasValue &&
- current.Get == null &&
- current.Set == null &&
- current.Value == null)
- {
- return true;
- }
- // Step 6
- if (
- current.Configurable == desc.Configurable &&
- current.Writable == desc.Writable &&
- current.Enumerable == desc.Enumerable &&
- ((current.Get == null && desc.Get == null) || (current.Get != null && desc.Get != null && ExpressionInterpreter.SameValue(current.Get, desc.Get))) &&
- ((current.Set == null && desc.Set == null) || (current.Set != null && desc.Set != null && ExpressionInterpreter.SameValue(current.Set, desc.Set))) &&
- ((current.Value == null && desc.Value == null) || (current.Value != null && desc.Value != null && ExpressionInterpreter.StrictlyEqual(current.Value, desc.Value)))
- )
- {
- return true;
- }
- if (!current.Configurable.HasValue || !current.Configurable.Value)
- {
- if (desc.Configurable.HasValue && desc.Configurable.Value)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- if (desc.Enumerable.HasValue && (!current.Enumerable.HasValue || desc.Enumerable.Value != current.Enumerable.Value))
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- }
- if (!desc.IsGenericDescriptor())
- {
- if (current.IsDataDescriptor() != desc.IsDataDescriptor())
- {
- if (!current.Configurable.HasValue || !current.Configurable.Value)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- if (current.IsDataDescriptor())
- {
- SetOwnProperty(propertyName, current = new PropertyDescriptor(
- get: JsValue.Undefined,
- set: JsValue.Undefined,
- enumerable: current.Enumerable,
- configurable: current.Configurable
- ));
- }
- else
- {
- SetOwnProperty(propertyName, current = new PropertyDescriptor(
- value: JsValue.Undefined,
- writable: null,
- enumerable: current.Enumerable,
- configurable: current.Configurable
- ));
- }
- }
- else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
- {
- if (!current.Configurable.HasValue || current.Configurable.Value == false)
- {
- if (!current.Writable.HasValue || !current.Writable.Value && desc.Writable.HasValue && desc.Writable.Value)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- if (!current.Writable.Value)
- {
- if (desc.Value != null && !ExpressionInterpreter.SameValue(desc.Value, current.Value))
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- }
- }
- }
- else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
- {
- if (!current.Configurable.HasValue || !current.Configurable.Value)
- {
- if ((desc.Set != null && !ExpressionInterpreter.SameValue(desc.Set, current.Set != null ? current.Set : Undefined))
- ||
- (desc.Get != null && !ExpressionInterpreter.SameValue(desc.Get, current.Get != null ? current.Get : Undefined)))
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- }
- }
- }
- if (desc.Value != null)
- {
- current.Value = desc.Value;
- }
- PropertyDescriptor mutable = null;
- if (desc.Writable.HasValue)
- {
- current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
- mutable.Writable = desc.Writable;
- }
- if (desc.Enumerable.HasValue)
- {
- current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
- mutable.Enumerable = desc.Enumerable;
- }
- if (desc.Configurable.HasValue)
- {
- current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
- mutable.Configurable = desc.Configurable;
- }
- if (desc.Get != null)
- {
- current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
- mutable.Get = desc.Get;
- }
- if (desc.Set != null)
- {
- mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
- mutable.Set = desc.Set;
- }
- if (mutable != null)
- {
- 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, IPropertyDescriptor value)
- {
- SetOwnProperty(name, value);
- }
- protected virtual void EnsureInitialized()
- {
- }
- public override string ToString()
- {
- return TypeConverter.ToString(this);
- }
- protected uint GetLengthValue() => TypeConverter.ToUint32(_length.Value);
- public override Types Type => Types.Object;
- [Pure]
- public override bool IsArray()
- {
- return this is ArrayInstance;
- }
- [Pure]
- public override bool IsDate()
- {
- return this is DateInstance;
- }
- [Pure]
- public override bool IsRegExp()
- {
- return this is RegExpInstance;
- }
- [Pure]
- public override ObjectInstance AsObject()
- {
- return this;
- }
- [Pure]
- public override TInstance AsInstance<TInstance>()
- {
- return this as TInstance;
- }
- [Pure]
- public override ArrayInstance AsArray()
- {
- if (!IsArray())
- {
- throw new ArgumentException("The value is not an array");
- }
- return this as ArrayInstance;
- }
- [Pure]
- public override DateInstance AsDate()
- {
- if (!IsDate())
- {
- throw new ArgumentException("The value is not a date");
- }
- return this as DateInstance;
- }
- [Pure]
- public override RegExpInstance AsRegExp()
- {
- if (!IsRegExp())
- {
- throw new ArgumentException("The value is not a regex");
- }
- return this as RegExpInstance;
- }
- public override bool Is<T>()
- {
- return this is T;
- }
- public override T As<T>()
- {
- return this as T;
- }
- 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.AsString();
- }
- break;
- case "Date":
- if (this is DateInstance dateInstance)
- {
- return dateInstance.ToDateTime();
- }
- break;
- case "Boolean":
- if (this is BooleanInstance booleanInstance)
- {
- return booleanInstance.PrimitiveValue.AsBoolean();
- }
- 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.AsNumber();
- }
- 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.HasValue || p.Value.Enumerable.Value == false)
- {
- continue;
- }
- o.Add(p.Key, Get(p.Key).ToObject());
- }
- return o;
- }
- return this;
- }
- 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;
- }
- }
- }
|