MapPrototype.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(12, 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. ["has"] = new PropertyDescriptor(new ClrFunction(Engine, "has", Has, 1, PropertyFlag.Configurable), propertyFlags),
  36. ["keys"] = new PropertyDescriptor(new ClrFunction(Engine, "keys", Keys, 0, PropertyFlag.Configurable), propertyFlags),
  37. ["set"] = new PropertyDescriptor(new ClrFunction(Engine, "set", Set, 2, PropertyFlag.Configurable), propertyFlags),
  38. ["values"] = new PropertyDescriptor(new ClrFunction(Engine, "values", Values, 0, PropertyFlag.Configurable), propertyFlags),
  39. ["size"] = new GetSetPropertyDescriptor(get: new ClrFunction(Engine, "get size", Size, 0, PropertyFlag.Configurable), set: null, PropertyFlag.Configurable)
  40. };
  41. SetProperties(properties);
  42. var symbols = new SymbolDictionary(2)
  43. {
  44. [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(new ClrFunction(Engine, "iterator", Entries, 1, PropertyFlag.Configurable), propertyFlags),
  45. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("Map", false, false, true),
  46. };
  47. SetSymbols(symbols);
  48. }
  49. private JsValue Size(JsValue thisObject, JsCallArguments arguments)
  50. {
  51. AssertMapInstance(thisObject);
  52. return JsNumber.Create(0);
  53. }
  54. private JsValue Get(JsValue thisObject, JsCallArguments arguments)
  55. {
  56. var map = AssertMapInstance(thisObject);
  57. return map.Get(arguments.At(0));
  58. }
  59. private JsValue Clear(JsValue thisObject, JsCallArguments arguments)
  60. {
  61. var map = AssertMapInstance(thisObject);
  62. map.Clear();
  63. return Undefined;
  64. }
  65. private JsValue Delete(JsValue thisObject, JsCallArguments arguments)
  66. {
  67. var map = AssertMapInstance(thisObject);
  68. return map.Remove(arguments.At(0))
  69. ? JsBoolean.True
  70. : JsBoolean.False;
  71. }
  72. private JsValue Set(JsValue thisObject, JsCallArguments arguments)
  73. {
  74. var map = AssertMapInstance(thisObject);
  75. map.Set(arguments.At(0), arguments.At(1));
  76. return thisObject;
  77. }
  78. private JsValue Has(JsValue thisObject, JsCallArguments arguments)
  79. {
  80. var map = AssertMapInstance(thisObject);
  81. return map.Has(arguments.At(0))
  82. ? JsBoolean.True
  83. : JsBoolean.False;
  84. }
  85. private JsValue ForEach(JsValue thisObject, JsCallArguments arguments)
  86. {
  87. var map = AssertMapInstance(thisObject);
  88. var callbackfn = arguments.At(0);
  89. var thisArg = arguments.At(1);
  90. var callable = GetCallable(callbackfn);
  91. map.ForEach(callable, thisArg);
  92. return Undefined;
  93. }
  94. private ObjectInstance Entries(JsValue thisObject, JsCallArguments arguments)
  95. {
  96. var map = AssertMapInstance(thisObject);
  97. return map.Iterator();
  98. }
  99. private ObjectInstance Keys(JsValue thisObject, JsCallArguments arguments)
  100. {
  101. var map = AssertMapInstance(thisObject);
  102. return map.Keys();
  103. }
  104. private ObjectInstance Values(JsValue thisObject, JsCallArguments arguments)
  105. {
  106. var map = AssertMapInstance(thisObject);
  107. return map.Values();
  108. }
  109. private JsMap AssertMapInstance(JsValue thisObject)
  110. {
  111. if (thisObject is JsMap map)
  112. {
  113. return map;
  114. }
  115. ExceptionHelper.ThrowTypeError(_realm, "object must be a Map");
  116. return default;
  117. }
  118. }