DisposableStackConstructor.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/DisposableStack.h>
  8. #include <LibJS/Runtime/DisposableStackConstructor.h>
  9. namespace JS {
  10. JS_DEFINE_ALLOCATOR(DisposableStackConstructor);
  11. DisposableStackConstructor::DisposableStackConstructor(Realm& realm)
  12. : NativeFunction(realm.vm().names.DisposableStack.as_string(), realm.intrinsics().function_prototype())
  13. {
  14. }
  15. void DisposableStackConstructor::initialize(Realm& realm)
  16. {
  17. auto& vm = this->vm();
  18. Base::initialize(realm);
  19. // 26.2.2.1 DisposableStack.prototype, https://tc39.es/ecma262/#sec-finalization-registry.prototype
  20. define_direct_property(vm.names.prototype, realm.intrinsics().disposable_stack_prototype(), 0);
  21. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  22. }
  23. // 11.3.1.1 DisposableStack ( ), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack
  24. ThrowCompletionOr<Value> DisposableStackConstructor::call()
  25. {
  26. auto& vm = this->vm();
  27. // 1. If NewTarget is undefined, throw a TypeError exception.
  28. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.DisposableStack);
  29. }
  30. // 11.3.1.1 DisposableStack ( ), https://tc39.es/proposal-explicit-resource-management/#sec-disposablestack
  31. ThrowCompletionOr<NonnullGCPtr<Object>> DisposableStackConstructor::construct(FunctionObject& new_target)
  32. {
  33. auto& vm = this->vm();
  34. // 2. Let disposableStack be ? OrdinaryCreateFromConstructor(NewTarget, "%DisposableStack.prototype%", « [[DisposableState]], [[DisposableResourceStack]] »).
  35. // 3. Set disposableStack.[[DisposableState]] to pending.
  36. // 4. Set disposableStack.[[DisposableResourceStack]] to a new empty List.
  37. // 5. Return disposableStack.
  38. return TRY(ordinary_create_from_constructor<DisposableStack>(vm, new_target, &Intrinsics::disposable_stack_prototype, Vector<DisposableResource> {}));
  39. }
  40. }