MapPrototype.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Descriptors.Specialized;
  7. using Jint.Runtime.Interop;
  8. namespace Jint.Native.Map
  9. {
  10. /// <summary>
  11. /// https://www.ecma-international.org/ecma-262/6.0/#sec-map-objects
  12. /// </summary>
  13. public sealed class MapPrototype : ObjectInstance
  14. {
  15. private MapConstructor _mapConstructor;
  16. private MapPrototype(Engine engine) : base(engine)
  17. {
  18. }
  19. public static MapPrototype CreatePrototypeObject(Engine engine, MapConstructor mapConstructor)
  20. {
  21. var obj = new MapPrototype(engine)
  22. {
  23. _prototype = engine.Object.PrototypeObject,
  24. _mapConstructor = mapConstructor
  25. };
  26. return obj;
  27. }
  28. protected override void Initialize()
  29. {
  30. _properties = new StringDictionarySlim<PropertyDescriptor>(15)
  31. {
  32. ["length"] = new PropertyDescriptor(0, PropertyFlag.Configurable),
  33. ["constructor"] = new PropertyDescriptor(_mapConstructor, PropertyFlag.NonEnumerable),
  34. [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "iterator", Iterator, 1, PropertyFlag.Configurable), true, false, true),
  35. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("Map", false, false, true),
  36. ["clear"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "clear", Clear, 0, PropertyFlag.Configurable), true, false, true),
  37. ["delete"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "delete", Delete, 1, PropertyFlag.Configurable), true, false, true),
  38. ["entries"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "entries", Iterator, 0, PropertyFlag.Configurable), true, false, true),
  39. ["forEach"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "forEach", ForEach, 1, PropertyFlag.Configurable), true, false, true),
  40. ["get"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "get", Get, 1, PropertyFlag.Configurable), true, false, true),
  41. ["has"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), true, false, true),
  42. ["keys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "keys", Keys, 0, PropertyFlag.Configurable), true, false, true),
  43. ["set"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "set", Set, 2, PropertyFlag.Configurable), true, false, true),
  44. ["values"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "values", Values, 0, PropertyFlag.Configurable), true, false, true),
  45. ["size"] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get size", Size, 0, PropertyFlag.Configurable), set: null, PropertyFlag.Configurable)
  46. };
  47. }
  48. private JsValue Size(JsValue thisObj, JsValue[] arguments)
  49. {
  50. if (!(thisObj is MapInstance))
  51. {
  52. ExceptionHelper.ThrowTypeError(_engine);
  53. }
  54. return JsNumber.Create(0);
  55. }
  56. private JsValue Get(JsValue thisObj, JsValue[] arguments)
  57. {
  58. return ((MapInstance) thisObj).Get(arguments.At(0));
  59. }
  60. private JsValue Clear(JsValue thisObj, JsValue[] arguments)
  61. {
  62. var map = thisObj as MapInstance
  63. ?? ExceptionHelper.ThrowTypeError<MapInstance>(_engine, "object must be a Map");
  64. map.Clear();
  65. return Undefined;
  66. }
  67. private JsValue Delete(JsValue thisObj, JsValue[] arguments)
  68. {
  69. var map = thisObj as MapInstance
  70. ?? ExceptionHelper.ThrowTypeError<MapInstance>(_engine, "object must be a Map");
  71. return map.Delete(arguments[0])
  72. ? JsBoolean.True
  73. : JsBoolean.False;
  74. }
  75. private JsValue Set(JsValue thisObj, JsValue[] arguments)
  76. {
  77. ((MapInstance) thisObj).Set(arguments[0], arguments[1]);
  78. return thisObj;
  79. }
  80. private JsValue Has(JsValue thisObj, JsValue[] arguments)
  81. {
  82. return ((MapInstance) thisObj).Has(arguments[0])
  83. ? JsBoolean.True
  84. : JsBoolean.False;
  85. }
  86. private JsValue ForEach(JsValue thisObj, JsValue[] arguments)
  87. {
  88. var callbackfn = arguments.At(0);
  89. var thisArg = arguments.At(1);
  90. var map = (MapInstance) thisObj;
  91. var callable = GetCallable(callbackfn);
  92. map.ForEach(callable, thisArg);
  93. return Undefined;
  94. }
  95. private ObjectInstance Iterator(JsValue thisObj, JsValue[] arguments)
  96. {
  97. return ((MapInstance) thisObj).Iterator();
  98. }
  99. private ObjectInstance Keys(JsValue thisObj, JsValue[] arguments)
  100. {
  101. return ((MapInstance) thisObj).Keys();
  102. }
  103. private ObjectInstance Values(JsValue thisObj, JsValue[] arguments)
  104. {
  105. return ((MapInstance) thisObj).Values();
  106. }
  107. }
  108. }