ObjectInstance.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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.HasValue && desc.Writable.Value;
  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. else
  302. {
  303. return inherited.Writable.HasValue && inherited.Writable.Value;
  304. }
  305. }
  306. /// <summary>
  307. /// Returns a Boolean value indicating whether the
  308. /// object already has a property with the given
  309. /// name.
  310. /// </summary>
  311. /// <param name="propertyName"></param>
  312. /// <returns></returns>
  313. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  314. public bool HasProperty(string propertyName)
  315. {
  316. return GetProperty(propertyName) != PropertyDescriptor.Undefined;
  317. }
  318. /// <summary>
  319. /// Removes the specified named own property
  320. /// from the object. The flag controls failure
  321. /// handling.
  322. /// </summary>
  323. /// <param name="propertyName"></param>
  324. /// <param name="throwOnError"></param>
  325. /// <returns></returns>
  326. public virtual bool Delete(string propertyName, bool throwOnError)
  327. {
  328. var desc = GetOwnProperty(propertyName);
  329. if (desc == PropertyDescriptor.Undefined)
  330. {
  331. return true;
  332. }
  333. if (desc.Configurable.HasValue && desc.Configurable.Value)
  334. {
  335. RemoveOwnProperty(propertyName);
  336. return true;
  337. }
  338. else
  339. {
  340. if (throwOnError)
  341. {
  342. throw new JavaScriptException(Engine.TypeError);
  343. }
  344. return false;
  345. }
  346. }
  347. /// <summary>
  348. /// Hint is a String. Returns a default value for the
  349. /// object.
  350. /// </summary>
  351. /// <param name="hint"></param>
  352. /// <returns></returns>
  353. public JsValue DefaultValue(Types hint)
  354. {
  355. EnsureInitialized();
  356. if (hint == Types.String || (hint == Types.None && Class == "Date"))
  357. {
  358. var toString = Get("toString").TryCast<ICallable>();
  359. if (toString != null)
  360. {
  361. var str = toString.Call(this, Arguments.Empty);
  362. if (str.IsPrimitive())
  363. {
  364. return str;
  365. }
  366. }
  367. var valueOf = Get("valueOf").TryCast<ICallable>();
  368. if (valueOf != null)
  369. {
  370. var val = valueOf.Call(this, Arguments.Empty);
  371. if (val.IsPrimitive())
  372. {
  373. return val;
  374. }
  375. }
  376. throw new JavaScriptException(Engine.TypeError);
  377. }
  378. if (hint == Types.Number || hint == Types.None)
  379. {
  380. var valueOf = Get("valueOf").TryCast<ICallable>();
  381. if (valueOf != null)
  382. {
  383. var val = valueOf.Call(this, Arguments.Empty);
  384. if (val.IsPrimitive())
  385. {
  386. return val;
  387. }
  388. }
  389. var toString = Get("toString").TryCast<ICallable>();
  390. if (toString != null)
  391. {
  392. var str = toString.Call(this, Arguments.Empty);
  393. if (str.IsPrimitive())
  394. {
  395. return str;
  396. }
  397. }
  398. throw new JavaScriptException(Engine.TypeError);
  399. }
  400. return ToString();
  401. }
  402. /// <summary>
  403. /// Creates or alters the named own property to
  404. /// have the state described by a Property
  405. /// Descriptor. The flag controls failure handling.
  406. /// </summary>
  407. /// <param name="propertyName"></param>
  408. /// <param name="desc"></param>
  409. /// <param name="throwOnError"></param>
  410. /// <returns></returns>
  411. public virtual bool DefineOwnProperty(string propertyName, IPropertyDescriptor desc, bool throwOnError)
  412. {
  413. var current = GetOwnProperty(propertyName);
  414. if (current == desc)
  415. {
  416. return true;
  417. }
  418. if (current == PropertyDescriptor.Undefined)
  419. {
  420. if (!Extensible)
  421. {
  422. if (throwOnError)
  423. {
  424. throw new JavaScriptException(Engine.TypeError);
  425. }
  426. return false;
  427. }
  428. else
  429. {
  430. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  431. {
  432. IPropertyDescriptor propertyDescriptor;
  433. if (desc.Configurable.GetValueOrDefault() && desc.Enumerable.GetValueOrDefault() && desc.Writable.GetValueOrDefault())
  434. {
  435. propertyDescriptor = new ConfigurableEnumerableWritablePropertyDescriptor(desc.Value != null ? desc.Value : Undefined);
  436. }
  437. else if (!desc.Configurable.GetValueOrDefault() && !desc.Enumerable.GetValueOrDefault() && !desc.Writable.GetValueOrDefault())
  438. {
  439. propertyDescriptor = new AllForbiddenPropertyDescriptor(desc.Value != null ? desc.Value : Undefined);
  440. }
  441. else
  442. {
  443. propertyDescriptor = new PropertyDescriptor(desc)
  444. {
  445. Value = desc.Value != null ? desc.Value : Undefined,
  446. Writable = desc.Writable.HasValue ? desc.Writable.Value : false,
  447. Enumerable = desc.Enumerable.HasValue ? desc.Enumerable.Value : false,
  448. Configurable = desc.Configurable.HasValue ? desc.Configurable.Value : false
  449. };
  450. }
  451. SetOwnProperty(propertyName, propertyDescriptor);
  452. }
  453. else
  454. {
  455. SetOwnProperty(propertyName, new PropertyDescriptor(desc)
  456. {
  457. Get = desc.Get,
  458. Set = desc.Set,
  459. Enumerable = desc.Enumerable.HasValue ? desc.Enumerable : false,
  460. Configurable = desc.Configurable.HasValue ? desc.Configurable : false,
  461. });
  462. }
  463. }
  464. return true;
  465. }
  466. // Step 5
  467. if (!current.Configurable.HasValue &&
  468. !current.Enumerable.HasValue &&
  469. !current.Writable.HasValue &&
  470. current.Get == null &&
  471. current.Set == null &&
  472. current.Value == null)
  473. {
  474. return true;
  475. }
  476. // Step 6
  477. if (
  478. current.Configurable == desc.Configurable &&
  479. current.Writable == desc.Writable &&
  480. current.Enumerable == desc.Enumerable &&
  481. ((current.Get == null && desc.Get == null) || (current.Get != null && desc.Get != null && ExpressionInterpreter.SameValue(current.Get, desc.Get))) &&
  482. ((current.Set == null && desc.Set == null) || (current.Set != null && desc.Set != null && ExpressionInterpreter.SameValue(current.Set, desc.Set))) &&
  483. ((current.Value == null && desc.Value == null) || (current.Value != null && desc.Value != null && ExpressionInterpreter.StrictlyEqual(current.Value, desc.Value)))
  484. )
  485. {
  486. return true;
  487. }
  488. if (!current.Configurable.HasValue || !current.Configurable.Value)
  489. {
  490. if (desc.Configurable.HasValue && desc.Configurable.Value)
  491. {
  492. if (throwOnError)
  493. {
  494. throw new JavaScriptException(Engine.TypeError);
  495. }
  496. return false;
  497. }
  498. if (desc.Enumerable.HasValue && (!current.Enumerable.HasValue || desc.Enumerable.Value != current.Enumerable.Value))
  499. {
  500. if (throwOnError)
  501. {
  502. throw new JavaScriptException(Engine.TypeError);
  503. }
  504. return false;
  505. }
  506. }
  507. if (!desc.IsGenericDescriptor())
  508. {
  509. if (current.IsDataDescriptor() != desc.IsDataDescriptor())
  510. {
  511. if (!current.Configurable.HasValue || !current.Configurable.Value)
  512. {
  513. if (throwOnError)
  514. {
  515. throw new JavaScriptException(Engine.TypeError);
  516. }
  517. return false;
  518. }
  519. if (current.IsDataDescriptor())
  520. {
  521. SetOwnProperty(propertyName, current = new PropertyDescriptor(
  522. get: JsValue.Undefined,
  523. set: JsValue.Undefined,
  524. enumerable: current.Enumerable,
  525. configurable: current.Configurable
  526. ));
  527. }
  528. else
  529. {
  530. SetOwnProperty(propertyName, current = new PropertyDescriptor(
  531. value: JsValue.Undefined,
  532. writable: null,
  533. enumerable: current.Enumerable,
  534. configurable: current.Configurable
  535. ));
  536. }
  537. }
  538. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  539. {
  540. if (!current.Configurable.HasValue || current.Configurable.Value == false)
  541. {
  542. if (!current.Writable.HasValue || !current.Writable.Value && desc.Writable.HasValue && desc.Writable.Value)
  543. {
  544. if (throwOnError)
  545. {
  546. throw new JavaScriptException(Engine.TypeError);
  547. }
  548. return false;
  549. }
  550. if (!current.Writable.Value)
  551. {
  552. if (desc.Value != null && !ExpressionInterpreter.SameValue(desc.Value, current.Value))
  553. {
  554. if (throwOnError)
  555. {
  556. throw new JavaScriptException(Engine.TypeError);
  557. }
  558. return false;
  559. }
  560. }
  561. }
  562. }
  563. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  564. {
  565. if (!current.Configurable.HasValue || !current.Configurable.Value)
  566. {
  567. if ((desc.Set != null && !ExpressionInterpreter.SameValue(desc.Set, current.Set != null ? current.Set : Undefined))
  568. ||
  569. (desc.Get != null && !ExpressionInterpreter.SameValue(desc.Get, current.Get != null ? current.Get : Undefined)))
  570. {
  571. if (throwOnError)
  572. {
  573. throw new JavaScriptException(Engine.TypeError);
  574. }
  575. return false;
  576. }
  577. }
  578. }
  579. }
  580. if (desc.Value != null)
  581. {
  582. current.Value = desc.Value;
  583. }
  584. PropertyDescriptor mutable = null;
  585. if (desc.Writable.HasValue)
  586. {
  587. current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  588. mutable.Writable = desc.Writable;
  589. }
  590. if (desc.Enumerable.HasValue)
  591. {
  592. current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  593. mutable.Enumerable = desc.Enumerable;
  594. }
  595. if (desc.Configurable.HasValue)
  596. {
  597. current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  598. mutable.Configurable = desc.Configurable;
  599. }
  600. if (desc.Get != null)
  601. {
  602. current = mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  603. mutable.Get = desc.Get;
  604. }
  605. if (desc.Set != null)
  606. {
  607. mutable = current as PropertyDescriptor ?? new PropertyDescriptor(current);
  608. mutable.Set = desc.Set;
  609. }
  610. if (mutable != null)
  611. {
  612. FastSetProperty(propertyName, mutable);
  613. }
  614. return true;
  615. }
  616. /// <summary>
  617. /// Optimized version of [[Put]] when the property is known to be undeclared already
  618. /// </summary>
  619. /// <param name="name"></param>
  620. /// <param name="value"></param>
  621. /// <param name="writable"></param>
  622. /// <param name="configurable"></param>
  623. /// <param name="enumerable"></param>
  624. public void FastAddProperty(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  625. {
  626. SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  627. }
  628. /// <summary>
  629. /// Optimized version of [[Put]] when the property is known to be already declared
  630. /// </summary>
  631. /// <param name="name"></param>
  632. /// <param name="value"></param>
  633. public void FastSetProperty(string name, IPropertyDescriptor value)
  634. {
  635. SetOwnProperty(name, value);
  636. }
  637. protected virtual void EnsureInitialized()
  638. {
  639. }
  640. public override string ToString()
  641. {
  642. return TypeConverter.ToString(this);
  643. }
  644. public override Types Type => Types.Object;
  645. [Pure]
  646. public override bool IsArray()
  647. {
  648. return this is ArrayInstance;
  649. }
  650. [Pure]
  651. public override bool IsDate()
  652. {
  653. return this is DateInstance;
  654. }
  655. [Pure]
  656. public override bool IsRegExp()
  657. {
  658. return this is RegExpInstance;
  659. }
  660. [Pure]
  661. public override ObjectInstance AsObject()
  662. {
  663. return this;
  664. }
  665. [Pure]
  666. public override TInstance AsInstance<TInstance>()
  667. {
  668. return this as TInstance;
  669. }
  670. [Pure]
  671. public override ArrayInstance AsArray()
  672. {
  673. if (!IsArray())
  674. {
  675. throw new ArgumentException("The value is not an array");
  676. }
  677. return this as ArrayInstance;
  678. }
  679. [Pure]
  680. public override DateInstance AsDate()
  681. {
  682. if (!IsDate())
  683. {
  684. throw new ArgumentException("The value is not a date");
  685. }
  686. return this as DateInstance;
  687. }
  688. [Pure]
  689. public override RegExpInstance AsRegExp()
  690. {
  691. if (!IsRegExp())
  692. {
  693. throw new ArgumentException("The value is not a regex");
  694. }
  695. return this as RegExpInstance;
  696. }
  697. public override bool Is<T>()
  698. {
  699. return this is T;
  700. }
  701. public override T As<T>()
  702. {
  703. return this as T;
  704. }
  705. public override object ToObject()
  706. {
  707. if (this is IObjectWrapper wrapper)
  708. {
  709. return wrapper.Target;
  710. }
  711. switch (Class)
  712. {
  713. case "Array":
  714. if (this is ArrayInstance arrayInstance)
  715. {
  716. var len = TypeConverter.ToInt32(arrayInstance.Get("length"));
  717. var result = new object[len];
  718. for (var k = 0; k < len; k++)
  719. {
  720. var pk = TypeConverter.ToString(k);
  721. var kpresent = arrayInstance.HasProperty(pk);
  722. if (kpresent)
  723. {
  724. var kvalue = arrayInstance.Get(pk);
  725. result[k] = kvalue.ToObject();
  726. }
  727. else
  728. {
  729. result[k] = null;
  730. }
  731. }
  732. return result;
  733. }
  734. break;
  735. case "String":
  736. if (this is StringInstance stringInstance)
  737. {
  738. return stringInstance.PrimitiveValue.AsString();
  739. }
  740. break;
  741. case "Date":
  742. if (this is DateInstance dateInstance)
  743. {
  744. return dateInstance.ToDateTime();
  745. }
  746. break;
  747. case "Boolean":
  748. if (this is BooleanInstance booleanInstance)
  749. {
  750. return booleanInstance.PrimitiveValue.AsBoolean();
  751. }
  752. break;
  753. case "Function":
  754. if (this is FunctionInstance function)
  755. {
  756. return (Func<JsValue, JsValue[], JsValue>) function.Call;
  757. }
  758. break;
  759. case "Number":
  760. if (this is NumberInstance numberInstance)
  761. {
  762. return numberInstance.NumberData.AsNumber();
  763. }
  764. break;
  765. case "RegExp":
  766. if (this is RegExpInstance regeExpInstance)
  767. {
  768. return regeExpInstance.Value;
  769. }
  770. break;
  771. case "Arguments":
  772. case "Object":
  773. #if __IOS__
  774. IDictionary<string, object> o = new Dictionary<string, object>();
  775. #else
  776. IDictionary<string, object> o = new ExpandoObject();
  777. #endif
  778. foreach (var p in GetOwnProperties())
  779. {
  780. if (!p.Value.Enumerable.HasValue || p.Value.Enumerable.Value == false)
  781. {
  782. continue;
  783. }
  784. o.Add(p.Key, Get(p.Key).ToObject());
  785. }
  786. return o;
  787. }
  788. return this;
  789. }
  790. public override bool Equals(JsValue obj)
  791. {
  792. if (ReferenceEquals(null, obj))
  793. {
  794. return false;
  795. }
  796. if (!(obj is ObjectInstance s))
  797. {
  798. return false;
  799. }
  800. return Equals(s);
  801. }
  802. public bool Equals(ObjectInstance other)
  803. {
  804. if (ReferenceEquals(null, other))
  805. {
  806. return false;
  807. }
  808. if (ReferenceEquals(this, other))
  809. {
  810. return true;
  811. }
  812. return false;
  813. }
  814. internal void Clear()
  815. {
  816. _intrinsicProperties?.Clear();
  817. _properties?.Clear();
  818. }
  819. }
  820. }