Quellcode durchsuchen

- Fixed crash when classes doesn't exist + don't output debug classes if not in debug mode

- Fixed constructor support for native classes
- Added project for maven build
- Added missing StdLibs.zip
Stan Hebben vor 6 Jahren
Ursprung
Commit
2f7f3f415d

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/InvalidExpression.java Datei anzeigen

@@ -27,7 +27,7 @@ public class InvalidExpression extends Expression {
27 27
 	}
28 28
 	
29 29
 	public InvalidExpression(StoredType type, CompileException cause) {
30
-		this(cause.position, type, cause.code, cause.getMessage());
30
+		this(cause.position, type, cause.code, cause.message);
31 31
 	}
32 32
 
33 33
 	@Override

+ 3
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeRunUnit.java Datei anzeigen

@@ -56,6 +56,9 @@ public class JavaBytecodeRunUnit {
56 56
 	public void dump(File directory) {
57 57
 		writeScripts();
58 58
 		
59
+		if (!directory.exists())
60
+			directory.mkdirs();
61
+		
59 62
 		for (Map.Entry<String, byte[]> classEntry : classes.entrySet()) {
60 63
 			File output = new File(directory, classEntry.getKey() + ".class");
61 64
 			try (FileOutputStream outputStream = new FileOutputStream(output)) {

+ 0
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java Datei anzeigen

@@ -109,7 +109,6 @@ public class JavaCompiler {
109 109
 			return "generatedBlock" + (generatedScriptBlockCounter++);
110 110
 		} else {
111 111
 			// TODO: remove special characters
112
-			System.out.println("Writing script: " + filename);
113 112
 			return filename.substring(0, filename.lastIndexOf('.')).replace("/", "_");
114 113
 		}
115 114
 	}

+ 49
- 14
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java Datei anzeigen

@@ -262,14 +262,16 @@ public class JavaNativeModule {
262 262
 			compiled.setFieldInfo(member, new JavaField(javaClass, field.getName(), getDescriptor(field.getType())));
263 263
 		}
264 264
 		
265
-		for (Method method : cls.getDeclaredMethods()) {
266
-			ZenCodeType.Constructor constructorAnnotation = method.getAnnotation(ZenCodeType.Constructor.class);
267
-			if (constructorAnnotation != null || (!annotated && method.getName().equals("<init>"))) {
268
-				ConstructorMember member = asConstructor(definition, method);
265
+		for (java.lang.reflect.Constructor constructor : cls.getConstructors()) {
266
+			ZenCodeType.Constructor constructorAnnotation = (ZenCodeType.Constructor)constructor.getAnnotation(ZenCodeType.Constructor.class);
267
+			if (constructorAnnotation != null || !annotated) {
268
+				ConstructorMember member = asConstructor(definition, constructor);
269 269
 				definition.addMember(member);
270
-				compiled.setMethodInfo(member, getMethod(javaClass, method, BasicTypeID.VOID.stored));
270
+				compiled.setMethodInfo(member, getMethod(javaClass, constructor));
271 271
 			}
272
-			
272
+		}
273
+		
274
+		for (Method method : cls.getDeclaredMethods()) {
273 275
 			ZenCodeType.Method methodAnnotation = method.getAnnotation(ZenCodeType.Method.class);
274 276
 			if (methodAnnotation != null) {
275 277
 				MethodMember member = asMethod(definition, method, methodAnnotation);
@@ -338,12 +340,12 @@ public class JavaNativeModule {
338 340
 		return name;
339 341
 	}
340 342
 	
341
-	private ConstructorMember asConstructor(HighLevelDefinition definition, Method method) {
343
+	private ConstructorMember asConstructor(HighLevelDefinition definition, java.lang.reflect.Constructor method) {
342 344
 		FunctionHeader header = getHeader(method);
343 345
 		return new ConstructorMember(
344 346
 				CodePosition.NATIVE,
345 347
 				definition,
346
-				getMethodModifiers(method),
348
+				Modifiers.PUBLIC,
347 349
 				header,
348 350
 				null);
349 351
 	}
@@ -412,16 +414,35 @@ public class JavaNativeModule {
412 414
 		return new CasterMember(CodePosition.NATIVE, definition, modifiers, toType, null);
413 415
 	}
414 416
 	
417
+	private FunctionHeader getHeader(java.lang.reflect.Constructor constructor) {
418
+		return getHeader(
419
+				null,
420
+				constructor.getAnnotatedParameterTypes(),
421
+				constructor.getTypeParameters(),
422
+				constructor.getAnnotatedExceptionTypes());
423
+	}
424
+	
415 425
 	private FunctionHeader getHeader(Method method) {
416
-		StoredType returnType = loadStoredType(method.getAnnotatedReturnType());
417
-		
418
-		FunctionParameter[] parameters = new FunctionParameter[method.getParameterCount()];
419
-		AnnotatedType[] parameterTypes = method.getAnnotatedParameterTypes();
426
+		return getHeader(
427
+				method.getAnnotatedReturnType(),
428
+				method.getAnnotatedParameterTypes(),
429
+				method.getTypeParameters(),
430
+				method.getAnnotatedExceptionTypes());
431
+	}
432
+	
433
+	private FunctionHeader getHeader(
434
+			AnnotatedType javaReturnType,
435
+			AnnotatedType[] parameterTypes,
436
+			TypeVariable<Method>[] javaTypeParameters,
437
+			AnnotatedType[] exceptionTypes)
438
+	{
439
+		StoredType returnType = javaReturnType == null ? BasicTypeID.VOID.stored : loadStoredType(javaReturnType);
440
+		
441
+		FunctionParameter[] parameters = new FunctionParameter[parameterTypes.length];
420 442
 		for (int i = 0; i < parameters.length; i++) {
421 443
 			parameters[i] = new FunctionParameter(loadStoredType(parameterTypes[i]));
422 444
 		}
423 445
 		
424
-		TypeVariable<Method>[] javaTypeParameters = method.getTypeParameters();
425 446
 		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
426 447
 		for (int i = 0; i < javaTypeParameters.length; i++) {
427 448
 			TypeVariable<Method> javaTypeParameter = javaTypeParameters[i];
@@ -431,7 +452,6 @@ public class JavaNativeModule {
431 452
 				typeParameters[i].addBound(new ParameterTypeBound(CodePosition.NATIVE, loadType(bound)));
432 453
 		}
433 454
 		
434
-		AnnotatedType[] exceptionTypes = method.getAnnotatedExceptionTypes();
435 455
 		if (exceptionTypes.length > 1)
436 456
 			throw new IllegalArgumentException("A method can only throw a single exception type!");
437 457
 		
@@ -518,10 +538,25 @@ public class JavaNativeModule {
518 538
 		return org.objectweb.asm.Type.getDescriptor(cls);
519 539
 	}
520 540
 	
541
+	private static String getMethodDescriptor(java.lang.reflect.Constructor constructor) {
542
+		return org.objectweb.asm.Type.getConstructorDescriptor(constructor);
543
+	}
544
+	
521 545
 	private static String getMethodDescriptor(Method method) {
522 546
 		return org.objectweb.asm.Type.getMethodDescriptor(method);
523 547
 	}
524 548
 	
549
+	private static JavaMethod getMethod(JavaClass cls, java.lang.reflect.Constructor constructor) {
550
+		return new JavaMethod(
551
+				cls,
552
+				JavaMethod.Kind.CONSTRUCTOR,
553
+				"<init>",
554
+				false,
555
+				getMethodDescriptor(constructor),
556
+				constructor.getModifiers(),
557
+				false);
558
+	}
559
+	
525 560
 	private static JavaMethod getMethod(JavaClass cls, Method method, StoredType result) {
526 561
 		JavaMethod.Kind kind;
527 562
 		if (method.getName().equals("<init>"))

+ 4
- 1
JavaIntegration/src/main/java/org/openzen/zencode/java/ScriptingEngine.java Datei anzeigen

@@ -42,6 +42,8 @@ public class ScriptingEngine {
42 42
 	private final List<JavaNativeModule> nativeModules = new ArrayList<>();
43 43
 	private final List<SemanticModule> compiledModules = new ArrayList<>();
44 44
 	
45
+	public boolean debug = false;
46
+	
45 47
 	public ScriptingEngine() {
46 48
 		space = new ModuleSpace(registry, new ArrayList<>(), StorageType.getStandard());
47 49
 		
@@ -124,7 +126,8 @@ public class ScriptingEngine {
124 126
 		JavaBytecodeRunUnit runUnit = new JavaBytecodeRunUnit();
125 127
 		for (SemanticModule compiled : compiledModules)
126 128
 			runUnit.add(compiler.compile(compiled.name, compiled, javaSpace));
127
-		runUnit.dump(new File("classes"));
129
+		if (debug)
130
+			runUnit.dump(new File("classes"));
128 131
 		runUnit.run();
129 132
 	}
130 133
 }

BIN
JavaIntegration/src/main/resources/StdLibs.zip Datei anzeigen


+ 20
- 0
JavaScripting/build.gradle Datei anzeigen

@@ -0,0 +1,20 @@
1
+
2
+repositories {
3
+    mavenCentral()
4
+}
5
+
6
+dependencies {
7
+	compile 'org.ow2.asm:asm-debug-all:6.0_BETA'
8
+}
9
+
10
+def javaScriptingProjects = [':JavaIntegration',':JavaAnnotations',':JavaBytecodeCompiler', ':JavaShared', ':Validator', ':Parser', ':CodeModel', ':Shared']
11
+jar {
12
+	dependsOn javaScriptingProjects.collect{ it+":compileJava"}
13
+    from files(javaScriptingProjects.collect{ project(it).sourceSets.main.output })
14
+}
15
+
16
+install {
17
+	repositories.mavenInstaller {
18
+		pom.artifactId = 'zencode-javascripting'
19
+	}
20
+}

+ 2
- 0
JavaScripting/configuration.gradle Datei anzeigen

@@ -0,0 +1,2 @@
1
+group='org.openzen.zencode'
2
+version='0.1.0'

+ 5
- 0
ScriptingExample/scripts/integration.zs Datei anzeigen

@@ -0,0 +1,5 @@
1
+import example.TestClass;
2
+
3
+val instance = new TestClass("Instance");
4
+println("Name: " + instance.name);
5
+instance.dump();

+ 4
- 3
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java Datei anzeigen

@@ -22,9 +22,10 @@ public class Main {
22 22
 	public static void main(String[] args) throws CompileException, ParseException, IOException {
23 23
 		ScriptingEngine scriptingEngine = new ScriptingEngine();
24 24
 		
25
-		JavaNativeModule module = scriptingEngine.createNativeModule("globals", "org.openzen.zenscript.scriptingexample");
26
-		module.addGlobals(Globals.class);
27
-		scriptingEngine.registerNativeProvided(module);
25
+		JavaNativeModule example = scriptingEngine.createNativeModule("example", "org.openzen.zenscript.scriptingexample");
26
+		example.addGlobals(Globals.class);
27
+		example.addClass(TestClass.class);
28
+		scriptingEngine.registerNativeProvided(example);
28 29
 		
29 30
 		File inputDirectory = new File("scripts");
30 31
 		File[] inputFiles = Optional.ofNullable(inputDirectory.listFiles((dir, name) -> name.endsWith(".zs"))).orElseGet(() -> new File[0]);

+ 31
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/TestClass.java Datei anzeigen

@@ -0,0 +1,31 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.scriptingexample;
7
+
8
+import org.openzen.zencode.java.ZenCodeType;
9
+
10
+/**
11
+ *
12
+ * @author Hoofdgebruiker
13
+ */
14
+public class TestClass implements ZenCodeType {
15
+	private final String name;
16
+	
17
+	@Constructor
18
+	public TestClass(String name) {
19
+		this.name = name;
20
+	}
21
+	
22
+	@Getter
23
+	public String getName() {
24
+		return name;
25
+	}
26
+	
27
+	@Method
28
+	public void dump() {
29
+		System.out.println("TestClass " + name);
30
+	}
31
+}

+ 2
- 12
common.gradle Datei anzeigen

@@ -5,22 +5,17 @@
5 5
 apply plugin: 'java'
6 6
 apply plugin: 'maven'
7 7
 
8
-String mavenGroupId = 'org.openzen.zenscript'
9
-String mavenVersion = '1.0-SNAPSHOT'
8
+String mavenGroupId = 'org.openzen.zencode'
9
+String mavenVersion = '0.1.2'
10 10
 
11 11
 sourceCompatibility = '1.8'
12 12
 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
13 13
 
14 14
 repositories {
15 15
     mavenCentral();
16
-    // You may define additional repositories, or even remove "mavenCentral()".
17
-    // Read more about repositories here:
18
-    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
19 16
 }
20 17
 
21 18
 dependencies {
22
-    // Adding dependencies here will add the dependencies to each subproject.
23
-    testCompile group: 'junit', name: 'junit', version: '4.10'
24 19
 	compile 'net.sf.trove4j:trove4j:3.0.3'
25 20
 }
26 21
 
@@ -34,11 +29,6 @@ task sourcesJar(type: Jar, dependsOn: classes, description: 'Creates a jar from
34 29
     from sourceSets.main.allSource
35 30
 }
36 31
 
37
-artifacts {
38
-    archives jar
39
-    archives sourcesJar
40
-}
41
-
42 32
 configure(install.repositories.mavenInstaller) {
43 33
     pom.project {
44 34
         groupId = mavenGroupId

Laden…
Abbrechen
Speichern