ObjectInstance.cs 22 KB

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