PromiseResolvingFunction.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/StringView.h>
  8. #include <LibJS/Runtime/NativeFunction.h>
  9. namespace JS {
  10. struct AlreadyResolved final : public Cell {
  11. JS_CELL(AlreadyResolved, Cell);
  12. JS_DECLARE_ALLOCATOR(AlreadyResolved);
  13. bool value { false };
  14. protected:
  15. // Allocated cells must be >= sizeof(FreelistEntry), which is 24 bytes -
  16. // but AlreadyResolved is only 16 bytes without this.
  17. u8 dummy[8];
  18. };
  19. class PromiseResolvingFunction final : public NativeFunction {
  20. JS_OBJECT(PromiseResolvingFunction, NativeFunction);
  21. JS_DECLARE_ALLOCATOR(PromiseResolvingFunction);
  22. public:
  23. using FunctionType = Function<Value(VM&, Promise&, AlreadyResolved&)>;
  24. static NonnullGCPtr<PromiseResolvingFunction> create(Realm&, Promise&, AlreadyResolved&, FunctionType);
  25. virtual void initialize(Realm&) override;
  26. virtual ~PromiseResolvingFunction() override = default;
  27. virtual ThrowCompletionOr<Value> call() override;
  28. private:
  29. explicit PromiseResolvingFunction(Promise&, AlreadyResolved&, FunctionType, Object& prototype);
  30. virtual void visit_edges(Visitor&) override;
  31. NonnullGCPtr<Promise> m_promise;
  32. NonnullGCPtr<AlreadyResolved> m_already_resolved;
  33. FunctionType m_native_function;
  34. };
  35. }