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); }
static void Main(string[] args) { jdk.nashorn.api.scripting.NashornScriptEngineFactory fact = new jdk.nashorn.api.scripting.NashornScriptEngineFactory(); jdk.nashorn.api.scripting.NashornScriptEngine nashornengine = fact.getScriptEngine() as jdk.nashorn.api.scripting.NashornScriptEngine; nashornengine.eval("var x = 1/3;"); object result = nashornengine.get("x"); Console.WriteLine("{0}", result); }
public NashornRuntimeFactory() { this.scriptEngineFactory = new NashornScriptEngineFactory(); }
@Inject public ConfigEvalEngine() { this.jsonMapper = new ObjectMapper(); this.jsEngineFactory = new NashornScriptEngineFactory(); }
public NashornEngine(Vertx vertx) { this.vertx = vertx; // enable ES6 features if (System.getProperty("nashorn.args") == null) { System.setProperty("nashorn.args", "--language=es6"); } // create the factory this.factory = new NashornScriptEngineFactory(); }
ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine((className) -> { if ("java.lang.Thread".equals(className)) return false; if ("java.lang.Runnable".equals(className)) return false; if ("java.util.Timer".equals(className)) return false; if (className.startsWith("java.util.concurrency")) return false; if (className.startsWith("javafx")) return false; if (className.startsWith("javax.swing")) return false; if (className.startsWith("java.awt")) return false; return true; });
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);
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"); }
@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); }