Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix forloop variables scoping for closures #5537

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public Object rreturn(Function<EvaluationContext, Object> expression) {
while (ForExpressionNode.nextIteration(ctx, ictx)) {
Object result = expression.apply(ctx);
results.add(result);
ctx.exitFrame(); // last i-th scope unrolled, see also ForExpressionNode.nextIteration(...)
}
return results;
} catch (EndpointOfRangeNotOfNumberException e) {
Expand All @@ -278,6 +279,7 @@ private ForIteration[] initializeContexts(EvaluationContext ctx, List<IterationC
for (IterationContextCompiled icn : iterationContexts) {
ictx[i] = createQuantifiedExpressionIterationContext(ctx, icn);
if (i < iterationContexts.size() - 1 && ictx[i].hasNextValue()) {
ctx.enterFrame(); // open loop scope frame, for every iter ctx, except last one as guarded by if clause above
ForExpressionNode.setValueIntoContext(ctx, ictx[i]);
}
i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static ObjectCreationExpr internal(Expression parameters, Expression body

public static DirectCompilerResult declaration(FunctionDefNode n, MethodCallExpr list, Expression fnBody) {
LambdaExpr lambda = Expressions.lambda(fnBody);
String fnName = Constants.functionName(n.getBody().getText());
String fnName = Constants.functionName("FNBODY_" + n.getBody().getText());
DirectCompilerResult r = DirectCompilerResult.of(
Functions.internal(list, new NameExpr(fnName)),
BuiltInType.FUNCTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public Object evaluate(EvaluationContext ctx) {
while ( nextIteration( ctx, ictx ) ) {
Object result = expression.evaluate( ctx );
results.add( result );
ctx.exitFrame(); // last i-th scope unrolled, see also ForExpressionNode.nextIteration(...)
}
return results;
} catch (EndpointOfRangeNotOfNumberException e) {
Expand All @@ -90,9 +91,16 @@ public static boolean nextIteration(EvaluationContext ctx, ForIteration[] ictx)
int i = ictx.length-1;
while ( i >= 0 && i < ictx.length ) {
if ( ictx[i].hasNextValue() ) {
ctx.enterFrame(); // on first iter, open last scope frame; or new ones when prev unrolled
setValueIntoContext( ctx, ictx[i] );
i++;
} else {
if ( i > 0 ) {
// end of iter loop for this i-th scope; i-th scope is always unrolled as part of the
// for-loop cycle, so here must unroll the _prev_ scope;
// the if-guard for this code block makes sure NOT to unroll bottom one.
ctx.exitFrame();
}
i--;
}
}
Expand All @@ -114,6 +122,7 @@ private ForIteration[] initializeContexts(EvaluationContext ctx, List<IterationC
for ( IterationContextNode icn : iterationContexts ) {
ictx[i] = createQuantifiedExpressionIterationContext( ctx, icn );
if( i < iterationContexts.size()-1 && ictx[i].hasNextValue() ) {
ctx.enterFrame(); // open loop scope frame, for every iter ctx, except last one as guarded by if clause above
setValueIntoContext( ctx, ictx[i] );
}
i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.runners.Parameterized;
Expand Down Expand Up @@ -50,6 +51,7 @@ public static Collection<Object[]> data() {
{"{ a: 1, b : 3, c : for x in a+2..b-2 return x+1}", mapOf(entry("a", BigDecimal.valueOf(1)), entry("b", BigDecimal.valueOf(3)), entry("c", Stream.of(3, 2, 1 ).map(x -> BigDecimal.valueOf(x + 1 ) ).collect(Collectors.toList() )) ), null},
{"{ a: \"ciao\", b : 3, c : for x in a..b return x+1}", mapOf(entry("a", "ciao"), entry("b", BigDecimal.valueOf(3)), entry("c", null)), FEELEvent.Severity.ERROR},
{"for i in 0..10 return if i = 0 then 1 else i * partial[-1]", Stream.of(1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 ).map(BigDecimal::valueOf).collect(Collectors.toList() ), null},
{"{xs: for i in 1..10 return function() i, ys: for f in xs return f()}.ys", IntStream.range(1, 11).boxed().map(BigDecimal::valueOf).collect(Collectors.toList()), null},

};
return addAdditionalParameters(cases, false);
Expand Down