@author Bob McWhirter
| 14 | * @author Bob McWhirter |
| 15 | */ |
| 16 | public class DynJSScriptEngine extends AbstractScriptEngine implements Compilable, Invocable { |
| 17 | |
| 18 | public static final String GLOBAL_OBJECT = "org.dynjs.global-object"; |
| 19 | |
| 20 | private final DynJSScriptEngineFactory factory; |
| 21 | |
| 22 | DynJSScriptEngine(DynJSScriptEngineFactory factory) { |
| 23 | this.factory = factory; |
| 24 | this.context = new SimpleScriptContext(); |
| 25 | this.context.setBindings(factory.getGlobalBindings(), ScriptContext.GLOBAL_SCOPE); |
| 26 | } |
| 27 | |
| 28 | @Override |
| 29 | public DynJSCompiledScript compile(String script) throws ScriptException { |
| 30 | Compiler compiler = new DynJS().newCompiler(); |
| 31 | JSProgram program = compiler.withSource(script).compile(); |
| 32 | return new DynJSCompiledScript(this, program); |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public DynJSCompiledScript compile(Reader script) throws ScriptException { |
| 37 | Compiler compiler = new DynJS().newCompiler(); |
| 38 | JSProgram program = compiler.withSource(script).compile(); |
| 39 | return new DynJSCompiledScript(this, program); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public Object eval(String script, ScriptContext context) throws ScriptException { |
| 44 | DynJSCompiledScript program = compile(script); |
| 45 | return program.eval(context); |
| 46 | |
| 47 | //ScriptEngineGlobalObject global = getGlobalObject( context ); |
| 48 | //DynJS runtime = getRuntime( global, context ); |
| 49 | //return runtime.newRunner().withSource( script ).execute(); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Object eval(Reader reader, ScriptContext context) throws ScriptException { |
| 54 | DynJSCompiledScript program = compile(reader); |
| 55 | return program.eval(context); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Bindings createBindings() { |
| 60 | return new SimpleBindings(); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public ScriptEngineFactory getFactory() { |
| 65 | return this.factory; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { |
| 70 | if (!(thiz instanceof JSObject)) { |
| 71 | throw new IllegalArgumentException("'this' should be an instance of " + JSObject.class.getName()); |
| 72 | } |
| 73 |
nothing calls this directly
no outgoing calls
no test coverage detected