using System.Collections.Generic; using Jint.Native.Errors; using Jint.Runtime.Descriptors; namespace Jint.Native.Object { public class ObjectInstance { public ObjectInstance(ObjectInstance prototype) { Properties = new Dictionary(); Extensible = true; Prototype = prototype; DefineOwnProperty("prototype", new DataDescriptor(prototype), false); } public IDictionary Properties { get; private set; } /// /// The prototype of this object. /// public ObjectInstance Prototype { get; private set; } /// /// If true, own properties may be added to the /// object. /// public bool Extensible { get; set; } /// /// A String value indicating a specification defined /// classification of objects. /// public virtual string Class { get { return "Object"; } } /// /// Returns the value of the named property. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3 /// /// /// public object Get(string propertyName) { var desc = GetProperty(propertyName); if (desc == Undefined.Instance) { return Undefined.Instance; } return ((PropertyDescriptor)desc).Get(); } public void Set(string name, object value) { if (!HasProperty(name)) { DefineOwnProperty(name, new DataDescriptor(value), false); } else { Put(name, value, false); } } /// /// 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 /// /// /// public object GetOwnProperty(string propertyName) { PropertyDescriptor value; if (Properties.TryGetValue(propertyName, out value)) { return value; } return Undefined.Instance; } /// /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2 /// /// /// public object GetProperty(string propertyName) { var prop = GetOwnProperty(propertyName); if (prop != Undefined.Instance) { return prop; } if(Prototype == null) { return Undefined.Instance; } return Prototype.GetProperty(propertyName); } /// /// Sets the specified named property to the value /// of the second parameter. The flag controls /// failure handling. /// /// /// /// public void Put(string propertyName, object value, bool throwOnError) { if (!CanPut(propertyName)) { if (throwOnError) { throw new TypeError(); } return; } var ownDesc = (PropertyDescriptor)GetOwnProperty(propertyName); if (ownDesc.IsDataDescriptor()) { ownDesc.Set(value); return; } var desc = (PropertyDescriptor)GetProperty(propertyName); if (desc.IsAccessorDescriptor()) { desc.Set(value); } else { var newDesc = new DataDescriptor(value) {Writable = true, Enumerable = true, Configurable = true}; DefineOwnProperty(propertyName, newDesc, throwOnError); } } /// /// 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 /// /// /// public bool CanPut(string propertyName) { var desc = GetOwnProperty(propertyName); var pd = desc as PropertyDescriptor; if (desc != Undefined.Instance) { if (pd.IsAccessorDescriptor()) { return true; } else { return pd.Writable; } } if (Prototype == null) { return Extensible; } var inherited = (PropertyDescriptor)Prototype.GetProperty(propertyName); if (inherited == Undefined.Instance) { return Prototype.Extensible; } if (inherited.IsAccessorDescriptor()) { return true; } if (pd.IsAccessorDescriptor()) { return true; } else { if (!Extensible) { return false; } else { return inherited.Writable; } } } /// /// Returns a Boolean value indicating whether the /// object already has a property with the given /// name. /// /// /// public bool HasProperty(string propertyName) { return GetProperty(propertyName) != Undefined.Instance; } /// /// Removes the specified named own property /// from the object. The flag controls failure /// handling. /// /// /// /// public bool Delete(string propertyName, bool throwOnError) { var desc = GetOwnProperty(propertyName); var pd = desc as PropertyDescriptor; if (desc == Undefined.Instance) { return true; } if (pd.Configurable) { Properties.Remove(propertyName); return true; } else { if (throwOnError) { throw new TypeError(); } return false; } } /// /// Hint is a String. Returns a default value for the /// object. /// /// /// public object DefaultValue(string hintError) { return ToString(); } /// /// Creates or alters the named own property to /// have the state described by a Property /// Descriptor. The flag controls failure handling. /// /// /// /// /// public bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError) { var property = GetOwnProperty(propertyName); if (property == Undefined.Instance) { if (!Extensible) { if (throwOnError) { throw new TypeError(); } return false; } else { Properties.Add(propertyName, desc); } return true; } var current = (PropertyDescriptor)property; if (!current.Configurable) { if (desc.Configurable) { if (throwOnError) { throw new TypeError(); } return false; } if (desc.Enumerable != current.Enumerable) { if (throwOnError) { throw new TypeError(); } return false; } } /// todo: complete this implementation Properties[propertyName] = desc; return true; } } }