LibJS/Bytecode: Merge adjacent exception handlers

For example, this:
```
Exception handlers:
    from  678 to  698 handler  658 finalizer    0
    from  698 to  6f8 handler  658 finalizer    0
    from  6f8 to  708 handler  658 finalizer    0
    from  708 to  750 handler  658 finalizer    0
    from  750 to  788 handler  658 finalizer    0
    from  788 to  7a0 handler  658 finalizer    0
    from  7a0 to  7a8 handler  658 finalizer    0
```

Becomes:
```
Exception handlers:
    from  678 to  7a8 handler  658 finalizer    0
```
This commit is contained in:
Luke Wilde 2025-10-10 21:05:50 +01:00 committed by Jelle Raaijmakers
parent 86b95b1d7a
commit d0ef1aad2d
Notes: github-actions[bot] 2025-11-07 08:58:11 +00:00

View file

@ -462,7 +462,17 @@ CodeGenerationErrorOr<GC::Ref<Executable>> Generator::compile(VM& vm, ASTNode co
auto end_offset = unlinked_handler.end_offset;
auto handler_offset = unlinked_handler.handler ? block_offsets.get(unlinked_handler.handler).value() : Optional<size_t> {};
auto finalizer_offset = unlinked_handler.finalizer ? block_offsets.get(unlinked_handler.finalizer).value() : Optional<size_t> {};
linked_exception_handlers.append({ start_offset, end_offset, handler_offset, finalizer_offset });
auto maybe_exception_handler_to_merge_with = linked_exception_handlers.find_if([&](Executable::ExceptionHandlers const& exception_handler) {
return exception_handler.end_offset == start_offset && exception_handler.handler_offset == handler_offset && exception_handler.finalizer_offset == finalizer_offset;
});
if (!maybe_exception_handler_to_merge_with.is_end()) {
auto& exception_handler_to_merge_with = *maybe_exception_handler_to_merge_with;
exception_handler_to_merge_with.end_offset = end_offset;
} else {
linked_exception_handlers.append({ start_offset, end_offset, handler_offset, finalizer_offset });
}
}
quick_sort(linked_exception_handlers, [](auto const& a, auto const& b) {