WeakMapConstructor.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/WeakMap.h>
  10. #include <LibJS/Runtime/WeakMapConstructor.h>
  11. namespace JS {
  12. WeakMapConstructor::WeakMapConstructor(GlobalObject& global_object)
  13. : NativeFunction(vm().names.WeakMap, *global_object.function_prototype())
  14. {
  15. }
  16. void WeakMapConstructor::initialize(GlobalObject& global_object)
  17. {
  18. auto& vm = this->vm();
  19. NativeFunction::initialize(global_object);
  20. // 24.3.2.1 WeakMap.prototype, https://tc39.es/ecma262/#sec-weakmap.prototype
  21. define_property(vm.names.prototype, global_object.weak_map_prototype(), 0);
  22. define_property(vm.names.length, Value(0), Attribute::Configurable);
  23. }
  24. WeakMapConstructor::~WeakMapConstructor()
  25. {
  26. }
  27. // 24.3.1.1 WeakMap ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakmap-iterable
  28. Value WeakMapConstructor::call()
  29. {
  30. auto& vm = this->vm();
  31. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.WeakMap);
  32. return {};
  33. }
  34. // 24.3.1.1 WeakMap ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakmap-iterable
  35. Value WeakMapConstructor::construct(Function&)
  36. {
  37. auto& vm = this->vm();
  38. if (vm.argument(0).is_nullish())
  39. return WeakMap::create(global_object());
  40. auto* weak_map = WeakMap::create(global_object());
  41. auto adder = weak_map->get(vm.names.set);
  42. if (vm.exception())
  43. return {};
  44. if (!adder.is_function()) {
  45. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAFunction, "'set' property of WeakMap");
  46. return {};
  47. }
  48. get_iterator_values(global_object(), vm.argument(0), [&](Value iterator_value) {
  49. if (vm.exception())
  50. return IterationDecision::Break;
  51. if (!iterator_value.is_object()) {
  52. vm.throw_exception<TypeError>(global_object(), ErrorType::NotAnObject, String::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
  53. return IterationDecision::Break;
  54. }
  55. auto key = iterator_value.as_object().get(0).value_or(js_undefined());
  56. if (vm.exception())
  57. return IterationDecision::Break;
  58. auto value = iterator_value.as_object().get(1).value_or(js_undefined());
  59. if (vm.exception())
  60. return IterationDecision::Break;
  61. (void)vm.call(adder.as_function(), Value(weak_map), key, value);
  62. return vm.exception() ? IterationDecision::Break : IterationDecision::Continue;
  63. });
  64. if (vm.exception())
  65. return {};
  66. return weak_map;
  67. }
  68. }