5 Commits

Author SHA1 Message Date
  Jared c7466e5c37
Log more info about what package is trying to be registered into 5 years ago
  kindlich d4a722cd92
Fixed this call in primitive expansions 5 years ago
  kindlich 6ada4ffce1
Replaced hardcoded IntRange class name 5 years ago
  kindlich d2fdb13730
Fix expansion method signature and call 5 years ago
  kindlich 341f8c23f5
Jdk classes no longer 'created', stopped 2nd registration of empty classes 5 years ago

+ 6
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeModule.java View File

29
 	public void addClass(String name, byte[] bytecode) {
29
 	public void addClass(String name, byte[] bytecode) {
30
 		if (bytecode == null)
30
 		if (bytecode == null)
31
 			return;
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
 	public void addScript(JavaScriptMethod method) {
40
 	public void addScript(JavaScriptMethod method) {

+ 7
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java View File

103
 				target.addScript(method);
103
 				target.addScript(method);
104
 
104
 
105
 			entry.getValue().classWriter.visitEnd();
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
 		return target;
115
 		return target;

+ 22
- 8
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java View File

884
 				} else {
884
 				} else {
885
 					argument.accept(this);
885
 					argument.accept(this);
886
 					javaWriter.dup();
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
 					javaWriter.storeInt(tmp);
894
 					javaWriter.storeInt(tmp);
889
-					javaWriter.getField("zsynthetic/IntRange", "from", "I");
895
+					javaWriter.getField(owner, "from", "I");
890
 					javaWriter.loadInt(tmp);
896
 					javaWriter.loadInt(tmp);
891
-					javaWriter.getField("zsynthetic/IntRange", "to", "I");
897
+					javaWriter.getField(owner, "to", "I");
892
 				}
898
 				}
893
 				javaWriter.invokeVirtual(STRING_SUBSTRING);
899
 				javaWriter.invokeVirtual(STRING_SUBSTRING);
894
 				break;
900
 				break;
1007
 				} else {
1013
 				} else {
1008
 					argument.accept(this);
1014
 					argument.accept(this);
1009
 					javaWriter.dup();
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
 					javaWriter.storeInt(tmp);
1023
 					javaWriter.storeInt(tmp);
1012
-					javaWriter.getField("zsynthetic/IntRange", "from", "I");
1024
+					javaWriter.getField(owner, "from", "I");
1013
 					javaWriter.loadInt(tmp);
1025
 					javaWriter.loadInt(tmp);
1014
-					javaWriter.getField("zsynthetic/IntRange", "to", "I");
1026
+					javaWriter.getField(owner, "to", "I");
1015
 				}
1027
 				}
1016
 
1028
 
1017
 				if (type.elementType.type instanceof BasicTypeID) {
1029
 				if (type.elementType.type instanceof BasicTypeID) {
4126
 
4138
 
4127
 	@Override
4139
 	@Override
4128
 	public Void visitThis(ThisExpression expression) {
4140
 	public Void visitThis(ThisExpression expression) {
4129
-		javaWriter.loadObject(0);
4141
+		javaWriter.load(context.getType(expression.type), 0);
4130
 		return null;
4142
 		return null;
4131
 	}
4143
 	}
4132
 
4144
 
4220
 			getJavaWriter().invokeStatic(methodInfo);
4232
 			getJavaWriter().invokeStatic(methodInfo);
4221
 		} else if (methodInfo.kind == JavaMethod.Kind.INTERFACE) {
4233
 		} else if (methodInfo.kind == JavaMethod.Kind.INTERFACE) {
4222
 			getJavaWriter().invokeInterface(methodInfo);
4234
 			getJavaWriter().invokeInterface(methodInfo);
4235
+		} else if (methodInfo.kind == JavaMethod.Kind.EXPANSION) {
4236
+			getJavaWriter().invokeStatic(methodInfo);
4223
 		} else if (methodInfo.kind == JavaMethod.Kind.COMPILED) {
4237
 		} else if (methodInfo.kind == JavaMethod.Kind.COMPILED) {
4224
 			Objects.requireNonNull(methodInfo.translation).translate(expression, this);
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
 			getJavaWriter().invokeInterface(methodInfo);
4240
 			getJavaWriter().invokeInterface(methodInfo);
4227
 		} else {
4241
 		} else {
4228
 			getJavaWriter().invokeVirtual(methodInfo);
4242
 			getJavaWriter().invokeVirtual(methodInfo);

+ 5
- 3
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaForeachWriter.java View File

4
 import org.objectweb.asm.Type;
4
 import org.objectweb.asm.Type;
5
 import org.openzen.zenscript.codemodel.statement.Statement;
5
 import org.openzen.zenscript.codemodel.statement.Statement;
6
 import org.openzen.zenscript.codemodel.statement.VarStatement;
6
 import org.openzen.zenscript.codemodel.statement.VarStatement;
7
+import org.openzen.zenscript.codemodel.type.RangeTypeID;
7
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
8
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
8
 import org.openzen.zenscript.javashared.JavaClass;
9
 import org.openzen.zenscript.javashared.JavaClass;
9
 import org.openzen.zenscript.javashared.JavaMethod;
10
 import org.openzen.zenscript.javashared.JavaMethod;
32
         this.unboxingTypeVisitor = new JavaUnboxingTypeVisitor(this.javaWriter);
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
 		javaWriter.dup();
38
 		javaWriter.dup();
37
-		javaWriter.getField("zsynthetic/IntRange", "to", "I");
39
+		javaWriter.getField(owner, "to", "I");
38
 		javaWriter.swap();
40
 		javaWriter.swap();
39
-		javaWriter.getField("zsynthetic/IntRange", "from", "I");
41
+		javaWriter.getField(owner, "from", "I");
40
 
42
 
41
 		final int z = javaWriter.getLocalVariable(variables[0].variable).local;
43
 		final int z = javaWriter.getLocalVariable(variables[0].variable).local;
42
 		javaWriter.storeInt(z);
44
 		javaWriter.storeInt(z);

+ 3
- 2
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java View File

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

+ 1
- 1
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

265
 				className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
265
 				className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
266
 			} else if (specifiedName.indexOf('.') >= 0) {
266
 			} else if (specifiedName.indexOf('.') >= 0) {
267
 				if (!specifiedName.startsWith(pkg.fullName))
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
 				classPkg = getPackage(basePackage + specifiedName.substring(pkg.fullName.length()));
270
 				classPkg = getPackage(basePackage + specifiedName.substring(pkg.fullName.length()));
271
 				className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);
271
 				className = specifiedName.substring(specifiedName.lastIndexOf('.') + 1);

+ 19
- 4
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaContext.java View File

221
 	}
221
 	}
222
 	
222
 	
223
 	public String getMethodDescriptor(FunctionHeader header) {
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
     public String getMethodSignature(FunctionHeader header) {
231
     public String getMethodSignature(FunctionHeader header) {
229
     }
233
     }
230
 	
234
 	
231
 	public String getEnumConstructorDescriptor(FunctionHeader header) {
235
 	public String getEnumConstructorDescriptor(FunctionHeader header) {
232
-		return getMethodDescriptor(header, true);
236
+		return getMethodDescriptor(header, true, "");
233
 	}
237
 	}
234
 	
238
 	
235
 	public JavaSynthesizedFunctionInstance getFunction(FunctionTypeID type) {
239
 	public JavaSynthesizedFunctionInstance getFunction(FunctionTypeID type) {
339
 			return new JavaSynthesizedClass(range.cls, new TypeID[] { type.baseType.type });
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
         StringBuilder descBuilder = new StringBuilder("(");
355
         StringBuilder descBuilder = new StringBuilder("(");
345
 		for (int i = 0; i < header.getNumberOfTypeParameters(); i++)
356
 		for (int i = 0; i < header.getNumberOfTypeParameters(); i++)
346
 			descBuilder.append("Ljava/lang/Class;");
357
 			descBuilder.append("Ljava/lang/Class;");
347
 		
358
 		
348
         if (isEnumConstructor)
359
         if (isEnumConstructor)
349
             descBuilder.append("Ljava/lang/String;I");
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
         for (FunctionParameter parameter : header.parameters) {
366
         for (FunctionParameter parameter : header.parameters) {
352
 			descBuilder.append(getDescriptor(parameter.type));
367
 			descBuilder.append(getDescriptor(parameter.type));

+ 13
- 4
JavaShared/src/main/java/org/openzen/zenscript/javashared/prepare/JavaPrepareExpansionMethodVisitor.java View File

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

Loading…
Cancel
Save