Switch the parameter order for lambda expressions

This commit is contained in:
Volker Berlin 2022-02-13 17:15:54 +01:00
parent 84f2f45d4c
commit 1f045f312d
3 changed files with 25 additions and 11 deletions

View File

@ -191,6 +191,9 @@ class LocaleVariableManager {
}
resetAddVar( null, i );
}
// in Eclipse for lambda expression only the method parameters has an entry in the variable table. But the not define parameter of the lambda constructor come before.
Arrays.sort( variables, 0, size );
}
/**
@ -455,7 +458,7 @@ class LocaleVariableManager {
/**
* The state of a single local variable slot.
*/
private static class Variable {
private static class Variable implements Comparable<Variable> {
private AnyType valueType;
@ -477,5 +480,13 @@ class LocaleVariableManager {
public boolean matchCodePosition( int codePosition ) {
return startPos <= codePosition && codePosition <= endPos;
}
/**
* {@inheritDoc}
*/
@Override
public int compareTo( Variable o ) {
return Integer.compare( idx, o.idx );
}
}
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2018 - 2021 Volker Berlin (i-net software)
Copyright 2018 - 2022 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -1208,6 +1208,13 @@ public class TypeManager {
}
watParser.reset( null, null, sig.iterator() );
// first add the values from the Lambda constructor which is saves in the syntetic class
for( int i = 0; i < paramFields.size(); i++ ) {
codebuilder.addLoadStoreInstruction( LambdaType.this, true, 0, 0, -1 );
codebuilder.addStructInstruction( StructOperator.GET, name, paramFields.get( i ), 0, -1 );
}
// forward the parameter from the current call without the THIS parameter because the call lambda method is static
for( int i = 1; i < sig.size(); i++ ) {
AnyType anyType = sig.get( i );
if( anyType == null ) {
@ -1216,11 +1223,6 @@ public class TypeManager {
codebuilder.addLoadStoreInstruction( anyType, true, i, 0, -1 );
}
for( int i = 0; i < paramFields.size(); i++ ) {
codebuilder.addLoadStoreInstruction( LambdaType.this, true, 0, 0, -1 );
codebuilder.addStructInstruction( StructOperator.GET, name, paramFields.get( i ), 0, -1 );
}
codebuilder.addCallInstruction( syntheticLambdaFunctionName, false, 0, -1 );
return watParser;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2018 - 2021 Volker Berlin (i-net software)
* Copyright 2018 - 2022 Volker Berlin (i-net software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.function.DoubleUnaryOperator;
import java.util.function.IntSupplier;
import java.util.function.IntUnaryOperator;
@ -315,11 +316,11 @@ public class StructsNonGC extends AbstractBaseTest {
}
@Export
static int lambda3() {
static double lambda3() {
int v1 = 42;
int v2 = 7;
IntUnaryOperator val = (x) -> x + v2 + v1;
return val.applyAsInt( 13 );
DoubleUnaryOperator val = (x) -> x * v2 + v1;
return val.applyAsDouble( 13 );
}
@Export