2
0

ObjectInstance.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. else
  329. {
  330. Properties[propertyName] = new PropertyDescriptor(desc);
  331. }
  332. }
  333. return true;
  334. }
  335. // Step 5
  336. if (!current.Configurable.HasValue && !current.Enumerable.HasValue && !(current.IsDataDescriptor() && current.Writable.HasValue))
  337. {
  338. if (!desc.IsDataDescriptor())
  339. {
  340. return true;
  341. }
  342. }
  343. // Step 6
  344. var configurableIsSame = current.Configurable.HasValue
  345. ? desc.Configurable.HasValue && (current.Configurable.Value == desc.Configurable.Value)
  346. : !desc.Configurable.HasValue;
  347. var enumerableIsSame = current.Enumerable.HasValue
  348. ? desc.Enumerable.HasValue && (current.Enumerable.Value == desc.Enumerable.Value)
  349. : !desc.Enumerable.HasValue;
  350. var writableIsSame = true;
  351. var valueIsSame = true;
  352. if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  353. {
  354. var currentDataDescriptor = current;
  355. var descDataDescriptor = desc;
  356. writableIsSame = currentDataDescriptor.Writable.HasValue
  357. ? descDataDescriptor.Writable.HasValue && (currentDataDescriptor.Writable.Value == descDataDescriptor.Writable.Value)
  358. : !descDataDescriptor.Writable.HasValue;
  359. var valueA = currentDataDescriptor.Value.HasValue
  360. ? currentDataDescriptor.Value.Value
  361. : Undefined.Instance;
  362. var valueB = descDataDescriptor.Value.HasValue
  363. ? descDataDescriptor.Value.Value
  364. : Undefined.Instance;
  365. valueIsSame = ExpressionInterpreter.SameValue(valueA, valueB);
  366. }
  367. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  368. {
  369. var currentAccessorDescriptor = current;
  370. var descAccessorDescriptor = desc;
  371. var getValueA = currentAccessorDescriptor.Get.HasValue
  372. ? currentAccessorDescriptor.Get.Value
  373. : Undefined.Instance;
  374. var getValueB = descAccessorDescriptor.Get.HasValue
  375. ? descAccessorDescriptor.Get.Value
  376. : Undefined.Instance;
  377. var setValueA = currentAccessorDescriptor.Set.HasValue
  378. ? currentAccessorDescriptor.Set.Value
  379. : Undefined.Instance;
  380. var setValueB = descAccessorDescriptor.Set.HasValue
  381. ? descAccessorDescriptor.Set.Value
  382. : Undefined.Instance;
  383. valueIsSame = ExpressionInterpreter.SameValue(getValueA, getValueB)
  384. && ExpressionInterpreter.SameValue(setValueA, setValueB);
  385. }
  386. else
  387. {
  388. valueIsSame = false;
  389. }
  390. if (configurableIsSame && enumerableIsSame && writableIsSame && valueIsSame)
  391. {
  392. return true;
  393. }
  394. if (!current.Configurable.HasValue || !current.Configurable.Value.AsBoolean())
  395. {
  396. if (desc.Configurable.HasValue && desc.Configurable.Value.AsBoolean())
  397. {
  398. if (throwOnError)
  399. {
  400. throw new JavaScriptException(Engine.TypeError);
  401. }
  402. return false;
  403. }
  404. if (desc.Enumerable.HasValue && (!current.Enumerable.HasValue || desc.Enumerable.Value != current.Enumerable.Value))
  405. {
  406. if (throwOnError)
  407. {
  408. throw new JavaScriptException(Engine.TypeError);
  409. }
  410. return false;
  411. }
  412. }
  413. if (!desc.IsGenericDescriptor())
  414. {
  415. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  416. {
  417. if (!current.Configurable.HasValue || !current.Configurable.Value.AsBoolean())
  418. {
  419. if (throwOnError)
  420. {
  421. throw new JavaScriptException(Engine.TypeError);
  422. }
  423. return false;
  424. }
  425. if (current.IsDataDescriptor())
  426. {
  427. Properties[propertyName] = current = new PropertyDescriptor(
  428. get: Undefined.Instance,
  429. set: Undefined.Instance,
  430. enumerable: current.Enumerable.HasValue && current.Enumerable.Value.AsBoolean(),
  431. configurable: current.Configurable.HasValue && current.Configurable.Value.AsBoolean()
  432. );
  433. }
  434. else
  435. {
  436. Properties[propertyName] = current = new PropertyDescriptor(
  437. value: Undefined.Instance,
  438. writable: null,
  439. enumerable: current.Enumerable.HasValue && current.Enumerable.Value.AsBoolean(),
  440. configurable: current.Configurable.HasValue && current.Configurable.Value.AsBoolean()
  441. );
  442. }
  443. }
  444. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  445. {
  446. if (!current.Configurable.HasValue || current.Configurable.Value.AsBoolean() == false)
  447. {
  448. if (!current.Writable.HasValue || !current.Writable.Value.AsBoolean() && desc.Writable.HasValue && desc.Writable.Value.AsBoolean())
  449. {
  450. if (throwOnError)
  451. {
  452. throw new JavaScriptException(Engine.TypeError);
  453. }
  454. return false;
  455. }
  456. if (!current.Writable.Value.AsBoolean())
  457. {
  458. if (desc.Value.HasValue && !valueIsSame)
  459. {
  460. if (throwOnError)
  461. {
  462. throw new JavaScriptException(Engine.TypeError);
  463. }
  464. return false;
  465. }
  466. }
  467. }
  468. if (!desc.Writable.HasValue && current.Writable.HasValue)
  469. {
  470. desc.Enumerable = current.Enumerable;
  471. }
  472. }
  473. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  474. {
  475. if (!current.Configurable.HasValue || !current.Configurable.Value.AsBoolean())
  476. {
  477. if ((desc.Set.HasValue && !ExpressionInterpreter.SameValue(desc.Set.Value, current.Set.HasValue ? current.Set.Value : Undefined.Instance))
  478. ||
  479. (desc.Get.HasValue && !ExpressionInterpreter.SameValue(desc.Get.Value, current.Get.HasValue ? current.Get.Value : Undefined.Instance)))
  480. {
  481. if (throwOnError)
  482. {
  483. throw new JavaScriptException(Engine.TypeError);
  484. }
  485. return false;
  486. }
  487. }
  488. }
  489. }
  490. if (desc.Value.HasValue)
  491. {
  492. current.Value = desc.Value;
  493. }
  494. if (desc.Writable.HasValue)
  495. {
  496. current.Writable = desc.Writable;
  497. }
  498. if (desc.Enumerable.HasValue)
  499. {
  500. current.Enumerable = desc.Enumerable;
  501. }
  502. if (desc.Configurable.HasValue)
  503. {
  504. current.Configurable = desc.Configurable;
  505. }
  506. if (desc.Get.HasValue)
  507. {
  508. current.Get = desc.Get;
  509. }
  510. if (desc.Set.HasValue)
  511. {
  512. current.Set = desc.Set;
  513. }
  514. return true;
  515. }
  516. /// <summary>
  517. /// Optimized version of [[Put]] when the property is known to be undeclared already
  518. /// </summary>
  519. /// <param name="name"></param>
  520. /// <param name="value"></param>
  521. /// <param name="writable"></param>
  522. /// <param name="configurable"></param>
  523. /// <param name="enumerable"></param>
  524. public void FastAddProperty(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  525. {
  526. Properties.Add(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  527. }
  528. /// <summary>
  529. /// Optimized version of [[Put]] when the property is known to be already declared
  530. /// </summary>
  531. /// <param name="name"></param>
  532. /// <param name="value"></param>
  533. public void FastSetProperty(string name, PropertyDescriptor value)
  534. {
  535. Properties[name] = value;
  536. }
  537. public override string ToString()
  538. {
  539. return TypeConverter.ToString(this);
  540. }
  541. }
  542. }