Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bytecodealliance/wasmtime-rb
Ruby WebAssembly runtime powered by Wasmtime
https://github.com/bytecodealliance/wasmtime-rb
ruby rust wasm wasmtime
Last synced: 2 months ago
JSON representation
Ruby WebAssembly runtime powered by Wasmtime
- Host: GitHub
- URL: https://github.com/bytecodealliance/wasmtime-rb
- Owner: bytecodealliance
- License: apache-2.0
- Created: 2022-08-10T13:32:51.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-21T16:16:12.000Z (3 months ago)
- Last Synced: 2024-11-21T17:23:16.242Z (3 months ago)
- Topics: ruby, rust, wasm, wasmtime
- Language: Rust
- Homepage: https://bytecodealliance.github.io/wasmtime-rb/latest/
- Size: 4.29 MB
- Stars: 96
- Watchers: 8
- Forks: 19
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
## Goal
`wasmtime-rb`'s goal is to expose the full power of Wasmtime in Ruby with
minimal overhead, serving as a foundation layer for other projects or gems.## Installation
Add the `wasmtime` gem to your Gemfile and run `bundle install`:
```ruby
gem "wasmtime"
```Alternatively, you can install the gem manually:
```sh
gem install wasmtime
```### Precompiled gems
We recommend installing the `wasmtime` precompiled gems available for Linux, macOS, and Windows. Installing a precompiled gem avoids the need to compile from source code, which is generally slower and less reliable.
When installing the `wasmtime` gem for the first time using `bundle install`, Bundler will automatically download the precompiled gem for your current platform. However, you will need to inform Bundler of any additional platforms you plan to use.
To do this, lock your Bundle to the required platforms you will need from the list of supported platforms below:
```sh
bundle lock --add-platform x86_64-linux # Standard Linux (e.g. Heroku, GitHub Actions, etc.)
bundle lock --add-platform x86_64-linux-musl # MUSL Linux deployments (i.e. Alpine Linux)
bundle lock --add-platform aarch64-linux # ARM64 Linux deployments (i.e. AWS Graviton2)
bundle lock --add-platform x86_64-darwin # Intel MacOS (i.e. pre-M1)
bundle lock --add-platform arm64-darwin # Apple Silicon MacOS (i.e. M1)
```## Usage
Example usage:
```ruby
require "wasmtime"# Create an engine. Generally, you only need a single engine and can
# re-use it throughout your program.
engine = Wasmtime::Engine.new# Compile a Wasm module from either Wasm or WAT. The compiled module is
# specific to the Engine's configuration.
mod = Wasmtime::Module.new(engine, <<~WAT)
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)
WAT# Create a store. Store can keep state to be re-used in Funcs.
store = Wasmtime::Store.new(engine, {count: 0})# Define a Wasm function from Ruby code.
func = Wasmtime::Func.new(store, [], []) do |caller|
puts "Hello from Func!"
caller.store_data[:count] += 1
puts "Ran #{caller.store_data[:count]} time(s)"
end# Build the Wasm instance by providing its imports.
instance = Wasmtime::Instance.new(store, mod, [func])# Run the `run` export.
instance.invoke("run")# Or: get the `run` export and call it.
instance.export("run").to_func.call
```For more, see [examples](https://github.com/bytecodealliance/wasmtime-rb/tree/main/examples)
or the [API documentation](https://bytecodealliance.github.io/wasmtime-rb/latest/).