npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ruby/wasm-wasi

v2.6.0

Published

WebAssembly port of CRuby with WASI

Downloads

7,887

Readme

@ruby/wasm-wasi

npm version

This package provides core APIs for Ruby on WebAssembly targeting WASI-compatible environments. WebAssembly binaries are distributed in version-specific packages.

See Cheat Sheet for how to use this package.

Ruby Version Support

| Version | Package | | ------- | --------------------------------------------------------------------------------------------------------------- | | head | @ruby/head-wasm-wasi | | 3.3 | @ruby/3.3-wasm-wasi | | 3.2 | @ruby/3.2-wasm-wasi |

API

consolePrinter

Create a console printer that can be used as an overlay of WASI imports. See the example below for how to use it.

const imports = {
  wasi_snapshot_preview1: wasi.wasiImport,
};
const printer = consolePrinter();
printer.addToImports(imports);

const instance = await WebAssembly.instantiate(module, imports);
printer.setMemory(instance.exports.memory);

Note that the stdout and stderr functions are called with text, not bytes. This means that bytes written to stdout/stderr will be decoded as UTF-8 and then passed to the stdout/stderr functions every time a write occurs without buffering.

Parameters

  • $0 Object (optional, default {stdout:console.log,stderr:console.warn})

    • $0.stdout
    • $0.stderr
  • stdout A function that will be called when stdout is written to. Defaults to console.log.

  • stderr A function that will be called when stderr is written to. Defaults to console.warn.

Returns any An object that can be used as an overlay of WASI imports.

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","-EUTF-8","-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", vm.wrap(4));
console.log(ary.call("sample").toString());

Returns any The result of the method call as a new RbValue.

callAsync

Call a given method that may call JS::Object#await 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 client = vm.eval(`
  require 'js'
  class HttpClient
    def get(url)
      JS.global.fetch(url).await
    end
  end
  HttpClient.new
`);
const response = await client.callAsync(
  "get",
  vm.eval(`"https://example.com"`),
);

Returns any A Promise that resolves to the result of the method call as a new RbValue.

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

RbFatalError

Extends RbError

Error class thrown by Ruby execution when it is not possible to recover. This is usually caused when Ruby VM is in an inconsistent state.