MapPrototype.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Jint.Native.Object;
  2. using Jint.Native.Symbol;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Descriptors.Specialized;
  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 MapPrototype(Engine engine) : base(engine)
  15. {
  16. }
  17. public static MapPrototype CreatePrototypeObject(Engine engine, MapConstructor mapConstructor)
  18. {
  19. var obj = new MapPrototype(engine)
  20. {
  21. Extensible = true,
  22. Prototype = engine.Object.PrototypeObject
  23. };
  24. obj.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.Configurable));
  25. obj.SetOwnProperty("constructor", new PropertyDescriptor(mapConstructor, PropertyFlag.NonEnumerable));
  26. return obj;
  27. }
  28. public void Configure()
  29. {
  30. FastAddProperty(GlobalSymbolRegistry.Iterator._value, new ClrFunctionInstance(Engine, "iterator", Iterator, 1, PropertyFlag.Configurable), true, false, true);
  31. FastAddProperty(GlobalSymbolRegistry.ToStringTag._value, "Map", false, false, true);
  32. FastAddProperty("clear", new ClrFunctionInstance(Engine, "clear", Clear, 0, PropertyFlag.Configurable), true, false, true);
  33. FastAddProperty("delete", new ClrFunctionInstance(Engine, "delete", Delete, 1, PropertyFlag.Configurable), true, false, true);
  34. FastAddProperty("entries", new ClrFunctionInstance(Engine, "entries", Iterator, 0, PropertyFlag.Configurable), true, false, true);
  35. FastAddProperty("forEach", new ClrFunctionInstance(Engine, "forEach", ForEach, 1, PropertyFlag.Configurable), true, false, true);
  36. FastAddProperty("get", new ClrFunctionInstance(Engine, "get", Get, 1, PropertyFlag.Configurable), true, false, true);
  37. FastAddProperty("has", new ClrFunctionInstance(Engine, "has", Has, 1, PropertyFlag.Configurable), true, false, true);
  38. FastAddProperty("keys", new ClrFunctionInstance(Engine, "keys", Keys, 0, PropertyFlag.Configurable), true, false, true);
  39. FastAddProperty("set", new ClrFunctionInstance(Engine, "set", Set, 2, PropertyFlag.Configurable), true, false, true);
  40. FastAddProperty("values", new ClrFunctionInstance(Engine, "values", Values, 0, PropertyFlag.Configurable), true, false, true);
  41. AddProperty(
  42. "size",
  43. new GetSetPropertyDescriptor(
  44. get: new ClrFunctionInstance(Engine, "get size", Size, 0, PropertyFlag.Configurable),
  45. set: null,
  46. PropertyFlag.Configurable));
  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. }