ObjectInstance.cs 20 KB

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