Skip to content

Commit

Permalink
[GR-58996] GraalWasm: Remove the content duplicated on GitHub.
Browse files Browse the repository at this point in the history
PullRequest: graal/19013
  • Loading branch information
olyagpl committed Oct 13, 2024
2 parents 0129b47 + 34291de commit aed5e74
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 98 deletions.
4 changes: 2 additions & 2 deletions docs/reference-manual/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,6 @@ wasm [OPTION...] [--entry-point=FN] FILE [ARG...]

### Related Documentation

- [Embed C in Java Using GraalWasm](guides/embed-c-in-java.md)
- [Embed C in Java Using GraalWasm](https://github.com/graalvm/graal-languages-demos/tree/main/graalwasm/graalwasm-embed-c-code-guide){:target="_blank"}
- [Embedding Languages documentation](../embedding/embed-languages.md)
- [GraalWasm](https://github.com/oracle/graal/tree/master/wasm)
- [GraalWasm](https://github.com/oracle/graal/tree/master/wasm){:target="_blank"}
98 changes: 2 additions & 96 deletions docs/reference-manual/wasm/guides/embed-c-in-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,101 +3,7 @@ layout: ni-docs
toc_group: how-to-guides
link_title: Embed C in Java Using GraalWasm
permalink: /reference-manual/wasm/guides/embed-c-in-java/
redirect_to: https://github.com/graalvm/graal-languages-demos/tree/main/graalwasm/graalwasm-embed-c-code-guide
---

# Embed C in Java Using GraalWasm

The example below demonstrates how to compile a C function to WebAssembly and run it embedded in a Java application.

### Prerequisites

To run the demo, you need the following:
- [GraalVM JDK](https://www.graalvm.org/downloads/)
- [Emscripten compiler frontend](https://emscripten.org/docs/tools_reference/emcc.html)
- [Maven](https://maven.apache.org/)

### Demo Part

1. Put the following C program in a file named _floyd.c_:
```c
#include <stdio.h>

void floyd() {
int number = 1;
int rows = 10;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", number);
++number;
}
printf(".\n");
}
}

int main() {
floyd();
return 0;
}
```
Note that `floyd` is defined as a separate function and can be exported.

2. Compile the C code using the most recent version of the [Emscripten compiler frontend](https://emscripten.org/docs/tools_reference/emcc.html):
```bash
emcc --no-entry -s EXPORTED_FUNCTIONS=_floyd -o floyd.wasm floyd.c
```
> 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.

It produces a standalone file _floyd.wasm_ in the current working directory.

3. Add dependencies. The GraalVM SDK Polyglot API can be easily added as a Maven dependency to your Java project.
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).

- To add the Polyglot API:
```xml
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>${graalwasm.version}</version>
</dependency>
```
- To add GraalWasm:
```xml
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>wasm</artifactId>
<version>${graalwasm.version}</version>
<type>pom</type>
</dependency>
```

4. Now you can embed this WebAssembly function in a Java application, for example:

```java
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.io.ByteSequence;
// Load the WebAssembly contents into a byte array
byte[] binary = Files.readAllBytes(Path.of("path", "to", "wasm", "file", "floyd.wasm"));
// Setup context
Context.Builder contextBuilder = Context.newBuilder("wasm").option("wasm.Builtins", "wasi_snapshot_preview1");
Source.Builder sourceBuilder = Source.newBuilder("wasm", ByteSequence.create(binary), "example");
Source source = sourceBuilder.build();
Context context = contextBuilder.build();
// Evaluate the WebAssembly module
context.eval(source);
// Execute the floyd function
context.getBindings("wasm").getMember("example").getMember("_initialize").executeVoid();
Value mainFunction =context.getBindings("wasm").getMember("example").getMember("floyd");
mainFunction.execute();
context.close();
```

5. Compile and run this Java application with Maven as usual.

### Related Documentation

- [Embedding Languages documentation](../../embedding/embed-languages.md)
- [GraalWasm](../README.md)
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 comments on commit aed5e74

Please sign in to comment.