From f7d2f1a9f3a374b729377dbc43b030b0585187b4 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 6 Feb 2017 19:12:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=96=B5=E5=BC=8F?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=BC=95=E6=93=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../yumc/YumCore/engine/MiaoScriptEngine.java | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java diff --git a/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java b/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java new file mode 100644 index 0000000..c3cb023 --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/engine/MiaoScriptEngine.java @@ -0,0 +1,131 @@ +package pw.yumc.YumCore.engine; + +import java.io.Reader; + +import javax.script.Bindings; +import javax.script.Invocable; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +/** + * 喵式脚本引擎 + * + * @author 喵♂呜 + * @since 2016年8月29日 上午7:51:43 + */ +public class MiaoScriptEngine implements ScriptEngine, Invocable { + private static MiaoScriptEngine DEFAULT; + private ScriptEngine engine; + + public MiaoScriptEngine() { + this("js"); + } + + public MiaoScriptEngine(final String engineType) { + try { + engine = new ScriptEngineManager().getEngineByName(engineType); + } catch (final NullPointerException ex) { + engine = new ScriptEngineManager().getEngineByName("javascript"); + } + } + + public static MiaoScriptEngine getDefault() { + if (DEFAULT == null) { + DEFAULT = new MiaoScriptEngine("javascript"); + } + return DEFAULT; + } + + @Override + public Bindings createBindings() { + return engine.createBindings(); + } + + @Override + public Object eval(final Reader reader) throws ScriptException { + return engine.eval(reader); + } + + @Override + public Object eval(final Reader reader, final Bindings n) throws ScriptException { + return engine.eval(reader, n); + } + + @Override + public Object eval(final Reader reader, final ScriptContext context) throws ScriptException { + return engine.eval(reader, context); + } + + @Override + public Object eval(final String script) throws ScriptException { + return engine.eval(script); + } + + @Override + public Object eval(final String script, final Bindings n) throws ScriptException { + return engine.eval(script, n); + } + + @Override + public Object eval(final String script, final ScriptContext context) throws ScriptException { + return engine.eval(script, context); + } + + @Override + public Object get(final String key) { + return engine.get(key); + } + + @Override + public Bindings getBindings(final int scope) { + return engine.getBindings(scope); + } + + @Override + public ScriptContext getContext() { + return engine.getContext(); + } + + @Override + public ScriptEngineFactory getFactory() { + return engine.getFactory(); + } + + @Override + public T getInterface(final Class clasz) { + return ((Invocable) engine).getInterface(clasz); + } + + @Override + public T getInterface(final Object thiz, final Class clasz) { + return ((Invocable) engine).getInterface(thiz, clasz); + } + + @Override + public Object invokeFunction(final String name, final Object... args) throws ScriptException, NoSuchMethodException { + return ((Invocable) engine).invokeFunction(name, args); + } + + @Override + public Object invokeMethod(final Object thiz, final String name, final Object... args) throws ScriptException, NoSuchMethodException { + return ((Invocable) engine).invokeMethod(thiz, name, args); + } + + @Override + public void put(final String key, final Object value) { + engine.put(key, value); + } + + @Override + public void setBindings(final Bindings bindings, final int scope) { + engine.setBindings(bindings, scope); + } + + @Override + public void setContext(final ScriptContext context) { + engine.setContext(context); + } +}