ObjectInstance.cs 16 KB

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