WeakRefPrototype.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibJS/Runtime/WeakRefPrototype.h>
  8. namespace JS {
  9. GC_DEFINE_ALLOCATOR(WeakRefPrototype);
  10. WeakRefPrototype::WeakRefPrototype(Realm& realm)
  11. : PrototypeObject(realm.intrinsics().object_prototype())
  12. {
  13. }
  14. void WeakRefPrototype::initialize(Realm& realm)
  15. {
  16. auto& vm = this->vm();
  17. Base::initialize(realm);
  18. define_native_function(realm, vm.names.deref, deref, 0, Attribute::Writable | Attribute::Configurable);
  19. define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, vm.names.WeakRef.as_string()), Attribute::Configurable);
  20. }
  21. // 26.1.3.2 WeakRef.prototype.deref ( ), https://tc39.es/ecma262/#sec-weak-ref.prototype.deref
  22. JS_DEFINE_NATIVE_FUNCTION(WeakRefPrototype::deref)
  23. {
  24. // 1. Let weakRef be the this value.
  25. // 2. Perform ? RequireInternalSlot(weakRef, [[WeakRefTarget]]).
  26. auto weak_ref = TRY(typed_this_object(vm));
  27. // 3. Return WeakRefDeref(weakRef).
  28. weak_ref->update_execution_generation();
  29. return weak_ref->value().visit(
  30. [](Empty) -> Value { return js_undefined(); },
  31. [](auto value) -> Value { return value; });
  32. }
  33. }