Skip to content

Commit

Permalink
feat(objectionary#750): handle try-catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Nov 8, 2024
1 parent 8873fb2 commit 67bfc4d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Object elementType() {
break;
case Opcodes.ALOAD:
case Opcodes.ASTORE:
result = Opcodes.NULL;
result = Opcodes.TOP;
break;
default:
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,12 @@ Label handlerLabel() {
return this.handler;
}

/**
* Exception type.
* @return Type.
*/
String descriptor() {
return this.type;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -78,14 +79,12 @@ final class StackMapFrames {
* @return The list of frames.
*/
List<BytecodeFrame> frames() {
// Initialization
final Deque<Entry> worklist = new ArrayDeque<>(0);
final Map<Integer, Entry> res = new LinkedHashMap<>(0);
final int size = this.entries.size();
//Work
final Entry initial = this.initial();
// res.put(0, initial);
worklist.push(initial);
this.blocks.stream().map(this::block).forEach(worklist::push);
while (!worklist.isEmpty()) {
Entry current = worklist.pop();
for (int index = current.indx(); index < size; ++index) {
Expand Down Expand Up @@ -124,6 +123,7 @@ List<BytecodeFrame> frames() {
res.values()
.stream()
.filter(Entry::joined)
.sorted(Comparator.comparingInt(Entry::indx))
.collect(Collectors.toList())
);
}
Expand All @@ -141,12 +141,20 @@ private void logEntires(final Map<Integer, Entry> res) {
}
}

private Entry block(final BytecodeTryCatchBlock block) {
final int index = this.index(block.handlerLabel());
final Entry initial = this.initial();
final LinkedHashMap<Integer, Object> stack = new LinkedHashMap<>(0);
stack.put(0, block.descriptor());
return initial.withStack(stack).join(index);
}

private Entry initial() {
final String descriptor = this.props.descriptor();
final boolean stat = this.props.isStatic();
final Type[] types = Type.getArgumentTypes(descriptor);
int indx = stat ? 0 : 1;
LinkedHashMap<Integer, Object> locals = new LinkedHashMap<>(0);
final LinkedHashMap<Integer, Object> locals = new LinkedHashMap<>(0);
if (!stat) {
locals.put(0, Opcodes.TOP);
}
Expand Down Expand Up @@ -298,6 +306,10 @@ Entry copy(final int indx) {
return new Entry(indx, this.locals, this.stack, false);
}

Entry withStack(final LinkedHashMap<Integer, Object> stack) {
return new Entry(this.indx, this.locals, stack, this.join);
}

Entry append(final Entry next) {
final int max = Math.max(
this.locals.keySet().stream().mapToInt(Integer::intValue).max().orElse(0),
Expand Down

0 comments on commit 67bfc4d

Please sign in to comment.