Browse Source

fix optionals not inheriting nullable and expecting a default type

Jared 5 years ago
parent
commit
2513cf66ce
No account linked to committer's email address

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

5
  */
5
  */
6
 package org.openzen.zencode.java;
6
 package org.openzen.zencode.java;
7
 
7
 
8
-import java.lang.reflect.AnnotatedType;
9
-import java.lang.reflect.Field;
10
-import java.lang.reflect.Method;
11
-import java.lang.reflect.Modifier;
12
-import java.lang.reflect.Parameter;
13
-import java.lang.reflect.ParameterizedType;
14
-import java.lang.reflect.Type;
15
-import java.lang.reflect.TypeVariable;
16
-import java.util.Collections;
17
-import java.util.HashMap;
18
-import java.util.Map;
19
 import org.openzen.zencode.shared.CodePosition;
8
 import org.openzen.zencode.shared.CodePosition;
20
 import org.openzen.zenscript.codemodel.FunctionHeader;
9
 import org.openzen.zenscript.codemodel.FunctionHeader;
21
 import org.openzen.zenscript.codemodel.FunctionParameter;
10
 import org.openzen.zenscript.codemodel.FunctionParameter;
79
 import org.openzen.zenscript.javashared.JavaModifiers;
68
 import org.openzen.zenscript.javashared.JavaModifiers;
80
 import stdlib.Strings;
69
 import stdlib.Strings;
81
 
70
 
71
+import java.lang.reflect.AnnotatedType;
72
+import java.lang.reflect.Field;
73
+import java.lang.reflect.Method;
74
+import java.lang.reflect.Modifier;
75
+import java.lang.reflect.Parameter;
76
+import java.lang.reflect.ParameterizedType;
77
+import java.lang.reflect.Type;
78
+import java.lang.reflect.TypeVariable;
79
+import java.util.Collections;
80
+import java.util.HashMap;
81
+import java.util.Map;
82
+
82
 /**
83
 /**
83
  * @author Stan Hebben
84
  * @author Stan Hebben
84
  */
85
  */
532
 		if (parameter.isAnnotationPresent(ZenCodeType.Optional.class)) {
533
 		if (parameter.isAnnotationPresent(ZenCodeType.Optional.class)) {
533
 			Expression defaultValue = type.type.getDefaultValue();
534
 			Expression defaultValue = type.type.getDefaultValue();
534
 			if (defaultValue == null)
535
 			if (defaultValue == null)
535
-				throw new IllegalArgumentException(type.toString() + " doesn't have a default value");
536
+			    defaultValue = new org.openzen.zenscript.codemodel.expression.NullExpression(CodePosition.NATIVE);
536
 			return defaultValue;
537
 			return defaultValue;
537
 		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalInt.class)) {
538
 		} else if (parameter.isAnnotationPresent(ZenCodeType.OptionalInt.class)) {
538
 			ZenCodeType.OptionalInt annotation = parameter.getAnnotation(ZenCodeType.OptionalInt.class);
539
 			ZenCodeType.OptionalInt annotation = parameter.getAnnotation(ZenCodeType.OptionalInt.class);
630
 		else if (annotatedType.isAnnotationPresent(ZenCodeType.NullableUSize.class))
631
 		else if (annotatedType.isAnnotationPresent(ZenCodeType.NullableUSize.class))
631
 			return registry.getOptional(BasicTypeID.USIZE).stored();
632
 			return registry.getOptional(BasicTypeID.USIZE).stored();
632
 		
633
 		
633
-		boolean nullable = annotatedType.isAnnotationPresent(ZenCodeType.Nullable.class);
634
+		boolean nullable = annotatedType.isAnnotationPresent(ZenCodeType.Nullable.class) || annotatedType.isAnnotationPresent(ZenCodeType.Optional.class);
634
 		boolean unsigned = annotatedType.isAnnotationPresent(ZenCodeType.Unsigned.class);
635
 		boolean unsigned = annotatedType.isAnnotationPresent(ZenCodeType.Unsigned.class);
635
 		
636
 		
636
 		Type type = annotatedType.getType();
637
 		Type type = annotatedType.getType();

+ 1
- 1
Validator/src/main/java/org/openzen/zenscript/validator/visitors/ExpressionValidator.java View File

776
 			}
776
 			}
777
 			
777
 			
778
 			FunctionParameter parameter = instancedHeader.getParameter(isVariadic, i);
778
 			FunctionParameter parameter = instancedHeader.getParameter(isVariadic, i);
779
-			if (!parameter.type.equals(argument.type)) {
779
+			if (!parameter.type.equals(argument.type) && !parameter.defaultValue.type.equals(argument.type)) {
780
 				validator.logError(
780
 				validator.logError(
781
 						ValidationLogEntry.Code.INVALID_CALL_ARGUMENT,
781
 						ValidationLogEntry.Code.INVALID_CALL_ARGUMENT,
782
 						position,
782
 						position,

Loading…
Cancel
Save