MapPrototype.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using Jint.Native.Object;
  3. using Jint.Native.Symbol;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. using Jint.Runtime.Interop;
  7. namespace Jint.Native.Map;
  8. /// <summary>
  9. /// https://tc39.es/ecma262/#sec-map-objects
  10. /// </summary>
  11. internal sealed class MapPrototype : Prototype
  12. {
  13. private readonly MapConstructor _mapConstructor;
  14. internal MapPrototype(
  15. Engine engine,
  16. Realm realm,
  17. MapConstructor mapConstructor,
  18. ObjectPrototype objectPrototype) : base(engine, realm)
  19. {
  20. _prototype = objectPrototype;
  21. _mapConstructor = mapConstructor;
  22. }
  23. protected override void Initialize()
  24. {
  25. const PropertyFlag PropertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  26. var properties = new PropertyDictionary(14, checkExistingKeys: false)
  27. {
  28. ["length"] = new PropertyDescriptor(0, PropertyFlag.Configurable),
  29. ["constructor"] = new PropertyDescriptor(_mapConstructor, PropertyFlag.NonEnumerable),
  30. ["clear"] = new PropertyDescriptor(new ClrFunction(Engine, "clear", Clear, 0, PropertyFlag.Configurable), PropertyFlags),
  31. ["delete"] = new PropertyDescriptor(new ClrFunction(Engine, "delete", Delete, 1, PropertyFlag.Configurable), PropertyFlags),
  32. ["entries"] = new PropertyDescriptor(new ClrFunction(Engine, "entries", Entries, 0, PropertyFlag.Configurable), PropertyFlags),
  33. ["forEach"] = new PropertyDescriptor(new ClrFunction(Engine, "forEach", ForEach, 1, PropertyFlag.Configurable), PropertyFlags),
  34. ["get"] = new PropertyDescriptor(new ClrFunction(Engine, "get", Get, 1, PropertyFlag.Configurable), PropertyFlags),
  35. ["getOrInsert"] = new PropertyDescriptor(new ClrFunction(Engine, "getOrInsert", GetOrInsert, 2, PropertyFlag.Configurable), PropertyFlags),
  36. ["getOrInsertComputed"] = new PropertyDescriptor(new ClrFunction(Engine, "getOrInsertComputed", GetOrInsertComputed, 2, PropertyFlag.Configurable), PropertyFlags),
  37. ["has"] = new PropertyDescriptor(new ClrFunction(Engine, "has", Has, 1, PropertyFlag.Configurable), PropertyFlags),
  38. ["keys"] = new PropertyDescriptor(new ClrFunction(Engine, "keys", Keys, 0, PropertyFlag.Configurable), PropertyFlags),
  39. ["set"] = new PropertyDescriptor(new ClrFunction(Engine, "set", Set, 2, PropertyFlag.Configurable), PropertyFlags),
  40. ["values"] = new PropertyDescriptor(new ClrFunction(Engine, "values", Values, 0, PropertyFlag.Configurable), PropertyFlags),
  41. ["size"] = new GetSetPropertyDescriptor(get: new ClrFunction(Engine, "get size", Size, 0, PropertyFlag.Configurable), set: null, PropertyFlag.Configurable)
  42. };
  43. SetProperties(properties);
  44. var symbols = new SymbolDictionary(2)
  45. {
  46. [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(new ClrFunction(Engine, "iterator", Entries, 1, PropertyFlag.Configurable), PropertyFlags),
  47. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("Map", false, false, true),
  48. };
  49. SetSymbols(symbols);
  50. }
  51. private JsValue Size(JsValue thisObject, JsCallArguments arguments)
  52. {
  53. AssertMapInstance(thisObject);
  54. return JsNumber.Create(0);
  55. }
  56. private JsValue Get(JsValue thisObject, JsCallArguments arguments)
  57. {
  58. var map = AssertMapInstance(thisObject);
  59. return map.Get(arguments.At(0));
  60. }
  61. private JsValue GetOrInsert(JsValue thisObject, JsCallArguments arguments)
  62. {
  63. var map = AssertMapInstance(thisObject);
  64. var key = arguments.At(0).CanonicalizeKeyedCollectionKey();
  65. var value = arguments.At(1);
  66. return map.GetOrInsert(key, value);
  67. }
  68. private JsValue GetOrInsertComputed(JsValue thisObject, JsCallArguments arguments)
  69. {
  70. var map = AssertMapInstance(thisObject);
  71. var key = arguments.At(0).CanonicalizeKeyedCollectionKey();
  72. var callbackfn = arguments.At(1).GetCallable(_realm);
  73. return map.GetOrInsertComputed(key, callbackfn);
  74. }
  75. private JsValue Clear(JsValue thisObject, JsCallArguments arguments)
  76. {
  77. var map = AssertMapInstance(thisObject);
  78. map.Clear();
  79. return Undefined;
  80. }
  81. private JsValue Delete(JsValue thisObject, JsCallArguments arguments)
  82. {
  83. var map = AssertMapInstance(thisObject);
  84. return map.Remove(arguments.At(0))
  85. ? JsBoolean.True
  86. : JsBoolean.False;
  87. }
  88. private JsValue Set(JsValue thisObject, JsCallArguments arguments)
  89. {
  90. var map = AssertMapInstance(thisObject);
  91. map.Set(arguments.At(0), arguments.At(1));
  92. return thisObject;
  93. }
  94. private JsValue Has(JsValue thisObject, JsCallArguments arguments)
  95. {
  96. var map = AssertMapInstance(thisObject);
  97. return map.Has(arguments.At(0))
  98. ? JsBoolean.True
  99. : JsBoolean.False;
  100. }
  101. private JsValue ForEach(JsValue thisObject, JsCallArguments arguments)
  102. {
  103. var map = AssertMapInstance(thisObject);
  104. var callbackfn = arguments.At(0);
  105. var thisArg = arguments.At(1);
  106. var callable = GetCallable(callbackfn);
  107. map.ForEach(callable, thisArg);
  108. return Undefined;
  109. }
  110. private ObjectInstance Entries(JsValue thisObject, JsCallArguments arguments)
  111. {
  112. var map = AssertMapInstance(thisObject);
  113. return map.Iterator();
  114. }
  115. private ObjectInstance Keys(JsValue thisObject, JsCallArguments arguments)
  116. {
  117. var map = AssertMapInstance(thisObject);
  118. return map.Keys();
  119. }
  120. private ObjectInstance Values(JsValue thisObject, JsCallArguments arguments)
  121. {
  122. var map = AssertMapInstance(thisObject);
  123. return map.Values();
  124. }
  125. private JsMap AssertMapInstance(JsValue thisObject)
  126. {
  127. if (thisObject is JsMap map)
  128. {
  129. return map;
  130. }
  131. Throw.TypeError(_realm, "object must be a Map");
  132. return default;
  133. }
  134. }