Re: Nashorn fails to evaluate compiled script

Viktor Gamov viktor.gamov at gmail.com
Wed Sep 18 06:01:31 PDT 2013


Sundar,
Thanks for update. I pulled recent changes, built nashorn.jar once again and replaced nashorn.jar  from jdk8 installation.
My use case still failing. Could you check on your side?

Thanks
-- 
With Best Regards, 
Vik Gamov

On September 18, 2013 at 1:07:10 AM, Viktor Gamov (viktor.gamov at gmail.com) wrote:

Hello nashorn team,

Another question from my side ;-).

I want to test 2 versions of same javascript file - compiled and not.

var json = {
    engine: {
        name: String(factory.engineName),
        version: String(factory.engineVersion),
        threading: String(factory.getParameter("THREADING"))
    },
    language: {
        name: String(factory.languageName),
        version: String(factory.languageVersion)
    }
};
print(JSON.stringify(json));

Here are my test cases:

@Test
public void contexts() throws ScriptException, FileNotFoundException {
    System.out.println("Passing JavaObject via ScriptContext to the engine");
    File f = new File(this.getClass().getClassLoader().getResource("environment.js").getFile());
    ScriptContext scriptContext = new SimpleScriptContext();
    int scope = ScriptContext.ENGINE_SCOPE;
    scriptContext.setAttribute("factory", engine.getFactory(), scope);
    engine.eval(new FileReader(f), scriptContext);
}

This test method prints json as expected

Passing JavaObject via ScriptContext to the engine
{"engine":{"name":"Oracle Nashorn","version":"1.8.0","threading":"null"},"language":{"name":"ECMAScript","version":"ECMA - 262 Edition 5.1"},"script":{}}

But same script fails with compiled version.

Test method:

@Test
public void contextWithCompiled() throws ScriptException, FileNotFoundException {
    System.out.println("Passing JavaObject via ScriptContext to CompiledScript");
    File f = new File(this.getClass().getClassLoader().getResource("environment.js").getFile());
    Compilable compilable = (Compilable) engine;
    CompiledScript compiledScript = compilable.compile(new FileReader(f));
    ScriptContext scriptContext = new SimpleScriptContext();
    int scope = ScriptContext.ENGINE_SCOPE;
    scriptContext.setAttribute("factory", engine.getFactory(), scope);
    compiledScript.eval(scriptContext);
}


Output:

Passing JavaObject via ScriptContext to CompiledScript

javax.script.ScriptException: [object Error] in <eval> at line number 3
	at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:553)
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:537)
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:517)
	at jdk.nashorn.api.scripting.NashornScriptEngine.access$100(NashornScriptEngine.java:77)
	at jdk.nashorn.api.scripting.NashornScriptEngine$4.eval(NashornScriptEngine.java:568)
	at com.faratasystems.javaone.NashornTest.contextWithCompiled(NashornTest.java:83)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: <eval>:3 [object Error]
	at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
	at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
	at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
	at jdk.nashorn.api.scripting.NashornScriptEngine.__noSuchProperty__(NashornScriptEngine.java:286)
	at jdk.nashorn.internal.scripts.Script$engine._L35(nashorn:engine/resources/engine.js:37)
	at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:3)
	at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:527)
	at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:204)
	at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:395)
	at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:535)
	... 30 more


Process finished with exit code 255

java -version
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b106)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b48, mixed mode)

nashorn.jar built based on Revision: 553

Apparently, I missing something again :-) . Please advise...

p.s. Is there any doc about what global objects/methods (like, print, load, JSON, etc) available for js scripts in Nashorn/jjs repl? 

Thanks

-- 
With Best Regards, 
Vik Gamov


More information about the nashorn-dev mailing list