ObjectInstance.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using System.Runtime.CompilerServices;
  5. using Jint.Native.Array;
  6. using Jint.Native.Boolean;
  7. using Jint.Native.Date;
  8. using Jint.Native.Function;
  9. using Jint.Native.Number;
  10. using Jint.Native.RegExp;
  11. using Jint.Native.String;
  12. using Jint.Runtime;
  13. using Jint.Runtime.Descriptors;
  14. using Jint.Runtime.Descriptors.Specialized;
  15. using Jint.Runtime.Interop;
  16. namespace Jint.Native.Object
  17. {
  18. public class ObjectInstance : JsValue, IEquatable<ObjectInstance>
  19. {
  20. private MruPropertyCache2<PropertyDescriptor> _intrinsicProperties;
  21. private MruPropertyCache2<PropertyDescriptor> _properties;
  22. private readonly string _class;
  23. protected readonly Engine _engine;
  24. public ObjectInstance(Engine engine) : this(engine, "Object")
  25. {
  26. _engine = engine;
  27. }
  28. protected ObjectInstance(Engine engine, in string objectClass) : base(Types.Object)
  29. {
  30. _engine = engine;
  31. _class = objectClass;
  32. }
  33. public Engine Engine => _engine;
  34. protected bool TryGetIntrinsicValue(JsSymbol symbol, out JsValue value)
  35. {
  36. if (_intrinsicProperties != null && _intrinsicProperties.TryGetValue(symbol.AsSymbol(), out var descriptor))
  37. {
  38. value = descriptor.Value;
  39. return true;
  40. }
  41. if (ReferenceEquals(Prototype, null))
  42. {
  43. value = Undefined;
  44. return false;
  45. }
  46. return Prototype.TryGetIntrinsicValue(symbol, out value);
  47. }
  48. public void SetIntrinsicValue(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  49. {
  50. SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  51. }
  52. protected void SetIntrinsicValue(JsSymbol symbol, JsValue value, bool writable, bool enumerable, bool configurable)
  53. {
  54. if (_intrinsicProperties == null)
  55. {
  56. _intrinsicProperties = new MruPropertyCache2<PropertyDescriptor>();
  57. }
  58. _intrinsicProperties[symbol.AsSymbol()] = new PropertyDescriptor(value, writable, enumerable, configurable);
  59. }
  60. /// <summary>
  61. /// The prototype of this object.
  62. /// </summary>
  63. public ObjectInstance Prototype { get; set; }
  64. /// <summary>
  65. /// If true, own properties may be added to the
  66. /// object.
  67. /// </summary>
  68. public bool Extensible { get; set; }
  69. /// <summary>
  70. /// A String value indicating a specification defined
  71. /// classification of objects.
  72. /// </summary>
  73. public ref readonly string Class => ref _class;
  74. public virtual IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  75. {
  76. EnsureInitialized();
  77. if (_properties != null)
  78. {
  79. foreach (var pair in _properties.GetEnumerator())
  80. {
  81. yield return pair;
  82. }
  83. }
  84. }
  85. protected virtual void AddProperty(string propertyName, PropertyDescriptor descriptor)
  86. {
  87. if (_properties == null)
  88. {
  89. _properties = new MruPropertyCache2<PropertyDescriptor>();
  90. }
  91. _properties.Add(propertyName, descriptor);
  92. }
  93. protected virtual bool TryGetProperty(string propertyName, out PropertyDescriptor descriptor)
  94. {
  95. if (_properties == null)
  96. {
  97. descriptor = null;
  98. return false;
  99. }
  100. return _properties.TryGetValue(propertyName, out descriptor);
  101. }
  102. public virtual bool HasOwnProperty(string propertyName)
  103. {
  104. EnsureInitialized();
  105. return _properties?.ContainsKey(propertyName) ?? false;
  106. }
  107. public virtual void RemoveOwnProperty(string propertyName)
  108. {
  109. EnsureInitialized();
  110. _properties?.Remove(propertyName);
  111. }
  112. /// <summary>
  113. /// Returns the value of the named property.
  114. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3
  115. /// </summary>
  116. /// <param name="propertyName"></param>
  117. /// <returns></returns>
  118. public virtual JsValue Get(string propertyName)
  119. {
  120. var desc = GetProperty(propertyName);
  121. return UnwrapJsValue(desc);
  122. }
  123. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  124. internal JsValue UnwrapJsValue(PropertyDescriptor desc)
  125. {
  126. if (desc == PropertyDescriptor.Undefined)
  127. {
  128. return Undefined;
  129. }
  130. if (desc.IsDataDescriptor())
  131. {
  132. var val = desc.Value;
  133. return val ?? Undefined;
  134. }
  135. var getter = desc.Get ?? Undefined;
  136. if (getter.IsUndefined())
  137. {
  138. return Undefined;
  139. }
  140. // if getter is not undefined it must be ICallable
  141. var callable = getter.TryCast<ICallable>();
  142. return callable.Call(this, Arguments.Empty);
  143. }
  144. /// <summary>
  145. /// Returns the Property Descriptor of the named
  146. /// own property of this object, or undefined if
  147. /// absent.
  148. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  149. /// </summary>
  150. /// <param name="propertyName"></param>
  151. /// <returns></returns>
  152. public virtual PropertyDescriptor GetOwnProperty(string propertyName)
  153. {
  154. EnsureInitialized();
  155. if (_properties != null && _properties.TryGetValue(propertyName, out var x))
  156. {
  157. return x;
  158. }
  159. return PropertyDescriptor.Undefined;
  160. }
  161. protected internal virtual void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  162. {
  163. EnsureInitialized();
  164. if (_properties == null)
  165. {
  166. _properties = new MruPropertyCache2<PropertyDescriptor>();
  167. }
  168. _properties[propertyName] = desc;
  169. }
  170. /// <summary>
  171. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  172. /// </summary>
  173. /// <param name="propertyName"></param>
  174. /// <returns></returns>
  175. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  176. public PropertyDescriptor GetProperty(string propertyName)
  177. {
  178. var prop = GetOwnProperty(propertyName);
  179. if (prop != PropertyDescriptor.Undefined)
  180. {
  181. return prop;
  182. }
  183. if (ReferenceEquals(Prototype, null))
  184. {
  185. return PropertyDescriptor.Undefined;
  186. }
  187. return Prototype.GetProperty(propertyName);
  188. }
  189. public bool TryGetValue(string propertyName, out JsValue value)
  190. {
  191. value = Undefined;
  192. var desc = GetOwnProperty(propertyName);
  193. if (desc != null && desc != PropertyDescriptor.Undefined)
  194. {
  195. if (desc == PropertyDescriptor.Undefined)
  196. {
  197. return false;
  198. }
  199. var descValue = desc.Value;
  200. if (desc.WritableSet && !ReferenceEquals(descValue, null))
  201. {
  202. value = descValue;
  203. return true;
  204. }
  205. var getter = desc.Get ?? Undefined;
  206. if (getter.IsUndefined())
  207. {
  208. value = Undefined;
  209. return false;
  210. }
  211. // if getter is not undefined it must be ICallable
  212. var callable = getter.TryCast<ICallable>();
  213. value = callable.Call(this, Arguments.Empty);
  214. return true;
  215. }
  216. if (ReferenceEquals(Prototype, null))
  217. {
  218. return false;
  219. }
  220. return Prototype.TryGetValue(propertyName, out value);
  221. }
  222. /// <summary>
  223. /// Sets the specified named property to the value
  224. /// of the second parameter. The flag controls
  225. /// failure handling.
  226. /// </summary>
  227. /// <param name="propertyName"></param>
  228. /// <param name="value"></param>
  229. /// <param name="throwOnError"></param>
  230. public virtual void Put(string propertyName, JsValue value, bool throwOnError)
  231. {
  232. if (!CanPut(propertyName))
  233. {
  234. if (throwOnError)
  235. {
  236. throw new JavaScriptException(Engine.TypeError);
  237. }
  238. return;
  239. }
  240. var ownDesc = GetOwnProperty(propertyName);
  241. if (ownDesc.IsDataDescriptor())
  242. {
  243. ownDesc.Value = value;
  244. return;
  245. // as per specification
  246. // var valueDesc = new PropertyDescriptor(value: value, writable: null, enumerable: null, configurable: null);
  247. // DefineOwnProperty(propertyName, valueDesc, throwOnError);
  248. // return;
  249. }
  250. // property is an accessor or inherited
  251. var desc = GetProperty(propertyName);
  252. if (desc.IsAccessorDescriptor())
  253. {
  254. var setter = desc.Set.TryCast<ICallable>();
  255. setter.Call(this, new[] {value});
  256. }
  257. else
  258. {
  259. var newDesc = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  260. DefineOwnProperty(propertyName, newDesc, throwOnError);
  261. }
  262. }
  263. /// <summary>
  264. /// Returns a Boolean value indicating whether a
  265. /// [[Put]] operation with PropertyName can be
  266. /// performed.
  267. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.4
  268. /// </summary>
  269. /// <param name="propertyName"></param>
  270. /// <returns></returns>
  271. public bool CanPut(string propertyName)
  272. {
  273. var desc = GetOwnProperty(propertyName);
  274. if (desc != PropertyDescriptor.Undefined)
  275. {
  276. if (desc.IsAccessorDescriptor())
  277. {
  278. var set = desc.Set;
  279. if (ReferenceEquals(set, null) || set.IsUndefined())
  280. {
  281. return false;
  282. }
  283. return true;
  284. }
  285. return desc.Writable;
  286. }
  287. if (ReferenceEquals(Prototype, null))
  288. {
  289. return Extensible;
  290. }
  291. var inherited = Prototype.GetProperty(propertyName);
  292. if (inherited == PropertyDescriptor.Undefined)
  293. {
  294. return Extensible;
  295. }
  296. if (inherited.IsAccessorDescriptor())
  297. {
  298. var set = inherited.Set;
  299. if (ReferenceEquals(set, null) || set.IsUndefined())
  300. {
  301. return false;
  302. }
  303. return true;
  304. }
  305. if (!Extensible)
  306. {
  307. return false;
  308. }
  309. return inherited.Writable;
  310. }
  311. /// <summary>
  312. /// Returns a Boolean value indicating whether the
  313. /// object already has a property with the given
  314. /// name.
  315. /// </summary>
  316. /// <param name="propertyName"></param>
  317. /// <returns></returns>
  318. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  319. public bool HasProperty(string propertyName)
  320. {
  321. return GetProperty(propertyName) != PropertyDescriptor.Undefined;
  322. }
  323. /// <summary>
  324. /// Removes the specified named own property
  325. /// from the object. The flag controls failure
  326. /// handling.
  327. /// </summary>
  328. /// <param name="propertyName"></param>
  329. /// <param name="throwOnError"></param>
  330. /// <returns></returns>
  331. public virtual bool Delete(string propertyName, bool throwOnError)
  332. {
  333. var desc = GetOwnProperty(propertyName);
  334. if (desc == PropertyDescriptor.Undefined)
  335. {
  336. return true;
  337. }
  338. if (desc.Configurable)
  339. {
  340. RemoveOwnProperty(propertyName);
  341. return true;
  342. }
  343. else
  344. {
  345. if (throwOnError)
  346. {
  347. throw new JavaScriptException(Engine.TypeError);
  348. }
  349. return false;
  350. }
  351. }
  352. /// <summary>
  353. /// Hint is a String. Returns a default value for the
  354. /// object.
  355. /// </summary>
  356. /// <param name="hint"></param>
  357. /// <returns></returns>
  358. public JsValue DefaultValue(Types hint)
  359. {
  360. EnsureInitialized();
  361. if (hint == Types.String || (hint == Types.None && Class == "Date"))
  362. {
  363. var toString = Get("toString").TryCast<ICallable>();
  364. if (toString != null)
  365. {
  366. var str = toString.Call(this, Arguments.Empty);
  367. if (str.IsPrimitive())
  368. {
  369. return str;
  370. }
  371. }
  372. var valueOf = Get("valueOf").TryCast<ICallable>();
  373. if (valueOf != null)
  374. {
  375. var val = valueOf.Call(this, Arguments.Empty);
  376. if (val.IsPrimitive())
  377. {
  378. return val;
  379. }
  380. }
  381. throw new JavaScriptException(Engine.TypeError);
  382. }
  383. if (hint == Types.Number || hint == Types.None)
  384. {
  385. var valueOf = Get("valueOf").TryCast<ICallable>();
  386. if (valueOf != null)
  387. {
  388. var val = valueOf.Call(this, Arguments.Empty);
  389. if (val.IsPrimitive())
  390. {
  391. return val;
  392. }
  393. }
  394. var toString = Get("toString").TryCast<ICallable>();
  395. if (toString != null)
  396. {
  397. var str = toString.Call(this, Arguments.Empty);
  398. if (str.IsPrimitive())
  399. {
  400. return str;
  401. }
  402. }
  403. throw new JavaScriptException(Engine.TypeError);
  404. }
  405. return ToString();
  406. }
  407. /// <summary>
  408. /// Creates or alters the named own property to
  409. /// have the state described by a Property
  410. /// Descriptor. The flag controls failure handling.
  411. /// </summary>
  412. /// <param name="propertyName"></param>
  413. /// <param name="desc"></param>
  414. /// <param name="throwOnError"></param>
  415. /// <returns></returns>
  416. public virtual bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  417. {
  418. var current = GetOwnProperty(propertyName);
  419. if (current == desc)
  420. {
  421. return true;
  422. }
  423. var descValue = desc.Value;
  424. if (current == PropertyDescriptor.Undefined)
  425. {
  426. if (!Extensible)
  427. {
  428. if (throwOnError)
  429. {
  430. throw new JavaScriptException(Engine.TypeError);
  431. }
  432. return false;
  433. }
  434. else
  435. {
  436. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  437. {
  438. PropertyDescriptor propertyDescriptor;
  439. if (desc.Configurable && desc.Enumerable && desc.Writable)
  440. {
  441. propertyDescriptor = new PropertyDescriptor(descValue ?? Undefined, PropertyFlag.ConfigurableEnumerableWritable);
  442. }
  443. else if (!desc.Configurable && !desc.Enumerable && !desc.Writable)
  444. {
  445. propertyDescriptor = new PropertyDescriptor(descValue ?? Undefined, PropertyFlag.AllForbidden);
  446. }
  447. else
  448. {
  449. propertyDescriptor = new PropertyDescriptor(desc)
  450. {
  451. Value = descValue ?? Undefined
  452. };
  453. }
  454. SetOwnProperty(propertyName, propertyDescriptor);
  455. }
  456. else
  457. {
  458. SetOwnProperty(propertyName, new GetSetPropertyDescriptor(desc));
  459. }
  460. }
  461. return true;
  462. }
  463. // Step 5
  464. var currentGet = current.Get;
  465. var currentSet = current.Set;
  466. var currentValue = current.Value;
  467. if (!current.ConfigurableSet &&
  468. !current.EnumerableSet &&
  469. !current.WritableSet &&
  470. ReferenceEquals(currentGet, null) &&
  471. ReferenceEquals(currentSet, null) &&
  472. ReferenceEquals(currentValue, null))
  473. {
  474. return true;
  475. }
  476. // Step 6
  477. var descGet = desc.Get;
  478. var descSet = desc.Set;
  479. if (
  480. current.Configurable == desc.Configurable && current.ConfigurableSet == desc.ConfigurableSet &&
  481. current.Writable == desc.Writable && current.WritableSet == desc.WritableSet &&
  482. current.Enumerable == desc.Enumerable && current.EnumerableSet == desc.EnumerableSet &&
  483. ((ReferenceEquals(currentGet, null) && ReferenceEquals(descGet, null)) || (!ReferenceEquals(currentGet, null) && !ReferenceEquals(descGet, null) && ExpressionInterpreter.SameValue(currentGet, descGet))) &&
  484. ((ReferenceEquals(currentSet, null) && ReferenceEquals(descSet, null)) || (!ReferenceEquals(currentSet, null) && !ReferenceEquals(descSet, null) && ExpressionInterpreter.SameValue(currentSet, descSet))) &&
  485. ((ReferenceEquals(currentValue, null) && ReferenceEquals(descValue, null)) || (!ReferenceEquals(currentValue, null) && !ReferenceEquals(descValue, null) && ExpressionInterpreter.StrictlyEqual(currentValue, descValue)))
  486. )
  487. {
  488. return true;
  489. }
  490. if (!current.Configurable)
  491. {
  492. if (desc.Configurable)
  493. {
  494. if (throwOnError)
  495. {
  496. throw new JavaScriptException(Engine.TypeError);
  497. }
  498. return false;
  499. }
  500. if (desc.EnumerableSet && (desc.Enumerable != current.Enumerable))
  501. {
  502. if (throwOnError)
  503. {
  504. throw new JavaScriptException(Engine.TypeError);
  505. }
  506. return false;
  507. }
  508. }
  509. if (!desc.IsGenericDescriptor())
  510. {
  511. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  512. {
  513. if (!current.Configurable)
  514. {
  515. if (throwOnError)
  516. {
  517. throw new JavaScriptException(Engine.TypeError);
  518. }
  519. return false;
  520. }
  521. if (current.IsDataDescriptor())
  522. {
  523. var flags = current.Flags & ~(PropertyFlag.Writable | PropertyFlag.WritableSet);
  524. SetOwnProperty(propertyName, current = new GetSetPropertyDescriptor(
  525. get: JsValue.Undefined,
  526. set: JsValue.Undefined,
  527. flags
  528. ));
  529. }
  530. else
  531. {
  532. var flags = current.Flags & ~(PropertyFlag.Writable | PropertyFlag.WritableSet);
  533. SetOwnProperty(propertyName, current = new PropertyDescriptor(
  534. value: JsValue.Undefined,
  535. flags
  536. ));
  537. }
  538. }
  539. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  540. {
  541. if (!current.Configurable)
  542. {
  543. if (!current.Writable && desc.Writable)
  544. {
  545. if (throwOnError)
  546. {
  547. throw new JavaScriptException(Engine.TypeError);
  548. }
  549. return false;
  550. }
  551. if (!current.Writable)
  552. {
  553. if (!ReferenceEquals(descValue, null) && !ExpressionInterpreter.SameValue(descValue, currentValue))
  554. {
  555. if (throwOnError)
  556. {
  557. throw new JavaScriptException(Engine.TypeError);
  558. }
  559. return false;
  560. }
  561. }
  562. }
  563. }
  564. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  565. {
  566. if (!current.Configurable)
  567. {
  568. if ((!ReferenceEquals(descSet, null) && !ExpressionInterpreter.SameValue(descSet, currentSet ?? Undefined))
  569. ||
  570. (!ReferenceEquals(descGet, null) && !ExpressionInterpreter.SameValue(descGet, currentGet ?? Undefined)))
  571. {
  572. if (throwOnError)
  573. {
  574. throw new JavaScriptException(Engine.TypeError);
  575. }
  576. return false;
  577. }
  578. }
  579. }
  580. }
  581. if (!ReferenceEquals(descValue, null))
  582. {
  583. current.Value = descValue;
  584. }
  585. if (desc.WritableSet)
  586. {
  587. current.Writable = desc.Writable;
  588. }
  589. if (desc.EnumerableSet)
  590. {
  591. current.Enumerable = desc.Enumerable;
  592. }
  593. if (desc.ConfigurableSet)
  594. {
  595. current.Configurable = desc.Configurable;
  596. }
  597. PropertyDescriptor mutable = null;
  598. if (!ReferenceEquals(descGet, null))
  599. {
  600. mutable = new GetSetPropertyDescriptor(mutable ?? current);
  601. ((GetSetPropertyDescriptor) mutable).SetGet(descGet);
  602. }
  603. if (!ReferenceEquals(descSet, null))
  604. {
  605. mutable = new GetSetPropertyDescriptor(mutable ?? current);
  606. ((GetSetPropertyDescriptor) mutable).SetSet(descSet);
  607. }
  608. if (mutable != null)
  609. {
  610. // replace old with new type that supports get and set
  611. FastSetProperty(propertyName, mutable);
  612. }
  613. return true;
  614. }
  615. /// <summary>
  616. /// Optimized version of [[Put]] when the property is known to be undeclared already
  617. /// </summary>
  618. /// <param name="name"></param>
  619. /// <param name="value"></param>
  620. /// <param name="writable"></param>
  621. /// <param name="configurable"></param>
  622. /// <param name="enumerable"></param>
  623. public void FastAddProperty(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  624. {
  625. SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  626. }
  627. /// <summary>
  628. /// Optimized version of [[Put]] when the property is known to be already declared
  629. /// </summary>
  630. /// <param name="name"></param>
  631. /// <param name="value"></param>
  632. public void FastSetProperty(string name, PropertyDescriptor value)
  633. {
  634. SetOwnProperty(name, value);
  635. }
  636. protected virtual void EnsureInitialized()
  637. {
  638. }
  639. public override string ToString()
  640. {
  641. return TypeConverter.ToString(this);
  642. }
  643. public override object ToObject()
  644. {
  645. if (this is IObjectWrapper wrapper)
  646. {
  647. return wrapper.Target;
  648. }
  649. switch (Class)
  650. {
  651. case "Array":
  652. if (this is ArrayInstance arrayInstance)
  653. {
  654. var len = TypeConverter.ToInt32(arrayInstance.Get("length"));
  655. var result = new object[len];
  656. for (var k = 0; k < len; k++)
  657. {
  658. var pk = TypeConverter.ToString(k);
  659. var kpresent = arrayInstance.HasProperty(pk);
  660. if (kpresent)
  661. {
  662. var kvalue = arrayInstance.Get(pk);
  663. result[k] = kvalue.ToObject();
  664. }
  665. else
  666. {
  667. result[k] = null;
  668. }
  669. }
  670. return result;
  671. }
  672. break;
  673. case "String":
  674. if (this is StringInstance stringInstance)
  675. {
  676. return stringInstance.PrimitiveValue.AsStringWithoutTypeCheck();
  677. }
  678. break;
  679. case "Date":
  680. if (this is DateInstance dateInstance)
  681. {
  682. return dateInstance.ToDateTime();
  683. }
  684. break;
  685. case "Boolean":
  686. if (this is BooleanInstance booleanInstance)
  687. {
  688. return ((JsBoolean) booleanInstance.PrimitiveValue)._value
  689. ? JsBoolean.BoxedTrue
  690. : JsBoolean.BoxedFalse;
  691. }
  692. break;
  693. case "Function":
  694. if (this is FunctionInstance function)
  695. {
  696. return (Func<JsValue, JsValue[], JsValue>) function.Call;
  697. }
  698. break;
  699. case "Number":
  700. if (this is NumberInstance numberInstance)
  701. {
  702. return ((JsNumber) numberInstance.NumberData)._value;
  703. }
  704. break;
  705. case "RegExp":
  706. if (this is RegExpInstance regeExpInstance)
  707. {
  708. return regeExpInstance.Value;
  709. }
  710. break;
  711. case "Arguments":
  712. case "Object":
  713. #if __IOS__
  714. IDictionary<string, object> o = new Dictionary<string, object>();
  715. #else
  716. IDictionary<string, object> o = new ExpandoObject();
  717. #endif
  718. foreach (var p in GetOwnProperties())
  719. {
  720. if (!p.Value.Enumerable)
  721. {
  722. continue;
  723. }
  724. o.Add(p.Key, Get(p.Key).ToObject());
  725. }
  726. return o;
  727. }
  728. return this;
  729. }
  730. /// <summary>
  731. /// Handles the generic find of (callback[, thisArg])
  732. /// </summary>
  733. internal virtual bool FindWithCallback(
  734. JsValue[] arguments,
  735. out uint index,
  736. out JsValue value)
  737. {
  738. uint GetLength()
  739. {
  740. var desc = GetProperty("length");
  741. var descValue = desc.Value;
  742. if (desc.IsDataDescriptor() && !ReferenceEquals(descValue, null))
  743. {
  744. return TypeConverter.ToUint32(descValue);
  745. }
  746. var getter = desc.Get ?? Undefined;
  747. if (getter.IsUndefined())
  748. {
  749. return 0;
  750. }
  751. // if getter is not undefined it must be ICallable
  752. return TypeConverter.ToUint32(((ICallable) getter).Call(this, Arguments.Empty));
  753. }
  754. bool TryGetValue(uint idx, out JsValue jsValue)
  755. {
  756. var property = TypeConverter.ToString(idx);
  757. var kPresent = HasProperty(property);
  758. jsValue = kPresent ? Get(property) : Undefined;
  759. return kPresent;
  760. }
  761. var len = GetLength();
  762. if (len == 0)
  763. {
  764. index = 0;
  765. value = Undefined;
  766. return false;
  767. }
  768. var callbackfn = arguments.At(0);
  769. var thisArg = arguments.At(1);
  770. var callable = GetCallable(callbackfn);
  771. var args = Engine.JsValueArrayPool.RentArray(3);
  772. for (uint k = 0; k < len; k++)
  773. {
  774. if (TryGetValue(k, out var kvalue))
  775. {
  776. args[0] = kvalue;
  777. args[1] = k;
  778. args[2] = this;
  779. var testResult = callable.Call(thisArg, args);
  780. if (TypeConverter.ToBoolean(testResult))
  781. {
  782. index = k;
  783. value = kvalue;
  784. return true;
  785. }
  786. }
  787. }
  788. Engine.JsValueArrayPool.ReturnArray(args);
  789. index = 0;
  790. value = Undefined;
  791. return false;
  792. }
  793. protected ICallable GetCallable(JsValue source)
  794. {
  795. if (source is ICallable callable)
  796. {
  797. return callable;
  798. }
  799. throw new JavaScriptException(Engine.TypeError, "Argument must be callable");
  800. }
  801. public override bool Equals(JsValue obj)
  802. {
  803. if (ReferenceEquals(null, obj))
  804. {
  805. return false;
  806. }
  807. if (!(obj is ObjectInstance s))
  808. {
  809. return false;
  810. }
  811. return Equals(s);
  812. }
  813. public bool Equals(ObjectInstance other)
  814. {
  815. if (ReferenceEquals(null, other))
  816. {
  817. return false;
  818. }
  819. if (ReferenceEquals(this, other))
  820. {
  821. return true;
  822. }
  823. return false;
  824. }
  825. internal void Clear()
  826. {
  827. _intrinsicProperties?.Clear();
  828. _properties?.Clear();
  829. }
  830. }
  831. }