1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:48:11 +00:00

LibJS: Allow GeneratorObject to be subclassed

In the Iterator Helpers proposal, we must create a generator object with
additional internal slots and behavior differences.

GeneratorObject is currently implemented assuming it wraps around an
ECMAScriptFunctionObject with generated bytecode. In this proposal, we
instead have "Abstract Closure" blocks. So this marks the `execute`
method as virtual, to allow the future subclass to essentially just
invoke those closures.

We will also require mutable access to the [[GeneratorState]] internal
slot.
This commit is contained in:
Timothy Flynn 2023-07-16 14:37:35 -04:00 committed by Linus Groh
parent 60adeb11c9
commit 566a8dfd93
3 changed files with 14 additions and 10 deletions

View file

@ -12,7 +12,7 @@
namespace JS {
class GeneratorObject final : public Object {
class GeneratorObject : public Object {
JS_OBJECT(GeneratorObject, Object);
public:
@ -23,19 +23,22 @@ public:
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,
};
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);
ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion);
virtual ThrowCompletionOr<Value> execute(VM&, JS::Completion const& completion);
private:
ExecutionContext m_execution_context;
GCPtr<ECMAScriptFunctionObject> m_generating_function;
Value m_previous_value;