Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e256e8e

Browse files
blakehawkinsshekhargulati
authored andcommitted
reflow paragraphs in section 10 (#14)
1 parent 88cf513 commit e256e8e

File tree

1 file changed

+67
-21
lines changed

1 file changed

+67
-21
lines changed

‎10-nashorn.md‎

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@ Nashorn: Run JavaScript on the JVM
33

44
![Nashorn](https://upload.wikimedia.org/wikipedia/commons/7/7a/Dortmunder_Nashorn_-_Hell_wieherte_der_Hippogryph.jpg)
55

6-
Nashorn is a high-performance JavaScript runtime written in Java for the JVM. It allows developers to embed JavaScript code inside their Java applications and even use Java classes and methods from their JavaScript code. You can think it as an alternative to Google's V8 JavaScript engine. It is a successor to Rhino JavaScript runtime which came bundled with earlier JDK versions. Nashorn is written from scratch using new language features like JSR 292(Supporting Dynamically Typed Languages) and `invokedynamic`.
6+
Nashorn is a high-performance JavaScript runtime written in Java for the JVM. It
7+
allows developers to embed JavaScript code inside their Java applications and
8+
even use Java classes and methods from their JavaScript code. You can think it
9+
as an alternative to Google's V8 JavaScript engine. It is a successor to Rhino
10+
JavaScript runtime which came bundled with earlier JDK versions. Nashorn is
11+
written from scratch using new language features like JSR 292(Supporting
12+
Dynamically Typed Languages) and `invokedynamic`.
713

814
From the Nashorn documentation:
9-
> Nashorn uses invokedynamic to implement all of its invocations. If an invocation has a Java object receiver, Nashorn attempts to bind the call to an appropriate Java method instead of a JavaScript function. Nashorn has full discretion about how it resolves methods. As an example, if it can't find a field in the receiver, it looks for an equivalent Java Bean method. The result is completely transparent for calls from JavaScript to Java.
1015

11-
Currently, Nashorn supports [ECMAScript 5.1 specification](http://www.ecma-international.org/ecma-262/5.1/) and work is in progress to support [ECMAScript 6](http://www.ecma-international.org/ecma-262/6.0/) as well. Few ECMAScript 6 features like `let` and `const` are available in latest JDK 8 updates(40 and above) and we will cover them later in this chapter.
16+
> Nashorn uses invokedynamic to implement all of its invocations. If an
17+
> invocation has a Java object receiver, Nashorn attempts to bind the call to an
18+
> appropriate Java method instead of a JavaScript function. Nashorn has full
19+
> discretion about how it resolves methods. As an example, if it can't find a
20+
> field in the receiver, it looks for an equivalent Java Bean method. The result
21+
> is completely transparent for calls from JavaScript to Java.
22+
23+
Currently, Nashorn supports [ECMAScript 5.1
24+
specification](http://www.ecma-international.org/ecma-262/5.1/) and work is in
25+
progress to support [ECMAScript
26+
6](http://www.ecma-international.org/ecma-262/6.0/) as well. Few ECMAScript 6
27+
features like `let` and `const` are available in latest JDK 8 updates(40 and
28+
above) and we will cover them later in this chapter.
1229

1330
In this chapter, we will cover the following:
1431

@@ -23,23 +40,30 @@ In this chapter, we will cover the following:
2340

2441
## Working with Nashorn command-line
2542

26-
JDK 8 comes bundled with two command-line tools that can be used to work with Nashorn engine. These two command-line tools are `jrunscript` and `jjs`. `jjs` is recommended to be used when working with Nashorn so we will only discuss it. To use `jjs`, you have to add `jjs` to the path. On *nix machines, you can do that adding a symbolic link as shown below.
43+
JDK 8 comes bundled with two command-line tools that can be used to work with
44+
Nashorn engine. These two command-line tools are `jrunscript` and `jjs`. `jjs`
45+
is recommended to be used when working with Nashorn so we will only discuss it.
46+
To use `jjs`, you have to add `jjs` to the path. On *nix machines, you can do
47+
that adding a symbolic link as shown below.
2748

2849
```bash
2950
$ cd /usr/bin
3051
$ ln -s $JAVA_HOME/bin/jjs jjs
3152
```
53+
3254
Windows users can add `$JAVA_HOME/bin` to the path for easy access.
3355

34-
Once you have set the symbolic link you can access `jjs` from your terminal. To check version of `jjs`, run the following command.
56+
Once you have set the symbolic link you can access `jjs` from your terminal. To
57+
check version of `jjs`, run the following command.
3558

3659
```bash
3760
$ jjs -v
3861
nashorn 1.8.0_60
3962
jjs>
4063
```
4164

42-
It will render the version and then show `jjs>` prompt. You can view the full version of `jjs` by using `jjs -fv` command.
65+
It will render the version and then show `jjs>` prompt. You can view the full
66+
version of `jjs` by using `jjs -fv` command.
4367

4468
To quit the `jjs` shell, you can use `Ctrl-C`.
4569

@@ -65,7 +89,8 @@ jjs> add(5,10)
6589

6690
## Accessing Java classes and methods
6791

68-
It is very easy to access Java classes from within Nashorn. Assuming you are inside the `jjs` shell, you can create an instance of HashMap as shown below.
92+
It is very easy to access Java classes from within Nashorn. Assuming you are
93+
inside the `jjs` shell, you can create an instance of HashMap as shown below.
6994

7095
```bash
7196
jjs> var HashMap = Java.type("java.util.HashMap")
@@ -78,23 +103,29 @@ jjs> userAndAge.get("shekhar")
78103
32
79104
```
80105

81-
In the code shown above we have used `Java` global object to create HashMap object. `Java` global object has `type` method that takes a string with the fully qualified Java class name, and returns the corresponding `JavaClass` function object.
106+
In the code shown above we have used `Java` global object to create HashMap
107+
object. `Java` global object has `type` method that takes a string with the
108+
fully qualified Java class name, and returns the corresponding `JavaClass`
109+
function object.
82110

83111
```bash
84112
jjs> HashMap
85113
[JavaClass java.util.HashMap]
86114
```
87115

88-
The `var userAndAge = new HashMap()` is used to instantiate `java.util.HashMap` class using the `new` keyword.
116+
The `var userAndAge = new HashMap()` is used to instantiate `java.util.HashMap`
117+
class using the `new` keyword.
89118

90-
You can access values by either calling the `get` method or using the `[]` notation as shown below.
119+
You can access values by either calling the `get` method or using the `[]`
120+
notation as shown below.
91121

92122
```bash
93123
jjs> userAndAge["shekhar"]
94124
32
95125
```
96126

97-
Similarly, you can work with other Java collections. To use an `ArrayList` you will write code as shown below.
127+
Similarly, you can work with other Java collections. To use an `ArrayList` you
128+
will write code as shown below.
98129

99130
```bash
100131
jjs> var List = Java.type("java.util.ArrayList")
@@ -113,7 +144,8 @@ rahul
113144

114145
### Accessing static methods
115146

116-
To access static methods you have to first get the Java type using `Java.type` method and then calling method on `JavaClass` function object.
147+
To access static methods you have to first get the Java type using `Java.type`
148+
method and then calling method on `JavaClass` function object.
117149

118150
```bash
119151
jjs> var UUID = Java.type("java.util.UUID")
@@ -135,7 +167,10 @@ jjs>
135167

136168
## Using external JavaScript libraries
137169

138-
Let's suppose we want to use an external JavaScript library in our JavaScript code. Nashorn comes up with a built-in function -- `load` that loads and evaluates a script from a path, URL, or script object. To use `lodash` library we can write code as shown below.
170+
Let's suppose we want to use an external JavaScript library in our JavaScript
171+
code. Nashorn comes up with a built-in function -- `load` that loads and
172+
evaluates a script from a path, URL, or script object. To use `lodash` library
173+
we can write code as shown below.
139174

140175
```
141176
jjs> load("https://raw.github.com/lodash/lodash/3.10.1/lodash.js")
@@ -146,7 +181,9 @@ jjs> _.map([1, 2, 3], function(n) { return n * 3; });
146181

147182
## Writing scripts
148183

149-
You can use Nashorn extensions that enable users to write scripts that can use Unix shell scripting features. To enable shell scripting features, you have to start `jjs` with `-scripting` option as shown below.
184+
You can use Nashorn extensions that enable users to write scripts that can use
185+
Unix shell scripting features. To enable shell scripting features, you have to
186+
start `jjs` with `-scripting` option as shown below.
150187

151188
```bash
152189
jjs -scripting
@@ -155,7 +192,9 @@ jjs>
155192

156193
Now you have access to Nashorn shell scripting global objects.
157194

158-
**$ARG:** This global object can be used to access the arguments passed to the script
195+
**$ARG:** This global object can be used to access the arguments passed to the
196+
script
197+
159198
```
160199
$ jjs -scripting -- hello hey
161200
jjs>
@@ -182,7 +221,9 @@ jjs> $EXEC("pwd")
182221

183222
### Writing executable scripts
184223

185-
You can use shebang(#!) at the beginning of the script to make a script file run as shell executable. Let's write a simple script that reads content of a file. We will use Java's `Files` and `Paths` API.
224+
You can use shebang(#!) at the beginning of the script to make a script file run
225+
as shell executable. Let's write a simple script that reads content of a file.
226+
We will use Java's `Files` and `Paths` API.
186227

187228
```javascript
188229
#!/usr/bin/jjs
@@ -201,7 +242,9 @@ $ jjs ch10/lines.js -- README.md
201242

202243
## Using Nashorn from Java code
203244

204-
To use Nashorn from inside Java code, you have to create an instance of ScriptEngine from `ScriptEngineManager` as shown below. Once you have `ScriptEngine` you can evaluate expressions.
245+
To use Nashorn from inside Java code, you have to create an instance of
246+
ScriptEngine from `ScriptEngineManager` as shown below. Once you have
247+
`ScriptEngine` you can evaluate expressions.
205248

206249
```java
207250
import javax.script.ScriptEngine;
@@ -219,7 +262,6 @@ public class NashornExample1 {
219262
}
220263
```
221264

222-
223265
Using bindings
224266

225267
```java
@@ -247,7 +289,9 @@ public class NashornExample2 {
247289

248290
## Using Java 8 features like Streams and Lambdas inside JavaScript code
249291

250-
Java 8 supports lambdas and many API in JDK make use of them. Every collection in Java has `forEach` method that accepts a consumer. Consumer is an interface with one method. In Java, you can write following:
292+
Java 8 supports lambdas and many API in JDK make use of them. Every collection
293+
in Java has `forEach` method that accepts a consumer. Consumer is an interface
294+
with one method. In Java, you can write following:
251295

252296
```java
253297
Arrays.asList("shekhar","rahul","sameer").forEach(name -> System.out.println(name));
@@ -257,7 +301,8 @@ Arrays.asList("shekhar","rahul","sameer").forEach(name -> System.out.println(nam
257301
// sameer
258302
```
259303

260-
In Nashorn, you can use them same API but you will pass JavaScript function instead as shown below.
304+
In Nashorn, you can use them same API but you will pass JavaScript function
305+
instead as shown below.
261306

262307
```javascript
263308
jjs> var Arrays = Java.type("java.util.Arrays")
@@ -283,7 +328,8 @@ sameer
283328

284329
## Turning off Java language access
285330

286-
In case you need to disallow Java usage, you can very easily turn off by passing `--no-java` option to `jjs` as shown below.
331+
In case you need to disallow Java usage, you can very easily turn off by passing
332+
`--no-java` option to `jjs` as shown below.
287333

288334
```
289335
→ jjs --no-java

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /