JsMap.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Collections;
  2. using System.Diagnostics.CodeAnalysis;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. namespace Jint.Native;
  7. public sealed class JsMap : ObjectInstance, IEnumerable<KeyValuePair<JsValue, JsValue>>
  8. {
  9. private readonly Realm _realm;
  10. internal readonly OrderedDictionary<JsValue, JsValue> _map;
  11. public JsMap(Engine engine, Realm realm) : base(engine)
  12. {
  13. _realm = realm;
  14. _map = new OrderedDictionary<JsValue, JsValue>(SameValueZeroComparer.Instance);
  15. }
  16. public override PropertyDescriptor GetOwnProperty(JsValue property)
  17. {
  18. if (CommonProperties.Size.Equals(property))
  19. {
  20. return new PropertyDescriptor(_map.Count, PropertyFlag.AllForbidden);
  21. }
  22. return base.GetOwnProperty(property);
  23. }
  24. protected override bool TryGetProperty(JsValue property, [NotNullWhen(true)] out PropertyDescriptor? descriptor)
  25. {
  26. if (CommonProperties.Size.Equals(property))
  27. {
  28. descriptor = new PropertyDescriptor(_map.Count, PropertyFlag.AllForbidden);
  29. return true;
  30. }
  31. return base.TryGetProperty(property, out descriptor);
  32. }
  33. public int Size => _map.Count;
  34. public void Clear() => _map.Clear();
  35. public bool Has(JsValue key) => _map.ContainsKey(key);
  36. public bool Remove(JsValue key) => _map.Remove(key);
  37. public new JsValue Get(JsValue key)
  38. {
  39. if (!_map.TryGetValue(key, out var value))
  40. {
  41. return Undefined;
  42. }
  43. return value;
  44. }
  45. internal JsValue GetOrInsert(JsValue key, JsValue value)
  46. {
  47. if (_map.TryGetValue(key, out var temp))
  48. {
  49. return temp;
  50. }
  51. _map[key] = value;
  52. return value;
  53. }
  54. internal JsValue GetOrInsertComputed(JsValue key, ICallable callbackfn)
  55. {
  56. if (_map.TryGetValue(key, out var temp))
  57. {
  58. return temp;
  59. }
  60. var value = callbackfn.Call(Undefined, key);
  61. _map[key] = value;
  62. return value;
  63. }
  64. public new void Set(JsValue key, JsValue value)
  65. {
  66. if (key is JsNumber number && number.IsNegativeZero())
  67. {
  68. key = JsNumber.PositiveZero;
  69. }
  70. _map[key] = value;
  71. }
  72. internal void ForEach(ICallable callable, JsValue thisArg)
  73. {
  74. var args = _engine._jsValueArrayPool.RentArray(3);
  75. args[2] = this;
  76. for (var i = 0; i < _map.Count; i++)
  77. {
  78. args[0] = _map[i];
  79. args[1] = _map.GetKey(i);
  80. callable.Call(thisArg, args);
  81. }
  82. _engine._jsValueArrayPool.ReturnArray(args);
  83. }
  84. internal ObjectInstance Iterator() => _realm.Intrinsics.MapIteratorPrototype.ConstructEntryIterator(this);
  85. internal ObjectInstance Keys() => _realm.Intrinsics.MapIteratorPrototype.ConstructKeyIterator(this);
  86. internal ObjectInstance Values() => _realm.Intrinsics.MapIteratorPrototype.ConstructValueIterator(this);
  87. public IEnumerator<KeyValuePair<JsValue, JsValue>> GetEnumerator() => _map.GetEnumerator();
  88. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  89. }