java类javax.script.ScriptEngineManager的实例源码

InvocableTest.java 文件源码 项目:openjdk-jdk10 阅读 20 收藏 0 点赞 0 评论 0
@Test
/**
 * Check that getInterface on null 'thiz' results in
 * IllegalArgumentException.
 */
public void getInterfaceNullThizTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        ((Invocable) e).getInterface(null, Runnable.class);
        fail("should have thrown IllegalArgumentException");
    } catch (final Exception exp) {
        if (!(exp instanceof IllegalArgumentException)) {
            exp.printStackTrace();
            fail(exp.getMessage());
        }
    }
}
Main.java 文件源码 项目:mo-vnfcp 阅读 19 收藏 0 点赞 0 评论 0
public static void testScriptEngine() throws Exception {
    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine js = sem.getEngineByName("JavaScript");
    js.eval("blubb = 1\n" +
            "muh = blubb + 2\n" +
            "// just a comment...\n" +
            "crap = \"asd\"");
    System.out.println(js.get("muh").getClass());

    System.out.println("Testing ScriptEngine Performance...");
    long start = System.currentTimeMillis();
    Config c = Config.getInstance(new FileInputStream("res/customConfig.js"));
    double[] d = new double[]{1.0, 7.5, 1.3+2.5+6.3+3.1, 6.0};;
    for (int i = 0; i < 100000; i++) {
        //c.pReassignVnf(i);
        //d = new double[]{d[0] + 1.0, d[1] - 0.5, d[0] + d[1], d[3] * 1.1};
        c.objectiveVector(new double[Solution.Vals.values().length]);
    }
    System.out.println(Arrays.toString(d));
    long dur = System.currentTimeMillis() - start;
    System.out.println("Duration: " + dur + "ms");
    System.out.println(c.topologyFile.toAbsolutePath().toString());
}
ScriptObjectMirrorTest.java 文件源码 项目:openjdk-jdk10 阅读 31 收藏 0 点赞 0 评论 0
@Test
public void mapScriptObjectMirrorCallsiteTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine engine = m.getEngineByName("nashorn");
    final String TEST_SCRIPT = "typeof obj.foo";

    final Bindings global = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    engine.eval("var obj = java.util.Collections.emptyMap()");
    // this will drive callsite "obj.foo" of TEST_SCRIPT
    // to use "obj instanceof Map" as it's guard
    engine.eval(TEST_SCRIPT, global);
    // redefine 'obj' to be a script object
    engine.eval("obj = {}");

    final Bindings newGlobal = engine.createBindings();
    // transfer 'obj' from default global to new global
    // new global will get a ScriptObjectMirror wrapping 'obj'
    newGlobal.put("obj", global.get("obj"));

    // Every ScriptObjectMirror is a Map! If callsite "obj.foo"
    // does not see the new 'obj' is a ScriptObjectMirror, it'll
    // continue to use Map's get("obj.foo") instead of ScriptObjectMirror's
    // getMember("obj.foo") - thereby getting null instead of undefined
    assertEquals("undefined", engine.eval(TEST_SCRIPT, newGlobal));
}
TrustedScriptEngineTest.java 文件源码 项目:openjdk-jdk10 阅读 28 收藏 0 点赞 0 评论 0
@Test
public void factoryOptionsTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
        if (fac instanceof NashornScriptEngineFactory) {
            final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
            // specify --no-syntax-extensions flag
            final String[] options = new String[] { "--no-syntax-extensions" };
            final ScriptEngine e = nfac.getScriptEngine(options);
            try {
                // try nashorn specific extension
                e.eval("var f = funtion(x) 2*x;");
                fail("should have thrown exception!");
            } catch (final Exception ex) {
                //empty
            }
            return;
        }
    }

    fail("Cannot find nashorn factory!");
}
ScriptEngineTest.java 文件源码 项目:openjdk-jdk10 阅读 25 收藏 0 点赞 0 评论 0
@Test
public void windowItemTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Window window = new Window();

    try {
        e.put("window", window);
        final String item1 = (String)e.eval("window.item(65535)");
        assertEquals(item1, "ffff");
        final String item2 = (String)e.eval("window.item(255)");
        assertEquals(item2, "ff");
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
Java8Tester.java 文件源码 项目:mynlp 阅读 19 收藏 0 点赞 0 评论 0
public static void main(String args[]) throws Exception {
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");
    String name = "Mahesh";

    System.out.println(nashorn.getClass());

    Integer result = null;
    try {
        nashorn.eval("print('" + name + "')");
        result = (Integer) nashorn.eval("10 + 2");
    } catch (ScriptException e) {
        System.out.println("Error executing script: " + e.getMessage());
    }

    jdk.nashorn.api.scripting.NashornScriptEngine x = (jdk.nashorn.api.scripting.NashornScriptEngine)
            nashorn;

    CompiledScript v = x.compile("1+2");

    System.out.println(v.eval());


}
V8ScriptingEngineTest.java 文件源码 项目:v8-adapter 阅读 15 收藏 0 点赞 0 评论 0
@Test
public void shouldHaveACoolAPI() throws Exception {

    // Create the engine.
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("v8");
    Assert.assertNotNull(engine);

    // Perform basic evaluation.
    Assert.assertEquals(34, engine.eval("30 + 4"));

    // Perform advanced evaluation.
    Assert.assertEquals("l33t", engine.eval("\"l\" + (33).toString() + \"t\""));

    // Perform scope put-get.
    engine.put("myVar", 42);
    Assert.assertEquals(42, engine.get("myVar"));
}
TestMain.java 文件源码 项目:slardar 阅读 31 收藏 0 点赞 0 评论 0
public static void main(String[] args) {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine engine = engineManager.getEngineByName("javascript");
        try {
//            UserVO user = new UserVO();
//            user.setId(1000);
//            user.setUsername("xingtianyu");
//            Map<String,Object> usermap = new HashMap<>();
//            usermap.put("id",user.getId());
//            usermap.put("username",user.getUsername());
            JSContext context = new JSContext();
            engine.put(JSContext.CONTEXT,context.getCtx());
            engine.eval(new FileReader("/home/code4j/IDEAWorkspace/myutils/myutils-slardar/src/main/resources/mapper/usermapper.js"));
            Invocable func = (Invocable)engine;
//            Map<String,Object> resultMap = (Map<String, Object>) func.invokeFunction("findUserByCondition",usermap);
//            Map<String,Object> paramMap = (Map<String, Object>) resultMap.get("param");
//            System.out.println(resultMap.get("sql"));
//            System.out.println(paramMap.get("1"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
TrustedScriptEngineTest.java 文件源码 项目:openjdk-jdk10 阅读 23 收藏 0 点赞 0 评论 0
@Test
/**
 * Test that we can use same script name in repeated evals with --loader-per-compile=false
 * We used to get "class redefinition error" as name was derived from script name.
 */
public void noLoaderPerCompilerWithSameNameTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
        if (fac instanceof NashornScriptEngineFactory) {
            final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
            final String[] options = new String[] { "--loader-per-compile=false" };
            final ScriptEngine e = nfac.getScriptEngine(options);
            e.put(ScriptEngine.FILENAME, "test.js");
            try {
                e.eval("2 + 3");
                e.eval("4 + 4");
            } catch (final ScriptException se) {
                se.printStackTrace();
                fail(se.getMessage());
            }
            return;
        }
    }
    fail("Cannot find nashorn factory!");
}
ScriptRouter.java 文件源码 项目:github-test 阅读 28 收藏 0 点赞 0 评论 0
public ScriptRouter(URL url) {
    this.url = url;
    String type = url.getParameter(Constants.TYPE_KEY);
    this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
    String rule = url.getParameterAndDecoded(Constants.RULE_KEY);
    if (type == null || type.length() == 0) {
        type = Constants.DEFAULT_SCRIPT_TYPE_KEY;
    }
    if (rule == null || rule.length() == 0) {
        throw new IllegalStateException(new IllegalStateException("route rule can not be empty. rule:" + rule));
    }
    ScriptEngine engine = engines.get(type);
    if (engine == null) {
        engine = new ScriptEngineManager().getEngineByName(type);
        if (engine == null) {
            throw new IllegalStateException(new IllegalStateException("Unsupported route rule type: " + type + ", rule: " + rule));
        }
        engines.put(type, engine);
    }
    this.engine = engine;
    this.rule = rule;
}


问题


面经


文章

微信
公众号

扫码关注公众号