2
0

MapPrototype.cs 5.1 KB

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