Refine search
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); } }
public static CompiledScript compile(String js) throws ScriptException { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("JavaScript"); U.must(engine instanceof Compilable, "The JavaScript engine cannot compile!"); Compilable compilable = (Compilable) engine; return compilable.compile(js); }
protected Object evaluateScriptExpression(String expressionLanguage, VariableContext variableContext, String expressionText, CachedCompiledScriptSupport cachedCompiledScriptSupport) { ScriptEngine scriptEngine = getScriptEngineForName(expressionLanguage); // wrap script engine bindings + variable context and pass enhanced // bindings to the script engine. Bindings bindings = VariableContextScriptBindings.wrap(scriptEngine.createBindings(), variableContext); bindings.put("variableContext", variableContext); try { if (scriptEngine instanceof Compilable) { CompiledScript compiledScript = cachedCompiledScriptSupport.getCachedCompiledScript(); if (compiledScript == null) { synchronized (cachedCompiledScriptSupport) { compiledScript = cachedCompiledScriptSupport.getCachedCompiledScript(); if(compiledScript == null) { Compilable compilableScriptEngine = (Compilable) scriptEngine; compiledScript = compilableScriptEngine.compile(expressionText); cachedCompiledScriptSupport.cacheCompiledScript(compiledScript); } } } return compiledScript.eval(bindings); } else { return scriptEngine.eval(expressionText, bindings); } } catch (ScriptException e) { throw LOG.unableToEvaluateExpression(expressionText, scriptEngine.getFactory().getLanguageName(), e); } }
@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); } }
JavaScriptLoader(String name, String script) throws RedPenException { this.name = name; setValidatorName(name); ScriptEngine engine = manager.getEngineByName("nashorn"); try { engine.put("redpenToBeBound", this); for (String methodToBeExposed : methodsToBeExposedToJS) { engine.eval(String.format( "var %s = Function.prototype.bind.call(redpenToBeBound.%s, redpenToBeBound);", methodToBeExposed, methodToBeExposed)); } try { engine.eval("var _JavaScriptValidatorTest = Java.type('cc.redpen.validator.JavaScriptValidatorTest');"); } catch (RuntimeException e) { if (!(e.getCause() instanceof ClassNotFoundException)) { throw e; } } engine.eval("java = undefined; javax = undefined; Java = undefined; load = undefined; redpenToBeBound = undefined;"); CompiledScript compiledScript = ((Compilable) engine).compile(script); compiledScript.eval(); this.message = (String) engine.get("message"); this.invocable = (Invocable) engine; } catch (ScriptException e) { throw new RedPenException(e); } }
jsscope = jscx.getBindings( ScriptContext.ENGINE_SCOPE ); jscx.eval( strActiveStartScript, jsscope ); error_message = "Found Start Script. " + strActiveStartScriptName + " Processing OK"; cr = new CheckResult( CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta ); jsscript = ( (Compilable) jscx ).compile( strActiveScript ); jsscript.eval( jsscope ); /* Object endScript = */jscx.eval( strActiveEndScript, jsscope ); error_message = "Found End Script. " + strActiveEndScriptName + " Processing OK"; cr = new CheckResult( CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta );
@SuppressWarnings("unchecked") public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws JahhanException { try { List<Invoker<T>> invokersCopy = new ArrayList<Invoker<T>>(invokers); Compilable compilable = (Compilable) engine; Bindings bindings = engine.createBindings(); bindings.put("invokers", invokersCopy); bindings.put("invocation", invocation); bindings.put("context", RpcContext.getContext()); CompiledScript function = compilable.compile(rule); Object obj = function.eval(bindings); if (obj instanceof Invoker[]) { invokersCopy = Arrays.asList((Invoker<T>[]) obj); } else if (obj instanceof Object[]) { invokersCopy = new ArrayList<Invoker<T>>(); for (Object inv : (Object[]) obj) { invokersCopy.add((Invoker<T>)inv); } } else { invokersCopy = (List<Invoker<T>>) obj; } return invokersCopy; } catch (ScriptException e) { //fail then ignore rule .invokers. log.error("route error , rule has been ignored. rule: " + rule + ", method:" + invocation.getMethodName() + ", url: " + RpcContext.getContext().getUrl(), e); return invokers; } }
public static CompiledScript compile(String script, String lang) throws ScriptException { final ScriptEngine engine = manager.getEngineByName(lang); Compilable compilingEngine = (Compilable) engine; CompiledScript compiledScript = compilingEngine.compile(script); return compiledScript; }
ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby"); script = compilingEngine.compile(code); scripts.add(script); return scripts.size()-1;
compiledScript = c.compile(script); } catch (ScriptException e) { scriptManager.throwErrorMessage(e, script); .bindContextVariables(compiledScript.getEngine(), compiledScript.getEngine().getBindings(ScriptContext.ENGINE_SCOPE), database, null, params); final Object ob = compiledScript.eval(binding); return transformer.toResultSet(ob); } catch (ScriptException e) {
public ScriptedCollectionSetBuilder(File script, ScriptEngineManager manager) throws IOException, ScriptException { if (!script.canRead()) { throw new IllegalStateException("Cannot read script at '" + script + "'."); } final String ext = Files.getFileExtension(script.getAbsolutePath()); final ScriptEngine engine = manager.getEngineByExtension(ext); if (engine == null) { throw new IllegalStateException("No engine found for extension: " + ext); } if (!(engine instanceof Compilable)) { throw new IllegalStateException("Only engines that can compile scripts are supported."); } final Compilable compilable = (Compilable) engine; try (FileReader reader = new FileReader(script)) { compiledScript = compilable.compile(reader); } }
data.scope = data.cx.getBindings( ScriptContext.ENGINE_SCOPE ); CompiledScript startScript = ( (Compilable) data.cx ).compile( strStartScript ); startScript.eval( data.scope ); if ( log.isDetailed() ) { logDetailed( ( "Start Script found!" ) ); data.script = ( (Compilable) data.cx ).compile( strTransformScript ); } catch ( Exception e ) { throw new KettleValueException( BaseMessages.getString( PKG, "Script.Log.CouldNotCompileJavascript" ), e ); data.script.eval( data.scope );
@Override public CompiledScript compile(Reader script) throws ScriptException { return ((Compilable)engine).compile(script); }
public CompiledScript compile(ScriptEngine scriptEngine, String language, String src) { if(scriptEngine instanceof Compilable && !scriptEngine.getFactory().getLanguageName().equalsIgnoreCase("ecmascript")) { Compilable compilingEngine = (Compilable) scriptEngine; try { CompiledScript compiledScript = compilingEngine.compile(src); LOG.debugCompiledScriptUsing(language); return compiledScript; } catch (ScriptException e) { throw new ScriptCompilationException("Unable to compile script: " + e.getMessage(), e); } } else { // engine does not support compilation return null; } }
private NashornCompiler(String script) { NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); String engineVersion = factory.getEngineVersion(); String cacheLocation = Paths.get(System.getProperty("user.home"), ".code-story", "nashorn_code_cache_" + engineVersion).toFile().getAbsolutePath(); System.setProperty("nashorn.persistent.code.cache", cacheLocation); ScriptEngine nashorn = factory.getScriptEngine(nashornOptions()); try { compiledScript = ((Compilable) nashorn).compile(script); bindings = nashorn.getBindings(ENGINE_SCOPE); } catch (ScriptException e) { throw new IllegalStateException("Unable to compile javascript", e); } }
/** * Get the compiled script. * * @param packageAndClassName the package and class name * @return the compiled script */ public CompiledScript getCompiledScript(String packageAndClassName) throws ScriptException { CompiledScript compiledScript = compiledScripts.get(packageAndClassName); if (compiledScript == null) { String source = sources.get(packageAndClassName); final String lang; if (isJavascriptSource(source)) { lang = "javascript"; } else if (isRubySource(source)) { lang = "ruby"; } else { throw new IllegalStateException("Unknown language for " + source); } final Compilable jsEngine = (Compilable) new ScriptEngineManager().getEngineByName(lang); compiledScript = jsEngine.compile(source); compiledScripts.put(packageAndClassName, compiledScript); } return compiledScript; }
@Override public Object evaluate(final String scriptId, final String script, final Map<String, Object> variables) throws JPPFScriptingException { if (engine == null) return null; final Bindings bindings = engine.createBindings(); if (variables != null) bindings.putAll(variables); CompiledScript cs = null; if ((scriptId != null) && (engine instanceof Compilable)) { final String key = new StringBuilder().append(language).append(':').append(scriptId).toString(); cs = map.get(key); cacheRequests.incrementAndGet(); if (cs == null) { cacheMisses.incrementAndGet(); try { cs = ((Compilable) engine).compile(script); if (cs != null) map.put(key, cs); } catch (final Exception e) { throw buildScriptingException(e); } } else cacheHits.incrementAndGet(); } if (debugEnabled) log.debug("script cache statistics for '{}': requests={}, hits={}, misses={}", language, cacheRequests.get(), cacheHits.get(), cacheMisses.get()); try { final Object res = (cs != null) ? cs.eval(bindings) : engine.eval(script, bindings); return res; } catch(final Exception e) { throw buildScriptingException(e); } }
/** * 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); }