You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: web-image/export-java-function/README.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# Web Image: Export Java Method Example
2
2
3
-
This demo illustrates the use of **Web Image** - an experimental backend for [GraalVM Native Image](https://www.graalvm.org/latest/reference-manual/native-image/) that compiles a Java application ahead-of-time and produces a WebAssembly (WASM) module with a JavaScript wrapper.
3
+
This demo illustrates the use of **Web Image** - an experimental backend for [GraalVM Native Image](https://www.graalvm.org/latest/reference-manual/native-image/) that compiles a Java application ahead-of-time and produces a WebAssembly (Wasm) module with a JavaScript wrapper.
4
4
Then it can be run in browsers or with Node.js.
5
5
6
6
The key idea is to show how you can currently **call Java methods directly from JavaScript** without relying on the `main()` method.
7
-
This demo exposes a simple Java `add(int a, int b)`method to the global JavaScript scope using the `@JS` annotation from the [Annotation Interface](https://www.graalvm.org/sdk/javadoc/org/graalvm/webimage/api/JS.html).
7
+
This demo exposes a simple Java method to the global JavaScript scope using the `@JS` annotation from the [Annotation Interface](https://www.graalvm.org/sdk/javadoc/org/graalvm/webimage/api/JS.html).
8
8
9
9
> Note: Web Image is an experimental technology and under active development. APIs, tooling, and capabilities may change.
10
10
@@ -70,7 +70,7 @@ public class Adder {
70
70
```
71
71
72
72
- `@JS` annotation is part of [GraalVM Web Image API](https://www.graalvm.org/sdk/javadoc/org/graalvm/webimage/api/JS.html). It allows you to bridge Java and JavaScript.
73
-
- `args = {"adder"}` tells GraalVM that the BiFunction you pass in Java will be available as a JavaScript variable `adder`.
73
+
- `args = {"adder"}` tells GraalVM that the BiFunction you pass in Java will be available as a JavaScript variable `adder` (not necessary if the Java source code is compiled with the `-parameters` option).
74
74
- `value = "globalThis.adder = adder;"` is a raw JavaScript code executed when the export happens; it sets a variable called `adder` to be globally accessible in browsers.
75
75
76
76
Further down you see the `export` method:
@@ -87,7 +87,7 @@ This makes the lambda directly callable from JS as `globalThis.adder(...)`.
87
87
The next part is calling from JavaScript in HTML, which happens in this part of _index.html_:
88
88
```js
89
89
<script>
90
-
GraalVM.run([], {}).then(() => {
90
+
GraalVM.run([]).then(() => {
91
91
...
92
92
addButton.addEventListener("click", () => {
93
93
const a = parseInt(document.getElementById("num1").value);
- `GraalVM.run([], {})` initializes the WASM module and the Java runtime inside the browser.
106
+
- `GraalVM.run([], {})` initializes the Wasm module and the Java runtime inside the browser.
107
107
- `globalThis.adder(a, b)` calls the `add`functionyou exported via WebAssembly.
108
108
109
109
### Conclusion
110
110
111
111
The focus of this demo is to demonstrate direct interaction between Java and JavaScript in the browser via WebAssembly.
112
-
Note that the [GraalVM Web Image API](https://www.graalvm.org/sdk/javadoc/org/graalvm/webimage/api/JS.html) is still under active development.
112
+
Note that the [GraalVM Web Image API](https://www.graalvm.org/sdk/javadoc/org/graalvm/webimage/api/JS.html) is still under active development, there will be better ways to export Java methods to JavaScript.
0 commit comments