ObjectInstance.cs 30 KB

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