MapConstructor.cpp 3.0 KB

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