feat: add createEngine method

Signed-off-by: MiaoWoo <admin@yumc.pw>
master
MiaoWoo 2020-07-08 11:11:18 +08:00
parent 9bb67410ac
commit e71bd2a0cb
2 changed files with 20 additions and 16 deletions

View File

@ -2,11 +2,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pw.yumc</groupId>
<artifactId>MiaoScript</artifactId>
<version>0.7.3</version>
<version>0.7.5</version>
<developers>
<developer>
<id>502647092</id>
<name>喵♂呜</name>
<name>MiaoWoo</name>
<email>admin@yumc.pw</email>
<url>http://www.yumc.pw</url>
</developer>

View File

@ -1,13 +1,12 @@
package pw.yumc.MiaoScript;
import lombok.SneakyThrows;
import javax.script.ScriptEngineManager;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.script.ScriptEngineManager;
import lombok.SneakyThrows;
/**
* Created with IntelliJ IDEA
*
@ -27,21 +26,26 @@ public class ScriptEngine {
this.manager = new ScriptEngineManager();
}
@SneakyThrows
public synchronized void enableEngine() {
public synchronized MiaoScriptEngine createEngine() {
if (this.engine == null) {
this.engine = new MiaoScriptEngine(manager, "nashorn");
this.engine.put("base", this.base);
this.engine.put("ScriptEngineContextHolder", this);
Path bios = Paths.get(root, "bios.js");
// 如果存在自定义bios就加载自定义的
if (Files.exists(bios)) {
this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')");
} else {
this.engine.eval("load('classpath:bios.js')");
}
engine.invokeFunction("boot", root, logger);
}
return this.engine;
}
@SneakyThrows
public void enableEngine() {
createEngine();
Path bios = Paths.get(root, "bios.js");
// 如果存在自定义bios就加载自定义的
if (Files.exists(bios)) {
this.engine.eval("load('" + bios.toFile().getCanonicalPath() + "')");
} else {
this.engine.eval("load('classpath:bios.js')");
}
engine.invokeFunction("boot", root, logger);
}
@SneakyThrows