WeakMapConstructor.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/Iterator.h>
  10. #include <LibJS/Runtime/WeakMap.h>
  11. #include <LibJS/Runtime/WeakMapConstructor.h>
  12. namespace JS {
  13. WeakMapConstructor::WeakMapConstructor(Realm& realm)
  14. : NativeFunction(realm.vm().names.WeakMap.as_string(), realm.intrinsics().function_prototype())
  15. {
  16. }
  17. void WeakMapConstructor::initialize(Realm& realm)
  18. {
  19. auto& vm = this->vm();
  20. Base::initialize(realm);
  21. // 24.3.2.1 WeakMap.prototype, https://tc39.es/ecma262/#sec-weakmap.prototype
  22. define_direct_property(vm.names.prototype, realm.intrinsics().weak_map_prototype(), 0);
  23. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  24. }
  25. // 24.3.1.1 WeakMap ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakmap-iterable
  26. ThrowCompletionOr<Value> WeakMapConstructor::call()
  27. {
  28. auto& vm = this->vm();
  29. // 1. If NewTarget is undefined, throw a TypeError exception.
  30. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.WeakMap);
  31. }
  32. // 24.3.1.1 WeakMap ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakmap-iterable
  33. ThrowCompletionOr<NonnullGCPtr<Object>> WeakMapConstructor::construct(FunctionObject& new_target)
  34. {
  35. auto& vm = this->vm();
  36. auto iterable = vm.argument(0);
  37. // 2. Let map be ? OrdinaryCreateFromConstructor(NewTarget, "%WeakMap.prototype%", « [[WeakMapData]] »).
  38. // 3. Set map.[[WeakMapData]] to a new empty List.
  39. auto map = TRY(ordinary_create_from_constructor<WeakMap>(vm, new_target, &Intrinsics::weak_map_prototype));
  40. // 4. If iterable is either undefined or null, return map.
  41. if (iterable.is_nullish())
  42. return map;
  43. // 5. Let adder be ? Get(map, "set").
  44. auto adder = TRY(map->get(vm.names.set));
  45. // 6. If IsCallable(adder) is false, throw a TypeError exception.
  46. if (!adder.is_function())
  47. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "'set' property of WeakMap");
  48. // 7. Return ? AddEntriesFromIterable(map, iterable, adder).
  49. (void)TRY(get_iterator_values(vm, iterable, [&](Value iterator_value) -> Optional<Completion> {
  50. if (!iterator_value.is_object())
  51. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, DeprecatedString::formatted("Iterator value {}", iterator_value.to_string_without_side_effects()));
  52. auto key = TRY(iterator_value.as_object().get(0));
  53. auto value = TRY(iterator_value.as_object().get(1));
  54. TRY(JS::call(vm, adder.as_function(), map, key, value));
  55. return {};
  56. }));
  57. return map;
  58. }
  59. }