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