MapConstructor.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_old_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. ThrowCompletionOr<Value> MapConstructor::call()
  31. {
  32. auto& vm = this->vm();
  33. return vm.throw_completion<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Map);
  34. }
  35. // 24.1.1.1 Map ( [ iterable ] ), https://tc39.es/ecma262/#sec-map-iterable
  36. ThrowCompletionOr<Object*> MapConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. auto& global_object = this->global_object();
  40. auto* map = TRY(ordinary_create_from_constructor<Map>(global_object, new_target, &GlobalObject::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>(global_object, ErrorType::NotAFunction, "'set' property of Map");
  46. TRY(get_iterator_values(global_object, vm.argument(0), [&](Value iterator_value) -> Optional<Completion> {
  47. if (!iterator_value.is_object())
  48. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, String::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(vm.call(adder.as_function(), Value(map), key, value));
  52. return {};
  53. }));
  54. return map;
  55. }
  56. // 24.1.2.2 get Map [ @@species ], https://tc39.es/ecma262/#sec-get-map-@@species
  57. JS_DEFINE_OLD_NATIVE_FUNCTION(MapConstructor::symbol_species_getter)
  58. {
  59. return vm.this_value(global_object);
  60. }
  61. }