From 9b324fdacbf979f895278bba060edfff4ef1363d Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sat, 6 Nov 2021 16:59:30 +0100 Subject: [PATCH] Fallback on variable slot calculation --- .../module/LocaleVariableManager.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java index 83e3ad5..8157b27 100644 --- a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java +++ b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java @@ -384,6 +384,27 @@ class LocaleVariableManager { } } + // there is no variable slot declared for the given java code position + // in a second try we search for a slot that has the smallest distance + // TODO a better option can be to use the next slot and not the smallest distance because the position is define some time on first use and not on assign + int index = -1; + int distance = Integer.MAX_VALUE; + for( int i = 0; i < size; i++ ) { + Variable var = variables[i]; + if( slot != var.idx ) { + continue; + } + + int distanceDiff = Math.min( Math.abs( javaCodePos - var.startPos), Math.abs( javaCodePos - var.endPos ) ); + if( distanceDiff <= distance ) { + index = i; + distance = distanceDiff; + } + } + if( index >= 0 ) { + return index; + } + throw new WasmException( "Can not find local variable for slot: " + slot + " on code position " + javaCodePos, -1 ); }