Browse Source

Fix expansion method signature and call

Signature: Include the type in the signature if it is an expansion method
Call: Call invokeStatic if its an expansion method
kindlich 5 years ago
parent
commit
d2fdb13730
No known key found for this signature in database

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

4220
 			getJavaWriter().invokeStatic(methodInfo);
4220
 			getJavaWriter().invokeStatic(methodInfo);
4221
 		} else if (methodInfo.kind == JavaMethod.Kind.INTERFACE) {
4221
 		} else if (methodInfo.kind == JavaMethod.Kind.INTERFACE) {
4222
 			getJavaWriter().invokeInterface(methodInfo);
4222
 			getJavaWriter().invokeInterface(methodInfo);
4223
+		} else if (methodInfo.kind == JavaMethod.Kind.EXPANSION) {
4224
+			getJavaWriter().invokeStatic(methodInfo);
4223
 		} else if (methodInfo.kind == JavaMethod.Kind.COMPILED) {
4225
 		} else if (methodInfo.kind == JavaMethod.Kind.COMPILED) {
4224
 			Objects.requireNonNull(methodInfo.translation).translate(expression, this);
4226
 			Objects.requireNonNull(methodInfo.translation).translate(expression, this);
4225
-		} else if (methodInfo.cls.kind == JavaClass.Kind.INTERFACE) {
4227
+		} else if (methodInfo.cls != null && methodInfo.cls.kind == JavaClass.Kind.INTERFACE) {
4226
 			getJavaWriter().invokeInterface(methodInfo);
4228
 			getJavaWriter().invokeInterface(methodInfo);
4227
 		} else {
4229
 		} else {
4228
 			getJavaWriter().invokeVirtual(methodInfo);
4230
 			getJavaWriter().invokeVirtual(methodInfo);

+ 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