From e71bd2a0cb500624187bc62792ed260a39646308 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Wed, 8 Jul 2020 11:11:18 +0800 Subject: [PATCH] feat: add createEngine method Signed-off-by: MiaoWoo --- pom.xml | 4 +-- .../java/pw/yumc/MiaoScript/ScriptEngine.java | 32 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 405255e..79f68fe 100644 --- a/pom.xml +++ b/pom.xml @@ -2,11 +2,11 @@ 4.0.0 pw.yumc MiaoScript - 0.7.3 + 0.7.5 502647092 - 喵♂呜 + MiaoWoo admin@yumc.pw http://www.yumc.pw diff --git a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java index 3f856dc..8c95dca 100644 --- a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java @@ -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