MapConstructor.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Error.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/IteratorOperations.h>
  9. #include <LibJS/Runtime/Map.h>
  10. #include <LibJS/Runtime/MapConstructor.h>
  11. namespace JS {
  12. MapConstructor::MapConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.Map, *global_object.function_prototype())
  14. {
  15. }
  16. void MapConstructor::initialize(GlobalObject& global_object)
  17. {
  18. auto& vm = this->vm();
  19. NativeFunction::initialize(global_object);
  20. // 24.1.2.1 Map.prototype, https://tc39.es/ecma262/#sec-map.prototype
  21. define_property(vm.names.prototype, global_object.map_prototype(), 0);
  22. define_property(vm.names.length, Value(0), Attribute::Configurable);
  23. define_native_accessor(vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  24. }
  25. MapConstructor::~MapConstructor()
  26. {
  27. }
  28. // 24.1.1.1 Map ( [ iterable ] ), https://tc39.es/ecma262/#sec-map-iterable
  29. Value MapConstructor::call()
  30. {
  31. auto& vm = this->vm();
  32. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Map);
  33. return {};
  34. }
  35. // 24.1.1.1 Map ( [ iterable ] ), https://tc39.es/ecma262/#sec-map-iterable
  36. Value MapConstructor::construct(Function&)
  37. {
  38. auto& vm = this->vm();
  39. // FIXME: Use OrdinaryCreateFromConstructor(newTarget, "%Map.prototype%")
  40. auto* map = Map::create(global_object());
  41. if (vm.argument(0).is_nullish())
  42. return map;
  43. auto adder = map->get(vm.names.set);
  44. if (vm.exception())
  45. return {};
  46. if (!adder.is_function()) {
  47. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, "'set' property of Map");
  48. return {};
  49. }
  50. get_iterator_values(global_object(), vm.argument(0), [&](Value iterator_value) {
  51. if (vm.exception())
  52. return IterationDecision::Break;
  53. if (!iterator_value.is_object()) {
  54. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, String::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
  55. return IterationDecision::Break;
  56. }
  57. auto key = iterator_value.as_object().get(0).value_or(js_undefined());
  58. if (vm.exception())
  59. return IterationDecision::Break;
  60. auto value = iterator_value.as_object().get(1).value_or(js_undefined());
  61. if (vm.exception())
  62. return IterationDecision::Break;
  63. (void)vm.call(adder.as_function(), Value(map), key, value);
  64. return vm.exception() ? IterationDecision::Break : IterationDecision::Continue;
  65. });
  66. if (vm.exception())
  67. return {};
  68. return map;
  69. }
  70. // 24.1.2.2 get Map [ @@species ], https://tc39.es/ecma262/#sec-get-map-@@species
  71. JS_DEFINE_NATIVE_GETTER(MapConstructor::symbol_species_getter)
  72. {
  73. return vm.this_value(global_object);
  74. }
  75. }