MapInstance.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Runtime.CompilerServices;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. namespace Jint.Native.Map
  6. {
  7. public class MapInstance : ObjectInstance
  8. {
  9. internal readonly OrderedDictionary<JsValue, JsValue> _map;
  10. public MapInstance(Engine engine)
  11. : base(engine, objectClass: "Map")
  12. {
  13. _map = new OrderedDictionary<JsValue, JsValue>();
  14. }
  15. /// Implementation from ObjectInstance official specs as the one
  16. /// in ObjectInstance is optimized for the general case and wouldn't work
  17. /// for arrays
  18. public override void Put(in Key propertyName, JsValue value, bool throwOnError)
  19. {
  20. if (!CanPut(propertyName))
  21. {
  22. if (throwOnError)
  23. {
  24. ExceptionHelper.ThrowTypeError(Engine);
  25. }
  26. return;
  27. }
  28. var ownDesc = GetOwnProperty(propertyName);
  29. if (ownDesc.IsDataDescriptor())
  30. {
  31. var valueDesc = new PropertyDescriptor(value, PropertyFlag.None);
  32. DefineOwnProperty(propertyName, valueDesc, throwOnError);
  33. return;
  34. }
  35. // property is an accessor or inherited
  36. var desc = GetProperty(propertyName);
  37. if (desc.IsAccessorDescriptor())
  38. {
  39. var setter = desc.Set.TryCast<ICallable>();
  40. setter.Call(this, new[] {value});
  41. }
  42. else
  43. {
  44. var newDesc = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
  45. DefineOwnProperty(propertyName, newDesc, throwOnError);
  46. }
  47. }
  48. public override PropertyDescriptor GetOwnProperty(in Key propertyName)
  49. {
  50. if (propertyName == KnownKeys.Size)
  51. {
  52. return new PropertyDescriptor(_map.Count, PropertyFlag.None);
  53. }
  54. return base.GetOwnProperty(propertyName);
  55. }
  56. protected override bool TryGetProperty(in Key propertyName, out PropertyDescriptor descriptor)
  57. {
  58. if (propertyName == KnownKeys.Size)
  59. {
  60. descriptor = new PropertyDescriptor(_map.Count, PropertyFlag.None);
  61. return true;
  62. }
  63. return base.TryGetProperty(propertyName, out descriptor);
  64. }
  65. public void Clear()
  66. {
  67. _map.Clear();
  68. }
  69. public bool Has(JsValue key)
  70. {
  71. return _map.ContainsKey(key);
  72. }
  73. public bool Delete(JsValue key)
  74. {
  75. return _map.Remove(key);
  76. }
  77. public void Set(JsValue key, JsValue value)
  78. {
  79. _map[key] = value;
  80. }
  81. public void ForEach(ICallable callable, JsValue thisArg)
  82. {
  83. var args = _engine._jsValueArrayPool.RentArray(3);
  84. args[2] = this;
  85. for (var i = 0; i < _map.Count; i++)
  86. {
  87. args[0] = _map[i];
  88. args[1] = _map.GetKey(i);
  89. callable.Call(thisArg, args);
  90. }
  91. _engine._jsValueArrayPool.ReturnArray(args);
  92. }
  93. public JsValue Get(JsValue key)
  94. {
  95. if (!_map.TryGetValue(key, out var value))
  96. {
  97. return Undefined;
  98. }
  99. return value;
  100. }
  101. public ObjectInstance Iterator()
  102. {
  103. return _engine.Iterator.Construct(this);
  104. }
  105. public ObjectInstance Keys()
  106. {
  107. return _engine.Iterator.Construct(_map.Keys);
  108. }
  109. public ObjectInstance Values()
  110. {
  111. return _engine.Iterator.Construct(_map.Values);
  112. }
  113. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. internal uint GetSize()
  115. {
  116. return (uint) _map.Count;
  117. }
  118. }
  119. }