ObjectInstance.cs 20 KB

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