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); + } +}