ladybird/Userland/Libraries/LibJS/Runtime/Promise.h
Andreas Kling 3c74dc9f4d LibJS: Segregate GC-allocated objects by type
This patch adds two macros to declare per-type allocators:

- JS_DECLARE_ALLOCATOR(TypeName)
- JS_DEFINE_ALLOCATOR(TypeName)

When used, they add a type-specific CellAllocator that the Heap will
delegate allocation requests to.

The result of this is that GC objects of the same type always end up
within the same HeapBlock, drastically reducing the ability to perform
type confusion attacks.

It also improves HeapBlock utilization, since each block now has cells
sized exactly to the type used within that block. (Previously we only
had a handful of block sizes available, and most GC allocations ended
up with a large amount of slack in their tails.)

There is a small performance hit from this, but I'm sure we can make
up for it elsewhere.

Note that the old size-based allocators still exist, and we fall back
to them for any type that doesn't have its own CellAllocator.
2023-11-19 12:10:31 +01:00

69 lines
1.9 KiB
C++

/*
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <LibJS/Runtime/Object.h>
namespace JS {
ThrowCompletionOr<Object*> promise_resolve(VM&, Object& constructor, Value);
class Promise : public Object {
JS_OBJECT(Promise, Object);
JS_DECLARE_ALLOCATOR(Promise);
public:
enum class State {
Pending,
Fulfilled,
Rejected,
};
enum class RejectionOperation {
Reject,
Handle,
};
static NonnullGCPtr<Promise> create(Realm&);
virtual ~Promise() = default;
State state() const { return m_state; }
Value result() const { return m_result; }
struct ResolvingFunctions {
NonnullGCPtr<FunctionObject> resolve;
NonnullGCPtr<FunctionObject> reject;
};
ResolvingFunctions create_resolving_functions();
void fulfill(Value value);
void reject(Value reason);
Value perform_then(Value on_fulfilled, Value on_rejected, GCPtr<PromiseCapability> result_capability);
bool is_handled() const { return m_is_handled; }
void set_is_handled() { m_is_handled = true; }
protected:
explicit Promise(Object& prototype);
virtual void visit_edges(Visitor&) override;
private:
bool is_settled() const { return m_state == State::Fulfilled || m_state == State::Rejected; }
void trigger_reactions() const;
// 27.2.6 Properties of Promise Instances, https://tc39.es/ecma262/#sec-properties-of-promise-instances
State m_state { State::Pending }; // [[PromiseState]]
Value m_result; // [[PromiseResult]]
Vector<GCPtr<PromiseReaction>> m_fulfill_reactions; // [[PromiseFulfillReactions]]
Vector<GCPtr<PromiseReaction>> m_reject_reactions; // [[PromiseRejectReactions]]
bool m_is_handled { false }; // [[PromiseIsHandled]]
};
}