5 Коміти

Автор SHA1 Повідомлення Дата
  Jared c7466e5c37
Log more info about what package is trying to be registered into 5 роки тому
  kindlich d4a722cd92
Fixed this call in primitive expansions 5 роки тому
  kindlich 6ada4ffce1
Replaced hardcoded IntRange class name 5 роки тому
  kindlich d2fdb13730
Fix expansion method signature and call 5 роки тому
  kindlich 341f8c23f5
Jdk classes no longer 'created', stopped 2nd registration of empty classes 5 роки тому

+ 6
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeModule.java Переглянути файл

@@ -29,8 +29,12 @@ public class JavaBytecodeModule extends JavaCompiledModule {
29 29
 	public void addClass(String name, byte[] bytecode) {
30 30
 		if (bytecode == null)
31 31
 			return;
32
-		
33
-		classes.put(name, bytecode);
32
+
33
+		if(name.startsWith("java")) {
34
+			System.err.println("Invalid name " + name);
35
+		} else {
36
+			classes.put(name, bytecode);
37
+		}
34 38
 	}
35 39
 	
36 40
 	public void addScript(JavaScriptMethod method) {

+ 7
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java Переглянути файл

@@ -103,7 +103,13 @@ public class JavaCompiler {
103 103
 				target.addScript(method);
104 104
 
105 105
 			entry.getValue().classWriter.visitEnd();
106
-			target.addClass(entry.getKey(), entry.getValue().classWriter.toByteArray());
106
+
107
+			if (target.getClasses().containsKey(entry.getKey())) {
108
+				//TODO Scripts and definitions seem to create the same class. Bad!
109
+				System.err.println("Trying to register " + entry.getKey() + " a 2nd time");
110
+			}else{
111
+				target.addClass(entry.getKey(), entry.getValue().classWriter.toByteArray());
112
+			}
107 113
 		}
108 114
 
109 115
 		return target;

+ 22
- 8
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java Переглянути файл

@@ -884,11 +884,17 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
884 884
 				} else {
885 885
 					argument.accept(this);
886 886
 					javaWriter.dup();
887
-					int tmp = javaWriter.local(Type.getType("zsynthetic/IntRange"));
887
+					final String owner;
888
+					if (argument.type.type instanceof RangeTypeID) {
889
+						owner = context.getInternalName(argument.type);
890
+					} else {
891
+						owner = "zsynthetic/IntRange";
892
+					}
893
+					int tmp = javaWriter.local(Type.getType(owner));
888 894
 					javaWriter.storeInt(tmp);
889
-					javaWriter.getField("zsynthetic/IntRange", "from", "I");
895
+					javaWriter.getField(owner, "from", "I");
890 896
 					javaWriter.loadInt(tmp);
891
-					javaWriter.getField("zsynthetic/IntRange", "to", "I");
897
+					javaWriter.getField(owner, "to", "I");
892 898
 				}
893 899
 				javaWriter.invokeVirtual(STRING_SUBSTRING);
894 900
 				break;
@@ -1007,11 +1013,17 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
1007 1013
 				} else {
1008 1014
 					argument.accept(this);
1009 1015
 					javaWriter.dup();
1010
-					int tmp = javaWriter.local(Type.getType("zsynthetic/IntRange"));
1016
+					final String owner;
1017
+					if (argument.type.type instanceof RangeTypeID) {
1018
+						owner = context.getInternalName(argument.type);
1019
+					} else {
1020
+						owner = "zsynthetic/IntRange";
1021
+					}
1022
+					int tmp = javaWriter.local(Type.getType(owner));
1011 1023
 					javaWriter.storeInt(tmp);
1012
-					javaWriter.getField("zsynthetic/IntRange", "from", "I");
1024
+					javaWriter.getField(owner, "from", "I");
1013 1025
 					javaWriter.loadInt(tmp);
1014
-					javaWriter.getField("zsynthetic/IntRange", "to", "I");
1026
+					javaWriter.getField(owner, "to", "I");
1015 1027
 				}
1016 1028
 
1017 1029
 				if (type.elementType.type instanceof BasicTypeID) {
@@ -4126,7 +4138,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
4126 4138
 
4127 4139
 	@Override
4128 4140
 	public Void visitThis(ThisExpression expression) {
4129
-		javaWriter.loadObject(0);
4141
+		javaWriter.load(context.getType(expression.type), 0);
4130 4142
 		return null;
4131 4143
 	}
4132 4144
 
@@ -4220,9 +4232,11 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
4220 4232
 			getJavaWriter().invokeStatic(methodInfo);
4221 4233
 		} else if (methodInfo.kind == JavaMethod.Kind.INTERFACE) {
4222 4234
 			getJavaWriter().invokeInterface(methodInfo);
4235
+		} else if (methodInfo.kind == JavaMethod.Kind.EXPANSION) {
4236
+			getJavaWriter().invokeStatic(methodInfo);
4223 4237
 		} else if (methodInfo.kind == JavaMethod.Kind.COMPILED) {
4224 4238
 			Objects.requireNonNull(methodInfo.translation).translate(expression, this);
4225
-		} else if (methodInfo.cls.kind == JavaClass.Kind.INTERFACE) {
4239
+		} else if (methodInfo.cls != null && methodInfo.cls.kind == JavaClass.Kind.INTERFACE) {
4226 4240
 			getJavaWriter().invokeInterface(methodInfo);
4227 4241
 		} else {
4228 4242
 			getJavaWriter().invokeVirtual(methodInfo);

+ 5
- 3
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaForeachWriter.java Переглянути файл

@@ -4,6 +4,7 @@ import org.objectweb.asm.Label;
4 4
 import org.objectweb.asm.Type;
5 5
 import org.openzen.zenscript.codemodel.statement.Statement;
6 6
 import org.openzen.zenscript.codemodel.statement.VarStatement;
7
+import org.openzen.zenscript.codemodel.type.RangeTypeID;
7 8
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
8 9
 import org.openzen.zenscript.javashared.JavaClass;
9 10
 import org.openzen.zenscript.javashared.JavaMethod;
@@ -32,11 +33,12 @@ public class JavaForeachWriter {
32 33
         this.unboxingTypeVisitor = new JavaUnboxingTypeVisitor(this.javaWriter);
33 34
     }
34 35
 
35
-	public void visitIntRange() {
36
+	public void visitIntRange(RangeTypeID type) {
37
+		final String owner = statementVisitor.context.getInternalName(type);
36 38
 		javaWriter.dup();
37
-		javaWriter.getField("zsynthetic/IntRange", "to", "I");
39
+		javaWriter.getField(owner, "to", "I");
38 40
 		javaWriter.swap();
39
-		javaWriter.getField("zsynthetic/IntRange", "from", "I");
41
+		javaWriter.getField(owner, "from", "I");
40 42
 
41 43
 		final int z = javaWriter.getLocalVariable(variables[0].variable).local;
42 44
 		javaWriter.storeInt(z);

+ 3
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java Переглянути файл

@@ -3,6 +3,7 @@ package org.openzen.zenscript.javabytecode.compiler;
3 3
 import org.objectweb.asm.Label;
4 4
 import org.objectweb.asm.Type;
5 5
 import org.openzen.zenscript.codemodel.statement.*;
6
+import org.openzen.zenscript.codemodel.type.RangeTypeID;
6 7
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
7 8
 
8 9
 import java.util.Arrays;
@@ -14,7 +15,7 @@ import org.openzen.zenscript.javashared.JavaCompiledModule;
14 15
 
15 16
 public class JavaStatementVisitor implements StatementVisitor<Boolean> {
16 17
     private final JavaWriter javaWriter;
17
-	private final JavaBytecodeContext context;
18
+	final JavaBytecodeContext context;
18 19
     public JavaExpressionVisitor expressionVisitor;
19 20
 	public JavaNonPushingExpressionVisitor nonPushingExpressionVisitor;
20 21
 
@@ -124,7 +125,7 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
124 125
 		} else {
125 126
 			switch (statement.iterator.target.getBuiltin()) {
126 127
 				case ITERATOR_INT_RANGE:
127
-					iteratorWriter.visitIntRange();
128
+					iteratorWriter.visitIntRange(((RangeTypeID) statement.iterator.getOwnerType().type));
128 129
 					break;
129 130
 				case ITERATOR_ARRAY_VALUES:
130 131
 					iteratorWriter.visitArrayValueIterator();

+ 1
- 1
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java Переглянути файл

@@ -265,7 +265,7 @@ public class JavaNativeModule {
265 265
 				className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
266 266
 			} else if (specifiedName.indexOf('.') >= 0) {
267 267
 				if (!specifiedName.startsWith(pkg.fullName))
268
-					throw new IllegalArgumentException("Specified @Name as \"" + specifiedName + "\" for class: \"" + cls.toString() + "\" but it's not in the module root package");
268
+					throw new IllegalArgumentException("Specified @Name as \"" + specifiedName + "\" for class: \"" + cls.toString() + "\" but it's not in the module root package: \"" + pkg.fullName + "\"");
269 269
 
270 270
 				classPkg = getPackage(basePackage + specifiedName.substring(pkg.fullName.length()));
271 271
 				className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);

+ 19
- 4
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaContext.java Переглянути файл

@@ -221,7 +221,11 @@ public abstract class JavaContext {
221 221
 	}
222 222
 	
223 223
 	public String getMethodDescriptor(FunctionHeader header) {
224
-		return getMethodDescriptor(header, false);
224
+		return getMethodDescriptor(header, false, "");
225
+	}
226
+
227
+	public String getMethodDescriptorExpansion(FunctionHeader header, StoredType expandedType) {
228
+		return getMethodDescriptor(header, false, getDescriptor(expandedType));
225 229
 	}
226 230
 	
227 231
     public String getMethodSignature(FunctionHeader header) {
@@ -229,7 +233,7 @@ public abstract class JavaContext {
229 233
     }
230 234
 	
231 235
 	public String getEnumConstructorDescriptor(FunctionHeader header) {
232
-		return getMethodDescriptor(header, true);
236
+		return getMethodDescriptor(header, true, "");
233 237
 	}
234 238
 	
235 239
 	public JavaSynthesizedFunctionInstance getFunction(FunctionTypeID type) {
@@ -339,14 +343,25 @@ public abstract class JavaContext {
339 343
 			return new JavaSynthesizedClass(range.cls, new TypeID[] { type.baseType.type });
340 344
 		}
341 345
 	}
342
-	
343
-	private String getMethodDescriptor(FunctionHeader header, boolean isEnumConstructor) {
346
+
347
+	/**
348
+	 * @param header Function Header
349
+	 * @param isEnumConstructor If this is an enum constructor, add String, int as parameters
350
+	 * @param expandedType If this is for an expanded type, add the type at the beginning.
351
+	 *                        Can be null or an empty string if this is not an expansion method header
352
+	 * @return Method descriptor {@code (<LClass;*No.TypeParameters><LString;I if enum><expandedType><headerTypes>)<retType> }
353
+	 */
354
+	private String getMethodDescriptor(FunctionHeader header, boolean isEnumConstructor, String expandedType) {
344 355
         StringBuilder descBuilder = new StringBuilder("(");
345 356
 		for (int i = 0; i < header.getNumberOfTypeParameters(); i++)
346 357
 			descBuilder.append("Ljava/lang/Class;");
347 358
 		
348 359
         if (isEnumConstructor)
349 360
             descBuilder.append("Ljava/lang/String;I");
361
+
362
+        //TODO: Put this earlier? We'd need to agree on one...
363
+        if(expandedType != null)
364
+        	descBuilder.append(expandedType);
350 365
 		
351 366
         for (FunctionParameter parameter : header.parameters) {
352 367
 			descBuilder.append(getDescriptor(parameter.type));

+ 13
- 4
JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareExpansionMethodVisitor.java Переглянути файл

@@ -5,6 +5,7 @@
5 5
  */
6 6
 package org.openzen.zenscript.javashared.prepare;
7 7
 
8
+import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
8 9
 import org.openzen.zenscript.javashared.JavaNativeClass;
9 10
 import org.openzen.zencode.shared.StringExpansion;
10 11
 import org.openzen.zenscript.codemodel.FunctionHeader;
@@ -166,16 +167,24 @@ public class JavaPrepareExpansionMethodVisitor implements MemberVisitor<Void> {
166 167
 		JavaMethod method = null;
167 168
 		if (nativeTag != null && nativeClass != null)
168 169
 			method = nativeClass.getMethod(nativeTag.value);
169
-		if (method == null)
170
+		if (method == null) {
171
+			final JavaMethod.Kind kind = getKind(member);
172
+			final String descriptor;
173
+			if (kind == JavaMethod.Kind.EXPANSION && member.definition instanceof ExpansionDefinition) {
174
+				descriptor = context.getMethodDescriptorExpansion(header, ((ExpansionDefinition) member.definition).target);
175
+			} else {
176
+				descriptor = context.getMethodDescriptor(header);
177
+			}
170 178
 			method = new JavaMethod(
171 179
 					cls,
172
-					getKind(member),
180
+					kind,
173 181
 					name,
174 182
 					true,
175
-					context.getMethodDescriptor(header),
183
+					descriptor,
176 184
 					JavaModifiers.getJavaModifiers(member.getEffectiveModifiers()),
177 185
 					header.getReturnType().type instanceof GenericTypeID,
178
-					header.useTypeParameters()); 
186
+					header.useTypeParameters());
187
+		}
179 188
 		
180 189
 		if (method.compile) {
181 190
 			if (DEBUG_EMPTY && cls.empty)

Завантаження…
Відмінити
Зберегти