MapConstructor.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. public 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) { ["groupBy"] = new(new ClrFunction(Engine, "groupBy", GroupBy, 2, PropertyFlag.Configurable), PropertyFlags), };
  31. SetProperties(properties);
  32. var symbols = new SymbolDictionary(1) { [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunction(_engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable), set: Undefined, PropertyFlag.Configurable) };
  33. SetSymbols(symbols);
  34. }
  35. public JsMap Construct() => ConstructMap(this);
  36. /// <summary>
  37. /// https://tc39.es/ecma262/#sec-map-iterable
  38. /// </summary>
  39. public override ObjectInstance Construct(JsCallArguments arguments, JsValue newTarget)
  40. {
  41. var map = ConstructMap(newTarget);
  42. if (arguments.Length > 0 && !arguments[0].IsNullOrUndefined())
  43. {
  44. var adder = ((ObjectInstance) map).Get("set");
  45. var iterator = arguments.At(0).GetIterator(_realm);
  46. IteratorProtocol.AddEntriesFromIterable(map, iterator, adder);
  47. }
  48. return map;
  49. }
  50. private JsMap ConstructMap(JsValue newTarget)
  51. {
  52. if (newTarget.IsUndefined())
  53. {
  54. ExceptionHelper.ThrowTypeError(_realm);
  55. }
  56. var map = OrdinaryCreateFromConstructor(
  57. newTarget,
  58. static intrinsics => intrinsics.Map.PrototypeObject,
  59. static (Engine engine, Realm realm, object? _) => new JsMap(engine, realm));
  60. return map;
  61. }
  62. /// <summary>
  63. /// https://tc39.es/proposal-array-grouping/#sec-map.groupby
  64. /// </summary>
  65. private JsValue GroupBy(JsValue thisObject, JsCallArguments arguments)
  66. {
  67. var items = arguments.At(0);
  68. var callbackfn = arguments.At(1);
  69. var grouping = GroupByHelper.GroupBy(_engine, _realm, items, callbackfn, mapMode: true);
  70. var map = (JsMap) Construct(_realm.Intrinsics.Map);
  71. foreach (var pair in grouping)
  72. {
  73. map.Set(pair.Key, pair.Value);
  74. }
  75. return map;
  76. }
  77. private static JsValue Species(JsValue thisObject, JsCallArguments arguments)
  78. {
  79. return thisObject;
  80. }
  81. }