ladybird/Userland/Libraries/LibJS/Runtime/GeneratorObject.h
Timothy Flynn 60adeb11c9 LibJS: Convert GeneratorObject's [[GeneratorBrand]] to a StringView
This optional parameter is not currently set by any caller, but will be
for the Iterator Helpers proposal. In all specs, this parameter is a
static string, so we can just use a StringView rather than allocating a
(Deprecated)String on each invocation.

This also changes this optional parameter to be passed by const-ref, as
it does not need to be copied.
2023-07-16 23:56:55 +01:00

47 lines
1.5 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 final : public Object {
JS_OBJECT(GeneratorObject, Object);
public:
static ThrowCompletionOr<NonnullGCPtr<GeneratorObject>> create(Realm&, Value, ECMAScriptFunctionObject*, ExecutionContext, Bytecode::RegisterWindow);
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);
private:
GeneratorObject(Realm&, Object& prototype, ExecutionContext);
enum class GeneratorState {
SuspendedStart,
SuspendedYield,
Executing,
Completed,
};
ThrowCompletionOr<GeneratorState> validate(VM&, Optional<StringView> const& generator_brand);
ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion);
ExecutionContext m_execution_context;
GCPtr<ECMAScriptFunctionObject> m_generating_function;
Value m_previous_value;
Optional<Bytecode::RegisterWindow> m_frame;
GeneratorState m_generator_state { GeneratorState::SuspendedStart };
Optional<StringView> m_generator_brand;
};
}