MapConstructor.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/Iterator.h>
  11. #include <LibJS/Runtime/Map.h>
  12. #include <LibJS/Runtime/MapConstructor.h>
  13. namespace JS {
  14. JS_DEFINE_ALLOCATOR(MapConstructor);
  15. MapConstructor::MapConstructor(Realm& realm)
  16. : NativeFunction(realm.vm().names.Map.as_string(), realm.intrinsics().function_prototype())
  17. {
  18. }
  19. void MapConstructor::initialize(Realm& realm)
  20. {
  21. auto& vm = this->vm();
  22. Base::initialize(realm);
  23. // 24.1.2.2 Map.prototype, https://tc39.es/ecma262/#sec-map.prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().map_prototype(), 0);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(realm, vm.names.groupBy, group_by, 2, attr);
  27. define_native_accessor(realm, vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  28. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  29. }
  30. // 24.1.1.1 Map ( [ iterable ] ), https://tc39.es/ecma262/#sec-map-iterable
  31. ThrowCompletionOr<Value> MapConstructor::call()
  32. {
  33. auto& vm = this->vm();
  34. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.Map);
  35. }
  36. // 24.1.1.1 Map ( [ iterable ] ), https://tc39.es/ecma262/#sec-map-iterable
  37. ThrowCompletionOr<NonnullGCPtr<Object>> MapConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. auto map = TRY(ordinary_create_from_constructor<Map>(vm, new_target, &Intrinsics::map_prototype));
  41. if (vm.argument(0).is_nullish())
  42. return map;
  43. auto adder = TRY(map->get(vm.names.set));
  44. if (!adder.is_function())
  45. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "'set' property of Map");
  46. (void)TRY(get_iterator_values(vm, vm.argument(0), [&](Value iterator_value) -> Optional<Completion> {
  47. if (!iterator_value.is_object())
  48. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, ByteString::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
  49. auto key = TRY(iterator_value.as_object().get(0));
  50. auto value = TRY(iterator_value.as_object().get(1));
  51. TRY(JS::call(vm, adder.as_function(), map, key, value));
  52. return {};
  53. }));
  54. return map;
  55. }
  56. // 24.1.2.1 Map.groupBy ( items, callbackfn ), https://tc39.es/ecma262/#sec-map.groupby
  57. JS_DEFINE_NATIVE_FUNCTION(MapConstructor::group_by)
  58. {
  59. auto& realm = *vm.current_realm();
  60. auto items = vm.argument(0);
  61. auto callback_function = vm.argument(1);
  62. struct KeyedGroupTraits : public Traits<Handle<Value>> {
  63. static unsigned hash(Handle<Value> const& value_handle)
  64. {
  65. return ValueTraits::hash(value_handle.value());
  66. }
  67. static bool equals(Handle<Value> const& a, Handle<Value> const& b)
  68. {
  69. // AddValueToKeyedGroup uses SameValue on the keys on Step 1.a.
  70. return same_value(a.value(), b.value());
  71. }
  72. };
  73. // 1. Let groups be ? GroupBy(items, callbackfn, zero).
  74. auto groups = TRY((JS::group_by<OrderedHashMap<Handle<Value>, MarkedVector<Value>, KeyedGroupTraits>, void>(vm, items, callback_function)));
  75. // 2. Let map be ! Construct(%Map%).
  76. auto map = Map::create(realm);
  77. // 3. For each Record { [[Key]], [[Elements]] } g of groups, do
  78. for (auto& group : groups) {
  79. // a. Let elements be CreateArrayFromList(g.[[Elements]]).
  80. auto elements = Array::create_from(realm, group.value);
  81. // b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }.
  82. // c. Append entry to map.[[MapData]].
  83. map->map_set(group.key.value(), elements);
  84. }
  85. // 4. Return map.
  86. return map;
  87. }
  88. // 24.1.2.3 get Map [ @@species ], https://tc39.es/ecma262/#sec-get-map-@@species
  89. JS_DEFINE_NATIVE_FUNCTION(MapConstructor::symbol_species_getter)
  90. {
  91. return vm.this_value();
  92. }
  93. }