WebAssembly with Rhino #1485
Replies: 5 comments 8 replies
-
Tnx for sharing! Would be cool to have a project/repo that would implement the JavaScript WebAssembly interface, that an embedder you easily include in their project that uses Rhino, similar to how an embedder can expose the Such project could be hosted under https://github.com/rhino as it's own repo and in the future potentially be part of a WinterGC compatible opt-in |
Beta Was this translation helpful? Give feedback.
-
Hello @StSchnell, thanks for sharing! I'm one of the creators of Chicory. I recently did some similar with an emscripten build of sqlite. I was also able to get an emscripten build of an SNES emulator working. I see you went with writing the bindings yourself, which is probably the only viable path right now, but I've been thinking about the possibility of supporting Emscripten derived bindings explicitly. Having looked at the JavaScript bindings that emscripten generates, i think it should be possible to do something similar in Java. Let me know if you have any thoughts on this. For this project maybe emscripten bindings would just work given it can execute the generated JS bindings. |
Beta Was this translation helpful? Give feedback.
-
@p-bakker @StSchnell would be happy to contribute to this. It's exactly the kind of thing we had in mind when we started Chicory. |
Beta Was this translation helpful? Give feedback.
-
I've created an empty repo, so there's a place to send PRs to: https://github.com/rhino/rhino-wasm As for implementation directions:
Please let me know if this is enough to get going. |
Beta Was this translation helpful? Give feedback.
-
Hello @p-bakker, var wasm = Packages.com.dylibso.chicory.wasm;
var file = new java.io.File("./main.wasm");
var module = wasm.Parser.parse(file); ... to parse a wasm, but Rhino can not find the static method parse. The parse method is part of the Parser class of com.dylibso.chicory.wasm package. public static Module parse(File file) {
return parse(file.toPath());
} The error message is:
The message is true, it is not an instance method, it is a static method. |
Beta Was this translation helpful? Give feedback.
-
This post describes the use of WebAssembly with Rhino. WebAssembly, or Wasm for short, is a low-level binary instruction format which is primarily used on the web for client and server applications. This is intended to reduce sizes, minimize loading times and increase execution speed. dylibso, a group of software builders, offers Chicory, a runtime to use Wasm with the Java. This approach allows to run Wasm programs with zero native dependencies anywhere where the JVM can go.
From C to Wasm
To show an example how Wasm can be used with Rhino, first a simple C code that is compiled to Wasm with emscripten, a complete compiler toolchain. It contains only one function, with the name hello, with two parameters. If a name is passed the typical Hello World ... message is returned with a name, otherwise without.
The following command can be used to compile this code to Wasm:
@emcc hello.c -o hello.c.wasm --no-entry -s EXPORTED_FUNCTIONS=_malloc,_free
Strings are passed and returned in this example. In this case it is necessary to use the functions malloc, to allocate memory, and free, to release the memory again.
Use Wasm with Rhino
The use in Rhino is very easy to understand. In the first steps Wasm is loaded and the functions are instantiated. The arguments for the function are prepared in the following steps. Firstly the name and secondly for the return value. The function is then invoked and the return value is output. Finally, the occupied memory is released again. Further detailed explanations can be found at the Chicory project.
The Chicory JAR files can be downloaded from the MVN repository, they must be added to the classpath.
Conclusion
WebAssembly can be used with Chicory very easily with the Rhino engine. The question certainly arises whether necessity exists at all, because Rhino can easily use Java. But the situation may be differs if Wasm modules are already available or code reuse is planned, e.g. for browser and server. The use of WebAssembly with the Rhino engine opens up interesting perspectives and possibilities.
Beta Was this translation helpful? Give feedback.
All reactions