1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-12-27 07:28:52 +00:00

feat: 添加喵式脚本引擎

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-02-06 19:12:20 +08:00
parent b514702598
commit f7d2f1a9f3

View File

@ -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> T getInterface(final Class<T> clasz) {
return ((Invocable) engine).getInterface(clasz);
}
@Override
public <T> T getInterface(final Object thiz, final Class<T> 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);
}
}