diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index ad4ce64..f00e93a 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -995,18 +995,22 @@ public abstract class WasmCodeBuilder { int idx = StackInspector.findInstructionThatPushValue( instructions, paramCount, javaCodePos ).idx; int pos = instructions.size(); addStructInstruction( StructOperator.NEW_DEFAULT, lambdaTypeName, null, javaCodePos, lineNumber ); - if( !options.useGC() ) { + boolean extraDup = !options.useGC(); + if( extraDup ) { + // Bringing forward the DUP operation of the parameter loop to get a slot addDupInstruction( false, javaCodePos, lineNumber ); } - int slot = ((WasmLocalInstruction)findInstructionThatPushValue( 1, javaCodePos )).getSlot(); + int slot = ((WasmLocalInstruction)findInstructionThatPushValue( 1, javaCodePos )).getSlot(); // move the creating of the lambda instance before the parameters on the stack Collections.rotate( instructions.subList( idx, instructions.size() ), idx - pos ); for( int i = 0; i < paramCount; i++ ) { NamedStorageType field = paramFields.get( i ); - idx = StackInspector.findInstructionThatPushValue( instructions, paramCount - i, javaCodePos ).idx; - instructions.add( idx, new WasmLoadStoreInstruction( VariableOperator.get, slot, localVariables, javaCodePos, lineNumber ) ); + if( i > 0 || !extraDup ) { + idx = StackInspector.findInstructionThatPushValue( instructions, paramCount - i, javaCodePos ).idx; + instructions.add( idx, new WasmLoadStoreInstruction( VariableOperator.get, slot, localVariables, javaCodePos, lineNumber ) ); + } pos = instructions.size(); idx = paramCount - i - 1; idx = idx == 0 ? pos : StackInspector.findInstructionThatPushValue( instructions, idx, javaCodePos ).idx; diff --git a/test/de/inetsoftware/jwebassembly/runtime/StructsNonGC.java b/test/de/inetsoftware/jwebassembly/runtime/StructsNonGC.java index f7dac9f..474825b 100644 --- a/test/de/inetsoftware/jwebassembly/runtime/StructsNonGC.java +++ b/test/de/inetsoftware/jwebassembly/runtime/StructsNonGC.java @@ -77,6 +77,7 @@ public class StructsNonGC extends AbstractBaseTest { addParam( list, script, "lambda2" ); addParam( list, script, "lambda3" ); addParam( list, script, "lambdaWithInstanceAccess" ); + addParam( list, script, "lambdaInsideTryCatch" ); addParam( list, script, "simpleName_Object" ); addParam( list, script, "simpleName_Anonymous" ); addParam( list, script, "simpleName_Array" ); @@ -349,6 +350,19 @@ public class StructsNonGC extends AbstractBaseTest { return val.getAsInt(); } + @Export + public static int lambdaInsideTryCatch() { + int result = 17; + int field = 13; + try { + IntSupplier val = () -> field; + result = val.getAsInt(); + } catch( Throwable ex ) { + result = 42; + } + return result; + } + @Export static boolean isPrimitive_int() { return int.class.isPrimitive();