ObjectInstance.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using System.Runtime.CompilerServices;
  5. using Jint.Native.Array;
  6. using Jint.Native.Boolean;
  7. using Jint.Native.Date;
  8. using Jint.Native.Function;
  9. using Jint.Native.Number;
  10. using Jint.Native.RegExp;
  11. using Jint.Native.String;
  12. using Jint.Runtime;
  13. using Jint.Runtime.Descriptors;
  14. using Jint.Runtime.Descriptors.Specialized;
  15. using Jint.Runtime.Interop;
  16. namespace Jint.Native.Object
  17. {
  18. public class ObjectInstance : JsValue, IEquatable<ObjectInstance>
  19. {
  20. private Dictionary<string, PropertyDescriptor> _intrinsicProperties;
  21. private MruPropertyCache2<PropertyDescriptor> _properties;
  22. public ObjectInstance(Engine engine) : base(Types.Object)
  23. {
  24. Engine = engine;
  25. }
  26. public Engine Engine { get; }
  27. protected bool TryGetIntrinsicValue(JsSymbol symbol, out JsValue value)
  28. {
  29. if (_intrinsicProperties != null && _intrinsicProperties.TryGetValue(symbol.AsSymbol(), out var descriptor))
  30. {
  31. value = descriptor.Value;
  32. return true;
  33. }
  34. if (ReferenceEquals(Prototype, null))
  35. {
  36. value = Undefined;
  37. return false;
  38. }
  39. return Prototype.TryGetIntrinsicValue(symbol, out value);
  40. }
  41. public void SetIntrinsicValue(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  42. {
  43. SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  44. }
  45. protected void SetIntrinsicValue(JsSymbol symbol, JsValue value, bool writable, bool enumerable, bool configurable)
  46. {
  47. if (_intrinsicProperties == null)
  48. {
  49. _intrinsicProperties = new Dictionary<string, PropertyDescriptor>();
  50. }
  51. _intrinsicProperties[symbol.AsSymbol()] = new PropertyDescriptor(value, writable, enumerable, configurable);
  52. }
  53. /// <summary>
  54. /// The prototype of this object.
  55. /// </summary>
  56. public ObjectInstance Prototype { get; set; }
  57. /// <summary>
  58. /// If true, own properties may be added to the
  59. /// object.
  60. /// </summary>
  61. public bool Extensible { get; set; }
  62. /// <summary>
  63. /// A String value indicating a specification defined
  64. /// classification of objects.
  65. /// </summary>
  66. public virtual string Class => "Object";
  67. public virtual IEnumerable<KeyValuePair<string, PropertyDescriptor>> GetOwnProperties()
  68. {
  69. EnsureInitialized();
  70. if (_properties != null)
  71. {
  72. foreach (var pair in _properties.GetEnumerator())
  73. {
  74. yield return pair;
  75. }
  76. }
  77. }
  78. protected virtual void AddProperty(string propertyName, PropertyDescriptor descriptor)
  79. {
  80. if (_properties == null)
  81. {
  82. _properties = new MruPropertyCache2<PropertyDescriptor>();
  83. }
  84. _properties.Add(propertyName, descriptor);
  85. }
  86. protected virtual bool TryGetProperty(string propertyName, out PropertyDescriptor descriptor)
  87. {
  88. if (_properties == null)
  89. {
  90. descriptor = null;
  91. return false;
  92. }
  93. return _properties.TryGetValue(propertyName, out descriptor);
  94. }
  95. public virtual bool HasOwnProperty(string propertyName)
  96. {
  97. EnsureInitialized();
  98. return _properties?.ContainsKey(propertyName) ?? false;
  99. }
  100. public virtual void RemoveOwnProperty(string propertyName)
  101. {
  102. EnsureInitialized();
  103. _properties?.Remove(propertyName);
  104. }
  105. /// <summary>
  106. /// Returns the value of the named property.
  107. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3
  108. /// </summary>
  109. /// <param name="propertyName"></param>
  110. /// <returns></returns>
  111. public virtual JsValue Get(string propertyName)
  112. {
  113. var desc = GetProperty(propertyName);
  114. return UnwrapJsValue(desc);
  115. }
  116. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  117. internal JsValue UnwrapJsValue(PropertyDescriptor desc)
  118. {
  119. if (desc == PropertyDescriptor.Undefined)
  120. {
  121. return Undefined;
  122. }
  123. if (desc.IsDataDescriptor())
  124. {
  125. var val = desc.Value;
  126. return !ReferenceEquals(val, null) ? val : Undefined;
  127. }
  128. var getter = !ReferenceEquals(desc.Get, null) ? desc.Get : Undefined;
  129. if (getter.IsUndefined())
  130. {
  131. return Undefined;
  132. }
  133. // if getter is not undefined it must be ICallable
  134. var callable = getter.TryCast<ICallable>();
  135. return callable.Call(this, Arguments.Empty);
  136. }
  137. /// <summary>
  138. /// Returns the Property Descriptor of the named
  139. /// own property of this object, or undefined if
  140. /// absent.
  141. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.1
  142. /// </summary>
  143. /// <param name="propertyName"></param>
  144. /// <returns></returns>
  145. public virtual PropertyDescriptor GetOwnProperty(string propertyName)
  146. {
  147. EnsureInitialized();
  148. if (_properties != null && _properties.TryGetValue(propertyName, out var x))
  149. {
  150. return x;
  151. }
  152. return PropertyDescriptor.Undefined;
  153. }
  154. protected internal virtual void SetOwnProperty(string propertyName, PropertyDescriptor desc)
  155. {
  156. EnsureInitialized();
  157. if (_properties == null)
  158. {
  159. _properties = new MruPropertyCache2<PropertyDescriptor>();
  160. }
  161. _properties[propertyName] = desc;
  162. }
  163. /// <summary>
  164. /// http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.2
  165. /// </summary>
  166. /// <param name="propertyName"></param>
  167. /// <returns></returns>
  168. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  169. public PropertyDescriptor GetProperty(string propertyName)
  170. {
  171. var prop = GetOwnProperty(propertyName);
  172. if (prop != PropertyDescriptor.Undefined)
  173. {
  174. return prop;
  175. }
  176. if (ReferenceEquals(Prototype, null))
  177. {
  178. return PropertyDescriptor.Undefined;
  179. }
  180. return Prototype.GetProperty(propertyName);
  181. }
  182. public bool TryGetValue(string propertyName, out JsValue value)
  183. {
  184. value = Undefined;
  185. var desc = GetOwnProperty(propertyName);
  186. if (desc != null && desc != PropertyDescriptor.Undefined)
  187. {
  188. if (desc == PropertyDescriptor.Undefined)
  189. {
  190. return false;
  191. }
  192. var descValue = desc.Value;
  193. if (desc.WritableSet && !ReferenceEquals(descValue, null))
  194. {
  195. value = descValue;
  196. return true;
  197. }
  198. var getter = 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 (ReferenceEquals(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. var set = desc.Set;
  272. if (ReferenceEquals(set, null) || set.IsUndefined())
  273. {
  274. return false;
  275. }
  276. return true;
  277. }
  278. return desc.Writable;
  279. }
  280. if (ReferenceEquals(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. var set = inherited.Set;
  292. if (ReferenceEquals(set, null) || set.IsUndefined())
  293. {
  294. return false;
  295. }
  296. return true;
  297. }
  298. if (!Extensible)
  299. {
  300. return false;
  301. }
  302. return inherited.Writable;
  303. }
  304. /// <summary>
  305. /// Returns a Boolean value indicating whether the
  306. /// object already has a property with the given
  307. /// name.
  308. /// </summary>
  309. /// <param name="propertyName"></param>
  310. /// <returns></returns>
  311. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  312. public bool HasProperty(string propertyName)
  313. {
  314. return GetProperty(propertyName) != PropertyDescriptor.Undefined;
  315. }
  316. /// <summary>
  317. /// Removes the specified named own property
  318. /// from the object. The flag controls failure
  319. /// handling.
  320. /// </summary>
  321. /// <param name="propertyName"></param>
  322. /// <param name="throwOnError"></param>
  323. /// <returns></returns>
  324. public virtual bool Delete(string propertyName, bool throwOnError)
  325. {
  326. var desc = GetOwnProperty(propertyName);
  327. if (desc == PropertyDescriptor.Undefined)
  328. {
  329. return true;
  330. }
  331. if (desc.Configurable)
  332. {
  333. RemoveOwnProperty(propertyName);
  334. return true;
  335. }
  336. else
  337. {
  338. if (throwOnError)
  339. {
  340. throw new JavaScriptException(Engine.TypeError);
  341. }
  342. return false;
  343. }
  344. }
  345. /// <summary>
  346. /// Hint is a String. Returns a default value for the
  347. /// object.
  348. /// </summary>
  349. /// <param name="hint"></param>
  350. /// <returns></returns>
  351. public JsValue DefaultValue(Types hint)
  352. {
  353. EnsureInitialized();
  354. if (hint == Types.String || (hint == Types.None && Class == "Date"))
  355. {
  356. var toString = Get("toString").TryCast<ICallable>();
  357. if (toString != null)
  358. {
  359. var str = toString.Call(this, Arguments.Empty);
  360. if (str.IsPrimitive())
  361. {
  362. return str;
  363. }
  364. }
  365. var valueOf = Get("valueOf").TryCast<ICallable>();
  366. if (valueOf != null)
  367. {
  368. var val = valueOf.Call(this, Arguments.Empty);
  369. if (val.IsPrimitive())
  370. {
  371. return val;
  372. }
  373. }
  374. throw new JavaScriptException(Engine.TypeError);
  375. }
  376. if (hint == Types.Number || hint == Types.None)
  377. {
  378. var valueOf = Get("valueOf").TryCast<ICallable>();
  379. if (valueOf != null)
  380. {
  381. var val = valueOf.Call(this, Arguments.Empty);
  382. if (val.IsPrimitive())
  383. {
  384. return val;
  385. }
  386. }
  387. var toString = Get("toString").TryCast<ICallable>();
  388. if (toString != null)
  389. {
  390. var str = toString.Call(this, Arguments.Empty);
  391. if (str.IsPrimitive())
  392. {
  393. return str;
  394. }
  395. }
  396. throw new JavaScriptException(Engine.TypeError);
  397. }
  398. return ToString();
  399. }
  400. /// <summary>
  401. /// Creates or alters the named own property to
  402. /// have the state described by a Property
  403. /// Descriptor. The flag controls failure handling.
  404. /// </summary>
  405. /// <param name="propertyName"></param>
  406. /// <param name="desc"></param>
  407. /// <param name="throwOnError"></param>
  408. /// <returns></returns>
  409. public virtual bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
  410. {
  411. var current = GetOwnProperty(propertyName);
  412. if (current == desc)
  413. {
  414. return true;
  415. }
  416. var descValue = desc.Value;
  417. if (current == PropertyDescriptor.Undefined)
  418. {
  419. if (!Extensible)
  420. {
  421. if (throwOnError)
  422. {
  423. throw new JavaScriptException(Engine.TypeError);
  424. }
  425. return false;
  426. }
  427. else
  428. {
  429. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  430. {
  431. PropertyDescriptor propertyDescriptor;
  432. if (desc.Configurable && desc.Enumerable && desc.Writable)
  433. {
  434. propertyDescriptor = new PropertyDescriptor(descValue ?? Undefined, PropertyFlag.ConfigurableEnumerableWritable);
  435. }
  436. else if (!desc.Configurable && !desc.Enumerable && !desc.Writable)
  437. {
  438. propertyDescriptor = new PropertyDescriptor(descValue ?? Undefined, PropertyFlag.AllForbidden);
  439. }
  440. else
  441. {
  442. propertyDescriptor = new PropertyDescriptor(desc)
  443. {
  444. Value = descValue ?? Undefined
  445. };
  446. }
  447. SetOwnProperty(propertyName, propertyDescriptor);
  448. }
  449. else
  450. {
  451. SetOwnProperty(propertyName, new GetSetPropertyDescriptor(desc));
  452. }
  453. }
  454. return true;
  455. }
  456. // Step 5
  457. var currentGet = current.Get;
  458. var currentSet = current.Set;
  459. var currentValue = current.Value;
  460. if (!current.ConfigurableSet &&
  461. !current.EnumerableSet &&
  462. !current.WritableSet &&
  463. ReferenceEquals(currentGet, null) &&
  464. ReferenceEquals(currentSet, null) &&
  465. ReferenceEquals(currentValue, null))
  466. {
  467. return true;
  468. }
  469. // Step 6
  470. var descGet = desc.Get;
  471. var descSet = desc.Set;
  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. ((ReferenceEquals(currentGet, null) && ReferenceEquals(descGet, null)) || (!ReferenceEquals(currentGet, null) && !ReferenceEquals(descGet, null) && ExpressionInterpreter.SameValue(currentGet, descGet))) &&
  477. ((ReferenceEquals(currentSet, null) && ReferenceEquals(descSet, null)) || (!ReferenceEquals(currentSet, null) && !ReferenceEquals(descSet, null) && ExpressionInterpreter.SameValue(currentSet, descSet))) &&
  478. ((ReferenceEquals(currentValue, null) && ReferenceEquals(descValue, null)) || (!ReferenceEquals(currentValue, null) && !ReferenceEquals(descValue, null) && ExpressionInterpreter.StrictlyEqual(currentValue, descValue)))
  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. var flags = current.Flags & ~(PropertyFlag.Writable | PropertyFlag.WritableSet);
  517. SetOwnProperty(propertyName, current = new GetSetPropertyDescriptor(
  518. get: JsValue.Undefined,
  519. set: JsValue.Undefined,
  520. flags
  521. ));
  522. }
  523. else
  524. {
  525. var flags = current.Flags & ~(PropertyFlag.Writable | PropertyFlag.WritableSet);
  526. SetOwnProperty(propertyName, current = new PropertyDescriptor(
  527. value: JsValue.Undefined,
  528. flags
  529. ));
  530. }
  531. }
  532. else if (current.IsDataDescriptor() && desc.IsDataDescriptor())
  533. {
  534. if (!current.Configurable)
  535. {
  536. if (!current.Writable && desc.Writable)
  537. {
  538. if (throwOnError)
  539. {
  540. throw new JavaScriptException(Engine.TypeError);
  541. }
  542. return false;
  543. }
  544. if (!current.Writable)
  545. {
  546. if (!ReferenceEquals(descValue, null) && !ExpressionInterpreter.SameValue(descValue, currentValue))
  547. {
  548. if (throwOnError)
  549. {
  550. throw new JavaScriptException(Engine.TypeError);
  551. }
  552. return false;
  553. }
  554. }
  555. }
  556. }
  557. else if (current.IsAccessorDescriptor() && desc.IsAccessorDescriptor())
  558. {
  559. if (!current.Configurable)
  560. {
  561. if ((!ReferenceEquals(descSet, null) && !ExpressionInterpreter.SameValue(descSet, currentSet ?? Undefined))
  562. ||
  563. (!ReferenceEquals(descGet, null) && !ExpressionInterpreter.SameValue(descGet, currentGet ?? Undefined)))
  564. {
  565. if (throwOnError)
  566. {
  567. throw new JavaScriptException(Engine.TypeError);
  568. }
  569. return false;
  570. }
  571. }
  572. }
  573. }
  574. if (!ReferenceEquals(descValue, null))
  575. {
  576. current.Value = descValue;
  577. }
  578. if (desc.WritableSet)
  579. {
  580. current.Writable = desc.Writable;
  581. }
  582. if (desc.EnumerableSet)
  583. {
  584. current.Enumerable = desc.Enumerable;
  585. }
  586. if (desc.ConfigurableSet)
  587. {
  588. current.Configurable = desc.Configurable;
  589. }
  590. PropertyDescriptor mutable = null;
  591. if (!ReferenceEquals(descGet, null))
  592. {
  593. mutable = new GetSetPropertyDescriptor(mutable ?? current);
  594. ((GetSetPropertyDescriptor) mutable).SetGet(descGet);
  595. }
  596. if (!ReferenceEquals(descSet, null))
  597. {
  598. mutable = new GetSetPropertyDescriptor(mutable ?? current);
  599. ((GetSetPropertyDescriptor) mutable).SetSet(descSet);
  600. }
  601. if (mutable != null)
  602. {
  603. // replace old with new type that supports get and set
  604. FastSetProperty(propertyName, mutable);
  605. }
  606. return true;
  607. }
  608. /// <summary>
  609. /// Optimized version of [[Put]] when the property is known to be undeclared already
  610. /// </summary>
  611. /// <param name="name"></param>
  612. /// <param name="value"></param>
  613. /// <param name="writable"></param>
  614. /// <param name="configurable"></param>
  615. /// <param name="enumerable"></param>
  616. public void FastAddProperty(string name, JsValue value, bool writable, bool enumerable, bool configurable)
  617. {
  618. SetOwnProperty(name, new PropertyDescriptor(value, writable, enumerable, configurable));
  619. }
  620. /// <summary>
  621. /// Optimized version of [[Put]] when the property is known to be already declared
  622. /// </summary>
  623. /// <param name="name"></param>
  624. /// <param name="value"></param>
  625. public void FastSetProperty(string name, PropertyDescriptor value)
  626. {
  627. SetOwnProperty(name, value);
  628. }
  629. protected virtual void EnsureInitialized()
  630. {
  631. }
  632. public override string ToString()
  633. {
  634. return TypeConverter.ToString(this);
  635. }
  636. public override object ToObject()
  637. {
  638. if (this is IObjectWrapper wrapper)
  639. {
  640. return wrapper.Target;
  641. }
  642. switch (Class)
  643. {
  644. case "Array":
  645. if (this is ArrayInstance arrayInstance)
  646. {
  647. var len = TypeConverter.ToInt32(arrayInstance.Get("length"));
  648. var result = new object[len];
  649. for (var k = 0; k < len; k++)
  650. {
  651. var pk = TypeConverter.ToString(k);
  652. var kpresent = arrayInstance.HasProperty(pk);
  653. if (kpresent)
  654. {
  655. var kvalue = arrayInstance.Get(pk);
  656. result[k] = kvalue.ToObject();
  657. }
  658. else
  659. {
  660. result[k] = null;
  661. }
  662. }
  663. return result;
  664. }
  665. break;
  666. case "String":
  667. if (this is StringInstance stringInstance)
  668. {
  669. return stringInstance.PrimitiveValue.AsString();
  670. }
  671. break;
  672. case "Date":
  673. if (this is DateInstance dateInstance)
  674. {
  675. return dateInstance.ToDateTime();
  676. }
  677. break;
  678. case "Boolean":
  679. if (this is BooleanInstance booleanInstance)
  680. {
  681. return booleanInstance.PrimitiveValue.AsBoolean()
  682. ? JsBoolean.BoxedTrue
  683. : JsBoolean.BoxedFalse;
  684. }
  685. break;
  686. case "Function":
  687. if (this is FunctionInstance function)
  688. {
  689. return (Func<JsValue, JsValue[], JsValue>) function.Call;
  690. }
  691. break;
  692. case "Number":
  693. if (this is NumberInstance numberInstance)
  694. {
  695. return numberInstance.NumberData.AsNumber();
  696. }
  697. break;
  698. case "RegExp":
  699. if (this is RegExpInstance regeExpInstance)
  700. {
  701. return regeExpInstance.Value;
  702. }
  703. break;
  704. case "Arguments":
  705. case "Object":
  706. #if __IOS__
  707. IDictionary<string, object> o = new Dictionary<string, object>();
  708. #else
  709. IDictionary<string, object> o = new ExpandoObject();
  710. #endif
  711. foreach (var p in GetOwnProperties())
  712. {
  713. if (!p.Value.Enumerable)
  714. {
  715. continue;
  716. }
  717. o.Add(p.Key, Get(p.Key).ToObject());
  718. }
  719. return o;
  720. }
  721. return this;
  722. }
  723. public override bool Equals(JsValue obj)
  724. {
  725. if (ReferenceEquals(null, obj))
  726. {
  727. return false;
  728. }
  729. if (!(obj is ObjectInstance s))
  730. {
  731. return false;
  732. }
  733. return Equals(s);
  734. }
  735. public bool Equals(ObjectInstance other)
  736. {
  737. if (ReferenceEquals(null, other))
  738. {
  739. return false;
  740. }
  741. if (ReferenceEquals(this, other))
  742. {
  743. return true;
  744. }
  745. return false;
  746. }
  747. internal void Clear()
  748. {
  749. _intrinsicProperties?.Clear();
  750. _properties?.Clear();
  751. }
  752. }
  753. }