ladybird/Userland/Libraries/LibJS/Bytecode
Gunnar Beutner 67cc31a74f 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
2021-06-10 21:59:46 +02:00
..
ASTCodegen.cpp LibJS: Implement bytecode generation for try..catch..finally 2021-06-10 21:59:46 +02:00
BasicBlock.cpp LibJS: Store strings in a string table 2021-06-09 17:42:52 +02:00
BasicBlock.h LibJS: Implement bytecode generation for try..catch..finally 2021-06-10 21:59:46 +02:00
Generator.cpp LibJS: Implement bytecode generation for BreakStatement 2021-06-10 21:48:20 +04:30
Generator.h LibJS: Implement bytecode generation for try..catch..finally 2021-06-10 21:59:46 +02:00
Instruction.cpp LibJS: Move Instruction::length() to the Op.h header 2021-06-09 09:24:32 +02:00
Instruction.h LibJS: Implement bytecode generation for try..catch..finally 2021-06-10 21:59:46 +02:00
Interpreter.cpp LibJS: Implement bytecode generation for try..catch..finally 2021-06-10 21:59:46 +02:00
Interpreter.h LibJS: Implement bytecode generation for try..catch..finally 2021-06-10 21:59:46 +02:00
Label.h LibJS: Generate bytecode in basic blocks instead of one big block 2021-06-09 09:07:29 +02:00
Op.cpp LibJS: Implement bytecode generation for try..catch..finally 2021-06-10 21:59:46 +02:00
Op.h LibJS: Implement bytecode generation for try..catch..finally 2021-06-10 21:59:46 +02:00
Register.h LibJS: Introduce an accumulator register to Bytecode::Interpreter 2021-06-08 21:00:12 +02:00
StringTable.cpp LibJS: Store strings in a string table 2021-06-09 17:42:52 +02:00
StringTable.h LibJS: Store strings in a string table 2021-06-09 17:42:52 +02:00