WeakMapConstructor.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/WeakMap.h>
  11. #include <LibJS/Runtime/WeakMapConstructor.h>
  12. namespace JS {
  13. WeakMapConstructor::WeakMapConstructor(GlobalObject& global_object)
  14. : NativeFunction(vm().names.WeakMap.as_string(), *global_object.function_prototype())
  15. {
  16. }
  17. void WeakMapConstructor::initialize(GlobalObject& global_object)
  18. {
  19. auto& vm = this->vm();
  20. NativeFunction::initialize(global_object);
  21. // 24.3.2.1 WeakMap.prototype, https://tc39.es/ecma262/#sec-weakmap.prototype
  22. define_direct_property(vm.names.prototype, global_object.weak_map_prototype(), 0);
  23. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  24. }
  25. WeakMapConstructor::~WeakMapConstructor()
  26. {
  27. }
  28. // 24.3.1.1 WeakMap ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakmap-iterable
  29. Value WeakMapConstructor::call()
  30. {
  31. auto& vm = this->vm();
  32. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.WeakMap);
  33. return {};
  34. }
  35. // 24.3.1.1 WeakMap ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakmap-iterable
  36. Value WeakMapConstructor::construct(FunctionObject& new_target)
  37. {
  38. auto& vm = this->vm();
  39. auto& global_object = this->global_object();
  40. auto* weak_map = TRY_OR_DISCARD(ordinary_create_from_constructor<WeakMap>(global_object, new_target, &GlobalObject::weak_map_prototype));
  41. if (vm.argument(0).is_nullish())
  42. return weak_map;
  43. auto adder = TRY_OR_DISCARD(weak_map->get(vm.names.set));
  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_or_error = iterator_value.as_object().get(0);
  56. if (key_or_error.is_error())
  57. return IterationDecision::Break;
  58. auto key = key_or_error.release_value();
  59. auto value_or_error = iterator_value.as_object().get(1);
  60. if (value_or_error.is_error())
  61. return IterationDecision::Break;
  62. auto value = value_or_error.release_value();
  63. auto result = vm.call(adder.as_function(), Value(weak_map), key, value);
  64. return result.is_error() ? IterationDecision::Break : IterationDecision::Continue;
  65. });
  66. if (vm.exception())
  67. return {};
  68. return weak_map;
  69. }
  70. }