private SelectorFunction compileSelectorFunction() { final ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript"); try { ((Compilable) engine).compile("var apply = " + function).eval(); return ((Invocable) engine).getInterface(SelectorFunction.class); } catch (ScriptException e) { throw new RuntimeException(e); } }
private SelectorFunction compileSelectorFunction() { final ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript"); try { ((Compilable) engine).compile("var apply = " + function).eval(); return ((Invocable) engine).getInterface(SelectorFunction.class); } catch (ScriptException e) { throw Throwables.propagate(e); } }
private void loadJavaScriptSupport(Compilable compilable) throws IOException, ScriptException { // 加载 compilable.compile( IOUtils.toString( GaStringUtils.class.getResourceAsStream("/com/github/ompc/greys/core/res/javascript/gblocking.js"), Charset.forName("UTF-8") ) ).eval(); }
private void loadGreysModule(Compilable compilable) throws IOException, ScriptException { // 加载greys-module.js compilable.compile( IOUtils.toString( GaStringUtils.class.getResourceAsStream("/com/github/ompc/greys/core/res/javascript/greys-module.js"), Charset.forName("UTF-8") ) ).eval(); }
@Override public Object eval(Reader reader, ScriptContext _ctx) throws ScriptException { return compile(reader).eval(_ctx); }
@SuppressWarnings("unchecked") @Override public <T> T getElValue(String exp, Object target, Object[] arguments, Object retVal, boolean hasRetVal, Class<T> valueType) throws Exception { Bindings bindings = new SimpleBindings(); bindings.put(TARGET, target); bindings.put(ARGS, arguments); if (hasRetVal) { bindings.put(RET_VAL, retVal); } CompiledScript script = expCache.get(exp); if (null != script) { return (T) script.eval(bindings); } if (engine instanceof Compilable) { Compilable compEngine = (Compilable) engine; script = compEngine.compile(funcs + exp); expCache.put(exp, script); return (T) script.eval(bindings); } else { return (T) engine.eval(funcs + exp, bindings); } }
/** * 编译脚本 * * @param script 脚本内容 * @return {@link CompiledScript} * @throws ScriptRuntimeException 脚本异常 * @since 3.2.0 */ public static Object eval(String script) throws ScriptRuntimeException { try { return compile(script).eval(); } catch (ScriptException e) { throw new ScriptRuntimeException(e); } }
/** * 编译脚本 * * @param script 脚本内容 * @return {@link CompiledScript} * @throws ScriptRuntimeException 脚本异常 * @since 3.2.0 */ public static Object eval(String script) throws ScriptRuntimeException { try { return compile(script).eval(); } catch (ScriptException e) { throw new ScriptRuntimeException(e); } }
/** * 编译脚本 * * @param script 脚本内容 * @param bindings 绑定的参数 * @return {@link CompiledScript} * @throws ScriptRuntimeException 脚本异常 * @since 3.2.0 */ public static Object eval(String script, Bindings bindings) throws ScriptRuntimeException { try { return compile(script).eval(bindings); } catch (ScriptException e) { throw new ScriptRuntimeException(e); } }
/** * 编译脚本 * * @param script 脚本内容 * @param context 脚本上下文 * @return {@link CompiledScript} * @throws ScriptRuntimeException 脚本异常 * @since 3.2.0 */ public static Object eval(String script, ScriptContext context) throws ScriptRuntimeException { try { return compile(script).eval(context); } catch (ScriptException e) { throw new ScriptRuntimeException(e); } }
/** * 编译脚本 * * @param script 脚本内容 * @param context 脚本上下文 * @return {@link CompiledScript} * @throws ScriptRuntimeException 脚本异常 * @since 3.2.0 */ public static Object eval(String script, ScriptContext context) throws ScriptRuntimeException { try { return compile(script).eval(context); } catch (ScriptException e) { throw new ScriptRuntimeException(e); } }
/** * 编译脚本 * * @param script 脚本内容 * @param bindings 绑定的参数 * @return {@link CompiledScript} * @throws ScriptRuntimeException 脚本异常 * @since 3.2.0 */ public static Object eval(String script, Bindings bindings) throws ScriptRuntimeException { try { return compile(script).eval(bindings); } catch (ScriptException e) { throw new ScriptRuntimeException(e); } }
CompiledScript script = ((Compilable) scriptEngine).compile(reader); Bindings sb = new SimpleBindings(); script.eval(sb); // Put the Lua functions into the sb environment LuaValue luaDog = CoerceJavaToLua.coerce(dog); // Java to Lua LuaFunction onTalk = (LuaFunction) sb.get("onTalk"); // Get Lua function LuaValue b = onTalk.call(luaDog); // Call the function System.out.println("onTalk answered: " + b); LuaFunction onWalk = (LuaFunction) sb.get("onWalk"); LuaValue[] dogs = { luaDog }; Varargs dist = onWalk.invoke(LuaValue.varargsOf(dogs)); // Alternative System.out.println("onWalk returned: " + dist);
/** * Evaluate the script using the given bindings. * * @param script the script to evaluate * @param bindings the context-aware bindings * @return the result object * @throws ScriptException if an error occurs in script. */ public Object evaluate(Script script, Bindings bindings) throws ScriptException { // Always look for a compiled script in our cache. CompiledScript compiledScript = compiledScripts.get(script); // No compiled script found? if (compiledScript == null) { String source = script.getSource(); ScriptEngine scriptEngine = scriptEngines.computeIfAbsent(script.getEngine(), this::createScriptEngine); if (!(scriptEngine instanceof Compilable) || forceScriptEvaluation) { return scriptEngine.eval(source, bindings); } // Compile and store it in our cache. Fall-through for execution compiledScript = ((Compilable) scriptEngine).compile(source); compiledScripts.putIfAbsent(script, compiledScript); } // Let the cached compiled script do its work. return compiledScript.eval(bindings); }
protected Object evaluateExpression(Object expression, Map<String, Object> parameters) { try { Object result = null; StringWriter writer = new StringWriter(); ScriptContext context = new SimpleScriptContext(); for (Map.Entry<String, Object> property : engineProperties.entrySet()) { context.setAttribute(property.getKey(), property.getValue(), ScriptContext.ENGINE_SCOPE); } Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); bindings.putAll(parameters); context.setBindings(bindings, ScriptContext.ENGINE_SCOPE); context.setWriter(writer); if (expression instanceof CompiledScript) { logger.debug("About to evaluate compiled expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = ((CompiledScript) expression).eval(context); } else { logger.debug("About to evaluate expression {} with bindings {} on engine", expression, parameters, scriptEngine); result = scriptEngine.eval(expression.toString(), context); } if (result == null) { result = writer.toString(); } return result; } catch (ScriptException e) { throw new RuntimeException("Error when evaluating script", e); } }
@Test public void testCompilable() throws Exception { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("jphp"); CompiledScript script = ((Compilable)engine).compile("<?php return $foo;"); engine.put("foo", "first"); Assert.assertEquals("first", script.eval().toString()); engine.put("foo", "second"); Assert.assertEquals("second", script.eval().toString()); } }
@Override public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException { try { Bindings bindings = createBindings(invokers, invocation); Compilable compilable = (Compilable) engine; CompiledScript function = compilable.compile(rule); return getRoutedInvokers(function.eval(bindings)); } catch (ScriptException e) { logger.error("route error, rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e); return invokers; } }
private Trigger loadFromSource() { SourceCompiler compiler = database.getCompiler(); synchronized (compiler) { String fullClassName = Constants.USER_PACKAGE + ".trigger." + getName(); compiler.setSource(fullClassName, triggerSource); try { if (SourceCompiler.isJavaxScriptSource(triggerSource)) { return (Trigger) compiler.getCompiledScript(fullClassName).eval(); } else { final Method m = compiler.getMethod(fullClassName); if (m.getParameterTypes().length > 0) { throw new IllegalStateException("No parameters are allowed for a trigger"); } return (Trigger) m.invoke(null); } } catch (DbException e) { throw e; } catch (Exception e) { throw DbException.get(ErrorCode.SYNTAX_ERROR_1, e, triggerSource); } } }
@Override public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException { try { Bindings bindings = createBindings(invokers, invocation); Compilable compilable = (Compilable) engine; CompiledScript function = compilable.compile(rule); return getRoutedInvokers(function.eval(bindings)); } catch (ScriptException e) { logger.error("route error, rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e); return invokers; } }
Project.MSG_DEBUG); return compiledScript.eval(bindings);