MapConstructor.cs 3.3 KB

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