File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed
Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 11var Nashorn2 = Java . type ( 'com.winterbe.java8.Nashorn2' ) ;
22var result = Nashorn2 . fun ( 'John Doe' ) ;
3- print ( '\n' + result ) ;
3+ print ( '\n' + result ) ;
4+
5+ Nashorn2 . fun2 ( new Date ( ) ) ;
6+ Nashorn2 . fun2 ( new RegExp ( ) ) ;
7+ Nashorn2 . fun2 ( { foo : 'bar' } ) ;
8+
9+
10+ print ( 'passing object hash:' ) ;
11+ Nashorn2 . fun3 ( {
12+ foo : 'bar' ,
13+ bar : 'foo'
14+ } ) ;
15+
16+
17+ print ( 'passing custom person object:' ) ;
18+
19+ function Person ( firstName , lastName ) {
20+ this . firstName = firstName ;
21+ this . lastName = lastName ;
22+ this . getFullName = function ( ) {
23+ return this . firstName + " " + this . lastName ;
24+ }
25+ }
26+
27+ var person1 = new Person ( "Peter" , "Parker" ) ;
28+ Nashorn2 . fun3 ( person1 ) ;
29+ Nashorn2 . fun4 ( person1 ) ;
Original file line number Diff line number Diff line change 11package com .winterbe .java8 ;
22
3+ import jdk .nashorn .api .scripting .ScriptObjectMirror ;
4+
35import javax .script .ScriptEngine ;
46import javax .script .ScriptEngineManager ;
57import java .io .FileReader ;
8+ import java .util .Arrays ;
69
710/**
811 * Calling java methods from javascript with nashorn.
@@ -16,6 +19,18 @@ public static String fun(String name) {
1619 return "greetings from java" ;
1720 }
1821
22+ public static void fun2 (Object object ) {
23+ System .out .println (object .getClass ());
24+ }
25+
26+ public static void fun3 (ScriptObjectMirror mirror ) {
27+ System .out .println (mirror .getClassName () + ": " + Arrays .toString (mirror .getOwnKeys (true )));
28+ }
29+
30+ public static void fun4 (ScriptObjectMirror person ) {
31+ System .out .println ("Full Name is: " + person .callMember ("getFullName" ));
32+ }
33+
1934 public static void main (String [] args ) throws Exception {
2035 ScriptEngine engine = new ScriptEngineManager ().getEngineByName ("nashorn" );
2136 engine .eval (new FileReader ("res/nashorn2.js" ));
You can’t perform that action at this time.
0 commit comments