diff --git a/docs/reference-manual/wasm/README.md b/docs/reference-manual/wasm/README.md index cef1279dde7a..5241442dd95b 100644 --- a/docs/reference-manual/wasm/README.md +++ b/docs/reference-manual/wasm/README.md @@ -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) \ No newline at end of file +- [GraalWasm](https://github.com/oracle/graal/tree/master/wasm){:target="_blank"} \ No newline at end of file diff --git a/docs/reference-manual/wasm/guides/embed-c-in-java.md b/docs/reference-manual/wasm/guides/embed-c-in-java.md index 79827b801111..3b1621c053ff 100644 --- a/docs/reference-manual/wasm/guides/embed-c-in-java.md +++ b/docs/reference-manual/wasm/guides/embed-c-in-java.md @@ -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 - - 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 - - org.graalvm.polyglot - polyglot - ${graalwasm.version} - - ``` - - To add GraalWasm: - ```xml - - org.graalvm.polyglot - wasm - ${graalwasm.version} - pom - - ``` - -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) \ No newline at end of file +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). \ No newline at end of file