All Projects β†’ wasmerio β†’ Wasmer Ruby

wasmerio / Wasmer Ruby

Licence: mit
πŸ’ŽπŸ•Έ WebAssembly runtime for Ruby

Programming Languages

ruby
36898 projects - #4 most used programming language
rust
11053 projects

Projects that are alternatives of or similar to Wasmer Ruby

Wasabi
A dynamic analysis framework for WebAssembly programs.
Stars: ✭ 279 (-2.45%)
Mutual labels:  webassembly, wasm
Glas
WebGL in WebAssembly with AssemblyScript
Stars: ✭ 278 (-2.8%)
Mutual labels:  webassembly, wasm
learn-wasm
🎲 Learning WebAssembly
Stars: ✭ 57 (-80.07%)
Mutual labels:  webassembly, wasm
rustexp
A Rust regular expression editor and tester that runs entirely within the browser!
Stars: ✭ 59 (-79.37%)
Mutual labels:  webassembly, wasm
Octopus
Security Analysis tool for WebAssembly module (wasm) and Blockchain Smart Contracts (BTC/ETH/NEO/EOS)
Stars: ✭ 261 (-8.74%)
Mutual labels:  webassembly, wasm
holyc
An easy to use C++ to WASM compiler (Highly-experimental)
Stars: ✭ 33 (-88.46%)
Mutual labels:  webassembly, wasm
swam
WebAssembly engine in Scala
Stars: ✭ 38 (-86.71%)
Mutual labels:  webassembly, wasm
rustwasmc
Tool for building Rust functions for Node.js. Combine the performance of Rust, safety and portability of WebAssembly, and ease of use of JavaScript.
Stars: ✭ 97 (-66.08%)
Mutual labels:  webassembly, wasm
Wasm Git
GIT for nodejs and the browser using https://libgit2.org compiled to WebAssembly with https://emscripten.org
Stars: ✭ 261 (-8.74%)
Mutual labels:  webassembly, wasm
pywasm3
Python bindings for Wasm3, the fastest WebAssembly interpreter
Stars: ✭ 34 (-88.11%)
Mutual labels:  webassembly, wasm
wapc-rust
Rust-based WebAssembly Host Runtime for waPC-compliant modules
Stars: ✭ 75 (-73.78%)
Mutual labels:  webassembly, wasm
wasm-arrays
A couple of helper functions to make WebAssembly array parameters easy to use
Stars: ✭ 33 (-88.46%)
Mutual labels:  webassembly, wasm
go-wasm-examples
Some small examples of using Go and WebAssembly
Stars: ✭ 22 (-92.31%)
Mutual labels:  webassembly, wasm
idris-codegen-wasm
WebAssembly Code Generation Backend for Idris Compiler
Stars: ✭ 79 (-72.38%)
Mutual labels:  webassembly, wasm
warpy
WebAssembly interpreter in RPython
Stars: ✭ 54 (-81.12%)
Mutual labels:  webassembly, wasm
wasm-fizzbuzz
WebAssembly from Scratch: From FizzBuzz to DooM.
Stars: ✭ 1,364 (+376.92%)
Mutual labels:  webassembly, wasm
cabasa
Haxe Framework for WebAssembly
Stars: ✭ 30 (-89.51%)
Mutual labels:  webassembly, wasm
block-aligner
SIMD-accelerated library for computing global and X-drop affine gap penalty sequence-to-sequence or sequence-to-profile alignments using an adaptive block-based algorithm.
Stars: ✭ 58 (-79.72%)
Mutual labels:  webassembly, wasm
golang-wasm
Golang-WASM provides a simple idiomatic, and comprehensive API and bindings for working with WebAssembly for Go and JavaScript developers
Stars: ✭ 57 (-80.07%)
Mutual labels:  webassembly, wasm
x-img-diff-js
🎨 JavaScript library which compares 2 images and detect structural difference information using OpenCV(WebAssembly)
Stars: ✭ 43 (-84.97%)
Mutual labels:  webassembly, wasm

Wasmer logo Wasmer Ruby Wasmer Ruby Gem version Wasmer Ruby Gem Documentation Wasmer Ruby Gem downloads Wasmer Slack Channel

A complete and mature WebAssembly runtime for Ruby based on Wasmer.

Features:

  • Easy to use: The wasmer API mimics the standard WebAssembly API,
  • Fast: wasmer executes the WebAssembly modules as fast as possible, close to native speed,
  • Safe: All calls to WebAssembly will be fast, but more importantly, completely safe and sandboxed.

Note: Rust is required to install the Ruby library (Cargo β€”the build tool for Rustβ€” is used to compile the extension). See how to install Rust.

Install

To install the wasmer Ruby gem, just run this command in your shell:

$ gem install wasmer

View the wasmer gem on RubyGems.

Example

There is a toy program in examples/simple.rs, written in Rust (or any other language that compiles to WebAssembly):

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

After compilation to WebAssembly, the examples/simple.wasm binary file is generated. (Download it).

Then, we can execute it in Ruby (!) with the examples/simple.rb file:

require "wasmer"

bytes = IO.read "simple.wasm", mode: "rb"
instance = Wasmer::Instance.new bytes
puts instance.exports.sum 1, 2

And then, finally, enjoy by running:

$ ruby simple.rb
3

Documentation

Browse Wasmer Ruby Gem docs at https://www.rubydoc.info/gems/wasmer/.

The Instance class

Instantiates a WebAssembly module represented by bytes, and calls exported functions on it:

require "wasmer"

# Get the Wasm module as bytes.
wasm_bytes = IO.read "my_program.wasm", mode: "rb"

# Instantiates the Wasm module.
instance = Wasmer::Instance.new wasm_bytes

# Call a function on it.
result = instance.exports.sum 1, 2

puts result # 3

Exported functions

All exported functions are accessible on the exports getter. Arguments of these functions are automatically casted to WebAssembly values.

Exported memory

The memory getter exposes the Memory class representing the memory of that particular instance, e.g.:

view = instance.memory.uint8_view

Instance.memory throws an exception if no memory is exported.

See below for more information.

Exported globals

The globals getter exposes the ExportedGlobal class represented an exported global variable, e.g.:

# Get the `x` global.
x = instance.globals.x

# Check whether the global is mutable.
assert x.mutable
assert_equal x.value, 7

# Update its value.
x.value = 42

# Tada!
assert_equal x.value, 42

The Memory class

A WebAssembly instance has its own memory, represented by the Memory class. It is accessible by the Wasmer::Instance.memory getter.

The Memory.grow methods allows to grow the memory by a number of pages (of 65kb each).

instance.memory.grow 1

The Memory class offers methods to create views of the memory internal buffer, e.g. uint8_view, int8_view, uint16_view etc. All these methods accept one optional argument: offset, to subset the memory buffer at a particular offset. These methods return respectively a *Array object, i.e. uint8_view returns a Uint8Array object etc.

offset = 7
view = instance.memory.uint8_view offset

puts view[0]

The *Array classes

These classes represent views over a memory buffer of an instance.

Class View buffer as a sequence of… Bytes per element
Int8Array int8 1
Uint8Array uint8 1
Int16Array int16 2
Uint16Array uint16 2
Int32Array int32 4
Uint32Array uint32 4

All these classes share the same implementation. Taking the example of Uint8Array, the class looks like this:

class Uint8Array
    def bytes_per_element
    def length
    def [](index)
    def []=(index, value)
end

Let's see it in action:

require "wasmer"

# Get the Wasm module as bytes.
wasm_bytes = IO.read "my_program.wasm", mode: "rb"

# Instantiates the Wasm module.
instance = Wasmer::Instance.new wasm_bytes

# Call a function that returns a pointer to a string for instance.
pointer = instance.exports.return_string

# Get the memory view, with the offset set to `pointer` (default is 0).
memory = instance.memory.uint8_view pointer

# Read the string pointed by the pointer.

string = ""

memory.each do |char|
  break if char == 0
  string += char.chr
end

puts string # Hello, World!

Notice that *Array treat bytes in little-endian, as required by the WebAssembly specification, Chapter Structure, Section Instructions, Sub-Section Memory Instructions:

All values are read and written in little endian byte order.

Each view shares the same memory buffer internally. Let's have some fun:

int8 = instance.memory.int8_view
int16 = instance.memory.int16_view
int32 = instance.memory.int32_view

               b₁
            β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
int8[0] = 0b00000001
               bβ‚‚
            β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
int8[1] = 0b00000100
               b₃
            β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
int8[2] = 0b00010000
               bβ‚„
            β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
int8[3] = 0b01000000

// No surprise with the following assertions.
                  b₁
               β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
assert_equal 0b00000001, int8[0]
                  bβ‚‚
               β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
assert_equal 0b00000100, int8[1]
                  b₃
               β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
assert_equal 0b00010000, int8[2]
                  bβ‚„
               β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
assert_equal 0b01000000, int8[3]

// The `int16` view reads 2 bytes.
                  bβ‚‚       b₁
               β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β” β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
assert_equal 0b00000100_00000001, int16[0]
                  bβ‚„       b₃
               β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β” β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
assert_equal 0b01000000_00010000, int16[1]

// The `int32` view reads 4 bytes.
                  bβ‚„       b₃       bβ‚‚       b₁
               β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β” β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β” β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β” β”Œβ”¬β”¬β”¬β”¬β”¬β”¬β”
assert_equal 0b01000000_00010000_00000100_00000001, int32[0]

The Module class

The Module class contains one static method validate, that checks whether the given bytes represent valid WebAssembly bytes:

require "wasmer"

wasm_bytes = IO.read "my_program.wasm", mode: "rb"

if not Wasmer::Module.validate wasm_bytes
    puts "The program seems corrupted."
end

This function returns a boolean.

Install and Testing

To compile the entire project, run the following commands:

$ just build
$ just test
$ ruby examples/simple.rb

(Yes, you need just).

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 […].

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].