painless: make compound statement like a[1]++ work with def. There was also a bug with the size of LDefArray: fixed

This commit is contained in:
Uwe Schindler 2016-05-15 22:58:11 +02:00
parent d221cd14d2
commit 65aca4f71c
4 changed files with 17 additions and 1 deletions

View File

@ -23,6 +23,8 @@ import org.elasticsearch.painless.Definition.Type;
/** /**
* The superclass for all LDef* (link) nodes that store or return a DEF. (Internal only.) * The superclass for all LDef* (link) nodes that store or return a DEF. (Internal only.)
* For this node it is allowed to change {@link ALink#after} from outside, by default
* {@code after} is {@code DEF}.
*/ */
abstract class ADefLink extends ALink { abstract class ADefLink extends ALink {

View File

@ -215,6 +215,12 @@ public final class EChain extends AExpression {
there = AnalyzerCaster.getLegalCast(definition, location, last.after, promote, false); there = AnalyzerCaster.getLegalCast(definition, location, last.after, promote, false);
back = AnalyzerCaster.getLegalCast(definition, location, promote, last.after, true); back = AnalyzerCaster.getLegalCast(definition, location, promote, last.after, true);
if (last instanceof ADefLink) {
final ADefLink lastDef = (ADefLink) last;
// Unfortunately, we don't know the real type because we load from DEF and store to DEF!
lastDef.storeValueType = last.after;
}
statement = true; statement = true;
actual = read ? last.after : definition.voidType; actual = read ? last.after : definition.voidType;
} }

View File

@ -36,7 +36,7 @@ final class LDefArray extends ADefLink {
AExpression index; AExpression index;
LDefArray(final int line, final String location, final AExpression index) { LDefArray(final int line, final String location, final AExpression index) {
super(line, location, 0); super(line, location, 2);
this.index = index; this.index = index;
} }

View File

@ -54,6 +54,14 @@ public class ArrayTests extends ScriptTestCase {
assertEquals(5, exec("def x = new def[4]; x[0] = 5; return x[0];")); assertEquals(5, exec("def x = new def[4]; x[0] = 5; return x[0];"));
} }
public void testArrayCompoundInt() {
assertEquals(6, exec("int[] x = new int[5]; x[0] = 5; x[0]++; return x[0];"));
}
public void testArrayCompoundDef() {
assertEquals(6, exec("def x = new int[5]; x[0] = 5; x[0]++; return x[0];"));
}
public void testForLoop() { public void testForLoop() {
assertEquals(999*1000/2, exec("def a = new int[1000]; for (int x = 0; x < a.length; x++) { a[x] = x; } "+ assertEquals(999*1000/2, exec("def a = new int[1000]; for (int x = 0; x < a.length; x++) { a[x] = x; } "+
"int total = 0; for (int x = 0; x < a.length; x++) { total += a[x]; } return total;")); "int total = 0; for (int x = 0; x < a.length; x++) { total += a[x]; } return total;"));