Browse Source

Fixed List Foreach

Changes how Classes from Stdlibs are found in the NativeModule so that their Java type parameters properly match the ones from the Stdlibs files
kindlich 4 years ago
parent
commit
b30bfeb61d
No known key found for this signature in database

+ 3
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java View File

@@ -123,7 +123,9 @@ public final class TypeMembers {
123 123
 	
124 124
 	public void copyMembersTo(TypeMembers other, TypeMemberPriority priority) {
125 125
 		other.casters.addAll(casters);
126
-		other.iterators.addAll(iterators);
126
+        for(TypeMember<IteratorMemberRef> iterator : iterators) {
127
+            other.addIterator(iterator.member, priority);
128
+        }
127 129
 		
128 130
 		for (Map.Entry<String, EnumConstantMember> entry : enumMembers.entrySet())
129 131
 			other.addEnumMember(entry.getValue(), priority);

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

@@ -85,7 +85,20 @@ public class JavaForeachWriter {
85 85
 	}
86 86
 
87 87
 	public void visitCustomIterator() {
88
-		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
88
+        javaWriter.invokeInterface(JavaMethod.getVirtual(new JavaClass("java.lang", "Iterable", JavaClass.Kind.INTERFACE), "iterator", "()Ljava/util/Iterator;", 0));
89
+        
90
+        javaWriter.label(startLabel);
91
+        javaWriter.dup();
92
+        javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.ITERATOR, "hasNext", "()Z", 0));
93
+        javaWriter.ifEQ(endLabel);
94
+        javaWriter.dup();
95
+        javaWriter.invokeInterface(JavaMethod.getVirtual(JavaClass.ITERATOR, "next", "()Ljava/lang/Object;", 0));
96
+        
97
+        final JavaLocalVariableInfo keyVariable = javaWriter.getLocalVariable(variables[0].variable);
98
+        this.downCast(0, keyVariable.type);
99
+        javaWriter.store(keyVariable.type, keyVariable.local);
100
+        
101
+        content.accept(statementVisitor);
89 102
 	}
90 103
 
91 104
 	public void visitAssocKeyIterator() {

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

@@ -130,16 +130,17 @@ public class JavaNativeModule {
130 130
 	}
131 131
 
132 132
 	public void addGlobals(Class<?> cls) {
133
-	    HighLevelDefinition definition;
134
-	    JavaClass jcls;
135
-	    if(definitionByClass.containsKey(cls)) {
136
-	        definition = definitionByClass.get(cls);
137
-	        jcls = compiled.getClassInfo(definition);
133
+        
134
+        final HighLevelDefinition definition = addClass(cls);
135
+        final JavaClass jcls;
136
+        
137
+        if(compiled.hasClassInfo(definition)) {
138
+            jcls = compiled.getClassInfo(definition);
138 139
         } else {
139
-	        definition = new ClassDefinition(CodePosition.NATIVE, module, pkg, "__globals__", Modifiers.PUBLIC);
140 140
             jcls = JavaClass.fromInternalName(getInternalName(cls), JavaClass.Kind.CLASS);
141 141
             compiled.setClassInfo(definition, jcls);
142 142
         }
143
+
143 144
 		StoredType thisType = registry.getForMyDefinition(definition).stored();
144 145
 		//TypeVariableContext context = new TypeVariableContext();
145 146
 
@@ -273,14 +274,14 @@ public class JavaNativeModule {
273 274
 	}
274 275
 
275 276
 	private <T> HighLevelDefinition convertClass(Class<T> cls) {
276
-		if ((cls.getModifiers() & Modifier.PUBLIC) == 0)
277
-			throw new IllegalArgumentException("Class \" " + cls.getName() + "\" must be public");
278
-
279
-		if(cls.isAnnotationPresent(ZenCodeType.Expansion.class)) {
280
-			return convertExpansion(cls);
281
-		}
282
-
283
-		String className = cls.getName();
277
+        if((cls.getModifiers() & Modifier.PUBLIC) == 0)
278
+            throw new IllegalArgumentException("Class \" " + cls.getName() + "\" must be public");
279
+        
280
+        if(cls.isAnnotationPresent(ZenCodeType.Expansion.class)) {
281
+            return convertExpansion(cls);
282
+        }
283
+        
284
+        String className = cls.getName();
284 285
         boolean isStruct = cls.isAnnotationPresent(ZenCodeType.Struct.class);
285 286
 
286 287
 		HighLevelDefinition definition = checkRegistry(cls);
@@ -339,25 +340,33 @@ public class JavaNativeModule {
339 340
 
340 341
 		//TypeVariableContext context = new TypeVariableContext();
341 342
 		TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
342
-		TypeParameter[] typeParameters = new TypeParameter[cls.getTypeParameters().length];
343
-		definition.typeParameters = typeParameters;
343
+		if(!foundRegistry || definition.typeParameters == null || definition.typeParameters.length != cls.getTypeParameters().length) {
344
+            definition.typeParameters = new TypeParameter[cls.getTypeParameters().length];
345
+        }
344 346
 
345 347
 		for (int i = 0; i < javaTypeParameters.length; i++) {
346 348
 			//Put up here for nested parameters?
347 349
 			TypeVariable<Class<T>> typeVariable = javaTypeParameters[i];
348
-			TypeParameter parameter = new TypeParameter(CodePosition.NATIVE, typeVariable.getName());
349
-			typeParameters[i] = parameter;
350
+			TypeParameter parameter;
351
+			if(foundRegistry && definition.typeParameters.length == cls.getTypeParameters().length) {
352
+                parameter = definition.typeParameters[i];
353
+            } else {
354
+                parameter = new TypeParameter(CodePosition.NATIVE, typeVariable.getName());
355
+            }
356
+            definition.typeParameters[i] = parameter;
350 357
 			context.put(typeVariable, parameter);
351 358
 		}
352 359
 
353 360
 		for (int i = 0; i < javaTypeParameters.length; i++) {
354 361
 			TypeVariable<Class<T>> typeVariable = javaTypeParameters[i];
355
-			TypeParameter parameter = typeParameters[i];
362
+			TypeParameter parameter = definition.typeParameters[i];
356 363
 			for (AnnotatedType bound : typeVariable.getAnnotatedBounds()) {
364
+				if(bound.getType() == Object.class) {
365
+					continue; //Makes the stdlibs types work as they have "no" bounds for T
366
+				}
357 367
 				TypeID type = loadType(context, bound).type;
358 368
 				parameter.addBound(new ParameterTypeBound(CodePosition.NATIVE, type));
359 369
 			}
360
-			typeParameters[i] = parameter;
361 370
 		}
362 371
 
363 372
 		if (!foundRegistry && definition instanceof ClassDefinition && cls.getAnnotatedSuperclass() != null && shouldLoadType(cls.getAnnotatedSuperclass().getType())) {

BIN
ScriptingExample/src/main/resources/StdLibs.jar View File


Loading…
Cancel
Save