ObjectInstance.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. using System.Collections.Generic;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using System;
  5. using System.Collections.Specialized;
  6. namespace Jint.Native.Object
  7. {
  8. public class ObjectInstance
  9. {
  10. private Dictionary<string, PropertyDescriptor> _intrinsicProperties;
  11. public ObjectInstance(Engine engine)
  12. {
  13. Engine = engine;
  14. Properties = new MruPropertyCache2<string, PropertyDescriptor>();
  15. }
  16. public Engine Engine { get; set; }
  17. protected IDictionary<string, PropertyDescriptor> Properties { get; private set; }
  18. protected bool TryGetIntrinsicValue(JsSymbol symbol, out JsValue value)
  19. {
  20. PropertyDescriptor descriptor;
  21. if (_intrinsicProperties != null && _intrinsicProperties.TryGetValue(symbol.AsSymbol(), out descriptor))
  22. {
  23. value = descriptor.Value;
  24. return true;
  25. }
  26. if (Prototype == null)
  27. {
  28. value = JsValue.Undefined;
  29. return false;
  30. }
  31. return Prototype.TryGetIntrinsicValue(symbol, out value);
  32. }
  33. public void SetIntrinsicValue(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  34. {
  35. SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  36. }
  37. protected void SetIntrinsicValue(JsSymbol symbol, JsValue value, bool writable, bool enumerable, bool configurable)
  38. {
  39. if (_intrinsicProperties == null)
  40. {
  41. _intrinsicProperties = new Dictionary<string, PropertyDescriptor>();
  42. }
  43. _intrinsicProperties[symbol.AsSymbol()] = new PropertyDescriptor(value, writable, enumerable, configurable);
  44. }
  45. /// <summary>
  46. /// The prototype of this object.
  47. /// </summary>
  48. public ObjectInstance Prototype { get; set; }
  49. /// <summary>
  50. /// If true, own properties may be added to the
  51. /// object.
  52. /// </summary>
  53. public bool Extensible { get; set; }
  54. /// <summary>
  55. /// A String value indicating a specification defined
  56. /// classification of objects.
  57. /// </summary>
  58. public virtual string Class
  59. {
  60. get { return "Object"; }
  61. }
  62. public virtual IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  63. {
  64. EnsureInitialized();
  65. return Properties;
  66. }
  67. public virtual bool HasOwnProperty(string p)
  68. {
  69. EnsureInitialized();
  70. return Properties.ContainsKey(p);
  71. }
  72. public virtual void RemoveOwnProperty(string p)
  73. {
  74. EnsureInitialized();
  75. Properties.Remove(p);
  76. }
  77. /// <summary>
  78. /// Returns the value of the named property.
  79. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3
  80. /// </summary>
  81. /// <param name="propertyName"></param>
  82. /// <returns></returns>
  83. public virtual JsValue Get(string propertyName)
  84. {
  85. var desc = GetProperty(propertyName);
  86. if (desc == PropertyDescriptor.Undefined)
  87. {
  88. return JsValue.Undefined;
  89. }
  90. if (desc.IsDataDescriptor())
  91. {
  92. var val = desc.Value;
  93. return val != null ? val : Undefined.Instance;
  94. }
  95. var getter = desc.Get != null ? desc.Get : Undefined.Instance;
  96. if (getter.IsUndefined())
  97. {
  98. return Undefined.Instance;
  99. }
  100. // if getter is not undefined it must be ICallable
  101. var callable = getter.TryCast<ICallable>();
  102. return callable.Call(this, Arguments.Empty);
  103. }
  104. /// <summary>
  105. /// Returns the Property Descriptor of the named
  106. /// own property of this object, or undefined if
  107. /// absent.
  108. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  109. /// </summary>
  110. /// <param name="propertyName"></param>
  111. /// <returns></returns>
  112. public virtual PropertyDescriptor GetOwnProperty(string propertyName)
  113. {
  114. EnsureInitialized();
  115. PropertyDescriptor x;
  116. if (Properties.TryGetValue(propertyName, out x))
  117. {
  118. /* Spec implementation
  119. PropertyDescriptor d;
  120. if (x.IsDataDescriptor())
  121. {
  122. d = new PropertyDescriptor(x.As<DataDescriptor>());
  123. }
  124. else
  125. {
  126. d = new PropertyDescriptor(x.As<AccessorDescriptor>());
  127. }
  128. return d;
  129. */
  130. // optimmized implementation
  131. return x;
  132. }
  133. return PropertyDescriptor.Undefined;
  134. }
  135. protected virtual void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  136. {
  137. EnsureInitialized();
  138. Properties[propertyName] = desc;
  139. }
  140. /// <summary>
  141. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  142. /// </summary>
  143. /// <param name="propertyName"></param>
  144. /// <returns></returns>
  145. public PropertyDescriptor GetProperty(string propertyName)
  146. {
  147. var prop = GetOwnProperty(propertyName);
  148. if (prop != PropertyDescriptor.Undefined)
  149. {
  150. return prop;
  151. }
  152. if(Prototype == null)
  153. {
  154. return PropertyDescriptor.Undefined;
  155. }
  156. return Prototype.GetProperty(propertyName);
  157. }
  158. /// <summary>
  159. /// Sets the specified named property to the value
  160. /// of the second parameter. The flag controls
  161. /// failure handling.
  162. /// </summary>
  163. /// <param name="propertyName"></param>
  164. /// <param name="value"></param>
  165. /// <param name="throwOnError"></param>
  166. public virtual void Put(string propertyName, JsValue value, bool throwOnError)
  167. {
  168. if (!CanPut(propertyName))
  169. {
  170. if (throwOnError)
  171. {
  172. throw new JavaScriptException(Engine.TypeError);
  173. }
  174. return;
  175. }
  176. var ownDesc = GetOwnProperty(propertyName);
  177. if (ownDesc.IsDataDescriptor())
  178. {
  179. ownDesc.Value = value;
  180. return;
  181. // as per specification
  182. // var valueDesc = new PropertyDescriptor(value: value, writable: null, enumerable: null, configurable: null);
  183. // DefineOwnProperty(propertyName, valueDesc, throwOnError);
  184. // return;
  185. }
  186. // property is an accessor or inherited
  187. var desc = GetProperty(propertyName);
  188. if (desc.IsAccessorDescriptor())
  189. {
  190. var setter = desc.Set.TryCast<ICallable>();
  191. setter.Call(new JsValue(this), new [] {value});
  192. }
  193. else
  194. {
  195. var newDesc = new PropertyDescriptor(value, true, true, true);
  196. DefineOwnProperty(propertyName, newDesc, throwOnError);
  197. }
  198. }
  199. /// <summary>
  200. /// Returns a Boolean value indicating whether a
  201. /// [[Put]] operation with PropertyName can be
  202. /// performed.
  203. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  204. /// </summary>
  205. /// <param name="propertyName"></param>
  206. /// <returns></returns>
  207. public bool CanPut(string propertyName)
  208. {
  209. var desc = GetOwnProperty(propertyName);
  210. if (desc != PropertyDescriptor.Undefined)
  211. {
  212. if (desc.IsAccessorDescriptor())
  213. {
  214. if (desc.Set == null || desc.Set.IsUndefined())
  215. {
  216. return false;
  217. }
  218. return true;
  219. }
  220. return desc.Writable.HasValue && desc.Writable.Value;
  221. }
  222. if (Prototype == null)
  223. {
  224. return Extensible;
  225. }
  226. var inherited = Prototype.GetProperty(propertyName);
  227. if (inherited == PropertyDescriptor.Undefined)
  228. {
  229. return Extensible;
  230. }
  231. if (inherited.IsAccessorDescriptor())
  232. {
  233. if (inherited.Set == null || inherited.Set.IsUndefined())
  234. {
  235. return false;
  236. }
  237. return true;
  238. }
  239. if (!Extensible)
  240. {
  241. return false;
  242. }
  243. else
  244. {
  245. return inherited.Writable.HasValue && inherited.Writable.Value;
  246. }
  247. }
  248. /// <summary>
  249. /// Returns a Boolean value indicating whether the
  250. /// object already has a property with the given
  251. /// name.
  252. /// </summary>
  253. /// <param name="propertyName"></param>
  254. /// <returns></returns>
  255. public bool HasProperty(string propertyName)
  256. {
  257. return GetProperty(propertyName) != PropertyDescriptor.Undefined;
  258. }
  259. /// <summary>
  260. /// Removes the specified named own property
  261. /// from the object. The flag controls failure
  262. /// handling.
  263. /// </summary>
  264. /// <param name="propertyName"></param>
  265. /// <param name="throwOnError"></param>
  266. /// <returns></returns>
  267. public virtual bool Delete(string propertyName, bool throwOnError)
  268. {
  269. var desc = GetOwnProperty(propertyName);
  270. if (desc == PropertyDescriptor.Undefined)
  271. {
  272. return true;
  273. }
  274. if (desc.Configurable.HasValue && desc.Configurable.Value)
  275. {
  276. RemoveOwnProperty(propertyName);
  277. return true;
  278. }
  279. else
  280. {
  281. if (throwOnError)
  282. {
  283. throw new JavaScriptException(Engine.TypeError);
  284. }
  285. return false;
  286. }
  287. }
  288. /// <summary>
  289. /// Hint is a String. Returns a default value for the
  290. /// object.
  291. /// </summary>
  292. /// <param name="hint"></param>
  293. /// <returns></returns>
  294. public JsValue DefaultValue(Types hint)
  295. {
  296. EnsureInitialized();
  297. if (hint == Types.String || (hint == Types.None && Class == "Date"))
  298. {
  299. var toString = Get("toString").TryCast<ICallable>();
  300. if (toString != null)
  301. {
  302. var str = toString.Call(new JsValue(this), Arguments.Empty);
  303. if (str.IsPrimitive())
  304. {
  305. return str;
  306. }
  307. }
  308. var valueOf = Get("valueOf").TryCast<ICallable>();
  309. if (valueOf != null)
  310. {
  311. var val = valueOf.Call(new JsValue(this), Arguments.Empty);
  312. if (val.IsPrimitive())
  313. {
  314. return val;
  315. }
  316. }
  317. throw new JavaScriptException(Engine.TypeError);
  318. }
  319. if (hint == Types.Number || hint == Types.None)
  320. {
  321. var valueOf = Get("valueOf").TryCast<ICallable>();
  322. if (valueOf != null)
  323. {
  324. var val = valueOf.Call(new JsValue(this), Arguments.Empty);
  325. if (val.IsPrimitive())
  326. {
  327. return val;
  328. }
  329. }
  330. var toString = Get("toString").TryCast<ICallable>();
  331. if (toString != null)
  332. {
  333. var str = toString.Call(new JsValue(this), Arguments.Empty);
  334. if (str.IsPrimitive())
  335. {
  336. return str;
  337. }
  338. }
  339. throw new JavaScriptException(Engine.TypeError);
  340. }
  341. return ToString();
  342. }
  343. /// <summary>
  344. /// Creates or alters the named own property to
  345. /// have the state described by a Property
  346. /// Descriptor. The flag controls failure handling.
  347. /// </summary>
  348. /// <param name="propertyName"></param>
  349. /// <param name="desc"></param>
  350. /// <param name="throwOnError"></param>
  351. /// <returns></returns>
  352. public virtual bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  353. {
  354. var current = GetOwnProperty(propertyName);
  355. if (current == desc) {
  356. return true;
  357. }
  358. if (current == PropertyDescriptor.Undefined)
  359. {
  360. if (!Extensible)
  361. {
  362. if (throwOnError)
  363. {
  364. throw new JavaScriptException(Engine.TypeError);
  365. }
  366. return false;
  367. }
  368. else
  369. {
  370. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  371. {
  372. SetOwnProperty(propertyName, new PropertyDescriptor(desc)
  373. {
  374. Value = desc.Value != null ? desc.Value : JsValue.Undefined,
  375. Writable = desc.Writable.HasValue ? desc.Writable.Value : false,
  376. Enumerable = desc.Enumerable.HasValue ? desc.Enumerable.Value : false,
  377. Configurable = desc.Configurable.HasValue ? desc.Configurable.Value : false
  378. });
  379. }
  380. else
  381. {
  382. SetOwnProperty(propertyName, new PropertyDescriptor(desc)
  383. {
  384. Get = desc.Get,
  385. Set = desc.Set,
  386. Enumerable = desc.Enumerable.HasValue ? desc.Enumerable : false,
  387. Configurable = desc.Configurable.HasValue ? desc.Configurable : false,
  388. });
  389. }
  390. }
  391. return true;
  392. }
  393. // Step 5
  394. if (!current.Configurable.HasValue &&
  395. !current.Enumerable.HasValue &&
  396. !current.Writable.HasValue &&
  397. current.Get == null &&
  398. current.Set == null &&
  399. current.Value == null)
  400. {
  401. return true;
  402. }
  403. // Step 6
  404. if (
  405. current.Configurable == desc.Configurable &&
  406. current.Writable == desc.Writable &&
  407. current.Enumerable == desc.Enumerable &&
  408. ((current.Get == null && desc.Get == null) || (current.Get != null && desc.Get != null && ExpressionInterpreter.SameValue(current.Get, desc.Get))) &&
  409. ((current.Set == null && desc.Set == null) || (current.Set != null && desc.Set != null && ExpressionInterpreter.SameValue(current.Set, desc.Set))) &&
  410. ((current.Value == null && desc.Value == null) || (current.Value != null && desc.Value != null && ExpressionInterpreter.StrictlyEqual(current.Value, desc.Value)))
  411. ) {
  412. return true;
  413. }
  414. if (!current.Configurable.HasValue || !current.Configurable.Value)
  415. {
  416. if (desc.Configurable.HasValue && desc.Configurable.Value)
  417. {
  418. if (throwOnError)
  419. {
  420. throw new JavaScriptException(Engine.TypeError);
  421. }
  422. return false;
  423. }
  424. if (desc.Enumerable.HasValue && (!current.Enumerable.HasValue || desc.Enumerable.Value != current.Enumerable.Value))
  425. {
  426. if (throwOnError)
  427. {
  428. throw new JavaScriptException(Engine.TypeError);
  429. }
  430. return false;
  431. }
  432. }
  433. if (!desc.IsGenericDescriptor())
  434. {
  435. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  436. {
  437. if (!current.Configurable.HasValue || !current.Configurable.Value)
  438. {
  439. if (throwOnError)
  440. {
  441. throw new JavaScriptException(Engine.TypeError);
  442. }
  443. return false;
  444. }
  445. if (current.IsDataDescriptor())
  446. {
  447. SetOwnProperty(propertyName, current = new PropertyDescriptor(
  448. get: Undefined.Instance,
  449. set: Undefined.Instance,
  450. enumerable: current.Enumerable,
  451. configurable: current.Configurable
  452. ));
  453. }
  454. else
  455. {
  456. SetOwnProperty(propertyName, current = new PropertyDescriptor(
  457. value: Undefined.Instance,
  458. writable: null,
  459. enumerable: current.Enumerable,
  460. configurable: current.Configurable
  461. ));
  462. }
  463. }
  464. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  465. {
  466. if (!current.Configurable.HasValue || current.Configurable.Value == false)
  467. {
  468. if (!current.Writable.HasValue || !current.Writable.Value && desc.Writable.HasValue && desc.Writable.Value)
  469. {
  470. if (throwOnError)
  471. {
  472. throw new JavaScriptException(Engine.TypeError);
  473. }
  474. return false;
  475. }
  476. if (!current.Writable.Value)
  477. {
  478. if (desc.Value != null && !ExpressionInterpreter.SameValue(desc.Value, current.Value))
  479. {
  480. if (throwOnError)
  481. {
  482. throw new JavaScriptException(Engine.TypeError);
  483. }
  484. return false;
  485. }
  486. }
  487. }
  488. }
  489. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  490. {
  491. if (!current.Configurable.HasValue || !current.Configurable.Value)
  492. {
  493. if ((desc.Set != null && !ExpressionInterpreter.SameValue(desc.Set, current.Set != null ? current.Set : Undefined.Instance))
  494. ||
  495. (desc.Get != null && !ExpressionInterpreter.SameValue(desc.Get, current.Get != null ? current.Get : Undefined.Instance)))
  496. {
  497. if (throwOnError)
  498. {
  499. throw new JavaScriptException(Engine.TypeError);
  500. }
  501. return false;
  502. }
  503. }
  504. }
  505. }
  506. if (desc.Value != null)
  507. {
  508. current.Value = desc.Value;
  509. }
  510. if (desc.Writable.HasValue)
  511. {
  512. current.Writable = desc.Writable;
  513. }
  514. if (desc.Enumerable.HasValue)
  515. {
  516. current.Enumerable = desc.Enumerable;
  517. }
  518. if (desc.Configurable.HasValue)
  519. {
  520. current.Configurable = desc.Configurable;
  521. }
  522. if (desc.Get != null)
  523. {
  524. current.Get = desc.Get;
  525. }
  526. if (desc.Set != null)
  527. {
  528. current.Set = desc.Set;
  529. }
  530. return true;
  531. }
  532. /// <summary>
  533. /// Optimized version of [[Put]] when the property is known to be undeclared already
  534. /// </summary>
  535. /// <param name="name"></param>
  536. /// <param name="value"></param>
  537. /// <param name="writable"></param>
  538. /// <param name="configurable"></param>
  539. /// <param name="enumerable"></param>
  540. public void FastAddProperty(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  541. {
  542. SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  543. }
  544. /// <summary>
  545. /// Optimized version of [[Put]] when the property is known to be already declared
  546. /// </summary>
  547. /// <param name="name"></param>
  548. /// <param name="value"></param>
  549. public void FastSetProperty(string name, PropertyDescriptor value)
  550. {
  551. SetOwnProperty(name, value);
  552. }
  553. protected virtual void EnsureInitialized()
  554. {
  555. }
  556. public override string ToString()
  557. {
  558. return TypeConverter.ToString(this);
  559. }
  560. }
  561. }