mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 05:58:11 +00:00
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:
parent
b96118b5d1
commit
e4a7f1a696
3 changed files with 37 additions and 17 deletions
|
@ -39,7 +39,7 @@ Interpreter::~Interpreter()
|
||||||
s_current = nullptr;
|
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);
|
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();
|
auto block = entry_point ?: &executable.basic_blocks.first();
|
||||||
if (m_manually_entered_frames) {
|
if (!m_manually_entered_frames.is_empty() && m_manually_entered_frames.last()) {
|
||||||
VERIFY(registers().size() >= executable.number_of_registers);
|
m_register_windows.append(make<RegisterWindow>(m_register_windows.last()));
|
||||||
} else {
|
} else {
|
||||||
m_register_windows.append(make<RegisterWindow>());
|
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 (;;) {
|
for (;;) {
|
||||||
Bytecode::InstructionStreamIterator pc(block->instruction_stream());
|
Bytecode::InstructionStreamIterator pc(block->instruction_stream());
|
||||||
bool will_jump = false;
|
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());
|
vm().set_last_value(Badge<Interpreter> {}, accumulator());
|
||||||
|
|
||||||
if (!m_manually_entered_frames)
|
OwnPtr<RegisterWindow> frame;
|
||||||
m_register_windows.take_last();
|
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());
|
auto return_value = m_return_value.value_or(js_undefined());
|
||||||
m_return_value = {};
|
m_return_value = {};
|
||||||
|
@ -153,7 +158,7 @@ Value Interpreter::run(Executable const& executable, BasicBlock const* entry_poi
|
||||||
|
|
||||||
vm().finish_execution_generation();
|
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)
|
void Interpreter::enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target)
|
||||||
|
|
|
@ -32,7 +32,17 @@ public:
|
||||||
Realm& realm() { return m_realm; }
|
Realm& realm() { return m_realm; }
|
||||||
VM& vm() { return m_vm; }
|
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()); }
|
ALWAYS_INLINE Value& accumulator() { return reg(Register::accumulator()); }
|
||||||
Value& reg(Register const& r) { return registers()[r.index()]; }
|
Value& reg(Register const& r) { return registers()[r.index()]; }
|
||||||
|
@ -40,14 +50,15 @@ public:
|
||||||
|
|
||||||
void enter_frame(RegisterWindow const& frame)
|
void enter_frame(RegisterWindow const& frame)
|
||||||
{
|
{
|
||||||
++m_manually_entered_frames;
|
m_manually_entered_frames.append(true);
|
||||||
m_register_windows.append(make<RegisterWindow>(frame));
|
m_register_windows.append(make<RegisterWindow>(frame));
|
||||||
}
|
}
|
||||||
void leave_frame()
|
NonnullOwnPtr<RegisterWindow> pop_frame()
|
||||||
{
|
{
|
||||||
VERIFY(m_manually_entered_frames);
|
VERIFY(!m_manually_entered_frames.is_empty());
|
||||||
--m_manually_entered_frames;
|
VERIFY(m_manually_entered_frames.last());
|
||||||
m_register_windows.take_last();
|
m_manually_entered_frames.take_last();
|
||||||
|
return m_register_windows.take_last();
|
||||||
}
|
}
|
||||||
|
|
||||||
void jump(Label const& label)
|
void jump(Label const& label)
|
||||||
|
@ -77,9 +88,9 @@ private:
|
||||||
GlobalObject& m_global_object;
|
GlobalObject& m_global_object;
|
||||||
Realm& m_realm;
|
Realm& m_realm;
|
||||||
NonnullOwnPtrVector<RegisterWindow> m_register_windows;
|
NonnullOwnPtrVector<RegisterWindow> m_register_windows;
|
||||||
|
Vector<bool> m_manually_entered_frames;
|
||||||
Optional<BasicBlock const*> m_pending_jump;
|
Optional<BasicBlock const*> m_pending_jump;
|
||||||
Value m_return_value;
|
Value m_return_value;
|
||||||
size_t m_manually_entered_frames { 0 };
|
|
||||||
Executable const* m_current_executable { nullptr };
|
Executable const* m_current_executable { nullptr };
|
||||||
Vector<UnwindInfo> m_unwind_contexts;
|
Vector<UnwindInfo> m_unwind_contexts;
|
||||||
Handle<Exception> m_saved_exception;
|
Handle<Exception> m_saved_exception;
|
||||||
|
|
|
@ -769,15 +769,19 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body()
|
||||||
if (JS::Bytecode::g_dump_bytecode)
|
if (JS::Bytecode::g_dump_bytecode)
|
||||||
m_bytecode_executable->dump();
|
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())
|
if (auto* exception = vm.exception())
|
||||||
return throw_completion(exception->value());
|
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.
|
// NOTE: Running the bytecode should eventually return a completion.
|
||||||
// Until it does, we assume "return" and include the undefined fallback from the call site.
|
// Until it does, we assume "return" and include the undefined fallback from the call site.
|
||||||
if (m_kind != FunctionKind::Generator)
|
if (m_kind != FunctionKind::Generator)
|
||||||
return { Completion::Type::Return, result.value_or(js_undefined()), {} };
|
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, {} };
|
return { Completion::Type::Return, generator_object, {} };
|
||||||
} else {
|
} else {
|
||||||
if (m_kind == FunctionKind::Generator)
|
if (m_kind == FunctionKind::Generator)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue