123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- using System.Collections.Generic;
- using Jint.Native.Date;
- using Jint.Native.String;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- namespace Jint.Native.Object
- {
- public class ObjectInstance
- {
- public ObjectInstance(Engine engine)
- {
- Engine = engine;
- Properties = new Dictionary<string, PropertyDescriptor>();
- }
- public Engine Engine { get; set; }
- public IDictionary<string, PropertyDescriptor> Properties { get; private set; }
- /// <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
- {
- get { return "Object"; }
- }
- /// <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 object Get(string propertyName)
- {
- var desc = GetProperty(propertyName);
- if (desc == PropertyDescriptor.Undefined)
- {
- return Undefined.Instance;
- }
- if (desc.IsDataDescriptor())
- {
- return desc.Value.Value;
- }
- var getter = desc.Get.Value;
- if (getter == Undefined.Instance || getter == null)
- {
- return Undefined.Instance;
- }
- // if getter is not undefined it must be ICallable
- var callable = (ICallable)getter;
- return callable.Call(this, Arguments.Empty);
- }
- public void Set(string name, object value)
- {
- if (!HasProperty(name))
- {
- DefineOwnProperty(name, new PropertyDescriptor(value, true, true, true), false);
- }
- else
- {
- Put(name, value, false);
- }
- }
- /// <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)
- {
- PropertyDescriptor x;
- if (Properties.TryGetValue(propertyName, out x))
- {
- /* Spec implementation
- PropertyDescriptor d;
- if (x.IsDataDescriptor())
- {
- d = new PropertyDescriptor(x.As<DataDescriptor>());
- }
- else
- {
- d = new PropertyDescriptor(x.As<AccessorDescriptor>());
- }
- return d;
- */
- // optimmized implementation
- return x;
- }
-
- return PropertyDescriptor.Undefined;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
- /// </summary>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public PropertyDescriptor GetProperty(string propertyName)
- {
- var prop = GetOwnProperty(propertyName);
- if (prop != PropertyDescriptor.Undefined)
- {
- return prop;
- }
-
- if(Prototype == null)
- {
- return PropertyDescriptor.Undefined;
- }
- return Prototype.GetProperty(propertyName);
- }
- /// <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 void Put(string propertyName, object value, bool throwOnError)
- {
- if (!CanPut(propertyName))
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return;
- }
- var ownDesc = GetOwnProperty(propertyName);
- if (ownDesc.IsDataDescriptor())
- {
- var valueDesc = new PropertyDescriptor(value, null, null, null);
- DefineOwnProperty(propertyName, valueDesc, throwOnError);
- return;
- }
- // property is an accessor or inherited
- var desc = GetProperty(propertyName);
- if (desc.IsAccessorDescriptor())
- {
- var setter = (ICallable)desc.Set.Value;
- setter.Call(this, new [] {value});
- }
- else
- {
- var newDesc = new PropertyDescriptor(value, true, true, true);
- 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.Value == Undefined.Instance)
- {
- return false;
- }
- return true;
- }
- return 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.Value == Undefined.Instance)
- {
- return false;
- }
- return true;
- }
- if (!Extensible)
- {
- return false;
- }
- else
- {
- return 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.Value)
- {
- Properties.Remove(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 object DefaultValue(Types hint)
- {
- if ((hint == Types.String) || (hint == Types.None && this is StringInstance) || this is DateInstance)
- {
- var toString = Get("toString");
- var callable = toString as ICallable;
- if (callable != null)
- {
- var str = callable.Call(this, Arguments.Empty);
- if (TypeConverter.IsPrimitiveValue(str))
- {
- return str;
- }
- }
- var valueOf = Get("valueOf");
- callable = valueOf as ICallable;
- if (callable != null)
- {
- var val = callable.Call(this, Arguments.Empty);
- if (TypeConverter.IsPrimitiveValue(val))
- {
- return val;
- }
- }
- throw new JavaScriptException(Engine.TypeError);
- }
- if ((hint == Types.Number) || (hint == Types.None))
- {
- var valueOf = Get("valueOf");
- var callable = valueOf as ICallable;
- if (callable != null)
- {
- var val = callable.Call(this, Arguments.Empty);
- if (TypeConverter.IsPrimitiveValue(val))
- {
- return val;
- }
- }
- var toString = Get("toString");
- callable = toString as ICallable;
- if (callable != null)
- {
- var str = callable.Call(this, Arguments.Empty);
- if (TypeConverter.IsPrimitiveValue(str))
- {
- 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 == PropertyDescriptor.Undefined)
- {
- if (!Extensible)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- else
- {
- if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
- {
- Properties[propertyName] = new PropertyDescriptor(desc);
- }
- else
- {
- Properties[propertyName] = new PropertyDescriptor(desc);
- }
- }
- return true;
- }
- // Step 5
- if(!current.Configurable.IsPresent && !current.Enumerable.IsPresent && !(current.IsDataDescriptor() && current.Writable.IsPresent))
- {
- if (!desc.IsDataDescriptor() || desc.Value.Value == null)
- {
- return true;
- }
- }
- // Step 6
- var configurableIsSame = current.Configurable.IsPresent
- ? desc.Configurable.IsPresent && (current.Configurable.Value == desc.Configurable.Value)
- : !desc.Configurable.IsPresent;
- var enumerableIsSame = current.Enumerable.IsPresent
- ? desc.Enumerable.IsPresent && (current.Enumerable.Value == desc.Enumerable.Value)
- : !desc.Enumerable.IsPresent;
- var writableIsSame = true;
- var valueIsSame = true;
- if (current.IsDataDescriptor() && desc.IsDataDescriptor())
- {
- var currentDataDescriptor = current;
- var descDataDescriptor = desc;
- writableIsSame = currentDataDescriptor.Writable.IsPresent
- ? descDataDescriptor.Writable.IsPresent && (currentDataDescriptor.Writable.Value == descDataDescriptor.Writable.Value)
- : !descDataDescriptor.Writable.IsPresent;
- valueIsSame = ExpressionInterpreter.SameValue(currentDataDescriptor.Value, descDataDescriptor.Value);
- }
- else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
- {
- var currentAccessorDescriptor = current;
- var descAccessorDescriptor = desc;
- valueIsSame = ExpressionInterpreter.SameValue(currentAccessorDescriptor.Get, descAccessorDescriptor.Get)
- && ExpressionInterpreter.SameValue(currentAccessorDescriptor.Set, descAccessorDescriptor.Set);
- }
- else
- {
- valueIsSame = false;
- }
- if (configurableIsSame && enumerableIsSame && writableIsSame && valueIsSame)
- {
- return true;
- }
- if (current.Configurable.Value == false)
- {
- if (desc.Configurable.Value)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- if (desc.Enumerable.IsPresent && 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.Value)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- if (current.IsDataDescriptor())
- {
- Properties[propertyName] = current = new PropertyDescriptor(
- get: Undefined.Instance,
- set: Undefined.Instance,
- enumerable: current.Enumerable.Value,
- configurable: current.Configurable.Value
- );
- }
- else
- {
- Properties[propertyName] = current = new PropertyDescriptor(
- value: Undefined.Instance,
- writable: null,
- enumerable: current.Enumerable.Value,
- configurable: current.Configurable.Value
- );
- }
- }
- else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
- {
- if (current.Configurable.Value == false)
- {
- if (!current.Writable.Value && desc.Writable.Value)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- if (!current.Writable.Value)
- {
- if (desc.Value.IsPresent && !valueIsSame)
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- }
- }
- if (desc.Writable.IsAbsent && current.Writable.IsPresent)
- {
- desc.Enumerable = current.Enumerable;
- }
- }
- else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
- {
- if (current.Configurable.Value == false)
- {
- if ((desc.Set.Value != Undefined.Instance &&
- !ExpressionInterpreter.SameValue(desc.Set.Value, current.Set.Value))
- ||
- (desc.Get.Value != Undefined.Instance &&
- !ExpressionInterpreter.SameValue(desc.Get.Value, current.Get.Value)))
- {
- if (throwOnError)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return false;
- }
- }
- }
- }
- if (desc.Value.IsPresent)
- {
- current.Value = desc.Value;
- }
- if (desc.Writable.IsPresent)
- {
- current.Writable = desc.Writable;
- }
- if (desc.Enumerable.IsPresent)
- {
- current.Enumerable = desc.Enumerable;
- }
- if (desc.Configurable.IsPresent)
- {
- current.Configurable = desc.Configurable;
- }
- if (desc.Get.IsPresent)
- {
- current.Get = desc.Get;
- }
- if (desc.Set.IsPresent)
- {
- current.Set = desc.Set;
- }
- 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, object value, bool writable, bool enumerable, bool configurable)
- {
- Properties.Add(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)
- {
- Properties[name] = value;
- }
- public override string ToString()
- {
- return TypeConverter.ToString(this);
- }
- }
- }
|