MapConstructor.cpp 4.0 KB

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