ObjectInstance.cs 31 KB

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