• Stars
    star
    493
  • Rank 85,748 (Top 2 %)
  • Language
    Elixir
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Execute WebAssembly from Elixir

Wasmex logo

License CI

Wasmex is a fast and secure WebAssembly and WASI runtime for Elixir. It enables lightweight WebAssembly containers to be run in your Elixir backend.

It uses wasmtime to execute Wasm binaries through a Rust NIF.

Documentation can be found at https://hexdocs.pm/wasmex.

Install

The package can be installed by adding wasmex to your list of dependencies in mix.exs:

def deps do
  [
    {:wasmex, "~> 0.8.3"}
  ]
end

Example

There is a toy Wasm program in test/wasm_test/src/lib.rs, written in Rust (but could potentially be any other language that compiles to WebAssembly). It defines many functions we use for end-to-end testing, but also serves as example code. For example:

#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
    x + y
}

Once this program compiled to WebAssembly (which we do every time when running tests), we end up with a wasmex_test.wasm binary file.

This Wasm file can be executed in Elixir:

bytes = File.read!("wasmex_test.wasm")
{:ok, pid} = Wasmex.start_link(%{bytes: bytes}) # starts a GenServer running a Wasm instance
{:ok, [42]} == Wasmex.call_function(pid, "sum", [50, -8])

What is WebAssembly?

Quoting the WebAssembly site:

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.

About speed:

WebAssembly aims to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms.

About safety:

WebAssembly describes a memory-safe, sandboxed execution environment […].

Using WebAssembly on an Elixir host is great not only, but especially for the following use cases:

  • Running user-provided code safely
  • Share business logic between systems written in different programming languages. E.g. a JS frontend and Elixir backend
  • Run existing libraries/programs and easily interface them from Elixir

Development

To set up a development environment install the latest stable rust and rust-related tooling:

rustup component add rustfmt
rustup component add clippy
rustup target add wasm32-unknown-unknown # to compile our example Wasm files for testing
rustup target add wasm32-wasi # to compile our example Wasm/WASI files for testing

Then install the erlang/elixir dependencies:

asdf install # assuming you install elixir, erlang with asdf. if not, make sure to install them your way
mix deps.get

If you plan to change something on the Rust part of this project, set the following ENV WASMEX_BUILD=true so that your changes will be picked up.

IΒ΄m looking forward to your contributions. Please open a PR containing the motivation of your change. If it is a bigger change or refactoring, consider creating an issue first. We can discuss changes there first which might safe us time down the road :)

Any changes should be covered by tests, they can be run with mix test. In addition to tests, we expect the formatters and linters (cargo fmt, cargo clippy, mix format, mix dialyzer, mix credo) to pass.

Release

To release this package, make sure CI is green, increase the package version, and:

git tag -a v0.8.0 # change version accordingly, copy changelog into tag message
git push --tags
mix rustler_precompiled.download Wasmex.Native --all --ignore-unavailable --print

Inspect it's output carefully, but ignore NIF version 2.14 and arm-unknown-linux-gnueabihf arch errors because we don't build for them. Now inspect the checksum-Elixir.Wasmex.Native.exs file - it should include all prebuilt binaries in their checksums

Then continue with

mix hex.publish

License

The entire project is under the MIT License. Please read theLICENSE file.

Licensing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions.

More Repositories

1

malloc_trim

Gives some ruby memory back to the OS
Makefile
34
star
2

LazyNethack

A Nethack Screensaver for MacOS -- watch others play nethack when you're too lazy to play yourself :)
JavaScript
17
star
3

rpiet

The Piet interpreter written in Rust πŸ–Ό
Rust
15
star
4

capistrano-uberspace

Deploy your rails app on an uberspace with Capistrano 3
Ruby
14
star
5

ecto_fragment_extras

Ecto fragment() with named or inline params instead of just ?'s
Elixir
11
star
6

hpi_planer

CURRENTLY NOT MAINTAINED
JavaScript
10
star
7

openproject-emoji

[not maintained] Add emoji support to OpenProject
JavaScript
5
star
8

programming_paddle

Stole my wifes sewing machine foot pedal. Using it for programming now.
Ruby
5
star
9

cucumber-pretty_fail_formatter

A cucumber formatter which is concise on success and pretty on failures
Ruby
3
star
10

rspec-pretty_fail_formatter

An rspec formatter which is concise on success and pretty on failures
Ruby
3
star
11

primetime

An HTML page that tells you if the current unix timestamp is a prime number
JavaScript
2
star
12

references.js

A small JavaScript library that lets you reference your sources (e.g. in a scientific paper like styled presentation)
JavaScript
2
star
13

wasmex-wasmtime

wasmex build on wasmtime (very experimental)
Elixir
2
star
14

myo_scripts

A collection of scripts for the myo (https://www.thalmic.com/en/myo/)
1
star
15

Am-I-Alive

Checks your Webprojects for availability
Ruby
1
star
16

docker-rails-base

A docker image with rails stuff in it
1
star
17

pull_multiple_git_repos

Expect Script that updates (git pull -p) all git repositories
Shell
1
star
18

heic_to_png

Elixir lib to convert HEIC files to PNG
Rust
1
star
19

screenshot_to_pdf

A shell scripts which does screenshots of your presentation/apps/whatever and bundles them into pdf.
Shell
1
star
20

wasm-pdf-demo

A Demo WASM program in Rust which creates a PDF file. Also includes example clients which run the WASM file.
Elixir
1
star
21

walking-the-ulam-spiral

Numbers mapped into a triangle, primes marked black. Patterns emerge!
JavaScript
1
star