Stan Hebben před 6 roky
rodič
revize
364fc793a8

+ 4
- 0
.gitignore Zobrazit soubor

@@ -0,0 +1,4 @@
1
+.gradle
2
+.nb-gradle
3
+build
4
+*.class

+ 44
- 0
build.gradle Zobrazit soubor

@@ -0,0 +1,44 @@
1
+apply plugin: 'java'
2
+
3
+sourceCompatibility = '1.8'
4
+[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
5
+
6
+// NetBeans will automatically add "run" and "debug" tasks relying on the
7
+// "mainClass" property. You may however define the property prior executing
8
+// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
9
+//
10
+// Note however, that you may define your own "run" and "debug" task if you
11
+// prefer. In this case NetBeans will not add these tasks but you may rely on
12
+// your own implementation.
13
+if (!hasProperty('mainClass')) {
14
+    ext.mainClass = ''
15
+}
16
+
17
+repositories {
18
+    mavenCentral()
19
+	maven { url "http://maven.openzen.org/" }
20
+}
21
+
22
+dependencies {
23
+    compile 'org.openzen.zencode:zencode-javascripting:0.1.2'
24
+}
25
+
26
+task fatJar(type: Jar) {
27
+	manifest {
28
+        attributes 'Implementation-Title': 'ZenCode Java Scripting Engine Example',
29
+			'Class-Path': '.',
30
+        	'Main-Class': 'org.openzen.javascriptingexample.Main'
31
+    }
32
+    baseName = project.name + '-all'
33
+    from {
34
+		configurations.compile.collect {
35
+			it.isDirectory() ? it : zipTree(it)
36
+		}
37
+	} {
38
+		exclude "META-INF/INDEX.LIST"
39
+		exclude "META-INF/*.SF"
40
+        exclude "META-INF/*.DSA"
41
+        exclude "META-INF/*.RSA"
42
+	}
43
+    with jar
44
+}

+ 7
- 0
scripts/helloworld.zs Zobrazit soubor

@@ -0,0 +1,7 @@
1
+import example.TestClass;
2
+
3
+println("Hello world!");
4
+
5
+val test = new TestClass("something");
6
+println("Name: " + test.name);
7
+test.dump();

+ 1
- 0
settings.gradle Zobrazit soubor

@@ -0,0 +1 @@
1
+rootProject.name = 'JavaScriptingExample'

+ 33
- 0
src/main/java/org/openzen/javascriptingexample/Globals.java Zobrazit soubor

@@ -0,0 +1,33 @@
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.javascriptingexample;
7
+
8
+import java.io.BufferedReader;
9
+import java.io.IOException;
10
+import java.io.InputStreamReader;
11
+import org.openzen.zencode.java.ZenCodeGlobals;
12
+
13
+/**
14
+ *
15
+ * @author Hoofdgebruiker
16
+ */
17
+public class Globals implements ZenCodeGlobals {
18
+	@Global
19
+	public static void println(String message) {
20
+		System.out.println(message);
21
+	}
22
+	
23
+	@Global
24
+	public static String readLine(String prompt) {
25
+		System.out.print(prompt);
26
+		try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
27
+			return reader.readLine();
28
+		} catch (IOException ex) {
29
+			ex.printStackTrace();
30
+			return "";
31
+		}
32
+	}
33
+}

+ 46
- 0
src/main/java/org/openzen/javascriptingexample/Main.java Zobrazit soubor

@@ -0,0 +1,46 @@
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.javascriptingexample;
7
+
8
+import java.io.File;
9
+import java.util.Optional;
10
+import org.openzen.zencode.java.JavaNativeModule;
11
+import org.openzen.zencode.java.ScriptingEngine;
12
+import org.openzen.zencode.shared.CompileException;
13
+import org.openzen.zencode.shared.FileSourceFile;
14
+import org.openzen.zencode.shared.SourceFile;
15
+import org.openzen.zenscript.codemodel.SemanticModule;
16
+import org.openzen.zenscript.lexer.ParseException;
17
+
18
+/**
19
+ *
20
+ * @author Hoofdgebruiker
21
+ */
22
+public class Main {
23
+	public static void main(String[] args) throws CompileException, ParseException {
24
+		ScriptingEngine engine = new ScriptingEngine();
25
+		
26
+		// The name of the module can be freely chosen (but make sure it matches the dependency name below)
27
+		// The name of the base package MUST match with the package of the classes that will be registered below
28
+		JavaNativeModule example = engine.createNativeModule("example", "org.openzen.javascriptingexample");
29
+		example.addGlobals(Globals.class);
30
+		example.addClass(TestClass.class);
31
+		engine.registerNativeProvided(example);
32
+		
33
+		File inputDirectory = new File("scripts");
34
+		File[] inputFiles = Optional.ofNullable(inputDirectory.listFiles((dir, name) -> name.endsWith(".zs"))).orElseGet(() -> new File[0]);
35
+		SourceFile[] sourceFiles = new SourceFile[inputFiles.length];
36
+		for (int i = 0; i < inputFiles.length; i++)
37
+			sourceFiles[i] = new FileSourceFile(inputFiles[i].getName(), inputFiles[i]);
38
+		
39
+		SemanticModule scripts = engine.createScriptedModule("scripts", sourceFiles, "example");
40
+		if (!scripts.isValid())
41
+			return;
42
+		
43
+		engine.registerCompiled(scripts);
44
+		engine.run();
45
+	}
46
+}

+ 31
- 0
src/main/java/org/openzen/javascriptingexample/TestClass.java Zobrazit soubor

@@ -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.javascriptingexample;
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
+}

Loading…
Zrušit
Uložit