Skip to content

Commit 94e9b2d

Browse files
committed
Upgrade graalwasm-embed-c-code-guide.
1 parent 73433d2 commit 94e9b2d

File tree

5 files changed

+80
-52
lines changed

5 files changed

+80
-52
lines changed

.github/workflows/graalwasm-embed-c-code-guide.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ jobs:
3939
source ./emsdk/emsdk_env.sh
4040
cd graalwasm/graalwasm-embed-c-code-guide
4141
./mvnw --no-transfer-progress package
42-
./mvnw --no-transfer-progress exec:java
42+
./mvnw --no-transfer-progress exec:exec

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ Put the following C program in _src/main/c/floyd.c_:
6060
```c
6161
#include <stdio.h>
6262

63-
void floyd() {
63+
void floyd(int rows) {
6464
int number = 1;
65-
int rows = 10;
6665
for (int i = 1; i <= rows; i++) {
6766
for (int j = 1; j <= i; j++) {
6867
printf("%d ", number);
@@ -73,9 +72,10 @@ void floyd() {
7372
}
7473

7574
int main() {
76-
floyd();
75+
floyd(10);
7776
return 0;
7877
}
78+
7979
```
8080

8181
Note that `floyd` is defined as a separate function and can be exported.
@@ -154,23 +154,28 @@ import org.graalvm.polyglot.Value;
154154

155155
public class App {
156156
public static void main(String[] args) throws IOException {
157-
// Find the WebAssembly module resource
158-
URL wasmFile = App.class.getResource("floyd.wasm");
159-
160-
// Setup context
161-
Context.Builder contextBuilder = Context.newBuilder("wasm").option("wasm.Builtins", "wasi_snapshot_preview1");
162-
Source.Builder sourceBuilder = Source.newBuilder("wasm", wasmFile).name("example");
163-
Source source = sourceBuilder.build();
164-
Context context = contextBuilder.build();
165-
166-
// Evaluate the WebAssembly module
167-
context.eval(source);
168-
169-
// Execute the floyd function
170-
context.getBindings("wasm").getMember("example").getMember("_initialize").executeVoid();
171-
Value mainFunction = context.getBindings("wasm").getMember("example").getMember("floyd");
172-
mainFunction.execute();
173-
context.close();
157+
// Find the WebAssembly module resource
158+
URL wasmFile = App.class.getResource("floyd.wasm");
159+
Source source = Source.newBuilder("wasm", wasmFile).name("example").build();
160+
161+
// Create Wasm context
162+
try (Context context = Context.newBuilder("wasm").option("wasm.Builtins", "wasi_snapshot_preview1").build()) {
163+
// Compile and instantiate the module
164+
Value module = context.eval(source);
165+
Value instance = module.newInstance();
166+
167+
// Get the exports member from the module instance
168+
Value exports = instance.getMember("exports");
169+
170+
// Invoke an exported functions
171+
exports.invokeMember("_initialize");
172+
exports.invokeMember("floyd", 10);
173+
174+
// Or if you need to call a function multiple times
175+
Value floyd = exports.getMember("floyd");
176+
floyd.execute(4);
177+
floyd.execute(8);
178+
}
174179
}
175180
}
176181
```
@@ -181,7 +186,7 @@ Compile and run this Java application with Maven:
181186

182187
```shell
183188
mvw package
184-
mvn exec:java -Dexec.mainClass=com.example.App
189+
mvn exec:exec
185190
```
186191

187192
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/pom.xml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<maven.compiler.release>21</maven.compiler.release>
16+
<truffle.jvm.arg1>--enable-native-access=ALL-UNNAMED</truffle.jvm.arg1>
17+
<truffle.jvm.arg2>--sun-misc-unsafe-memory-access=allow</truffle.jvm.arg2>
1618
</properties>
1719

1820
<dependencyManagement>
@@ -95,13 +97,23 @@
9597
<artifactId>maven-project-info-reports-plugin</artifactId>
9698
<version>3.6.1</version>
9799
</plugin>
100+
<plugin>
101+
<artifactId>exec-maven-plugin</artifactId>
102+
<version>1.2.1</version>
103+
</plugin>
98104
</plugins>
99105
</pluginManagement>
100106
<plugins>
107+
<plugin>
108+
<groupId>org.apache.maven.plugins</groupId>
109+
<artifactId>maven-surefire-plugin</artifactId>
110+
<configuration>
111+
<argLine>${truffle.jvm.arg1} ${truffle.jvm.arg2}</argLine>
112+
</configuration>
113+
</plugin>
101114
<plugin>
102115
<groupId>org.codehaus.mojo</groupId>
103116
<artifactId>exec-maven-plugin</artifactId>
104-
<version>1.2.1</version>
105117
<executions>
106118
<execution>
107119
<id>create-output-directory</id>
@@ -129,7 +141,14 @@
129141
</execution>
130142
</executions>
131143
<configuration>
132-
<mainClass>com.example.App</mainClass>
144+
<executable>java</executable>
145+
<arguments>
146+
<argument>-classpath</argument>
147+
<classpath/>
148+
<argument>${truffle.jvm.arg1}</argument>
149+
<argument>${truffle.jvm.arg2}</argument>
150+
<argument>com.example.App</argument>
151+
</arguments>
133152
</configuration>
134153
</plugin>
135154
</plugins>
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
#include <stdio.h>
1+
#include <stdio.h>
22

3-
void floyd() {
4-
int number = 1;
5-
int rows = 10;
6-
for (int i = 1; i <= rows; i++) {
7-
for (int j = 1; j <= i; j++) {
8-
printf("%d ", number);
9-
++number;
10-
}
11-
printf(".\n");
12-
}
13-
}
3+
void floyd(int rows) {
4+
int number = 1;
5+
for (int i = 1; i <= rows; i++) {
6+
for (int j = 1; j <= i; j++) {
7+
printf("%d ", number);
8+
++number;
9+
}
10+
printf(".\n");
11+
}
12+
}
1413

15-
int main() {
16-
floyd();
17-
return 0;
18-
}
14+
int main() {
15+
floyd(10);
16+
return 0;
17+
}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,25 @@ public class App {
1717
public static void main(String[] args) throws IOException {
1818
// Find the WebAssembly module resource
1919
URL wasmFile = App.class.getResource("floyd.wasm");
20+
Source source = Source.newBuilder("wasm", wasmFile).name("example").build();
2021

21-
// Setup context
22-
Context.Builder contextBuilder = Context.newBuilder("wasm").option("wasm.Builtins", "wasi_snapshot_preview1");
23-
Source.Builder sourceBuilder = Source.newBuilder("wasm", wasmFile).name("example");
24-
Source source = sourceBuilder.build();
25-
Context context = contextBuilder.build();
22+
// Create Wasm context
23+
try (Context context = Context.newBuilder("wasm").option("wasm.Builtins", "wasi_snapshot_preview1").build()) {
24+
// Compile and instantiate the module
25+
Value module = context.eval(source);
26+
Value instance = module.newInstance();
2627

27-
// Evaluate the WebAssembly module
28-
context.eval(source);
28+
// Get the exports member from the module instance
29+
Value exports = instance.getMember("exports");
2930

30-
// Execute the floyd function
31-
context.getBindings("wasm").getMember("example").getMember("_initialize").executeVoid();
32-
Value mainFunction = context.getBindings("wasm").getMember("example").getMember("floyd");
33-
mainFunction.execute();
34-
context.close();
31+
// Invoke an exported functions
32+
exports.invokeMember("_initialize");
33+
exports.invokeMember("floyd", 10);
34+
35+
// Or if you need to call a function multiple times
36+
Value floyd = exports.getMember("floyd");
37+
floyd.execute(4);
38+
floyd.execute(8);
39+
}
3540
}
3641
}

0 commit comments

Comments
 (0)