1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:38:11 +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:
Gunnar Beutner 2021-06-10 15:04:38 +02:00 committed by Andreas Kling
parent d2f1476428
commit 67cc31a74f
9 changed files with 188 additions and 3 deletions

View file

@ -261,6 +261,21 @@ void Throw::execute(Bytecode::Interpreter& interpreter) const
interpreter.vm().throw_exception(interpreter.global_object(), interpreter.accumulator());
}
void EnterUnwindContext::execute(Bytecode::Interpreter& interpreter) const
{
interpreter.enter_unwind_context(m_handler_target, m_finalizer_target);
}
void LeaveUnwindContext::execute(Bytecode::Interpreter& interpreter) const
{
interpreter.leave_unwind_context();
}
void ContinuePendingUnwind::execute(Bytecode::Interpreter& interpreter) const
{
interpreter.continue_pending_unwind(m_resume_target);
}
String Load::to_string(Bytecode::Executable const&) const
{
return String::formatted("Load {}", m_src);
@ -394,4 +409,21 @@ String Throw::to_string(Bytecode::Executable const&) const
return "Throw";
}
String EnterUnwindContext::to_string(Bytecode::Executable const&) const
{
auto handler_string = m_handler_target.has_value() ? String::formatted("{}", *m_handler_target) : "<empty>";
auto finalizer_string = m_finalizer_target.has_value() ? String::formatted("{}", *m_finalizer_target) : "<empty>";
return String::formatted("EnterUnwindContext handler:{} finalizer:{}", handler_string, finalizer_string);
}
String LeaveUnwindContext::to_string(Bytecode::Executable const&) const
{
return "LeaveUnwindContext";
}
String ContinuePendingUnwind::to_string(Bytecode::Executable const&) const
{
return String::formatted("ContinuePendingUnwind resume:{}", m_resume_target);
}
}