Skip to content

Commit aed5e74

Browse files
committed
[GR-58996] GraalWasm: Remove the content duplicated on GitHub.
PullRequest: graal/19013
2 parents 0129b47 + 34291de commit aed5e74

File tree

2 files changed

+4
-98
lines changed

2 files changed

+4
-98
lines changed

docs/reference-manual/wasm/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,6 @@ wasm [OPTION...] [--entry-point=FN] FILE [ARG...]
223223

224224
### Related Documentation
225225

226-
- [Embed C in Java Using GraalWasm](guides/embed-c-in-java.md)
226+
- [Embed C in Java Using GraalWasm](https://github.com/graalvm/graal-languages-demos/tree/main/graalwasm/graalwasm-embed-c-code-guide){:target="_blank"}
227227
- [Embedding Languages documentation](../embedding/embed-languages.md)
228-
- [GraalWasm](https://github.com/oracle/graal/tree/master/wasm)
228+
- [GraalWasm](https://github.com/oracle/graal/tree/master/wasm){:target="_blank"}

docs/reference-manual/wasm/guides/embed-c-in-java.md

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,7 @@ layout: ni-docs
33
toc_group: how-to-guides
44
link_title: Embed C in Java Using GraalWasm
55
permalink: /reference-manual/wasm/guides/embed-c-in-java/
6+
redirect_to: https://github.com/graalvm/graal-languages-demos/tree/main/graalwasm/graalwasm-embed-c-code-guide
67
---
78

8-
# Embed C in Java Using GraalWasm
9-
10-
The example below demonstrates how to compile a C function to WebAssembly and run it embedded in a Java application.
11-
12-
### Prerequisites
13-
14-
To run the demo, you need the following:
15-
- [GraalVM JDK](https://www.graalvm.org/downloads/)
16-
- [Emscripten compiler frontend](https://emscripten.org/docs/tools_reference/emcc.html)
17-
- [Maven](https://maven.apache.org/)
18-
19-
### Demo Part
20-
21-
1. Put the following C program in a file named _floyd.c_:
22-
```c
23-
#include <stdio.h>
24-
25-
void floyd() {
26-
int number = 1;
27-
int rows = 10;
28-
for (int i = 1; i <= rows; i++) {
29-
for (int j = 1; j <= i; j++) {
30-
printf("%d ", number);
31-
++number;
32-
}
33-
printf(".\n");
34-
}
35-
}
36-
37-
int main() {
38-
floyd();
39-
return 0;
40-
}
41-
```
42-
Note that `floyd` is defined as a separate function and can be exported.
43-
44-
2. Compile the C code using the most recent version of the [Emscripten compiler frontend](https://emscripten.org/docs/tools_reference/emcc.html):
45-
```bash
46-
emcc --no-entry -s EXPORTED_FUNCTIONS=_floyd -o floyd.wasm floyd.c
47-
```
48-
> The exported functions must be prefixed by `_`. If you reference that function in, for example, the Java code, the exported name should not contain the underscore.
49-
50-
It produces a standalone file _floyd.wasm_ in the current working directory.
51-
52-
3. Add dependencies. The GraalVM SDK Polyglot API can be easily added as a Maven dependency to your Java project.
53-
The GraalWasm artifact should be on the Java module or class path too. Add the following set of dependencies to the project configuration file (_pom.xml_ in the case of Maven).
54-
55-
- To add the Polyglot API:
56-
```xml
57-
<dependency>
58-
<groupId>org.graalvm.polyglot</groupId>
59-
<artifactId>polyglot</artifactId>
60-
<version>${graalwasm.version}</version>
61-
</dependency>
62-
```
63-
- To add GraalWasm:
64-
```xml
65-
<dependency>
66-
<groupId>org.graalvm.polyglot</groupId>
67-
<artifactId>wasm</artifactId>
68-
<version>${graalwasm.version}</version>
69-
<type>pom</type>
70-
</dependency>
71-
```
72-
73-
4. Now you can embed this WebAssembly function in a Java application, for example:
74-
75-
```java
76-
import org.graalvm.polyglot.*;
77-
import org.graalvm.polyglot.io.ByteSequence;
78-
79-
// Load the WebAssembly contents into a byte array
80-
byte[] binary = Files.readAllBytes(Path.of("path", "to", "wasm", "file", "floyd.wasm"));
81-
82-
// Setup context
83-
Context.Builder contextBuilder = Context.newBuilder("wasm").option("wasm.Builtins", "wasi_snapshot_preview1");
84-
Source.Builder sourceBuilder = Source.newBuilder("wasm", ByteSequence.create(binary), "example");
85-
Source source = sourceBuilder.build();
86-
Context context = contextBuilder.build();
87-
88-
// Evaluate the WebAssembly module
89-
context.eval(source);
90-
91-
// Execute the floyd function
92-
context.getBindings("wasm").getMember("example").getMember("_initialize").executeVoid();
93-
Value mainFunction =context.getBindings("wasm").getMember("example").getMember("floyd");
94-
mainFunction.execute();
95-
context.close();
96-
```
97-
98-
5. Compile and run this Java application with Maven as usual.
99-
100-
### Related Documentation
101-
102-
- [Embedding Languages documentation](../../embedding/embed-languages.md)
103-
- [GraalWasm](../README.md)
9+
The content was moved to [Graal Languages - Demos and Guides repository](https://github.com/graalvm/graal-languages-demos/tree/main/graalwasm/graalwasm-embed-c-code-guide).

0 commit comments

Comments
 (0)