Mix.install([{:kino_wasm, git: "https://github.com/spawnfest/kino_wasm.git"}])
This Smart Cell leverages WebAssembly to achieve multi language support in Livebook.
It includes a secondary library (wasm_runner) that receives the code, compiles it to web assembly and executes said code using wasmex.
Supported languages:
- Rust
- Zig
When you add the cell Rust Code Cell
it will include two fields:
- A Monaco editor for the target language
- Second editor to specify the arguments
When you change the code in the first editor, it will trigger the blur
event and send the altered source code to wasm_runner to compile and run based on the args given
output =
WasmRunner.Backend.run(
:rust,
"#[no_mangle]\npub extern fn run(x: i32, y: i32) -> i32 {\n x + y\n}\n",
[4, 2]
)
The output of the function is stored in a variable called output
so it can be used in other livebook cells
output
With Zig Code Cell
we can follow the same idea, some code and then the arguments
output =
WasmRunner.Backend.run(:zig, "export fn run(a: i32, b: i32) i32 {\n return a + b;\n}\n", [
10,
20
])
output
This were only tested in OSX.
You'll need to install this two dependencies
- Install Rust with https://www.rust-lang.org/tools/install
- Install wasm-pack
- Rust binaries in
~/.cargo/bin/cargo
- Install Zig 0.11.0 with Homebrew
- Zig binary in
/opt/homebrew/Cellar/zig/0.11.0/bin/zig
- Paths are not properly assigned when running System.cmd so at the moment paths for executables are too hard coded... This is the first thing I will need to fix!