Sfoglia il codice sorgente

Fixed function definitions not generating properly

kindlich 4 anni fa
parent
commit
eb48f35453
Non sono state trovate chiavi note per questa firma nel database

+ 36
- 34
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java Vedi File

@@ -5,32 +5,19 @@
5 5
  */
6 6
 package org.openzen.zenscript.javabytecode;
7 7
 
8
-import java.util.*;
9
-
10
-import org.objectweb.asm.ClassWriter;
11
-import org.objectweb.asm.Opcodes;
12
-import org.openzen.zencode.shared.CodePosition;
13
-import org.openzen.zencode.shared.SourceFile;
8
+import org.objectweb.asm.*;
9
+import org.openzen.zencode.shared.*;
14 10
 import org.openzen.zencode.shared.logging.*;
15
-import org.openzen.zenscript.codemodel.FunctionHeader;
16
-import org.openzen.zenscript.codemodel.FunctionParameter;
17
-import org.openzen.zenscript.codemodel.HighLevelDefinition;
18
-import org.openzen.zenscript.codemodel.ScriptBlock;
19
-import org.openzen.zenscript.codemodel.SemanticModule;
20
-import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
21
-import org.openzen.zenscript.codemodel.statement.Statement;
22
-import org.openzen.zenscript.codemodel.type.BasicTypeID;
23
-import org.openzen.zenscript.javabytecode.compiler.JavaClassWriter;
24
-import org.openzen.zenscript.javabytecode.compiler.JavaScriptFile;
25
-import org.openzen.zenscript.javabytecode.compiler.JavaStatementVisitor;
26
-import org.openzen.zenscript.javabytecode.compiler.JavaWriter;
27
-import org.openzen.zenscript.javabytecode.compiler.definitions.JavaDefinitionVisitor;
28
-import org.openzen.zenscript.javashared.JavaClass;
29
-import org.openzen.zenscript.javashared.JavaCompileSpace;
30
-import org.openzen.zenscript.javashared.JavaMethod;
31
-import org.openzen.zenscript.javashared.JavaParameterInfo;
32
-import org.openzen.zenscript.javashared.prepare.JavaPrepareDefinitionMemberVisitor;
33
-import org.openzen.zenscript.javashared.prepare.JavaPrepareDefinitionVisitor;
11
+import org.openzen.zenscript.codemodel.*;
12
+import org.openzen.zenscript.codemodel.definition.*;
13
+import org.openzen.zenscript.codemodel.statement.*;
14
+import org.openzen.zenscript.codemodel.type.*;
15
+import org.openzen.zenscript.javabytecode.compiler.*;
16
+import org.openzen.zenscript.javabytecode.compiler.definitions.*;
17
+import org.openzen.zenscript.javashared.*;
18
+import org.openzen.zenscript.javashared.prepare.*;
19
+
20
+import java.util.*;
34 21
 
35 22
 /**
36 23
  * @author Hoofdgebruiker
@@ -55,22 +42,37 @@ public class JavaCompiler {
55 42
 		
56 43
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
57 44
 			final String className = getClassName(getFilename(definition));
58
-			String filename = className + "_" + (definition.name == null ? "generated" : definition.name) + "_" + expansionCounter++;
59
-			JavaPrepareDefinitionVisitor definitionPreparer = new JavaPrepareDefinitionVisitor(context, target, filename, null, filename);
60
-			definition.accept(definitionPreparer);
45
+			final String filename;
46
+            if(definition instanceof FunctionDefinition) {
47
+                filename = className;
48
+            } else {
49
+                filename = className + "_" + (definition.name == null ? "generated" : definition.name) + "_" + expansionCounter++;
50
+            }
51
+            JavaPrepareDefinitionVisitor definitionPreparer = new JavaPrepareDefinitionVisitor(context, target, filename, null, filename);
52
+            definition.accept(definitionPreparer);
61 53
 		}
62 54
 		
63 55
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
64 56
 			JavaPrepareDefinitionMemberVisitor memberPreparer = new JavaPrepareDefinitionMemberVisitor(context, target);
65 57
 			definition.accept(memberPreparer);
66 58
 		}
67
-		
68
-		for (HighLevelDefinition definition : module.definitions.getAll()) {
69
-            JavaClass cls = definition instanceof ExpansionDefinition ? context.getJavaExpansionClass(definition) : context.getJavaClass(definition);
70
-            JavaScriptFile scriptFile = getScriptFile(scriptBlocks, cls.fullName);
59
+        
60
+        for(HighLevelDefinition definition : module.definitions.getAll()) {
61
+            final String internalName;
62
+            final JavaScriptFile scriptFile;
63
+            if(definition instanceof FunctionDefinition) {
64
+                internalName = getClassName(getFilename(definition));
65
+                scriptFile = getScriptFile(scriptBlocks, module.modulePackage.fullName + "/" + internalName);
66
+                scriptFilesThatAreActuallyUsedInScripts.add(scriptFile);
67
+            } else {
68
+                JavaClass cls = definition instanceof ExpansionDefinition ? context.getJavaExpansionClass(definition) : context
69
+                        .getJavaClass(definition);
70
+                scriptFile = getScriptFile(scriptBlocks, cls.fullName);
71
+                internalName = cls.internalName;
72
+            }
71 73
             scriptFile.classWriter.visitSource(definition.position.getFilename(), null);
72
-			target.addClass(cls.internalName, definition.accept(new JavaDefinitionVisitor(context, scriptFile.classWriter)));
73
-		}
74
+            target.addClass(internalName, definition.accept(new JavaDefinitionVisitor(context, scriptFile.classWriter)));
75
+        }
74 76
 		
75 77
 		FunctionHeader scriptHeader = new FunctionHeader(BasicTypeID.VOID, module.parameters);
76 78
 		String scriptDescriptor = context.getMethodDescriptor(scriptHeader);

+ 35
- 0
ScriptingExample/src/test/java/org/openzen/zenscript/scriptingexample/tests/actual_test/functions/Function.java Vedi File

@@ -0,0 +1,35 @@
1
+package org.openzen.zenscript.scriptingexample.tests.actual_test.functions;
2
+
3
+import org.junit.jupiter.api.*;
4
+import org.openzen.zenscript.scriptingexample.tests.helpers.*;
5
+
6
+public class Function extends ZenCodeTest {
7
+    
8
+    @Test
9
+    public void testCallSameFile() {
10
+        ScriptBuilder.create()
11
+                .add("public function addSomeRecipe() as void {")
12
+                .add("    println('Hello World');")
13
+                .add("}")
14
+                .add("")
15
+                .add("addSomeRecipe();")
16
+                .execute(this);
17
+        
18
+        logger.assertPrintOutputSize(1);
19
+        logger.assertPrintOutput(0, "Hello World");
20
+    }
21
+    
22
+    @Test
23
+    public void testCallDifferentFile() {
24
+        ScriptBuilder.create()
25
+                .add("public function addSomeRecipe() as void {")
26
+                .add("    println('Hello World');")
27
+                .add("}")
28
+                .startNewScript()
29
+                .add("addSomeRecipe();")
30
+                .execute(this);
31
+        
32
+        logger.assertPrintOutputSize(1);
33
+        logger.assertPrintOutput(0, "Hello World");
34
+    }
35
+}

Loading…
Annulla
Salva