MapPrototype.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Jint.Collections;
  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. {
  9. /// <summary>
  10. /// https://www.ecma-international.org/ecma-262/6.0/#sec-map-objects
  11. /// </summary>
  12. public 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 ClrFunctionInstance(Engine, "clear", Clear, 0, PropertyFlag.Configurable), propertyFlags),
  32. ["delete"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "delete", Delete, 1, PropertyFlag.Configurable), propertyFlags),
  33. ["entries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "entries", Iterator, 0, PropertyFlag.Configurable), propertyFlags),
  34. ["forEach"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "forEach", ForEach, 1, PropertyFlag.Configurable), propertyFlags),
  35. ["get"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "get", Get, 1, PropertyFlag.Configurable), propertyFlags),
  36. ["has"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), propertyFlags),
  37. ["keys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keys", Keys, 0, PropertyFlag.Configurable), propertyFlags),
  38. ["set"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "set", Set, 2, PropertyFlag.Configurable), propertyFlags),
  39. ["values"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "values", Values, 0, PropertyFlag.Configurable), propertyFlags),
  40. ["size"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(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 ClrFunctionInstance(Engine, "iterator", Iterator, 1, PropertyFlag.Configurable), propertyFlags),
  46. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("Map", false, false, true),
  47. };
  48. SetSymbols(symbols);
  49. }
  50. private JsValue Size(JsValue thisObj, JsValue[] arguments)
  51. {
  52. AssertMapInstance(thisObj);
  53. return JsNumber.Create(0);
  54. }
  55. private JsValue Get(JsValue thisObj, JsValue[] arguments)
  56. {
  57. var map = AssertMapInstance(thisObj);
  58. return map.MapGet(arguments.At(0));
  59. }
  60. private JsValue Clear(JsValue thisObj, JsValue[] arguments)
  61. {
  62. var map = AssertMapInstance(thisObj);
  63. map.Clear();
  64. return Undefined;
  65. }
  66. private JsValue Delete(JsValue thisObj, JsValue[] arguments)
  67. {
  68. var map = AssertMapInstance(thisObj);
  69. return map.MapDelete(arguments[0])
  70. ? JsBoolean.True
  71. : JsBoolean.False;
  72. }
  73. private JsValue Set(JsValue thisObj, JsValue[] arguments)
  74. {
  75. var map = AssertMapInstance(thisObj);
  76. map.MapSet(arguments[0], arguments[1]);
  77. return thisObj;
  78. }
  79. private JsValue Has(JsValue thisObj, JsValue[] arguments)
  80. {
  81. var map = AssertMapInstance(thisObj);
  82. return map.Has(arguments[0])
  83. ? JsBoolean.True
  84. : JsBoolean.False;
  85. }
  86. private JsValue ForEach(JsValue thisObj, JsValue[] arguments)
  87. {
  88. var map = AssertMapInstance(thisObj);
  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 Iterator(JsValue thisObj, JsValue[] arguments)
  96. {
  97. var map = AssertMapInstance(thisObj);
  98. return map.Iterator();
  99. }
  100. private ObjectInstance Keys(JsValue thisObj, JsValue[] arguments)
  101. {
  102. var map = AssertMapInstance(thisObj);
  103. return map.Keys();
  104. }
  105. private ObjectInstance Values(JsValue thisObj, JsValue[] arguments)
  106. {
  107. var map = AssertMapInstance(thisObj);
  108. return map.Values();
  109. }
  110. private MapInstance AssertMapInstance(JsValue thisObj)
  111. {
  112. var map = thisObj as MapInstance;
  113. if (map is null)
  114. {
  115. ExceptionHelper.ThrowTypeError(_realm, "object must be a Map");
  116. }
  117. return map;
  118. }
  119. }
  120. }