ObjectInstance.cs 25 KB

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