
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.
51 lines
1.8 KiB
C++
51 lines
1.8 KiB
C++
/*
|
|
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibJS/Bytecode/Interpreter.h>
|
|
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
namespace JS {
|
|
|
|
class GeneratorObject : public Object {
|
|
JS_OBJECT(GeneratorObject, Object);
|
|
JS_DECLARE_ALLOCATOR(GeneratorObject);
|
|
|
|
public:
|
|
static ThrowCompletionOr<NonnullGCPtr<GeneratorObject>> create(Realm&, Value, ECMAScriptFunctionObject*, ExecutionContext, Bytecode::CallFrame);
|
|
virtual ~GeneratorObject() override = default;
|
|
void visit_edges(Cell::Visitor&) override;
|
|
|
|
ThrowCompletionOr<Value> resume(VM&, Value value, Optional<StringView> const& generator_brand);
|
|
ThrowCompletionOr<Value> resume_abrupt(VM&, JS::Completion abrupt_completion, Optional<StringView> const& generator_brand);
|
|
|
|
enum class GeneratorState {
|
|
SuspendedStart,
|
|
SuspendedYield,
|
|
Executing,
|
|
Completed,
|
|
};
|
|
GeneratorState generator_state() const { return m_generator_state; }
|
|
void set_generator_state(GeneratorState generator_state) { m_generator_state = generator_state; }
|
|
|
|
protected:
|
|
GeneratorObject(Realm&, Object& prototype, ExecutionContext, Optional<StringView> generator_brand = {});
|
|
|
|
ThrowCompletionOr<GeneratorState> validate(VM&, Optional<StringView> const& generator_brand);
|
|
virtual ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion);
|
|
|
|
private:
|
|
ExecutionContext m_execution_context;
|
|
GCPtr<ECMAScriptFunctionObject> m_generating_function;
|
|
Value m_previous_value;
|
|
Optional<Bytecode::CallFrame> m_frame;
|
|
GeneratorState m_generator_state { GeneratorState::SuspendedStart };
|
|
Optional<StringView> m_generator_brand;
|
|
};
|
|
|
|
}
|