|
|
@@ -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
|
+}
|