Skip to content

Commit 132efe3

Browse files
Andrea Peruffofniephaus
authored andcommitted
example java function invocation from C
1 parent f9d9c71 commit 132efe3

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

graalwasm/graalwasm-embed-c-code-guide/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ public class App {
185185
Compile and run this Java application with Maven:
186186

187187
```shell
188-
mvw package
189-
mvn exec:exec
188+
./mvnw package
189+
./mvnw exec:exec
190190
```
191191

192192
The expected output should contain the first 10 lines of [Floyd's triangle](https://en.wikipedia.org/wiki/Floyd%27s_triangle), printed using the C function:

graalwasm/graalwasm-embed-c-code-guide/src/main/c/floyd.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#include <stdio.h>
22

3+
extern int javaInc(int number)
4+
__attribute__((
5+
__import_module__("env"),
6+
__import_name__("java-increment"),
7+
));
8+
39
void floyd(int rows) {
410
int number = 1;
511
for (int i = 1; i <= rows; i++) {
612
for (int j = 1; j <= i; j++) {
713
printf("%d ", number);
8-
++number;
14+
number = javaInc(number);
915
}
1016
printf(".\n");
1117
}

graalwasm/graalwasm-embed-c-code-guide/src/main/java/com/example/App.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,36 @@
88

99
import java.io.IOException;
1010
import java.net.URL;
11+
import java.util.Map;
1112

1213
import org.graalvm.polyglot.Context;
1314
import org.graalvm.polyglot.Source;
1415
import org.graalvm.polyglot.Value;
16+
import org.graalvm.polyglot.proxy.ProxyExecutable;
17+
import org.graalvm.polyglot.proxy.ProxyObject;
1518

1619
public class App {
20+
21+
public static Object javaIncrement(Value... v) {
22+
return Value.asValue(v[0].asInt() + 1);
23+
}
24+
1725
public static void main(String[] args) throws IOException {
1826
// Find the WebAssembly module resource
1927
URL wasmFile = App.class.getResource("floyd.wasm");
2028
Source source = Source.newBuilder("wasm", wasmFile).name("example").build();
2129

2230
// Create Wasm context
23-
try (Context context = Context.newBuilder("wasm").option("wasm.Builtins", "wasi_snapshot_preview1").build()) {
24-
// Compile and instantiate the module
31+
try (Context context = Context.newBuilder("wasm")
32+
.option("wasm.Builtins", "wasi_snapshot_preview1")
33+
.build()) {
34+
// Compile and instantiate the module with host function
2535
Value module = context.eval(source);
26-
Value instance = module.newInstance();
36+
Value instance = module.newInstance(ProxyObject.fromMap(
37+
Map.of("env", ProxyObject.fromMap(
38+
Map.of("java-increment", (ProxyExecutable) App::javaIncrement)
39+
))
40+
));
2741

2842
// Get the exports member from the module instance
2943
Value exports = instance.getMember("exports");

0 commit comments

Comments
 (0)