Browse Source

- 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 6 years ago
parent
commit
2f7f3f415d

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/InvalidExpression.java View File

27
 	}
27
 	}
28
 	
28
 	
29
 	public InvalidExpression(StoredType type, CompileException cause) {
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
 	@Override
33
 	@Override

+ 3
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaBytecodeRunUnit.java View File

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

+ 0
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaCompiler.java View File

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

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

262
 			compiled.setFieldInfo(member, new JavaField(javaClass, field.getName(), getDescriptor(field.getType())));
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
 				definition.addMember(member);
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
 			ZenCodeType.Method methodAnnotation = method.getAnnotation(ZenCodeType.Method.class);
275
 			ZenCodeType.Method methodAnnotation = method.getAnnotation(ZenCodeType.Method.class);
274
 			if (methodAnnotation != null) {
276
 			if (methodAnnotation != null) {
275
 				MethodMember member = asMethod(definition, method, methodAnnotation);
277
 				MethodMember member = asMethod(definition, method, methodAnnotation);
338
 		return name;
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
 		FunctionHeader header = getHeader(method);
344
 		FunctionHeader header = getHeader(method);
343
 		return new ConstructorMember(
345
 		return new ConstructorMember(
344
 				CodePosition.NATIVE,
346
 				CodePosition.NATIVE,
345
 				definition,
347
 				definition,
346
-				getMethodModifiers(method),
348
+				Modifiers.PUBLIC,
347
 				header,
349
 				header,
348
 				null);
350
 				null);
349
 	}
351
 	}
412
 		return new CasterMember(CodePosition.NATIVE, definition, modifiers, toType, null);
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
 	private FunctionHeader getHeader(Method method) {
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
 		for (int i = 0; i < parameters.length; i++) {
442
 		for (int i = 0; i < parameters.length; i++) {
421
 			parameters[i] = new FunctionParameter(loadStoredType(parameterTypes[i]));
443
 			parameters[i] = new FunctionParameter(loadStoredType(parameterTypes[i]));
422
 		}
444
 		}
423
 		
445
 		
424
-		TypeVariable<Method>[] javaTypeParameters = method.getTypeParameters();
425
 		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
446
 		TypeParameter[] typeParameters = new TypeParameter[javaTypeParameters.length];
426
 		for (int i = 0; i < javaTypeParameters.length; i++) {
447
 		for (int i = 0; i < javaTypeParameters.length; i++) {
427
 			TypeVariable<Method> javaTypeParameter = javaTypeParameters[i];
448
 			TypeVariable<Method> javaTypeParameter = javaTypeParameters[i];
431
 				typeParameters[i].addBound(new ParameterTypeBound(CodePosition.NATIVE, loadType(bound)));
452
 				typeParameters[i].addBound(new ParameterTypeBound(CodePosition.NATIVE, loadType(bound)));
432
 		}
453
 		}
433
 		
454
 		
434
-		AnnotatedType[] exceptionTypes = method.getAnnotatedExceptionTypes();
435
 		if (exceptionTypes.length > 1)
455
 		if (exceptionTypes.length > 1)
436
 			throw new IllegalArgumentException("A method can only throw a single exception type!");
456
 			throw new IllegalArgumentException("A method can only throw a single exception type!");
437
 		
457
 		
518
 		return org.objectweb.asm.Type.getDescriptor(cls);
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
 	private static String getMethodDescriptor(Method method) {
545
 	private static String getMethodDescriptor(Method method) {
522
 		return org.objectweb.asm.Type.getMethodDescriptor(method);
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
 	private static JavaMethod getMethod(JavaClass cls, Method method, StoredType result) {
560
 	private static JavaMethod getMethod(JavaClass cls, Method method, StoredType result) {
526
 		JavaMethod.Kind kind;
561
 		JavaMethod.Kind kind;
527
 		if (method.getName().equals("<init>"))
562
 		if (method.getName().equals("<init>"))

+ 4
- 1
JavaIntegration/src/main/java/org/openzen/zencode/java/ScriptingEngine.java View File

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

BIN
JavaIntegration/src/main/resources/StdLibs.zip View File


+ 20
- 0
JavaScripting/build.gradle View File

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 View File

1
+group='org.openzen.zencode'
2
+version='0.1.0'

+ 5
- 0
ScriptingExample/scripts/integration.zs View File

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 View File

22
 	public static void main(String[] args) throws CompileException, ParseException, IOException {
22
 	public static void main(String[] args) throws CompileException, ParseException, IOException {
23
 		ScriptingEngine scriptingEngine = new ScriptingEngine();
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
 		File inputDirectory = new File("scripts");
30
 		File inputDirectory = new File("scripts");
30
 		File[] inputFiles = Optional.ofNullable(inputDirectory.listFiles((dir, name) -> name.endsWith(".zs"))).orElseGet(() -> new File[0]);
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 View File

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 View File

5
 apply plugin: 'java'
5
 apply plugin: 'java'
6
 apply plugin: 'maven'
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
 sourceCompatibility = '1.8'
11
 sourceCompatibility = '1.8'
12
 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
12
 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
13
 
13
 
14
 repositories {
14
 repositories {
15
     mavenCentral();
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
 dependencies {
18
 dependencies {
22
-    // Adding dependencies here will add the dependencies to each subproject.
23
-    testCompile group: 'junit', name: 'junit', version: '4.10'
24
 	compile 'net.sf.trove4j:trove4j:3.0.3'
19
 	compile 'net.sf.trove4j:trove4j:3.0.3'
25
 }
20
 }
26
 
21
 
34
     from sourceSets.main.allSource
29
     from sourceSets.main.allSource
35
 }
30
 }
36
 
31
 
37
-artifacts {
38
-    archives jar
39
-    archives sourcesJar
40
-}
41
-
42
 configure(install.repositories.mavenInstaller) {
32
 configure(install.repositories.mavenInstaller) {
43
     pom.project {
33
     pom.project {
44
         groupId = mavenGroupId
34
         groupId = mavenGroupId

Loading…
Cancel
Save