MapPrototype.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 : ObjectInstance
  13. {
  14. private MapConstructor _mapConstructor;
  15. private MapPrototype(Engine engine) : base(engine)
  16. {
  17. }
  18. public static MapPrototype CreatePrototypeObject(Engine engine, MapConstructor mapConstructor)
  19. {
  20. var obj = new MapPrototype(engine)
  21. {
  22. _prototype = engine.Object.PrototypeObject,
  23. _mapConstructor = mapConstructor
  24. };
  25. return obj;
  26. }
  27. protected override void Initialize()
  28. {
  29. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  30. var properties = new PropertyDictionary(12, checkExistingKeys: false)
  31. {
  32. ["length"] = new PropertyDescriptor(0, PropertyFlag.Configurable),
  33. ["constructor"] = new PropertyDescriptor(_mapConstructor, PropertyFlag.NonEnumerable),
  34. ["clear"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "clear", Clear, 0, PropertyFlag.Configurable), propertyFlags),
  35. ["delete"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "delete", Delete, 1, PropertyFlag.Configurable), propertyFlags),
  36. ["entries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "entries", Iterator, 0, PropertyFlag.Configurable), propertyFlags),
  37. ["forEach"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "forEach", ForEach, 1, PropertyFlag.Configurable), propertyFlags),
  38. ["get"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "get", Get, 1, PropertyFlag.Configurable), propertyFlags),
  39. ["has"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), propertyFlags),
  40. ["keys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keys", Keys, 0, PropertyFlag.Configurable), propertyFlags),
  41. ["set"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "set", Set, 2, PropertyFlag.Configurable), propertyFlags),
  42. ["values"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "values", Values, 0, PropertyFlag.Configurable), propertyFlags),
  43. ["size"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get size", Size, 0, PropertyFlag.Configurable), set: null, PropertyFlag.Configurable)
  44. };
  45. SetProperties(properties);
  46. var symbols = new SymbolDictionary(2)
  47. {
  48. [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "iterator", Iterator, 1, PropertyFlag.Configurable), propertyFlags),
  49. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("Map", false, false, true),
  50. };
  51. SetSymbols(symbols);
  52. }
  53. private JsValue Size(JsValue thisObj, JsValue[] arguments)
  54. {
  55. AssertMapInstance(thisObj);
  56. return JsNumber.Create(0);
  57. }
  58. private JsValue Get(JsValue thisObj, JsValue[] arguments)
  59. {
  60. var map = AssertMapInstance(thisObj);
  61. return map.MapGet(arguments.At(0));
  62. }
  63. private JsValue Clear(JsValue thisObj, JsValue[] arguments)
  64. {
  65. var map = AssertMapInstance(thisObj);
  66. map.Clear();
  67. return Undefined;
  68. }
  69. private JsValue Delete(JsValue thisObj, JsValue[] arguments)
  70. {
  71. var map = AssertMapInstance(thisObj);
  72. return map.MapDelete(arguments[0])
  73. ? JsBoolean.True
  74. : JsBoolean.False;
  75. }
  76. private JsValue Set(JsValue thisObj, JsValue[] arguments)
  77. {
  78. var map = AssertMapInstance(thisObj);
  79. map.MapSet(arguments[0], arguments[1]);
  80. return thisObj;
  81. }
  82. private JsValue Has(JsValue thisObj, JsValue[] arguments)
  83. {
  84. var map = AssertMapInstance(thisObj);
  85. return map.Has(arguments[0])
  86. ? JsBoolean.True
  87. : JsBoolean.False;
  88. }
  89. private JsValue ForEach(JsValue thisObj, JsValue[] arguments)
  90. {
  91. var map = AssertMapInstance(thisObj);
  92. var callbackfn = arguments.At(0);
  93. var thisArg = arguments.At(1);
  94. var callable = GetCallable(callbackfn);
  95. map.ForEach(callable, thisArg);
  96. return Undefined;
  97. }
  98. private ObjectInstance Iterator(JsValue thisObj, JsValue[] arguments)
  99. {
  100. var map = AssertMapInstance(thisObj);
  101. return map.Iterator();
  102. }
  103. private ObjectInstance Keys(JsValue thisObj, JsValue[] arguments)
  104. {
  105. var map = AssertMapInstance(thisObj);
  106. return map.Keys();
  107. }
  108. private ObjectInstance Values(JsValue thisObj, JsValue[] arguments)
  109. {
  110. var map = AssertMapInstance(thisObj);
  111. return map.Values();
  112. }
  113. private MapInstance AssertMapInstance(JsValue thisObj)
  114. {
  115. if (!(thisObj is MapInstance map))
  116. {
  117. return ExceptionHelper.ThrowTypeError<MapInstance>(_engine, "object must be a Map");
  118. }
  119. return map;
  120. }
  121. }
  122. }