ObjectInstance.cs 29 KB

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