3

I am trying to initialize Oracle's javascript nashorn engine directy from jdk.nashorn.* namespace. (nashorn library is a beta version of 2013 Jan).

There is a web sample which calles Nashorn engine instance of engine, using javax.script.ScriptEngineManager utility class.

var engine = ScriptEngineManager.getEngineByName(*)

However, I like to keep away from ScriptEngineManager, so i need to call engine directly in the same way Rhino can.

Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();

How can I create nashorn engine instance directly?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • yeah, we can see the 'NashornScriptEngine' in the javadoc but looks like you can only get the engine by running your Eval class with ">java -cp nashorn.jar:. EvalFile .js – the_marcelo_r Mar 24 '13 at 04:06
  • Thank you for comment. theMarceloR. I am trying to use nashorn from .net. The API ScriptEngineManager.getEngineByName('nashorn') returning null in .NET. It may be jvm and IKVM issue, since nashorn uses latest JVM API dynamicinvoke which is different from what .NET has. – ultra mother hacker Mar 26 '13 at 15:59
  • In Java, if "ScriptEngineManager.getEngineByName('nashorn')" returns null, nashorn.jar is not in the classpath. Dunno what do you need to do in .NET to solve this. – the_marcelo_r Mar 27 '13 at 21:48

2 Answers2

3

javax script engine by type application/javascript Hashorn, get back a script engine and tell it to do stuff, it also provides invokable and compilable interfaces.

Yout might be interested to read this : How can I start coding with Oracle's Nashorn JS Engine and when will it replace Rhino in the OpenJDK?

Example usage:

import javax.*; //lib imports
// we should use the javax.script API for Nahsorn
ScriptEngineManager m = new ScripteEngineManager(); 
ScriptEngine e = m.getEngineByname("nashorn");
try {
   e.eval("print('hello nashorn !')");
} catch(Exception e) {
  // using jdk lower then version 8 maybe ?
}
Community
  • 1
  • 1
Peter
  • 689
  • 2
  • 8
  • 20
  • Thank you for the useful information, Peter. it will study jrunscript source code. – ultra mother hacker Mar 26 '13 at 16:06
  • The best way to learn it really is to read the source code of Nashorn which is mainly in C, and then do your stuff. – Peter Mar 26 '13 at 21:07
  • 2
    @Peter Nashorn is 100% Java-based implementation http://parleys.com/play/5148922b0364bc17fc56c90e/chapter50/about Best way to lean it would be learn jsr223 apis http://download.oracle.com/otn-pub/jcp/java_scripting-1.0-fr-eval-oth-JSpec/java_scripting-1_0-fr-spec.pdf – Vik Gamov Jul 16 '13 at 06:00
1

I found the way to init engine directly using .NET without "

    "javax.script.ScriptEngineManager"

Environment: IKVM.NET Version 8 + .NET Framework 4.52

    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);
    }

This allows me directly interact with nashorn context methods mot more directly.

 compile()
 getFactory()
 invokeMethod()
 invokeFunction()