ObjectInstance.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using System;
  2. using System.Collections.Generic;
  3. using Jint.Native.Errors;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. namespace Jint.Native.Object
  7. {
  8. public class ObjectInstance
  9. {
  10. public ObjectInstance(ObjectInstance prototype)
  11. {
  12. Properties = new SortedList<string, PropertyDescriptor>();
  13. Extensible = true;
  14. Prototype = prototype;
  15. DefineOwnProperty("prototype", new DataDescriptor(prototype), false);
  16. }
  17. public IDictionary<string, PropertyDescriptor> Properties { get; private set; }
  18. /// <summary>
  19. /// The prototype of this object.
  20. /// </summary>
  21. public ObjectInstance Prototype { get; private set; }
  22. /// <summary>
  23. /// If true, own properties may be added to the
  24. /// object.
  25. /// </summary>
  26. public bool Extensible { get; set; }
  27. /// <summary>
  28. /// A String value indicating a specification defined
  29. /// classification of objects.
  30. /// </summary>
  31. public virtual string Class
  32. {
  33. get { return "Object"; }
  34. }
  35. /// <summary>
  36. /// Returns the value of the named property.
  37. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3
  38. /// </summary>
  39. /// <param name="propertyName"></param>
  40. /// <returns></returns>
  41. public object Get(string propertyName)
  42. {
  43. var desc = GetProperty(propertyName);
  44. if (desc == Undefined.Instance)
  45. {
  46. return Undefined.Instance;
  47. }
  48. return desc.Get();
  49. }
  50. public void Set(string name, object value)
  51. {
  52. if (!HasProperty(name))
  53. {
  54. DefineOwnProperty(name, new DataDescriptor(value), false);
  55. }
  56. else
  57. {
  58. Put(name, value, false);
  59. }
  60. }
  61. /// <summary>
  62. /// Returns the Property Descriptor of the named
  63. /// own property of this object, or undefined if
  64. /// absent.
  65. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  66. /// </summary>
  67. /// <param name="propertyName"></param>
  68. /// <returns></returns>
  69. public dynamic GetOwnProperty(string propertyName)
  70. {
  71. PropertyDescriptor value;
  72. if (Properties.TryGetValue(propertyName, out value))
  73. {
  74. return value;
  75. }
  76. return Undefined.Instance;
  77. }
  78. /// <summary>
  79. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  80. /// </summary>
  81. /// <param name="propertyName"></param>
  82. /// <returns></returns>
  83. public dynamic GetProperty(string propertyName)
  84. {
  85. var prop = GetOwnProperty(propertyName);
  86. if (prop != Undefined.Instance)
  87. {
  88. return prop;
  89. }
  90. if(Prototype == null)
  91. {
  92. return Undefined.Instance;
  93. }
  94. return Prototype.GetProperty(propertyName);
  95. }
  96. /// <summary>
  97. /// Sets the specified named property to the value
  98. /// of the second parameter. The flag controls
  99. /// failure handling.
  100. /// </summary>
  101. /// <param name="propertyName"></param>
  102. /// <param name="value"></param>
  103. /// <param name="throwOnError"></param>
  104. public void Put(string propertyName, object value, bool throwOnError)
  105. {
  106. if (!CanPut(propertyName))
  107. {
  108. if (throwOnError)
  109. {
  110. throw new TypeError();
  111. }
  112. return;
  113. }
  114. PropertyDescriptor ownDesc = GetOwnProperty(propertyName);
  115. if (ownDesc.IsDataDescriptor())
  116. {
  117. ownDesc.Set(value);
  118. return;
  119. }
  120. var desc = GetProperty(propertyName);
  121. if (desc.IsAccessorDescriptor())
  122. {
  123. desc.Set(value);
  124. }
  125. else
  126. {
  127. var newDesc = new DataDescriptor(value) {Writable = true, Enumerable = true, Configurable = true};
  128. DefineOwnProperty(propertyName, newDesc, throwOnError);
  129. }
  130. }
  131. /// <summary>
  132. /// Returns a Boolean value indicating whether a
  133. /// [[Put]] operation with PropertyName can be
  134. /// performed.
  135. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  136. /// </summary>
  137. /// <param name="propertyName"></param>
  138. /// <returns></returns>
  139. public bool CanPut(string propertyName)
  140. {
  141. var desc = GetOwnProperty(propertyName);
  142. if (desc != Undefined.Instance)
  143. {
  144. if (desc.IsAccessorDescriptor())
  145. {
  146. return true;
  147. }
  148. else
  149. {
  150. return desc.Writable;
  151. }
  152. }
  153. if (Prototype == null)
  154. {
  155. return Extensible;
  156. }
  157. var inherited = Prototype.GetProperty(propertyName);
  158. if (inherited == Undefined.Instance)
  159. {
  160. return Prototype.Extensible;
  161. }
  162. if (inherited.IsAccessorDescriptor())
  163. {
  164. return true;
  165. }
  166. if (desc.IsAccessorDescriptor())
  167. {
  168. return true;
  169. }
  170. else
  171. {
  172. if (!Extensible)
  173. {
  174. return false;
  175. }
  176. else
  177. {
  178. return inherited.Writable;
  179. }
  180. }
  181. }
  182. /// <summary>
  183. /// Returns a Boolean value indicating whether the
  184. /// object already has a property with the given
  185. /// name.
  186. /// </summary>
  187. /// <param name="propertyName"></param>
  188. /// <returns></returns>
  189. public bool HasProperty(string propertyName)
  190. {
  191. return GetProperty(propertyName) != Undefined.Instance;
  192. }
  193. /// <summary>
  194. /// Removes the specified named own property
  195. /// from the object. The flag controls failure
  196. /// handling.
  197. /// </summary>
  198. /// <param name="propertyName"></param>
  199. /// <param name="throwOnError"></param>
  200. /// <returns></returns>
  201. public bool Delete(string propertyName, bool throwOnError)
  202. {
  203. var desc = GetOwnProperty(propertyName);
  204. if (desc == Undefined.Instance)
  205. {
  206. return true;
  207. }
  208. if (desc.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. PropertyDescriptor current = 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. }