Skip to content

Commit 9bc8ff3

Browse files
committed
Clean up user readme.
Fill in maven plugin versions. Bump GraalJS version to 24.1.0. Allow the example to be run without arguments. Remove trailing whitespace. Use maven-archetype-quickstart version 1.5. Fix maven project name. Flip steps 3 and 4 (specify compile dependencies first). Explain how to set graaljs.version property in pom.xml. Move explanation of the example app up to the code snippet (step 2).
1 parent 94fc310 commit 9bc8ff3

File tree

3 files changed

+83
-75
lines changed

3 files changed

+83
-75
lines changed

docs/user/JavaInteroperability.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ permalink: /reference-manual/js/JavaInteroperability/
77

88
# Java Interoperability
99

10-
This documentation shows you how to enable interoperability with Java and possible JavaScript-to-Java embedding scenarios.
10+
This documentation shows you how to enable interoperability with Java and possible JavaScript-to-Java embedding scenarios.
1111

1212
## Enabling Java Interoperability
1313

14-
As of GraalVM for JDK 21, all necessary artifacts can be downloaded directly from Maven Central.
14+
As of GraalVM for JDK 21, all necessary artifacts can be downloaded directly from Maven Central.
1515
All artifacts relevant to embedders can be found in the Maven dependency group [`org.graalvm.polyglot`](https://central.sonatype.com/namespace/org.graalvm.polyglot).
1616

1717
To embed JavaScript in a Java application, add the following dependencies to the Maven configuration file:
1818
```xml
19-
<dependency>
20-
<groupId>org.graalvm.polyglot</groupId>
21-
<artifactId>polyglot</artifactId>
22-
<version>${graaljs.version}</version>
19+
<dependency>
20+
<groupId>org.graalvm.polyglot</groupId>
21+
<artifactId>polyglot</artifactId>
22+
<version>${graaljs.version}</version>
2323
</dependency>
24-
<dependency>
25-
<groupId>org.graalvm.polyglot</groupId>
26-
<artifactId>js</artifactId>
27-
<version>${graaljs.version}</version>
24+
<dependency>
25+
<groupId>org.graalvm.polyglot</groupId>
26+
<artifactId>js</artifactId>
27+
<version>${graaljs.version}</version>
2828
<type>pom</type>
2929
</dependency>
3030
```
@@ -372,7 +372,7 @@ More detailed example usages are available in the GraalJS [unit tests](https://g
372372

373373
## Multithreading
374374

375-
GraalJS supports multithreading when used in combination with Java.
375+
GraalJS supports multithreading when used in combination with Java.
376376
More details about the GraalJS multithreading model can be found in the [Multithreading](Multithreading.md) documentation.
377377

378378
## Extending Java classes

docs/user/README.md

+67-59
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ permalink: /reference-manual/js/
77

88
# GraalJS
99

10-
GraalJS is a fast JavaScript language implementation built on top of GraalVM.
10+
GraalJS is a fast JavaScript language implementation built on top of GraalVM.
1111
It is ECMAScript-compliant, provides interoperability with Java and other Graal languages, common tooling, and, if run on the GraalVM JDK, provides the best performance with the Graal JIT compiler by default.
12-
You can also use GraalJS with Oracle JDK or OpenJDK.
12+
You can also use GraalJS with Oracle JDK or OpenJDK.
1313

1414
GraalJS is a suitable replacement for projects wanting to [migrate from Nashorn or Rhino](#migration-guides) to a JavaScript engine that supports new ECMAScript standards and features.
1515
You can easily add GraalJS to your Java application as shown below.
1616

1717
## Getting Started with GraalJS on the JVM
1818

1919
To embed JavaScript in a Java host application, enable GraalJS by adding it as a project dependency.
20-
All necessary artifacts can be downloaded directly from Maven Central.
21-
All artifacts relevant to embedders can be found in the Maven dependency group [org.graalvm.polyglot](https://central.sonatype.com/namespace/org.graalvm.polyglot).
20+
All necessary artifacts can be downloaded directly from Maven Central.
21+
All artifacts relevant to embedders can be found in the Maven dependency group [org.graalvm.polyglot](https://central.sonatype.com/namespace/org.graalvm.polyglot).
2222

2323
Below is the Maven configuration for a JavaScript embedding:
2424
```xml
@@ -38,10 +38,10 @@ This enables GraalJS which is built on top of Oracle GraalVM and licensed under
3838
Use `js-community` if you want to use GraalJS built on GraalVM Community Edition.
3939

4040
Go step-by-step to create a Maven project, embedding JavaScript in Java, and run it.
41-
This example application was tested with GraalVM for JDK 22 and the GraalVM Polyglot API version 24.0.2.
41+
This example application was tested with GraalVM for JDK 23 and the GraalVM Polyglot API version 24.1.0.
4242
See how to install GraalVM on the [Downloads page](https://www.graalvm.org/downloads/).
4343

44-
1. Create a new Maven Java project named "app" in your favorite IDE or from your terminal with the following structure:
44+
1. Create a new Maven Java project named "helloworld" in your favorite IDE or from your terminal with the following structure:
4545
```
4646
├── pom.xml
4747
└── src
@@ -53,7 +53,7 @@ See how to install GraalVM on the [Downloads page](https://www.graalvm.org/downl
5353
```
5454
For example, you can run this command to create a new Maven project using the quickstart archetype:
5555
```bash
56-
mvn archetype:generate -DgroupId=com.example -DartifactId=app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
56+
mvn archetype:generate -DgroupId=com.example -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
5757
```
5858
5959
2. Replace the contents of _App.java_ with the following code:
@@ -65,59 +65,21 @@ See how to install GraalVM on the [Downloads page](https://www.graalvm.org/downl
6565
6666
public class App {
6767
68-
static String JS_CODE = "(function myFun(param){console.log('hello '+param);})";
68+
static String JS_CODE = "(function myFun(param){console.log('Hello ' + param + ' from JS');})";
6969
7070
public static void main(String[] args) {
71-
System.out.println("Hello JavaScript from Java");
71+
String who = args.length == 0 ? "World" : args[0];
72+
System.out.println("Hello " + who + " from Java");
7273
try (Context context = Context.create()) {
7374
Value value = context.eval("js", JS_CODE);
74-
value.execute(args[0]);
75+
value.execute(who);
7576
}
7677
}
7778
}
7879
```
80+
This example application uses the [Polyglot API](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/package-summary.html) and returns a JavaScript function as a Java value.
7981
80-
3. Add the regular Maven plugins for compiling and assembling the project into a JAR file with all dependencies to your _pom.xml_ file:
81-
```xml
82-
<build>
83-
<plugins>
84-
<plugin>
85-
<groupId>org.apache.maven.plugins</groupId>
86-
<artifactId>maven-compiler-plugin</artifactId>
87-
<version>${maven-compiler-plugin.version}</version>
88-
<configuration>
89-
<fork>true</fork>
90-
</configuration>
91-
</plugin>
92-
<plugin>
93-
<groupId>org.apache.maven.plugins</groupId>
94-
<artifactId>maven-assembly-plugin</artifactId>
95-
<version>${maven-assembly-plugin.version}</version>
96-
<configuration>
97-
<archive>
98-
<manifest>
99-
<mainClass>com.example.App</mainClass>
100-
</manifest>
101-
</archive>
102-
<descriptorRefs>
103-
<descriptorRef>jar-with-dependencies</descriptorRef>
104-
</descriptorRefs>
105-
</configuration>
106-
<executions>
107-
<execution>
108-
<id>make-assembly</id>
109-
<phase>package</phase>
110-
<goals>
111-
<goal>single</goal>
112-
</goals>
113-
</execution>
114-
</executions>
115-
</plugin>
116-
</plugins>
117-
</build>
118-
```
119-
120-
4. Add the following dependencies to _pom.xml_ to include the JavaScript engine (GraalJS):
82+
3. Add the following dependencies to _pom.xml_ to include the JavaScript engine (GraalJS):
12183
```xml
12284
<dependencies>
12385
<dependency>
@@ -133,7 +95,54 @@ See how to install GraalVM on the [Downloads page](https://www.graalvm.org/downl
13395
</dependency>
13496
</dependencies>
13597
```
136-
Set the `${graaljs.version}` property to the GraalVM Polyglot API version. For this example, use `24.0.2`.
98+
Set the GraalJS and GraalVM Polyglot API versions by adding a `graaljs.version` property to the `<properties>` section.
99+
Alternatively, you can replace `${graaljs.version}` with the version string directly.
100+
For this example, use `24.1.0`:
101+
```xml
102+
<properties>
103+
<graaljs.version>24.1.0</graaljs.version>
104+
</properties>
105+
```
106+
107+
4. Add the regular Maven plugins for compiling and assembling the project into a JAR file with all dependencies to your _pom.xml_ file:
108+
```xml
109+
<build>
110+
<plugins>
111+
<plugin>
112+
<groupId>org.apache.maven.plugins</groupId>
113+
<artifactId>maven-compiler-plugin</artifactId>
114+
<version>3.13.0</version>
115+
<configuration>
116+
<fork>true</fork>
117+
</configuration>
118+
</plugin>
119+
<plugin>
120+
<groupId>org.apache.maven.plugins</groupId>
121+
<artifactId>maven-assembly-plugin</artifactId>
122+
<version>3.7.1</version>
123+
<configuration>
124+
<archive>
125+
<manifest>
126+
<mainClass>com.example.App</mainClass>
127+
</manifest>
128+
</archive>
129+
<descriptorRefs>
130+
<descriptorRef>jar-with-dependencies</descriptorRef>
131+
</descriptorRefs>
132+
</configuration>
133+
<executions>
134+
<execution>
135+
<id>make-assembly</id>
136+
<phase>package</phase>
137+
<goals>
138+
<goal>single</goal>
139+
</goals>
140+
</execution>
141+
</executions>
142+
</plugin>
143+
</plugins>
144+
</build>
145+
```
137146
138147
5. Package the project and run the application:
139148
```bash
@@ -142,13 +151,12 @@ See how to install GraalVM on the [Downloads page](https://www.graalvm.org/downl
142151
```bash
143152
java -jar target/helloworld-1.0-SNAPSHOT-jar-with-dependencies.jar GraalVM
144153
```
145-
146-
This example application uses the [Polyglot API](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/package-summary.html) and returns a JavaScript function as a Java value.
147-
A single JAR with all dependencies was created from language libraries.
148-
However, we recommend splitting and using Java modules on the module path, especially if you would like to compile this application ahead of time with GraalVM Native Image.
154+
155+
A single JAR with all dependencies was created from language libraries.
156+
However, we recommend splitting and using Java modules on the module path, especially if you would like to compile this application ahead of time with GraalVM Native Image.
149157
Learn more in the [Guide to Embedding Languages](https://www.graalvm.org/reference-manual/embed-languages/#dependency-setup).
150158
151-
The source code unit can be represented with a String, as in the example, a file, read from URL, and [other means](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/Source.html).
159+
The source code unit can be represented with a String, as shown in the example, a file, read from URL, and [other means](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/Source.html).
152160
By wrapping the function definition (`()`), you return the function immediately:
153161
```java
154162
Value f = context.eval("js", "(function f(x, y) { return x + y; })");
@@ -169,12 +177,12 @@ try (Context context = Context.newBuilder()
169177
}
170178
```
171179

172-
The Polyglot API offers many other ways to access a guest language code from Java, for example, by directly accessing JavaScript objects, numbers, strings, and arrays.
180+
The Polyglot API offers many other ways to access a guest language code from Java, for example, by directly accessing JavaScript objects, numbers, strings, and arrays.
173181
Learn more about JavaScript to Java interoperability and find more examples in the [Java Interoperability guide](JavaInteroperability.md).
174182

175183
### Related Documentation
176184

177-
GraalJS is also available as a standalone distribution that you can download from [GitHub](https://github.com/oracle/graaljs/releases).
185+
GraalJS is also available as a standalone distribution that you can download from [GitHub](https://github.com/oracle/graaljs/releases).
178186
Learn more [here](https://github.com/oracle/graaljs/blob/master/README.md#standalone-distributions).
179187

180188
We provide the following documentation for GraalJS users:

docs/user/ScriptEngine.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ An example _pom.xml_ file can be found in the [GraalJS repository on GitHub](htt
3737

3838
## Recommendation for Use
3939

40-
To avoid unnecessary recompilation of JavaScript sources, **it is recommended to use `CompiledScript.eval`** instead of `ScriptEngine.eval`.
40+
To avoid unnecessary recompilation of JavaScript sources, **it is recommended to use `CompiledScript.eval`** instead of `ScriptEngine.eval`.
4141
This prevents JIT-compiled code from being garbage-collected as long as the corresponding `CompiledScript` object is alive.
4242

4343
Single-threaded example:
@@ -70,8 +70,8 @@ script.eval();
7070

7171
## Setting Options via `Bindings`
7272

73-
The `ScriptEngine` interface does not provide a default way to set options.
74-
As a workaround, `GraalJSScriptEngine` supports setting some `Context` options through `Bindings`.
73+
The `ScriptEngine` interface does not provide a default way to set options.
74+
As a workaround, `GraalJSScriptEngine` supports setting some `Context` options through `Bindings`.
7575
These options are:
7676
* `polyglot.js.allowHostAccess <boolean>`
7777
* `polyglot.js.allowNativeAccess <boolean>`
@@ -88,7 +88,7 @@ These options control the sandboxing rules applied to evaluated JavaScript code
8888
Note that using `ScriptEngine` implies allowing experimental options.
8989
This is an exhaustive list of allowed options to be passed via `Bindings`; in case you need to pass additional options to GraalJS, you need to manually create a `Context` as shown below.
9090

91-
To set an option via `Bindings`, use `Bindings.put(<option name>, true)` **before** the engine's script context is initialized.
91+
To set an option via `Bindings`, use `Bindings.put(<option name>, true)` **before** the engine's script context is initialized.
9292
Note that even a call to `Bindings#get(String)` may lead to a context initialization.
9393
The following code shows how to enable `polyglot.js.allowHostAccess` via `Bindings`:
9494
```java
@@ -108,7 +108,7 @@ Options to the JavaScript engine can be set via system properties before startin
108108
java -Dpolyglot.js.ecmascript-version=2022 MyApplication
109109
```
110110

111-
Or, options to the JavaScript engine can be set programmatically from within a Java application before creating `ScriptEngine`.
111+
Or, options to the JavaScript engine can be set programmatically from within a Java application before creating `ScriptEngine`.
112112
This, however, only works for the options passed to the JavaScript engine (such as `js.ecmascript-version`), and not for the options mentioned in the example that can be set via `Bindings`.
113113
Another caveat is that those system properties are shared by all concurrently executed `ScriptEngine`s.
114114

0 commit comments

Comments
 (0)