LibJS: Make Bytecode::Interpreter return the popped frame

And use it to _correctly_ implement state saving for generators.
Prior to this, we were capturing the caller frame, which is completely
irrelevant to the generator frame.
This commit is contained in:
Ali Mohammad Pur 2021-11-11 00:34:44 +03:30 committed by Linus Groh
parent b96118b5d1
commit e4a7f1a696
Notes: sideshowbarker 2024-07-18 04:46:35 +09:00
3 changed files with 37 additions and 17 deletions

View File

@ -39,7 +39,7 @@ Interpreter::~Interpreter()
s_current = nullptr;
}
Value Interpreter::run(Executable const& executable, BasicBlock const* entry_point)
Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable const& executable, BasicBlock const* entry_point)
{
dbgln_if(JS_BYTECODE_DEBUG, "Bytecode::Interpreter will run unit {:p}", &executable);
@ -62,14 +62,16 @@ Value Interpreter::run(Executable const& executable, BasicBlock const* entry_poi
}
auto block = entry_point ?: &executable.basic_blocks.first();
if (m_manually_entered_frames) {
VERIFY(registers().size() >= executable.number_of_registers);
if (!m_manually_entered_frames.is_empty() && m_manually_entered_frames.last()) {
m_register_windows.append(make<RegisterWindow>(m_register_windows.last()));
} else {
m_register_windows.append(make<RegisterWindow>());
registers().resize(executable.number_of_registers);
registers()[Register::global_object_index] = Value(&global_object());
}
registers().resize(executable.number_of_registers);
registers()[Register::global_object_index] = Value(&global_object());
m_manually_entered_frames.append(false);
for (;;) {
Bytecode::InstructionStreamIterator pc(block->instruction_stream());
bool will_jump = false;
@ -138,8 +140,11 @@ Value Interpreter::run(Executable const& executable, BasicBlock const* entry_poi
vm().set_last_value(Badge<Interpreter> {}, accumulator());
if (!m_manually_entered_frames)
m_register_windows.take_last();
OwnPtr<RegisterWindow> frame;
if (!m_manually_entered_frames.last()) {
frame = m_register_windows.take_last();
m_manually_entered_frames.take_last();
}
auto return_value = m_return_value.value_or(js_undefined());
m_return_value = {};
@ -153,7 +158,7 @@ Value Interpreter::run(Executable const& executable, BasicBlock const* entry_poi
vm().finish_execution_generation();
return return_value;
return { return_value, move(frame) };
}
void Interpreter::enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target)

View File

@ -32,7 +32,17 @@ public:
Realm& realm() { return m_realm; }
VM& vm() { return m_vm; }
Value run(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point = nullptr);
Value run(Bytecode::Executable const& executable, Bytecode::BasicBlock const* entry_point = nullptr)
{
auto value_and_frame = run_and_return_frame(executable, entry_point);
return value_and_frame.value;
}
struct ValueAndFrame {
Value value;
OwnPtr<RegisterWindow> frame;
};
ValueAndFrame run_and_return_frame(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point);
ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
Value& reg(Register const& r) { return registers()[r.index()]; }
@ -40,14 +50,15 @@ public:
void enter_frame(RegisterWindow const& frame)
{
++m_manually_entered_frames;
m_manually_entered_frames.append(true);
m_register_windows.append(make<RegisterWindow>(frame));
}
void leave_frame()
NonnullOwnPtr<RegisterWindow> pop_frame()
{
VERIFY(m_manually_entered_frames);
--m_manually_entered_frames;
m_register_windows.take_last();
VERIFY(!m_manually_entered_frames.is_empty());
VERIFY(m_manually_entered_frames.last());
m_manually_entered_frames.take_last();
return m_register_windows.take_last();
}
void jump(Label const& label)
@ -77,9 +88,9 @@ private:
GlobalObject& m_global_object;
Realm& m_realm;
NonnullOwnPtrVector<RegisterWindow> m_register_windows;
Vector<bool> m_manually_entered_frames;
Optional<BasicBlock const*> m_pending_jump;
Value m_return_value;
size_t m_manually_entered_frames { 0 };
Executable const* m_current_executable { nullptr };
Vector<UnwindInfo> m_unwind_contexts;
Handle<Exception> m_saved_exception;

View File

@ -769,15 +769,19 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body()
if (JS::Bytecode::g_dump_bytecode)
m_bytecode_executable->dump();
}
auto result = bytecode_interpreter->run(*m_bytecode_executable);
auto result_and_frame = bytecode_interpreter->run_and_return_frame(*m_bytecode_executable, nullptr);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
VERIFY(result_and_frame.frame != nullptr);
auto result = result_and_frame.value;
// NOTE: Running the bytecode should eventually return a completion.
// Until it does, we assume "return" and include the undefined fallback from the call site.
if (m_kind != FunctionKind::Generator)
return { Completion::Type::Return, result.value_or(js_undefined()), {} };
auto generator_object = TRY(GeneratorObject::create(global_object(), result, this, vm.running_execution_context().lexical_environment, bytecode_interpreter->snapshot_frame()));
auto generator_object = TRY(GeneratorObject::create(global_object(), result, this, vm.running_execution_context().lexical_environment, move(*result_and_frame.frame)));
return { Completion::Type::Return, generator_object, {} };
} else {
if (m_kind == FunctionKind::Generator)