Skip to content

Commit 314e6d0

Browse files
committed
Fix: Map keys and values getters
1 parent e643a46 commit 314e6d0

File tree

2 files changed

+37
-4
lines changed
  • JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler
  • ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test

2 files changed

+37
-4
lines changed

JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
122122
private static final JavaMethod MAP_CONTAINS_KEY = JavaMethod.getInterface(JavaClass.MAP, "containsKey", "(Ljava/lang/Object;)Z");
123123
private static final JavaMethod MAP_SIZE = JavaMethod.getInterface(JavaClass.MAP, "size", "()I");
124124
private static final JavaMethod MAP_ISEMPTY = JavaMethod.getInterface(JavaClass.MAP, "isEmpty", "()Z");
125-
private static final JavaMethod MAP_KEYS = JavaMethod.getInterface(JavaClass.MAP, "keys", "()Ljava/lang/Object;");
126-
private static final JavaMethod MAP_VALUES = JavaMethod.getInterface(JavaClass.MAP, "values", "()Ljava/lang/Object;");
125+
private static final JavaMethod MAP_KEYS = JavaMethod.getInterface(JavaClass.MAP, "keySet", "()Ljava/util/Set;");
126+
private static final JavaMethod MAP_VALUES = JavaMethod.getInterface(JavaClass.MAP, "values", "()Ljava/util/Collection;");
127127
private static final JavaMethod ARRAYS_COPY_OF_RANGE_OBJECTS = JavaMethod.getNativeStatic(JavaClass.ARRAYS, "copyOfRange", "([Ljava/lang/Object;II)[Ljava/lang/Object;");
128128
private static final JavaMethod ARRAYS_COPY_OF_RANGE_BOOLS = JavaMethod.getNativeStatic(JavaClass.ARRAYS, "copyOfRange", "([ZII)[Z");
129129
private static final JavaMethod ARRAYS_COPY_OF_RANGE_BYTES = JavaMethod.getNativeStatic(JavaClass.ARRAYS, "copyOfRange", "([BII)[B");
@@ -2433,7 +2433,7 @@ public Void visitGetter(GetterExpression expression) {
24332433
javaWriter.invokeVirtual(MAP_KEYS);
24342434
javaWriter.dup();
24352435
javaWriter.invokeVirtual(COLLECTION_SIZE);
2436-
javaWriter.newArray(resultType);
2436+
javaWriter.newArray(resultType.getElementType());
24372437
javaWriter.invokeVirtual(COLLECTION_TOARRAY);
24382438
javaWriter.checkCast(resultType);
24392439
break;
@@ -2444,7 +2444,7 @@ public Void visitGetter(GetterExpression expression) {
24442444
javaWriter.invokeVirtual(MAP_VALUES);
24452445
javaWriter.dup();
24462446
javaWriter.invokeVirtual(COLLECTION_SIZE);
2447-
javaWriter.newArray(resultType);
2447+
javaWriter.newArray(resultType.getElementType());
24482448
javaWriter.invokeVirtual(COLLECTION_TOARRAY);
24492449
javaWriter.checkCast(resultType);
24502450
break;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.openzen.zenscript.scriptingexample.tests.actual_test;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openzen.zenscript.scriptingexample.tests.helpers.ScriptBuilder;
5+
import org.openzen.zenscript.scriptingexample.tests.helpers.ZenCodeTest;
6+
7+
class MapTests extends ZenCodeTest {
8+
@Test
9+
void keysWork() {
10+
ScriptBuilder.create()
11+
.add("var map = {'Hello': 'World'};")
12+
.add("println(map.keys.length);")
13+
.add("println(map.keys[$-1]);")
14+
.execute(this);
15+
16+
logger.assertPrintOutputSize(2);
17+
logger.assertPrintOutput(0, "1");
18+
logger.assertPrintOutput(1, "Hello");
19+
}
20+
21+
@Test
22+
void valuesWork() {
23+
ScriptBuilder.create()
24+
.add("var map = {'Hello': 'World'};")
25+
.add("println(map.values.length);")
26+
.add("println(map.values[$-1]);")
27+
.execute(this);
28+
29+
logger.assertPrintOutputSize(2);
30+
logger.assertPrintOutput(0, "1");
31+
logger.assertPrintOutput(1, "World");
32+
}
33+
}

0 commit comments

Comments
 (0)