ObjectInstance.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System.Collections.Generic;
  2. using Jint.Native.Errors;
  3. using Jint.Runtime.Descriptors;
  4. namespace Jint.Native.Object
  5. {
  6. public class ObjectInstance
  7. {
  8. public ObjectInstance(ObjectInstance prototype)
  9. {
  10. Properties = new Dictionary<string, PropertyDescriptor>();
  11. Extensible = true;
  12. Prototype = prototype;
  13. DefineOwnProperty("prototype", new DataDescriptor(prototype), false);
  14. }
  15. public IDictionary<string, PropertyDescriptor> Properties { get; private set; }
  16. /// <summary>
  17. /// The prototype of this object.
  18. /// </summary>
  19. public ObjectInstance Prototype { get; private set; }
  20. /// <summary>
  21. /// If true, own properties may be added to the
  22. /// object.
  23. /// </summary>
  24. public bool Extensible { get; set; }
  25. /// <summary>
  26. /// A String value indicating a specification defined
  27. /// classification of objects.
  28. /// </summary>
  29. public virtual string Class
  30. {
  31. get { return "Object"; }
  32. }
  33. /// <summary>
  34. /// Returns the value of the named property.
  35. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3
  36. /// </summary>
  37. /// <param name="propertyName"></param>
  38. /// <returns></returns>
  39. public object Get(string propertyName)
  40. {
  41. var desc = GetProperty(propertyName);
  42. if (desc == Undefined.Instance)
  43. {
  44. return Undefined.Instance;
  45. }
  46. return ((PropertyDescriptor)desc).Get();
  47. }
  48. public void Set(string name, object value)
  49. {
  50. if (!HasProperty(name))
  51. {
  52. DefineOwnProperty(name, new DataDescriptor(value), false);
  53. }
  54. else
  55. {
  56. Put(name, value, false);
  57. }
  58. }
  59. /// <summary>
  60. /// Returns the Property Descriptor of the named
  61. /// own property of this object, or undefined if
  62. /// absent.
  63. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  64. /// </summary>
  65. /// <param name="propertyName"></param>
  66. /// <returns></returns>
  67. public object GetOwnProperty(string propertyName)
  68. {
  69. PropertyDescriptor value;
  70. if (Properties.TryGetValue(propertyName, out value))
  71. {
  72. return value;
  73. }
  74. return Undefined.Instance;
  75. }
  76. /// <summary>
  77. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  78. /// </summary>
  79. /// <param name="propertyName"></param>
  80. /// <returns></returns>
  81. public object GetProperty(string propertyName)
  82. {
  83. var prop = GetOwnProperty(propertyName);
  84. if (prop != Undefined.Instance)
  85. {
  86. return prop;
  87. }
  88. if(Prototype == null)
  89. {
  90. return Undefined.Instance;
  91. }
  92. return Prototype.GetProperty(propertyName);
  93. }
  94. /// <summary>
  95. /// Sets the specified named property to the value
  96. /// of the second parameter. The flag controls
  97. /// failure handling.
  98. /// </summary>
  99. /// <param name="propertyName"></param>
  100. /// <param name="value"></param>
  101. /// <param name="throwOnError"></param>
  102. public void Put(string propertyName, object value, bool throwOnError)
  103. {
  104. if (!CanPut(propertyName))
  105. {
  106. if (throwOnError)
  107. {
  108. throw new TypeError();
  109. }
  110. return;
  111. }
  112. var ownDesc = (PropertyDescriptor)GetOwnProperty(propertyName);
  113. if (ownDesc.IsDataDescriptor())
  114. {
  115. ownDesc.Set(value);
  116. return;
  117. }
  118. var desc = (PropertyDescriptor)GetProperty(propertyName);
  119. if (desc.IsAccessorDescriptor())
  120. {
  121. desc.Set(value);
  122. }
  123. else
  124. {
  125. var newDesc = new DataDescriptor(value) {Writable = true, Enumerable = true, Configurable = true};
  126. DefineOwnProperty(propertyName, newDesc, throwOnError);
  127. }
  128. }
  129. /// <summary>
  130. /// Returns a Boolean value indicating whether a
  131. /// [[Put]] operation with PropertyName can be
  132. /// performed.
  133. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  134. /// </summary>
  135. /// <param name="propertyName"></param>
  136. /// <returns></returns>
  137. public bool CanPut(string propertyName)
  138. {
  139. var desc = GetOwnProperty(propertyName);
  140. var pd = desc as PropertyDescriptor;
  141. if (desc != Undefined.Instance)
  142. {
  143. if (pd.IsAccessorDescriptor())
  144. {
  145. return true;
  146. }
  147. else
  148. {
  149. return pd.Writable;
  150. }
  151. }
  152. if (Prototype == null)
  153. {
  154. return Extensible;
  155. }
  156. var inherited = (PropertyDescriptor)Prototype.GetProperty(propertyName);
  157. if (inherited == Undefined.Instance)
  158. {
  159. return Prototype.Extensible;
  160. }
  161. if (inherited.IsAccessorDescriptor())
  162. {
  163. return true;
  164. }
  165. if (pd.IsAccessorDescriptor())
  166. {
  167. return true;
  168. }
  169. else
  170. {
  171. if (!Extensible)
  172. {
  173. return false;
  174. }
  175. else
  176. {
  177. return inherited.Writable;
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// Returns a Boolean value indicating whether the
  183. /// object already has a property with the given
  184. /// name.
  185. /// </summary>
  186. /// <param name="propertyName"></param>
  187. /// <returns></returns>
  188. public bool HasProperty(string propertyName)
  189. {
  190. return GetProperty(propertyName) != Undefined.Instance;
  191. }
  192. /// <summary>
  193. /// Removes the specified named own property
  194. /// from the object. The flag controls failure
  195. /// handling.
  196. /// </summary>
  197. /// <param name="propertyName"></param>
  198. /// <param name="throwOnError"></param>
  199. /// <returns></returns>
  200. public bool Delete(string propertyName, bool throwOnError)
  201. {
  202. var desc = GetOwnProperty(propertyName);
  203. var pd = desc as PropertyDescriptor;
  204. if (desc == Undefined.Instance)
  205. {
  206. return true;
  207. }
  208. if (pd.Configurable)
  209. {
  210. Properties.Remove(propertyName);
  211. return true;
  212. }
  213. else
  214. {
  215. if (throwOnError)
  216. {
  217. throw new TypeError();
  218. }
  219. return false;
  220. }
  221. }
  222. /// <summary>
  223. /// Hint is a String. Returns a default value for the
  224. /// object.
  225. /// </summary>
  226. /// <param name="hintError"></param>
  227. /// <returns></returns>
  228. public object DefaultValue(string hintError)
  229. {
  230. return ToString();
  231. }
  232. /// <summary>
  233. /// Creates or alters the named own property to
  234. /// have the state described by a Property
  235. /// Descriptor. The flag controls failure handling.
  236. /// </summary>
  237. /// <param name="propertyName"></param>
  238. /// <param name="property"></param>
  239. /// <param name="throwOnError"></param>
  240. /// <returns></returns>
  241. public bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  242. {
  243. var property = GetOwnProperty(propertyName);
  244. if (property == Undefined.Instance)
  245. {
  246. if (!Extensible)
  247. {
  248. if (throwOnError)
  249. {
  250. throw new TypeError();
  251. }
  252. return false;
  253. }
  254. else
  255. {
  256. Properties.Add(propertyName, desc);
  257. }
  258. return true;
  259. }
  260. var current = (PropertyDescriptor)property;
  261. if (!current.Configurable)
  262. {
  263. if (desc.Configurable)
  264. {
  265. if (throwOnError)
  266. {
  267. throw new TypeError();
  268. }
  269. return false;
  270. }
  271. if (desc.Enumerable != current.Enumerable)
  272. {
  273. if (throwOnError)
  274. {
  275. throw new TypeError();
  276. }
  277. return false;
  278. }
  279. }
  280. /// todo: complete this implementation
  281. Properties[propertyName] = desc;
  282. return true;
  283. }
  284. }
  285. }