NASHORN-JS engine

Source: Internet
Author: User

Nashorn is the operating environment for efficient and dynamic execution of JS code in Java.

New Scriptenginemanager (). Getenginebyname ("Nashorn");        Engine.eval (//  execute JS from source        new File ("Resources/scripts.js");        Engine.eval (new//  Execute JS from file

Call JS

First, save the JS code that needs to be called as Scripts.js:

function Add (A, b) {    return a + b;} var function (name) {    print ("JS:" + name);     return "Greeting from JS";} var function (obj) {    print ("JS Class:" + Object.prototype.toString.call (obj));}

Called in Java:

ScriptEngine engine =NewScriptenginemanager (). Getenginebyname ("Nashorn"); Engine.eval ("Print (' Hello ');");//Execute JS from sourceFile f =NewFile ("Resources/scripts.js"); Engine.eval (NewFileReader (f));//Execute JS from file        invocable invocable = (invocable) engine; Object sum=invocable.invokefunction("Add", 1, 2);        SYSTEM.OUT.PRINTLN (sum); Invocable.invokefunction ("F2",NewDate ()); Invocable.invokefunction ("F2", Localdatetime.now ());

Because the code in JS is the original ecological call in the JVM, the type is not lost.

Calling Java

The Java method must be public . Otherwise, it cannot be found at the time of the call :

     Public Static string Greeting (string name) {        System.out.println ("from Java" + name);         return "From Java";    }      Public Static void getcls (Object obj) {        System.out.println (Obj.getclass ());    }

ScriptEngine engine = new Scriptenginemanager (). Getenginebyname ("Nashorn");
File F = new file ("Resources/scripts.js");
Engine.eval (new FileReader (f)); Execute JS from File

JS Code:

var cls = Java.type ("Nashorn". Nashorntest ");p rint (cls.greeting (' Zhijie '));

The original JS type can be converted into the appropriate Java wrapper class. A native JS object is represented as an internal adaptation class.

  

CLS.GETCLS (123);//class Java.lang.IntegerCls.getcls (49.99);//class Java.lang.DoubleCls.getcls (true);//class Java.lang.BooleanCls.getcls ("Hi There")//class Java.lang.StringCls.getcls (NewNumber (23));//class Jdk.nashorn.internal.objects.NativeNumberCls.getcls (NewDate ());//class Jdk.nashorn.internal.objects.NativeDateCls.getcls (NewRegExp ());//class Jdk.nashorn.internal.objects.NativeRegExpcls.getcls ({foo:' Bar '});//class Jdk.nashorn.internal.scripts.JO4

Scriptobjectmirror

Jdk.nashorn.api.scripting.ScriptObjectMirror is the mapping of JS objects in Java. This class implements the map interface.

 public  static  void   F ( scriptobjectmirror  Mirror) {System.out.println (mirr Or.  GetClassName  ()  + ":" + arrays.tostring (mirror.     Getownkeys  ( true   public  static  void   Callmethodofjsobject (scriptobjectmirror person) {System.out.println ( span> "Full Name was:" + person.  Callmember     ("Getfullname" 
CLS.F ({A: ' A ', B: ' B '}); function Person (FirstName, lastName) {    this. firstName = firstName;      this. lastName = lastName;      This function () {        returnthis. lastName;    }} var New Person ("Zhijie", "Liu"); Cls.callmethodofjsobject (Person1);

Nashorn expanded the ECMAScript 5:

  • Typed Arrays

  • Collections and for each

  • Lambda Expressions and Streams

  • Extending classes

  • Parameter overloading

  • Java Beans

  • Function literals

  • Binding Properties

  • Trimming strings

  • Whereis

  • Import Scopes

  • Convert arrays

  • Calling Super

  • Loading scripts

Can read further http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/

NASHORN-JS engine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.