Skip to content

Commit

Permalink
Processed PR remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
stanhebben committed May 31, 2024
1 parent b459454 commit 4ac76c2
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ public Void visitCall(CallExpression expression) {
if (expression.method.getHeader().getReturnType().isGeneric())
javaWriter.checkCast(context.getType(expression.type));

return null;
}
return null;
}

@Override
public Void visitCallStatic(CallStaticExpression expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import org.objectweb.asm.Label;
import org.objectweb.asm.Type;
import org.openzen.zenscript.codemodel.expression.*;
import org.openzen.zenscript.codemodel.statement.ReturnStatement;
import org.openzen.zenscript.codemodel.type.*;
import org.openzen.zenscript.codemodel.type.builtin.BuiltinMethodSymbol;
import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
import org.openzen.zenscript.javashared.*;

import java.util.Collections;
import java.util.concurrent.atomic.AtomicInteger;

public class JavaMethodBytecodeCompiler implements JavaMethodCompiler<Void> {
Expand Down Expand Up @@ -1300,7 +1298,7 @@ public Void specialStaticMethod(JavaSpecialMethod method, CallArguments args) {
arguments[0].accept(expressionVisitor);
javaWriter.invokeVirtual(STRINGBUILDER_LENGTH);
break;
case LIST_TO_ARRAY: {
case COLLECTION_TO_ARRAY: {
Expression value = arguments[0];
value.accept(expressionVisitor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ public String getMethodDescriptor(FunctionHeader header) {
return getMethodDescriptor(header, false, "");
}

// check if a type cast is needed after calling a method (such as, returning T or T[], but types like Map... are fine because any generic types would be erased)
public boolean isGenericReturn(TypeID type) {
return type.isGeneric() || type.asArray().map(array -> isGenericReturn(array.elementType)).orElse(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public enum JavaSpecialMethod implements JavaMethod {
STRINGBUILDER_ISEMPTY,
LIST_TO_ARRAY,
COLLECTION_TO_ARRAY,
CONTAINS_AS_INDEXOF,
SORTED,
SORTED_WITH_COMPARATOR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* @author Hoofdgebruiker
Expand All @@ -39,7 +38,7 @@ public class JavaPrepareDefinitionVisitor implements DefinitionVisitor<JavaClass
cls.addInstanceMethod("add", "add", "(Ljava/lang/Object;)Z");
cls.addInstanceMethod("remove", "remove", "(Ljava/lang/Object;)Z");
cls.addInstanceMethod("contains", "contains", "(Ljava/lang/Object;)Z");
cls.addMethod("toArray", JavaSpecialMethod.LIST_TO_ARRAY);
cls.addMethod("toArray", JavaSpecialMethod.COLLECTION_TO_ARRAY);
cls.addInstanceMethod("length", "size", "()I");
cls.addInstanceMethod("isEmpty", "isEmpty", "()Z");
cls.addInstanceMethod("iterate", "iterator", "()Ljava/util/Iterator;");
Expand Down Expand Up @@ -90,7 +89,7 @@ public class JavaPrepareDefinitionVisitor implements DefinitionVisitor<JavaClass
list.addInstanceMethod("getAtIndex", "get", "(I)Ljava/lang/Object;", true);
list.addInstanceMethod("setAtIndex", "set", "(ILjava/lang/Object;)Ljava/lang/Object;", true);
list.addInstanceMethod("contains", "contains", "(Ljava/lang/Object;)Z");
list.addMethod("toArray", JavaSpecialMethod.LIST_TO_ARRAY);
list.addMethod("toArray", JavaSpecialMethod.COLLECTION_TO_ARRAY);
list.addInstanceMethod("length", "size", "()I");
list.addInstanceMethod("isEmpty", "isEmpty", "()Z");
list.addInstanceMethod("iterate", "iterator", "()Ljava/util/Iterator;");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
package org.openzen.zenscript.scriptingexample.tests.actual_test.arrays;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openzen.zenscript.scriptingexample.tests.helpers.ScriptBuilder;
import org.openzen.zenscript.scriptingexample.tests.helpers.ZenCodeTest;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class ArrayOperatorTests extends ZenCodeTest {
private static String createString(int length) {
final StringBuilder stringBuilder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
stringBuilder.append('A');
}
return stringBuilder.toString();
}

@Test
public void containsReturnsTrueForMatch() {
ScriptBuilder.create()
Expand All @@ -41,41 +27,6 @@ public void containsReturnsFalseForNonMatch() {
logger.assertPrintOutput(0, "false");
}

@Test
public void canCastToList() {
ScriptBuilder.create()
.add("var array = ['a', 'b', 'c'] as string[];")
.add("var list = array as stdlib.List<string>;")
.add("var listImplicit as stdlib.List<string>;")
.add("listImplicit = array;")
.add("println(list.length);")
.add("println(listImplicit.length);")
.execute(this);

logger.assertPrintOutputSize(2);
logger.assertPrintOutput(0, "3");
logger.assertPrintOutput(1, "3");
}

@Test
public void canCastFromList() {
ScriptBuilder.create()
.add("var list = new stdlib.List<string>();")
.add("list.add('a');")
.add("list.add('b');")
.add("list.add('c');")
.add("var array = list as string[];")
.add("var arrayImplicit as string[];")
.add("arrayImplicit = list;")
.add("println(array.length);")
.add("println(arrayImplicit.length);")
.execute(this);

logger.assertPrintOutputSize(2);
logger.assertPrintOutput(0, "3");
logger.assertPrintOutput(1, "3");
}

@Test
public void indexMethodWorks() {
ScriptBuilder.create()
Expand Down

0 comments on commit 4ac76c2

Please sign in to comment.