ObjectInstance.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. using System;
  2. using System.Collections.Generic;
  3. using Jint.Native.Errors;
  4. using Jint.Native.String;
  5. using Jint.Runtime;
  6. using Jint.Runtime.Descriptors;
  7. namespace Jint.Native.Object
  8. {
  9. public class ObjectInstance
  10. {
  11. public ObjectInstance(ObjectInstance prototype)
  12. {
  13. Properties = new Dictionary<string, PropertyDescriptor>();
  14. Extensible = true;
  15. Prototype = prototype;
  16. DefineOwnProperty("prototype", new DataDescriptor(prototype), false);
  17. }
  18. public IDictionary<string, PropertyDescriptor> Properties { get; private set; }
  19. /// <summary>
  20. /// The prototype of this object.
  21. /// </summary>
  22. public ObjectInstance Prototype { get; private set; }
  23. /// <summary>
  24. /// If true, own properties may be added to the
  25. /// object.
  26. /// </summary>
  27. public bool Extensible { get; set; }
  28. /// <summary>
  29. /// A String value indicating a specification defined
  30. /// classification of objects.
  31. /// </summary>
  32. public virtual string Class
  33. {
  34. get { return "Object"; }
  35. }
  36. /// <summary>
  37. /// Returns the value of the named property.
  38. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3
  39. /// </summary>
  40. /// <param name="propertyName"></param>
  41. /// <returns></returns>
  42. public virtual object Get(string propertyName)
  43. {
  44. var desc = GetProperty(propertyName);
  45. if (desc == PropertyDescriptor.Undefined)
  46. {
  47. return Undefined.Instance;
  48. }
  49. if (desc.IsDataDescriptor())
  50. {
  51. return desc.As<DataDescriptor>().Value;
  52. }
  53. var getter = desc.As<AccessorDescriptor>().Get;
  54. return getter.Call(this, null);
  55. }
  56. public void Set(string name, object value)
  57. {
  58. if (!HasProperty(name))
  59. {
  60. DefineOwnProperty(name, new DataDescriptor(value) { Configurable = true, Enumerable = true, Writable = true }, false);
  61. }
  62. else
  63. {
  64. Put(name, value, false);
  65. }
  66. }
  67. /// <summary>
  68. /// Returns the Property Descriptor of the named
  69. /// own property of this object, or undefined if
  70. /// absent.
  71. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  72. /// </summary>
  73. /// <param name="propertyName"></param>
  74. /// <returns></returns>
  75. public virtual PropertyDescriptor GetOwnProperty(string propertyName)
  76. {
  77. PropertyDescriptor x;
  78. if (Properties.TryGetValue(propertyName, out x))
  79. {
  80. /* Spec implementation
  81. PropertyDescriptor d;
  82. if (x.IsDataDescriptor())
  83. {
  84. d = new DataDescriptor(x.As<DataDescriptor>());
  85. }
  86. else
  87. {
  88. d = new AccessorDescriptor(x.As<AccessorDescriptor>());
  89. }
  90. return d;
  91. */
  92. // optimmized implementation
  93. return x;
  94. }
  95. return PropertyDescriptor.Undefined;
  96. }
  97. /// <summary>
  98. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  99. /// </summary>
  100. /// <param name="propertyName"></param>
  101. /// <returns></returns>
  102. public PropertyDescriptor GetProperty(string propertyName)
  103. {
  104. var prop = GetOwnProperty(propertyName);
  105. if (prop != PropertyDescriptor.Undefined)
  106. {
  107. return prop;
  108. }
  109. if(Prototype == null)
  110. {
  111. return PropertyDescriptor.Undefined;
  112. }
  113. return Prototype.GetProperty(propertyName);
  114. }
  115. /// <summary>
  116. /// Sets the specified named property to the value
  117. /// of the second parameter. The flag controls
  118. /// failure handling.
  119. /// </summary>
  120. /// <param name="propertyName"></param>
  121. /// <param name="value"></param>
  122. /// <param name="throwOnError"></param>
  123. public void Put(string propertyName, object value, bool throwOnError)
  124. {
  125. if (!CanPut(propertyName))
  126. {
  127. if (throwOnError)
  128. {
  129. throw new TypeError();
  130. }
  131. return;
  132. }
  133. var ownDesc = GetOwnProperty(propertyName);
  134. if (ownDesc.IsDataDescriptor())
  135. {
  136. /* Spec implementation
  137. * var valueDesc = new DataDescriptor(value);
  138. * DefineOwnProperty(propertyName, valueDesc, throwOnError);
  139. */
  140. // optimized implementation which doesn't require to create a new descriptor
  141. if (!ownDesc.Configurable)
  142. {
  143. if (throwOnError)
  144. {
  145. throw new TypeError();
  146. }
  147. return;
  148. }
  149. ownDesc.As<DataDescriptor>().Value = value;
  150. return;
  151. }
  152. // property is an accessor or inherited
  153. var desc = GetProperty(propertyName);
  154. if (desc.IsAccessorDescriptor())
  155. {
  156. var setter = desc.As<AccessorDescriptor>().Set;
  157. setter.Call(this, new [] {value});
  158. }
  159. else
  160. {
  161. var newDesc = new DataDescriptor(value) {Writable = true, Enumerable = true, Configurable = true};
  162. DefineOwnProperty(propertyName, newDesc, throwOnError);
  163. }
  164. }
  165. /// <summary>
  166. /// Returns a Boolean value indicating whether a
  167. /// [[Put]] operation with PropertyName can be
  168. /// performed.
  169. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  170. /// </summary>
  171. /// <param name="propertyName"></param>
  172. /// <returns></returns>
  173. public bool CanPut(string propertyName)
  174. {
  175. var desc = GetOwnProperty(propertyName);
  176. if (desc != PropertyDescriptor.Undefined)
  177. {
  178. if (desc.IsAccessorDescriptor())
  179. {
  180. if (desc.As<AccessorDescriptor>().Set == null)
  181. {
  182. return false;
  183. }
  184. return true;
  185. }
  186. return desc.As<DataDescriptor>().Writable;
  187. }
  188. if (Prototype == null)
  189. {
  190. return Extensible;
  191. }
  192. var inherited = Prototype.GetProperty(propertyName);
  193. if (inherited == PropertyDescriptor.Undefined)
  194. {
  195. return Prototype.Extensible;
  196. }
  197. if (inherited.IsAccessorDescriptor())
  198. {
  199. if (inherited.As<AccessorDescriptor>().Set == null)
  200. {
  201. return false;
  202. }
  203. return true;
  204. }
  205. if (!Extensible)
  206. {
  207. return false;
  208. }
  209. else
  210. {
  211. return inherited.As<DataDescriptor>().Writable;
  212. }
  213. }
  214. /// <summary>
  215. /// Returns a Boolean value indicating whether the
  216. /// object already has a property with the given
  217. /// name.
  218. /// </summary>
  219. /// <param name="propertyName"></param>
  220. /// <returns></returns>
  221. public bool HasProperty(string propertyName)
  222. {
  223. return GetProperty(propertyName) != PropertyDescriptor.Undefined;
  224. }
  225. /// <summary>
  226. /// Removes the specified named own property
  227. /// from the object. The flag controls failure
  228. /// handling.
  229. /// </summary>
  230. /// <param name="propertyName"></param>
  231. /// <param name="throwOnError"></param>
  232. /// <returns></returns>
  233. public bool Delete(string propertyName, bool throwOnError)
  234. {
  235. var desc = GetOwnProperty(propertyName);
  236. if (desc == PropertyDescriptor.Undefined)
  237. {
  238. return true;
  239. }
  240. if (desc.Configurable)
  241. {
  242. Properties.Remove(propertyName);
  243. return true;
  244. }
  245. else
  246. {
  247. if (throwOnError)
  248. {
  249. throw new TypeError();
  250. }
  251. return false;
  252. }
  253. }
  254. /// <summary>
  255. /// Hint is a String. Returns a default value for the
  256. /// object.
  257. /// </summary>
  258. /// <param name="hint"></param>
  259. /// <returns></returns>
  260. public object DefaultValue(TypeCode hint)
  261. {
  262. if ((hint == TypeCode.String) || (hint == TypeCode.Empty && this is StringInstance))
  263. {
  264. var toString = this.Get("toString");
  265. var callable = toString as ICallable;
  266. if (callable != null)
  267. {
  268. var str = callable.Call(this, Arguments.Empty);
  269. if (str is IPrimitiveType)
  270. {
  271. return str;
  272. }
  273. }
  274. var valueOf = this.Get("valueOf");
  275. callable = valueOf as ICallable;
  276. if (callable != null)
  277. {
  278. var val = callable.Call(this, Arguments.Empty);
  279. if (val is IPrimitiveType)
  280. {
  281. return val;
  282. }
  283. }
  284. throw new TypeError();
  285. }
  286. if ((hint == TypeCode.Double) || (hint == TypeCode.Empty))
  287. {
  288. var valueOf = this.Get("valueOf");
  289. var callable = valueOf as ICallable;
  290. if (callable != null)
  291. {
  292. var val = callable.Call(this, Arguments.Empty);
  293. if (val is IPrimitiveType)
  294. {
  295. return val;
  296. }
  297. }
  298. var toString = this.Get("toString");
  299. callable = toString as ICallable;
  300. if (callable != null)
  301. {
  302. var str = callable.Call(this, Arguments.Empty);
  303. if (str is IPrimitiveType)
  304. {
  305. return str;
  306. }
  307. }
  308. throw new TypeError();
  309. }
  310. return ToString();
  311. }
  312. /// <summary>
  313. /// Creates or alters the named own property to
  314. /// have the state described by a Property
  315. /// Descriptor. The flag controls failure handling.
  316. /// </summary>
  317. /// <param name="propertyName"></param>
  318. /// <param name="desc"></param>
  319. /// <param name="throwOnError"></param>
  320. /// <returns></returns>
  321. public bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  322. {
  323. var current = GetOwnProperty(propertyName);
  324. if (current == PropertyDescriptor.Undefined)
  325. {
  326. if (!Extensible)
  327. {
  328. if (throwOnError)
  329. {
  330. throw new TypeError();
  331. }
  332. return false;
  333. }
  334. else
  335. {
  336. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  337. {
  338. Properties.Add(propertyName, new DataDescriptor(desc.As<DataDescriptor>()));
  339. }
  340. else
  341. {
  342. Properties.Add(propertyName, new AccessorDescriptor(desc.As<AccessorDescriptor>()));
  343. }
  344. }
  345. return true;
  346. }
  347. // todo: if desc and current are the same, return true
  348. if (!current.Configurable)
  349. {
  350. if (desc.Configurable)
  351. {
  352. if (throwOnError)
  353. {
  354. throw new TypeError();
  355. }
  356. return false;
  357. }
  358. if (desc.Enumerable != current.Enumerable)
  359. {
  360. if (throwOnError)
  361. {
  362. throw new TypeError();
  363. }
  364. return false;
  365. }
  366. }
  367. if (desc.IsGenericDescriptor())
  368. {
  369. // ????
  370. }
  371. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  372. {
  373. if (!current.Configurable)
  374. {
  375. if (throwOnError)
  376. {
  377. throw new TypeError();
  378. }
  379. return false;
  380. }
  381. if (current.IsDataDescriptor())
  382. {
  383. // todo: convert to accessor
  384. }
  385. }
  386. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  387. {
  388. var cd = current.As<DataDescriptor>();
  389. var dd = current.As<DataDescriptor>();
  390. if (!current.Configurable)
  391. {
  392. if (!cd.Writable && dd.Writable)
  393. {
  394. if (throwOnError)
  395. {
  396. throw new TypeError();
  397. }
  398. return false;
  399. }
  400. }
  401. }
  402. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  403. {
  404. var ca = current.As<AccessorDescriptor>();
  405. var da = current.As<AccessorDescriptor>();
  406. if (!current.Configurable)
  407. {
  408. if ( (da.Set != null && da.Set != ca.Set)
  409. || (da.Get != null && da.Get != ca.Get))
  410. {
  411. if (throwOnError)
  412. {
  413. throw new TypeError();
  414. }
  415. return false;
  416. }
  417. }
  418. }
  419. Properties[propertyName] = desc;
  420. return true;
  421. }
  422. /// <summary>
  423. /// Optimized version of [[Put]] when the property is known to be undeclared already
  424. /// </summary>
  425. /// <param name="name"></param>
  426. /// <param name="value"></param>
  427. /// <param name="writable"></param>
  428. /// <param name="configurable"></param>
  429. /// <param name="enumerable"></param>
  430. public void FastAddProperty(string name, object value, bool writable, bool enumerable, bool configurable)
  431. {
  432. Properties.Add(name, new DataDescriptor(value) { Writable = writable, Enumerable = enumerable, Configurable = configurable });
  433. }
  434. /// <summary>
  435. /// Optimized version of [[Put]] when the property is known to be already declared
  436. /// </summary>
  437. /// <param name="name"></param>
  438. /// <param name="value"></param>
  439. public void FastSetProperty(string name, PropertyDescriptor value)
  440. {
  441. Properties[name] = value;
  442. }
  443. }
  444. }