public static void main(String[] args) throws ScriptException { NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ScriptEngine nashorn = factory.getScriptEngine(new NoJavaFilter()); Integer eval = (Integer) nashorn.eval("var HashMap = Java.type('java.util.HashMap')"); System.out.println(eval); }
ScriptEngine engine = factory.getScriptEngine(args, classLoader, classFilter); Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
public static ScriptEngine getScriptEngine( final String... args ) { return FACTORY.getScriptEngine( args ); }
public NashornRuntime newRuntime( final ClassLoader loader ) { final ScriptEngine engine = this.scriptEngineFactory.getScriptEngine( ENGINE_ARGS, loader ); return new NashornRuntimeImpl( engine ); } }
import jdk.nashorn.api.scripting.NashornScriptEngineFactory; ... NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ... ScriptEngine engine = factory.getScriptEngine("-strict", "--no-java", "--no-syntax-extensions");
import javax.script.*; import jdk.nashorn.api.scripting.*; public class NashTest { public static void main(String[] args) throws ScriptException { NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(new String[] { "--global-per-engine" }); engine.eval("java.lang.System.out.println('hello world!')"); } }
NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ScriptEngine engine = factory.getScriptEngine(new String[]{"-scripting"}); String yourCommand = "ls"; Object eval = engine.eval("$EXEC(\"" + yourCommand + "\")"); System.out.println(eval);
static void init() { try { NashornScriptEngineFactory factory = (NashornScriptEngineFactory) scriptEngineManager.getEngineFactories().stream().filter(factories -> "Oracle Nashorn".equalsIgnoreCase(factories.getEngineName())).findFirst().orElse(null); scriptEngine = Objects.requireNonNull(factory).getScriptEngine("-doe", "--global-per-engine"); } catch (Exception ignored) { scriptEngine = scriptEngineManager.getEngineByName("JavaScript"); } }
protected ScriptEngine getScriptEngine(boolean cache, boolean filter) { NashornScriptEngineFactory nashorn = new NashornScriptEngineFactory(); String[] args = cache ? new String[] { "-strict", "--optimistic-types=true", "--persistent-code-cache", "--class-cache-size=50" } : new String[] { "-strict" }; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); ClassFilter classFilter = filter ? getClassFilter() : null; return nashorn.getScriptEngine(args, classLoader, classFilter); }
public NashornSandboxImpl(String... params) { this.maxPreparedStatements = 0; this.sandboxClassFilter = new SandboxClassFilter(); final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); for (String param : params) { if (param.equals("--no-java")) { throw new IllegalArgumentException("The engine parameter --no-java is not supported. Using it would interfere with the injected code to test for infinite loops."); } } this.scriptEngine = factory.getScriptEngine(params, this.getClass().getClassLoader(), this.sandboxClassFilter); this.allow(InterruptTest.class); }
public NashornSandboxImpl(String... params) { this.maxPreparedStatements = 0; this.sandboxClassFilter = new SandboxClassFilter(); final NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); for (String param : params) { if (param.equals("--no-java")) { throw new IllegalArgumentException("The engine parameter --no-java is not supported. Using it would interfere with the injected code to test for infinite loops."); } } this.scriptEngine = factory.getScriptEngine(params, this.getClass().getClassLoader(), this.sandboxClassFilter); this.allow(InterruptTest.class); }
NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); ScriptEngine scriptEngine = factory.getScriptEngine( new String[] { "--no-java" }, //a quick way to disable direct access to java API null, //a ClassLoader, let's just ignore it new ClassFilter() { //this one simply forbids use of any java classes, including reflection @Override public boolean exposeToScripts(String string) { return false; } } );
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); } }
private void initScriptEngine() { LOG.debug("Initializing Nashorn script engine ..."); NashornScriptEngineFactory factory = new NashornScriptEngineFactory(); scriptEngine = factory.getScriptEngine(s -> false); // create engine with class filter exposing no classes expressions = Caffeine.newBuilder() .maximumWeight(MAX_COMPILED_EXPRESSIONS_SCRIPTS_LENGTH) .weigher((Weigher<String, CompiledScript>) (key, value) -> key.length()) .build(((Compilable) this.scriptEngine)::compile); LOG.debug("Initialized Nashorn script engine"); }
public ScriptEngine createEngine(AuthenticationContext authenticationContext) { ScriptEngine engine = factory.getScriptEngine("--no-java"); Bindings bindings = engine.createBindings(); engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE); engine.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE); SelectAcrFromFunction selectAcrFromFunction = new SelectAcrFromFunction(); // todo move to functions registry bindings.put(FrameworkConstants.JSAttributes.JS_FUNC_SELECT_ACR_FROM, (SelectOneFunction) selectAcrFromFunction::evaluate); JsLogger jsLogger = new JsLogger(); bindings.put(FrameworkConstants.JSAttributes.JS_LOG, jsLogger); return engine; }
@Test public void calcSomethingWithJS() throws Exception { ScriptEngine jse = new NashornScriptEngineFactory().getScriptEngine(new ClassFilter_restrictAll()); jse.put("a", 33); jse.put("b", 44); jse.eval("var c = '' + (a + b);"); String c = (String)jse.get("c"); assertEquals("77", c); }
@Test public void createJavaObjectWithJS() throws Exception { try { ScriptEngine jse = new NashornScriptEngineFactory().getScriptEngine(new ClassFilter_allowSomething()); jse.put("obj", new CustomClass()); jse.eval("obj.funcA('text1');"); jse.eval("var id = com.icodici.universa.HashId.createRandom();"); jse.eval("obj.funcB(id);"); } catch (ScriptException e) { e.printStackTrace(); assertTrue(false); } }
@Test public void putJavaObjectIntoJS() throws Exception { try { ScriptEngine jse = new NashornScriptEngineFactory().getScriptEngine(new ClassFilter_restrictAll()); jse.put("obj", new CustomClass()); jse.eval("obj.funcA('text1');"); } catch (ScriptException e) { assertTrue(false); } }
@Test public void createContractWithJS() throws Exception { ScriptEngine jse = new NashornScriptEngineFactory().getScriptEngine(new ClassFilter_allowSomething()); jse.eval("var contract = new com.icodici.universa.contract.Contract();"); jse.eval("contract.getDefinition().getData().put('someKey', 'someValue');"); jse.eval("contract.seal()"); Contract contract = (Contract)jse.get("contract"); System.out.println("contract id: " + contract.getId()); System.out.println("contract someKey: " + contract.getDefinition().getData().getString("someKey")); assertEquals("someValue", contract.getDefinition().getData().getString("someKey")); }
@Test public void implementJavaInterfaceWithJS() throws Exception { ScriptEngine jse = new NashornScriptEngineFactory().getScriptEngine(new ClassFilter_restrictAll()); jse.eval("var customInterface = new Object(); customInterface.funcA = function() {print('custom funcA() hit!');}"); jse.eval("customInterface.funcB = function(prm) {print('custom funcB() hit! prm='+prm); return 'js_'+prm;}"); CustomInterface customInterfaceInstance = ((Invocable)jse).getInterface(jse.get("customInterface"), CustomInterface.class); customInterfaceInstance.funcA(); String res = customInterfaceInstance.funcB("java"); assertEquals("js_java", res); }