ObjectInstance.cs 30 KB

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