mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:38:10 +00:00
LibJS: Implement bytecode generation for try..catch..finally
EnterUnwindContext pushes an unwind context (exception handler and/or finalizer) onto a stack. LeaveUnwindContext pops the unwind context from that stack. Upon return to the interpreter loop we check whether the VM has an exception pending. If no unwind context is available we return from the loop. If an exception handler is available we clear the VM's exception, put the exception value into the accumulator register, clear the unwind context's handler and jump to the handler. If no handler is available but a finalizer is available we save the exception value + metadata (for later use by ContinuePendingUnwind), clear the VM's exception, pop the unwind context and jump to the finalizer. ContinuePendingUnwind checks whether a saved exception is available. If no saved exception is available it jumps to the resume label. Otherwise it stores the exception into the VM. The Jump after LeaveUnwindContext could be integrated into the LeaveUnwindContext instruction. I've kept them separate for now to make the bytecode more readable. > try { 1; throw "x" } catch (e) { 2 } finally { 3 }; 4 1: [ 0] EnterScope [ 10] EnterUnwindContext handler:@4 finalizer:@3 [ 38] EnterScope [ 48] LoadImmediate 1 [ 60] NewString 1 ("x") [ 70] Throw <for non-terminated blocks: insert LeaveUnwindContext + Jump @3 here> 2: [ 0] LoadImmediate 4 3: [ 0] EnterScope [ 10] LoadImmediate 3 [ 28] ContinuePendingUnwind resume:@2 4: [ 0] SetVariable 0 (e) [ 10] EnterScope [ 20] LoadImmediate 2 [ 38] LeaveUnwindContext [ 3c] Jump @3 String Table: 0: e 1: x
This commit is contained in:
parent
d2f1476428
commit
67cc31a74f
9 changed files with 188 additions and 3 deletions
|
@ -67,8 +67,25 @@ Value Interpreter::run(Executable const& executable)
|
|||
while (!pc.at_end()) {
|
||||
auto& instruction = *pc;
|
||||
instruction.execute(*this);
|
||||
if (vm().exception())
|
||||
break;
|
||||
if (vm().exception()) {
|
||||
m_saved_exception = {};
|
||||
if (m_unwind_contexts.is_empty())
|
||||
break;
|
||||
auto& unwind_context = m_unwind_contexts.last();
|
||||
if (unwind_context.handler) {
|
||||
block = unwind_context.handler;
|
||||
unwind_context.handler = nullptr;
|
||||
accumulator() = vm().exception()->value();
|
||||
vm().clear_exception();
|
||||
will_jump = true;
|
||||
} else if (unwind_context.finalizer) {
|
||||
block = unwind_context.finalizer;
|
||||
m_unwind_contexts.take_last();
|
||||
will_jump = true;
|
||||
m_saved_exception = Handle<Exception>::create(vm().exception());
|
||||
vm().clear_exception();
|
||||
}
|
||||
}
|
||||
if (m_pending_jump.has_value()) {
|
||||
block = m_pending_jump.release_value();
|
||||
will_jump = true;
|
||||
|
@ -121,4 +138,23 @@ Value Interpreter::run(Executable const& executable)
|
|||
return return_value;
|
||||
}
|
||||
|
||||
void Interpreter::enter_unwind_context(Optional<Label> handler_target, Optional<Label> finalizer_target)
|
||||
{
|
||||
m_unwind_contexts.empend(handler_target.has_value() ? &handler_target->block() : nullptr, finalizer_target.has_value() ? &finalizer_target->block() : nullptr);
|
||||
}
|
||||
|
||||
void Interpreter::leave_unwind_context()
|
||||
{
|
||||
m_unwind_contexts.take_last();
|
||||
}
|
||||
|
||||
void Interpreter::continue_pending_unwind(Label const& resume_label)
|
||||
{
|
||||
if (!m_saved_exception.is_null()) {
|
||||
vm().set_exception(*m_saved_exception.cell());
|
||||
m_saved_exception = {};
|
||||
} else {
|
||||
jump(resume_label);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue