2.6.2 • Published 13 days ago

@ruby/3.2-wasm-wasi v2.6.2

Weekly downloads
-
License
MIT
Repository
github
Last release
13 days ago

@ruby/3.2-wasm-wasi

WebAssembly port of CRuby with WASI.

This package distributes the CRuby 3.2.

Installation

For installing @ruby/3.2-wasm-wasi family, just run this command in your shell:

$ npm install --save @ruby/3.2-wasm-wasi@latest
# or if you want the nightly snapshot
$ npm install --save @ruby/3.2-wasm-wasi@next
# or you can specify the exact snapshot version
$ npm install --save @ruby/3.2-wasm-wasi@2.3.0-2023-11-26-a

Quick Start (for Node.js)

See the example project for more details.

import fs from "fs/promises";
import { DefaultRubyVM } from "@ruby/wasm-wasi/dist/node.cjs.js";

const main = async () => {
  const binary = await fs.readFile(
    //  Tips: Replace the binary with debug info if you want symbolicated stack trace.
    //  (only nightly release for now)
    //  "./node_modules/@ruby/3.2-wasm-wasi/dist/ruby.debug+stdlib.wasm"
    "./node_modules/@ruby/3.2-wasm-wasi/dist/ruby.wasm"
  );
  const module = await WebAssembly.compile(binary);
  const { vm } = await DefaultRubyVM(module);

  vm.eval(`
    luckiness = ["Lucky", "Unlucky"].sample
    puts "You are #{luckiness}"
  `);
};

main();

Then you can run the example project in your terminal:

$ node --experimental-wasi-unstable-preview1 index.node.js

Quick Start (for Browser)

In browser, you need a WASI polyfill. See the example project for more details.

<html>
  <script src="https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@latest/dist/browser.umd.js"></script>
  <script>
    const { DefaultRubyVM } = window["ruby-wasm-wasi"];
    const main = async () => {
      // Fetch and instantiate WebAssembly binary
      const response = await fetch(
        //      Tips: Replace the binary with debug info if you want symbolicated stack trace.
        //      (only nightly release for now)
        //      "https://cdn.jsdelivr.net/npm/@ruby/3.2-wasm-wasi@next/dist/ruby.debug+stdlib.wasm"
        "https://cdn.jsdelivr.net/npm/@ruby/3.2-wasm-wasi@latest/dist/ruby.wasm"
      );
      const buffer = await response.arrayBuffer();
      const module = await WebAssembly.compile(buffer);
      const { vm } = await DefaultRubyVM(module);

      vm.printVersion();
      vm.eval(`
        require "js"
        luckiness = ["Lucky", "Unlucky"].sample
        JS::eval("document.body.innerText = '#{luckiness}'")
      `);
    };

    main();
  </script>
  <body></body>
</html>

GC limitation with JavaScript interoperability

Since JavaScript's GC system and Ruby's GC system are separated and not cooperative, they cannot collect cyclic references between JavaScript and Ruby objects.

The following code will cause a memory leak:

class JNode {
  setRNode(rnode) {
    this.rnode = rnode;
  }
}
jnode = new JNode();

rnode = vm.eval(`
class RNode
  def set_jnode(jnode)
    @jnode = jnode
  end
end
RNode.new
`);

rnode.call("set_jnode", vm.wrap(jnode));
jnode.setRNode(rnode);

APIs

Table of Contents

RubyVM

A Ruby VM instance

Examples

const wasi = new WASI();
const vm = new RubyVM();
const imports = {
  wasi_snapshot_preview1: wasi.wasiImport,
};

vm.addToImports(imports);

const instance = await WebAssembly.instantiate(rubyModule, imports);
await vm.setInstance(instance);
wasi.initialize(instance);
vm.initialize();

initialize

Initialize the Ruby VM with the given command line arguments

Parameters
  • args The command line arguments to pass to Ruby. Must be an array of strings starting with the Ruby program name. (optional, default ["ruby.wasm","--disable-gems","-e_=0"])

setInstance

Set a given instance to interact JavaScript and Ruby's WebAssembly instance. This method must be called before calling Ruby API.

Parameters
  • instance The WebAssembly instance to interact with. Must be instantiated from a Ruby built with JS extension, and built with Reactor ABI instead of command line.

addToImports

Add intrinsic import entries, which is necessary to interact JavaScript and Ruby's WebAssembly instance.

Parameters
  • imports The import object to add to the WebAssembly instance

printVersion

Print the Ruby version to stdout

eval

Runs a string of Ruby code from JavaScript

Parameters
  • code The Ruby code to run
Examples
vm.eval("puts 'hello world'");
const result = vm.eval("1 + 2");
console.log(result.toString()); // 3

Returns any the result of the last expression

evalAsync

Runs a string of Ruby code with top-level JS::Object#await Returns a promise that resolves when execution completes.

Parameters
  • code The Ruby code to run
Examples
const text = await vm.evalAsync(`
  require 'js'
  response = JS.global.fetch('https://example.com').await
  response.text.await
`);
console.log(text.toString()); // <html>...</html>

Returns any a promise that resolves to the result of the last expression

wrap

Wrap a JavaScript value into a Ruby JS::Object

Parameters
  • value The value to convert to RbValue
Examples
const hash = vm.eval(`Hash.new`);
hash.call("store", vm.eval(`"key1"`), vm.wrap(new Object()));

Returns any the RbValue object representing the given JS value

RbValue

A RbValue is an object that represents a value in Ruby

call

Call a given method with given arguments

Parameters
  • callee name of the Ruby method to call
  • args ...any arguments to pass to the method. Must be an array of RbValue
Examples
const ary = vm.eval("[1, 2, 3]");
ary.call("push", 4);
console.log(ary.call("sample").toString());

toPrimitive

Parameters
  • hint Preferred type of the result primitive value. "number", "string", or "default".

toString

Returns a string representation of the value by calling to_s

toJS

Returns a JavaScript object representation of the value by calling to_js.

Returns null if the value is not convertible to a JavaScript object.

RbError

Extends Error

Error class thrown by Ruby execution

Building the package from source

The instructions for building a Ruby targeting WebAssembly are available here.

Then, you can run the following command in your shell:

# Check the directory structure of your Ruby build
$ tree -L 3 path/to/wasm32-unknown-wasi-full-js/
path/to/wasm32-unknown-wasi-full-js/
├── usr
│   └── local
│       ├── bin
│       ├── include
│       ├── lib
│       └── share
└── var
    └── lib
        └── gems
$ ./build-package.sh path/to/wasm32-unknown-wasi-full-js/
Generating "/Users/katei/.ghq/github.com/ruby/ruby.wasm/packages/ruby-3.2-wasm-wasi/src/bindgen/intrinsics.js"
Generating "/Users/katei/.ghq/github.com/ruby/ruby.wasm/packages/ruby-3.2-wasm-wasi/src/bindgen/rb-abi-guest.d.ts"
Generating "/Users/katei/.ghq/github.com/ruby/ruby.wasm/packages/ruby-3.2-wasm-wasi/src/bindgen/rb-abi-guest.js"
Generating "/Users/katei/.ghq/github.com/ruby/ruby.wasm/packages/ruby-3.2-wasm-wasi/src/bindgen/rb-js-abi-host.d.ts"
Generating "/Users/katei/.ghq/github.com/ruby/ruby.wasm/packages/ruby-3.2-wasm-wasi/src/bindgen/rb-js-abi-host.js"

src/index.ts → dist/index.umd.js, dist/index.esm.js, dist/index.cjs.js...
created dist/index.umd.js, dist/index.esm.js, dist/index.cjs.js in 682ms
2.6.2-2024-09-30-a

2 months ago

2.6.2-2024-10-01-a

2 months ago

2.6.2-2024-09-12-a

2 months ago

2.6.2-2024-09-09-a

2 months ago

2.6.2-2024-09-08-a

2 months ago

2.6.2-2024-09-11-a

2 months ago

2.6.2-2024-09-07-a

2 months ago

2.6.2-2024-09-10-a

2 months ago

2.6.2-2024-09-06-a

2 months ago

2.6.2-2024-09-05-a

2 months ago

2.6.2-2024-09-04-a

2 months ago

2.6.2-2024-09-03-a

2 months ago

2.6.2-2024-09-02-a

2 months ago

2.6.2-2024-09-01-a

3 months ago

2.6.2-2024-08-31-a

3 months ago

2.6.2-2024-08-30-a

3 months ago

2.6.2-2024-08-29-a

3 months ago

2.6.2-2024-08-28-a

3 months ago

2.6.2-2024-08-14-a

3 months ago

2.6.1-2024-06-25-b

5 months ago

2.6.2-2024-07-01-a

5 months ago

2.6.1-2024-05-24-a

6 months ago

2.6.2-2024-08-26-a

3 months ago

2.6.2-2024-08-03-a

3 months ago

2.6.1-2024-06-13-a

5 months ago

2.6.2-2024-07-13-a

4 months ago

2.6.1-2024-06-01-a

6 months ago

2.6.2-2024-08-13-a

3 months ago

2.6.1-2024-06-09-a

5 months ago

2.6.2-2024-07-02-a

5 months ago

2.6.2-2024-07-25-a

4 months ago

2.6.1-2024-05-23-a

6 months ago

2.6.1-2024-06-12-a

5 months ago

2.6.2-2024-08-02-a

4 months ago

2.6.2-2024-08-25-a

3 months ago

2.6.2-2024-07-14-a

4 months ago

2.6.2-2024-07-26-a

4 months ago

2.6.2-2024-06-29-a

5 months ago

2.6.1-2024-06-08-a

5 months ago

2.6.2-2024-07-22-a

4 months ago

2.6.2-2024-07-19-a

4 months ago

2.6.1-2024-05-26-a

6 months ago

2.6.1-2024-06-11-a

5 months ago

2.6.2-2024-08-05-a

3 months ago

2.6.2-2024-08-20-a

3 months ago

2.6.2-2024-07-11-a

4 months ago

2.6.2-2024-07-08-a

4 months ago

2.6.2-2024-08-17-a

3 months ago

2.6.1-2024-06-07-a

5 months ago

2.6.2-2024-08-15-a

3 months ago

2.6.2-2024-07-23-a

4 months ago

2.6.1-2024-06-10-a

5 months ago

2.6.1-2024-05-25-a

6 months ago

2.6.1-2024-06-18-a

5 months ago

2.6.2-2024-08-04-a

3 months ago

2.6.2-2024-07-12-a

4 months ago

2.6.2-2024-07-09-a

4 months ago

2.6.2-2024-08-16-a

3 months ago

2.6.1-2024-06-06-a

5 months ago

2.6.1-2024-05-31-a

6 months ago

2.6.2-2024-08-10-a

3 months ago

2.6.2-2024-07-20-a

4 months ago

2.6.2-2024-07-17-a

4 months ago

2.6.2-2024-06-30-a

5 months ago

2.6.1-2024-05-28-a

6 months ago

2.6.2-2024-08-07-a

3 months ago

2.6.2-2024-08-22-a

3 months ago

2.6.0-2024-05-21-a

6 months ago

2.6.2-2024-07-06-a

4 months ago

2.6.2-2024-07-29-a

4 months ago

2.6.2-2024-08-19-a

3 months ago

2.6.1-2024-06-28-a

5 months ago

2.6.1-2024-06-05-b

5 months ago

2.6.1-2024-05-30-a

6 months ago

2.6.1-2024-06-05-a

5 months ago

2.6.2-2024-07-21-a

4 months ago

2.6.2-2024-07-18-a

4 months ago

2.6.2-2024-08-06-a

3 months ago

2.6.1-2024-05-27-a

6 months ago

2.6.2-2024-08-21-a

3 months ago

2.6.1-2024-06-16-a

5 months ago

2.6.2-2024-07-10-a

4 months ago

2.6.2-2024-07-07-a

4 months ago

2.6.2-2024-08-18-a

3 months ago

2.6.2-2024-08-12-a

3 months ago

2.6.1-2024-06-27-a

5 months ago

2.6.1-2024-06-04-a

5 months ago

2.6.2-2024-07-03-a

5 months ago

2.6.2-2024-07-15-a

4 months ago

2.6.1

6 months ago

2.6.2

5 months ago

2.6.2-2024-08-09-a

3 months ago

2.6.2-2024-08-24-a

3 months ago

2.6.2-2024-08-01-a

4 months ago

2.6.1-2024-06-15-a

5 months ago

2.6.1-2024-05-22-a

6 months ago

2.6.2-2024-07-30-a

4 months ago

2.6.2-2024-07-27-a

4 months ago

2.6.1-2024-06-03-a

6 months ago

2.6.2-2024-08-11-a

3 months ago

2.6.1-2024-06-26-a

5 months ago

2.6.2-2024-07-16-a

4 months ago

2.6.2-2024-08-08-a

3 months ago

2.6.1-2024-05-29-a

6 months ago

2.6.2-2024-08-23-a

3 months ago

2.6.1-2024-06-14-a

5 months ago

2.6.1-2024-06-14-b

5 months ago

2.6.1-2024-06-14-c

5 months ago

2.6.2-2024-07-28-a

4 months ago

2.6.2-2024-07-05-a

4 months ago

2.6.0-2024-05-20-a

6 months ago

2.6.0-2024-05-18-a

6 months ago

2.6.0-2024-05-17-a

6 months ago

2.6.0-2024-05-19-a

6 months ago

2.6.0-2024-05-16-a

6 months ago

2.6.0-2024-05-15-a

6 months ago

2.6.0-2024-05-14-a

6 months ago

2.6.0-2024-05-13-a

6 months ago

2.6.0-2024-05-12-a

6 months ago

2.6.0-2024-05-11-a

6 months ago

2.6.0-2024-05-10-a

6 months ago

2.6.0-2024-05-09-a

6 months ago

2.6.0-2024-05-08-a

6 months ago

2.6.0-2024-05-07-a

6 months ago

2.6.0-2024-05-06-a

6 months ago

2.6.0

6 months ago

2.5.2-2024-05-05-a

6 months ago

2.5.2-2024-05-04-a

7 months ago

2.5.2

7 months ago

2.5.1-2024-05-03-a

7 months ago

2.5.1-2024-05-02-a

7 months ago

2.5.1-2024-05-01-a

7 months ago

2.5.1-2024-04-30-a

7 months ago

2.5.1-2024-04-28-a

7 months ago

2.5.1-2024-04-28-b

7 months ago

2.5.1-2024-04-28-c

7 months ago

2.5.1-2024-04-29-a

7 months ago

2.5.1-2024-04-27-a

7 months ago

2.5.1-2024-04-26-a

7 months ago

2.5.1-2024-04-24-a

7 months ago

2.5.1-2024-04-25-a

7 months ago

2.5.1-2024-04-23-a

7 months ago

2.5.1-2024-04-22-a

7 months ago

2.5.1

7 months ago

2.5.1-2024-04-21-a

7 months ago

2.5.0-2024-04-20-a

7 months ago

2.5.0-2024-04-19-a

7 months ago

2.5.0-2024-04-18-a

7 months ago

2.5.0-2024-04-17-a

7 months ago

2.5.0-2024-04-16-a

7 months ago

2.5.0-2024-04-15-a

7 months ago

2.5.0-2024-04-14-a

7 months ago

2.5.0-2024-04-13-a

7 months ago

2.5.0-2024-04-12-a

7 months ago

2.5.0-2024-04-11-a

7 months ago

2.5.0-2024-04-09-a

7 months ago

2.5.0-2024-04-10-a

7 months ago

2.5.0-2024-04-08-a

7 months ago

2.5.0-2024-04-07-a

7 months ago

2.5.0-2024-04-06-a

7 months ago

2.5.0-2024-04-05-a

7 months ago

2.5.0-2024-04-04-a

8 months ago

2.5.0-2024-04-01-a

8 months ago

2.5.0-2024-04-02-a

8 months ago

2.5.0-2024-04-03-a

8 months ago

2.5.0-2024-03-31-a

8 months ago

2.5.0-2024-03-30-a

8 months ago

2.5.0-2024-03-29-a

8 months ago

2.5.0-2024-03-28-a

8 months ago

2.5.0-2024-03-27-a

8 months ago

2.5.0-2024-03-18-a

8 months ago

2.5.0-2024-03-17-a

8 months ago

2.5.0-2024-03-16-a

8 months ago

2.5.0-2024-03-14-a

8 months ago

2.5.0-2024-03-13-a

8 months ago

2.5.0-2024-03-12-a

8 months ago

2.5.0-2024-03-11-a

8 months ago

2.5.0-2024-03-10-a

8 months ago

2.5.0-2024-03-09-a

8 months ago

2.5.0-2024-03-08-a

8 months ago

2.5.0-2024-03-07-a

8 months ago

2.5.0-2024-03-05-a

9 months ago

2.5.0-2024-03-04-a

9 months ago

2.5.0-2024-03-02-a

9 months ago

2.5.0-2024-03-03-a

9 months ago

2.5.0-2024-03-01-a

9 months ago

2.5.0-2024-02-29-a

9 months ago

2.5.0-2024-02-28-a

9 months ago

2.5.0-2024-02-26-a

9 months ago

2.5.0-2024-02-27-a

9 months ago

2.5.0-2024-02-24-a

9 months ago

2.5.0-2024-02-22-a

9 months ago

2.5.0-2024-02-25-a

9 months ago

2.5.0-2024-02-23-a

9 months ago

2.5.0-2024-02-21-a

9 months ago

2.5.0-2024-02-20-a

9 months ago

2.5.0-2024-02-19-a

9 months ago

2.5.0-2024-02-17-a

9 months ago

2.5.0-2024-02-14-a

9 months ago

2.5.0-2024-02-12-a

9 months ago

2.5.0-2024-02-13-a

9 months ago

2.5.0-2024-02-11-a

9 months ago

2.5.0-2024-02-10-a

9 months ago

2.5.0-2024-02-09-a

9 months ago

2.5.0-2024-02-08-a

9 months ago

2.5.0-2024-02-06-a

9 months ago

2.5.0-2024-02-05-a

9 months ago

2.5.0-2024-02-03-a

10 months ago

2.5.0-2024-02-04-a

10 months ago

2.5.0-2024-02-02-a

10 months ago

2.5.0-2024-02-01-a

10 months ago

2.5.0-2024-01-31-a

10 months ago

2.5.0-2024-01-29-a

10 months ago

2.5.0-2024-01-30-a

10 months ago

2.5.0

10 months ago

2.5.0-2024-01-28-a

10 months ago

2.4.1-2024-01-27-a

10 months ago

2.4.1-2024-01-25-a

10 months ago

2.4.1-2024-01-26-a

10 months ago

2.4.1-2024-01-24-a

10 months ago

2.4.1-2024-01-21-a

10 months ago

2.4.1-2024-01-22-a

10 months ago

2.4.1-2024-01-20-a

10 months ago

2.4.1-2024-01-19-a

10 months ago

2.4.1-2024-01-18-a

10 months ago

2.4.1-2024-01-17-a

10 months ago

2.4.1-2024-01-14-a

10 months ago

2.4.1-2024-01-16-a

10 months ago

2.4.1-2024-01-15-a

10 months ago

2.4.1-2024-01-13-a

10 months ago

2.4.1-2024-01-12-a

10 months ago

2.4.1-2024-01-11-a

10 months ago

2.4.1-2024-01-09-a

10 months ago

2.4.1-2024-01-08-a

10 months ago

2.4.1-2024-01-07-a

10 months ago

2.4.1-2024-01-06-a

10 months ago

2.4.1-2024-01-05-a

11 months ago

2.4.1-2024-01-04-b

11 months ago

2.4.1-2023-12-31-a

11 months ago

2.4.1-2023-12-29-a

11 months ago

2.4.1-2023-12-30-a

11 months ago

2.4.1-2023-12-28-a

11 months ago

2.4.1-2023-12-27-a

11 months ago

2.4.1-2023-12-26-a

11 months ago

2.4.0-2023-12-24-a

11 months ago

2.4.0-2023-12-23-a

11 months ago

2.4.1

11 months ago

2.4.1-2023-12-25-a

11 months ago

2.4.0-2023-12-22-a

11 months ago

2.4.0-2023-12-21-a

11 months ago

2.4.0-2023-12-20-a

11 months ago

2.4.0-2023-12-19-a

11 months ago

2.4.0-2023-12-18-a

11 months ago

2.4.0-2023-12-17-a

11 months ago

2.4.0-2023-12-16-a

11 months ago

2.4.0-2023-12-15-a

11 months ago

2.3.0-2023-12-14-a

11 months ago

2.4.0

11 months ago

2.3.0-2023-12-13-a

11 months ago

2.3.0-2023-12-12-a

11 months ago

2.3.0-2023-12-11-a

11 months ago

2.3.0-2023-12-10-a

11 months ago

2.3.0-2023-12-08-a

11 months ago

2.3.0-2023-12-09-a

11 months ago

2.3.0-2023-12-07-a

11 months ago

2.3.0-2023-12-06-a

12 months ago

2.3.0-2023-12-05-b

12 months ago

2.3.0-2023-12-05-a

12 months ago

2.3.0-2023-12-04-a

12 months ago

2.3.0-2023-12-03-a

12 months ago

2.3.0-2023-12-02-a

12 months ago

2.3.0-2023-12-01-a

12 months ago

2.3.0-2023-11-30-b

12 months ago

2.3.0

12 months ago