MapConstructor.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Jint.Collections;
  2. using Jint.Native.Function;
  3. using Jint.Native.Iterator;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native.Map;
  10. internal sealed class MapConstructor : Constructor
  11. {
  12. private static readonly JsString _functionName = new("Map");
  13. internal MapConstructor(
  14. Engine engine,
  15. Realm realm,
  16. FunctionPrototype functionPrototype,
  17. ObjectPrototype objectPrototype)
  18. : base(engine, realm, _functionName)
  19. {
  20. _prototype = functionPrototype;
  21. PrototypeObject = new MapPrototype(engine, realm, this, objectPrototype);
  22. _length = new PropertyDescriptor(0, PropertyFlag.Configurable);
  23. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  24. }
  25. private MapPrototype PrototypeObject { get; }
  26. protected override void Initialize()
  27. {
  28. const PropertyFlag PropertyFlags = PropertyFlag.Writable | PropertyFlag.Configurable;
  29. var properties = new PropertyDictionary(1, checkExistingKeys: false)
  30. {
  31. ["groupBy"] = new(new ClrFunctionInstance(Engine, "groupBy", GroupBy, 2, PropertyFlag.Configurable), PropertyFlags),
  32. };
  33. SetProperties(properties);
  34. var symbols = new SymbolDictionary(1)
  35. {
  36. [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(_engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlag.Configurable)
  37. };
  38. SetSymbols(symbols);
  39. }
  40. private static JsValue Species(JsValue thisObject, JsValue[] arguments)
  41. {
  42. return thisObject;
  43. }
  44. /// <summary>
  45. /// https://tc39.es/ecma262/#sec-map-iterable
  46. /// </summary>
  47. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  48. {
  49. if (newTarget.IsUndefined())
  50. {
  51. ExceptionHelper.ThrowTypeError(_realm);
  52. }
  53. var map = OrdinaryCreateFromConstructor(
  54. newTarget,
  55. static intrinsics => intrinsics.Map.PrototypeObject,
  56. static (Engine engine, Realm realm, object? _) => new MapInstance(engine, realm));
  57. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  58. {
  59. var adder = map.Get("set");
  60. var iterator = arguments.At(0).GetIterator(_realm);
  61. IteratorProtocol.AddEntriesFromIterable(map, iterator, adder);
  62. }
  63. return map;
  64. }
  65. /// <summary>
  66. /// https://tc39.es/proposal-array-grouping/#sec-map.groupby
  67. /// </summary>
  68. private JsValue GroupBy(JsValue thisObject, JsValue[] arguments)
  69. {
  70. var items = arguments.At(0);
  71. var callbackfn = arguments.At(1);
  72. var grouping = GroupByHelper.GroupBy(_engine, _realm, items, callbackfn, mapMode: true);
  73. var map = (MapInstance) Construct(_realm.Intrinsics.Map);
  74. foreach (var pair in grouping)
  75. {
  76. map.MapSet(pair.Key, pair.Value);
  77. }
  78. return map;
  79. }
  80. }