Sobes.tech
Middle

What is Nashorn?

sobes.tech AI

Answer from AI

Nashorn is a JavaScript engine implemented in Java, included in the JRE from version 8 to 14. It allows executing JavaScript code directly within the JVM. Its main advantage is providing high-performance interaction between JavaScript and Java code, enabling calling Java classes and methods from JavaScript, and vice versa.

Nashorn replaced the outdated Rhino engine. Although Nashorn was removed from Java SE 15, understanding its principles can be useful when working with older or legacy systems.

Main features of Nashorn:

  • Executing JavaScript: Allows running standard JavaScript (ECMAScript 5.1, along with some ES6 extensions).
  • Interaction with Java: Provides access to Java classes, objects, and methods from JavaScript code and vice versa.
  • Embedding: Nashorn can be easily embedded into Java applications for dynamic script execution.
  • High performance: The engine is optimized for fast operation thanks to the HotSpot JVM.

Example of using Nashorn from Java:

import javax.script.*;

public class NashornExample {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("nashorn"); // Get engine by name

        // Execute simple JavaScript code
        engine.eval("print('Hello from Nashorn!');");

        // Pass a variable from Java to JavaScript
        engine.put("javaVariable", "Hello from Java!");
        engine.eval("print(javaVariable);");

        // Call Java method from JavaScript
        engine.eval("var result = java.lang.Math.sqrt(16);");
        System.out.println("Result from JavaScript: " + engine.get("result"));

        // Call JavaScript function from Java
        engine.eval("function greet(name) { return 'Hello, ' + name + '!'; }");
        Invocable invocable = (Invocable) engine;
        Object greeting = invocable.invokeFunction("greet", "World");
        System.out.println(greeting);
    }
}

Example of using Java objects from JavaScript:

// Create a Java String object
var javaString = new java.lang.String("This is a Java string");
print(javaString);

// Call a method of the Java object
print(javaString.length());

// Create and populate a Java collection
var list = new java.util.ArrayList();
list.add("Element 1");
list.add("Element 2");
print(list.size());
print(list.get(0));

Main components involved in Nashorn:

  • ScriptEngineManager and ScriptEngine: Standard Java APIs for working with script engines.
  • Internal Nashorn components: Parser, compiler, and interpreter, which convert JavaScript code into JVM bytecode or execute it directly.

Nashorn's performance is achieved through:

  • JIT compilation: Nashorn uses the HotSpot Just-In-Time compiler to convert frequently used JavaScript code into native machine code.
  • Optimization of property access: Optimizations are used for fast access to JavaScript object properties.
  • Built-in JIT compilers.

Despite its capabilities, it is important to remember that Nashorn was removed from Java SE, and for new projects, alternative solutions such as GraalVM with its Polyglot API are recommended, which offer broader capabilities for executing various languages, including JavaScript (Graal.js engine).