Implementing a module template using Nashorn

I studied Nashorn in Java 8, and I was impressed with its features and the strength that it gives the developer.

In the interest of organizing JavaScript code, I thought that I would give a chance to show a module template.

this._sys = 
(function(){
    function hello() {
        print('hello world!');
    }

    return {
        hello: hello
    };

})();

_sys.hello();

Save the js code in main.js. The above code works flawlessly when I use jjs. But when I tried to run the same code through Groovy / Java, it failed. Can anyone know why this fails?

Test in Groovy:

class Test {
    public static void main(String[] args) {
        def engine = new ScriptEngineManager().getEngineByName("nashorn")
        engine.eval(new FileReader("E:/main.js"));
        println engine.context.getAttribute("_sys")
        def invocable = engine as Invocable

        def x = invocable.invokeFunction("this._sys.hello",null)
        println x
    }
}

Mistake:

Exception in thread "main" java.lang.NoSuchMethodException: No such function this._sys.hello
at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:184)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:508)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:229)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:189)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
at Test.main(Test.groovy:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
+4
source share
1 answer

- , sys JavaScript. hello , , Invocable.invokeMethod, , context.getAttribute("_sys"), .

:

class Test {
    public static void main(String[] args) {
        def engine = new ScriptEngineManager().getEngineByName("nashorn")
        engine.eval(new FileReader("E:/main.js"));
        def sys = engine.context.getAttribute("_sys")
        println sys
        def invocable = engine as Invocable

        def x = invocable.invokeMethod(sys, "hello")
        println x
    }
}

mustache.js Oracle: Oracle Nashorn: JavaScript- JVM

+2

Source: https://habr.com/ru/post/1607893/


All Articles