Lambdas with parameters had one value too many on the stack.

This commit is contained in:
Volker Berlin 2022-07-31 20:38:04 +02:00
parent 82cf49aa41
commit 50ca777dc3
No known key found for this signature in database
GPG Key ID: 988423EF815BE4CB
2 changed files with 22 additions and 4 deletions

View File

@ -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;

View File

@ -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();