ObjectInstance.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 == PropertyDescriptor.Undefined)
  43. {
  44. return Undefined.Instance;
  45. }
  46. if (desc.IsDataDescriptor())
  47. {
  48. return desc.As<DataDescriptor>().Value;
  49. }
  50. var getter = desc.As<AccessorDescriptor>().Get;
  51. return getter.Call(this, null);
  52. }
  53. public void Set(string name, object value)
  54. {
  55. if (!HasProperty(name))
  56. {
  57. DefineOwnProperty(name, new DataDescriptor(value), false);
  58. }
  59. else
  60. {
  61. Put(name, value, false);
  62. }
  63. }
  64. /// <summary>
  65. /// Returns the Property Descriptor of the named
  66. /// own property of this object, or undefined if
  67. /// absent.
  68. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  69. /// </summary>
  70. /// <param name="propertyName"></param>
  71. /// <returns></returns>
  72. public PropertyDescriptor GetOwnProperty(string propertyName)
  73. {
  74. PropertyDescriptor x;
  75. if (Properties.TryGetValue(propertyName, out x))
  76. {
  77. PropertyDescriptor d;
  78. if (x.IsDataDescriptor())
  79. {
  80. d = new DataDescriptor(x.As<DataDescriptor>());
  81. }
  82. else
  83. {
  84. d = new AccessorDescriptor(x.As<AccessorDescriptor>());
  85. }
  86. return d;
  87. }
  88. return PropertyDescriptor.Undefined;
  89. }
  90. /// <summary>
  91. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  92. /// </summary>
  93. /// <param name="propertyName"></param>
  94. /// <returns></returns>
  95. public PropertyDescriptor GetProperty(string propertyName)
  96. {
  97. var prop = GetOwnProperty(propertyName);
  98. if (prop != PropertyDescriptor.Undefined)
  99. {
  100. return prop;
  101. }
  102. if(Prototype == null)
  103. {
  104. return PropertyDescriptor.Undefined;
  105. }
  106. return Prototype.GetProperty(propertyName);
  107. }
  108. /// <summary>
  109. /// Sets the specified named property to the value
  110. /// of the second parameter. The flag controls
  111. /// failure handling.
  112. /// </summary>
  113. /// <param name="propertyName"></param>
  114. /// <param name="value"></param>
  115. /// <param name="throwOnError"></param>
  116. public void Put(string propertyName, object value, bool throwOnError)
  117. {
  118. if (!CanPut(propertyName))
  119. {
  120. if (throwOnError)
  121. {
  122. throw new TypeError();
  123. }
  124. return;
  125. }
  126. var ownDesc = GetOwnProperty(propertyName);
  127. if (ownDesc.IsDataDescriptor())
  128. {
  129. var valueDesc = new DataDescriptor(value);
  130. DefineOwnProperty(propertyName, valueDesc, throwOnError);
  131. return;
  132. }
  133. var desc = GetProperty(propertyName);
  134. if (desc.IsAccessorDescriptor())
  135. {
  136. var setter = desc.As<AccessorDescriptor>().Set;
  137. setter.Call(this, new [] {value});
  138. }
  139. else
  140. {
  141. var newDesc = new DataDescriptor(value) {Writable = true, Enumerable = true, Configurable = true};
  142. DefineOwnProperty(propertyName, newDesc, throwOnError);
  143. }
  144. }
  145. /// <summary>
  146. /// Returns a Boolean value indicating whether a
  147. /// [[Put]] operation with PropertyName can be
  148. /// performed.
  149. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  150. /// </summary>
  151. /// <param name="propertyName"></param>
  152. /// <returns></returns>
  153. public bool CanPut(string propertyName)
  154. {
  155. var desc = GetOwnProperty(propertyName);
  156. if (desc != PropertyDescriptor.Undefined)
  157. {
  158. if (desc.IsAccessorDescriptor())
  159. {
  160. if (desc.As<AccessorDescriptor>().Set == null)
  161. {
  162. return false;
  163. }
  164. return true;
  165. }
  166. return desc.As<DataDescriptor>().Writable;
  167. }
  168. if (Prototype == null)
  169. {
  170. return Extensible;
  171. }
  172. var inherited = Prototype.GetProperty(propertyName);
  173. if (inherited == PropertyDescriptor.Undefined)
  174. {
  175. return Prototype.Extensible;
  176. }
  177. if (inherited.IsAccessorDescriptor())
  178. {
  179. if (inherited.As<AccessorDescriptor>().Set == null)
  180. {
  181. return false;
  182. }
  183. return true;
  184. }
  185. if (!Extensible)
  186. {
  187. return false;
  188. }
  189. else
  190. {
  191. return inherited.As<DataDescriptor>().Writable;
  192. }
  193. }
  194. /// <summary>
  195. /// Returns a Boolean value indicating whether the
  196. /// object already has a property with the given
  197. /// name.
  198. /// </summary>
  199. /// <param name="propertyName"></param>
  200. /// <returns></returns>
  201. public bool HasProperty(string propertyName)
  202. {
  203. return GetProperty(propertyName) != PropertyDescriptor.Undefined;
  204. }
  205. /// <summary>
  206. /// Removes the specified named own property
  207. /// from the object. The flag controls failure
  208. /// handling.
  209. /// </summary>
  210. /// <param name="propertyName"></param>
  211. /// <param name="throwOnError"></param>
  212. /// <returns></returns>
  213. public bool Delete(string propertyName, bool throwOnError)
  214. {
  215. var desc = GetOwnProperty(propertyName);
  216. if (desc == PropertyDescriptor.Undefined)
  217. {
  218. return true;
  219. }
  220. if (desc.Configurable)
  221. {
  222. Properties.Remove(propertyName);
  223. return true;
  224. }
  225. else
  226. {
  227. if (throwOnError)
  228. {
  229. throw new TypeError();
  230. }
  231. return false;
  232. }
  233. }
  234. /// <summary>
  235. /// Hint is a String. Returns a default value for the
  236. /// object.
  237. /// </summary>
  238. /// <param name="hintError"></param>
  239. /// <returns></returns>
  240. public object DefaultValue(string hintError)
  241. {
  242. return ToString();
  243. }
  244. /// <summary>
  245. /// Creates or alters the named own property to
  246. /// have the state described by a Property
  247. /// Descriptor. The flag controls failure handling.
  248. /// </summary>
  249. /// <param name="propertyName"></param>
  250. /// <param name="desc"></param>
  251. /// <param name="throwOnError"></param>
  252. /// <returns></returns>
  253. public bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  254. {
  255. var current = GetOwnProperty(propertyName);
  256. if (current == PropertyDescriptor.Undefined)
  257. {
  258. if (!Extensible)
  259. {
  260. if (throwOnError)
  261. {
  262. throw new TypeError();
  263. }
  264. return false;
  265. }
  266. else
  267. {
  268. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  269. {
  270. Properties.Add(propertyName, new DataDescriptor(desc.As<DataDescriptor>()));
  271. }
  272. else
  273. {
  274. Properties.Add(propertyName, new AccessorDescriptor(desc.As<AccessorDescriptor>()));
  275. }
  276. }
  277. return true;
  278. }
  279. // todo: if desc and current are the same, return true
  280. if (!current.Configurable)
  281. {
  282. if (desc.Configurable)
  283. {
  284. if (throwOnError)
  285. {
  286. throw new TypeError();
  287. }
  288. return false;
  289. }
  290. if (desc.Enumerable != current.Enumerable)
  291. {
  292. if (throwOnError)
  293. {
  294. throw new TypeError();
  295. }
  296. return false;
  297. }
  298. }
  299. if (desc.IsGenericDescriptor())
  300. {
  301. // ????
  302. }
  303. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  304. {
  305. if (!current.Configurable)
  306. {
  307. if (throwOnError)
  308. {
  309. throw new TypeError();
  310. }
  311. return false;
  312. }
  313. if (current.IsDataDescriptor())
  314. {
  315. // todo: convert to accessor
  316. }
  317. }
  318. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  319. {
  320. var cd = current.As<DataDescriptor>();
  321. var dd = current.As<DataDescriptor>();
  322. if (!current.Configurable)
  323. {
  324. if (!cd.Writable && dd.Writable)
  325. {
  326. if (throwOnError)
  327. {
  328. throw new TypeError();
  329. }
  330. return false;
  331. }
  332. }
  333. }
  334. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  335. {
  336. var ca = current.As<AccessorDescriptor>();
  337. var da = current.As<AccessorDescriptor>();
  338. if (!current.Configurable)
  339. {
  340. if ( (da.Set != null && da.Set != ca.Set)
  341. || (da.Get != null && da.Get != ca.Get))
  342. {
  343. if (throwOnError)
  344. {
  345. throw new TypeError();
  346. }
  347. return false;
  348. }
  349. }
  350. }
  351. Properties[propertyName] = desc;
  352. return true;
  353. }
  354. }
  355. }