1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:07:35 +00:00

LibJS: Switch to bytecode interpreter to run generator functions for AST

The bytecode interpreter can execute generator functions while the AST
interpreter cannot. This simply makes it create a new bytecode
interpreter when one doesn't exist when executing a generator function.
Doing so makes it automatically switch to the bytecode interpreter to
execute any future code until it exits the generator.
This commit is contained in:
Luke Wilde 2022-11-26 15:46:24 +00:00 committed by Linus Groh
parent f3763a5275
commit a1c1ab5f8d
2 changed files with 20 additions and 1 deletions

View file

@ -103,11 +103,16 @@ ThrowCompletionOr<Value> GeneratorObject::execute(VM& vm, Completion const& comp
completion_object->define_direct_property(vm.names.value, completion.value().value(), default_attributes);
auto* bytecode_interpreter = Bytecode::Interpreter::current();
// If we're coming from a context which has no bytecode interpreter, e.g. from AST mode calling Generate.prototype.next,
// we need to make one to be able to continue, as generators are only supported in bytecode mode.
// See also ECMAScriptFunctionObject::ordinary_call_evaluate_body where this is done as well.
OwnPtr<Bytecode::Interpreter> temp_bc_interpreter;
if (!bytecode_interpreter) {
temp_bc_interpreter = make<Bytecode::Interpreter>(realm);
bytecode_interpreter = temp_bc_interpreter.ptr();
}
VERIFY(bytecode_interpreter);
auto const* next_block = generated_continuation(m_previous_value);