ObjectInstance.cs 31 KB

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