MapConstructor.cs 3.2 KB

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