ObjectInstance.cs 16 KB

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