@ruby/wasm-wasi
    Preparing search index...

    Class RubyVM

    A Ruby VM instance

    RubyVM.instantiateComponent and RubyVM.instantiateModule to create a new instance

    Index

    Essentials

    • Instantiate a Ruby VM with the given WebAssembly Core module with WASI Preview 1 implementation.

      Parameters

      Returns Promise<{ vm: RubyVM; instance: Instance }>

      A promise that resolves to the Ruby VM instance and the WebAssembly instance

      import { WASI } from "@bjorn3/browser_wasi_shim";
      const wasip1 = new WASI([], [], []);
      const module = await WebAssembly.compile("./path/to/ruby.wasm");
      const { vm } = await RubyVM.instantiateModule({ module, wasip1 });
    • Instantiate a Ruby VM with the given WebAssembly component with WASI Preview 2 implementation.

      Parameters

      Returns Promise<{ vm: RubyVM }>

      A promise that resolves to the Ruby VM instance

      // First, you need to transpile the Ruby component to a JavaScript module using jco.
      // $ jco transpile --no-wasi-shim --instantiation --valid-lifting-optimization ./ruby.component.wasm -o ./component
      // Then, you can instantiate the Ruby VM with the component:

      import * as wasip2 from "@bytecodealliance/preview2-shim"
      import fs from "fs/promises";
      import path from "path";

      const { instantiate } = await import("./component/ruby.component.js");
      const getCoreModule = async (relativePath) => {
      const buffer = await fs.readFile(path.join("./component", relativePath));
      return WebAssembly.compile(buffer);
      }

      const { vm } = await RubyVM.instantiateComponent({
      instantiate, getCoreModule, wasip2,
      });
    • Runs a string of Ruby code from JavaScript

      Parameters

      • code: string

        The Ruby code to run

      Returns RbValue

      the result of the last expression

      vm.eval("puts 'hello world'");
      const result = vm.eval("1 + 2");
      console.log(result.toString()); // 3
    • Runs a string of Ruby code with top-level JS::Object#await Returns a promise that resolves when execution completes.

      Parameters

      • code: string

        The Ruby code to run

      Returns Promise<RbValue>

      a promise that resolves to the result of the last expression

      const text = await vm.evalAsync(`
      require 'js'
      response = JS.global.fetch('https://example.com').await
      response.text.await
      `);
      console.log(text.toString()); // <html>...</html>

    Low-level initialization

    • Create a new Ruby VM instance with low-level initialization control.

      Returns RubyVM

      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 the Ruby VM with the given command line arguments

      Parameters

      • args: string[] = ...

        The command line arguments to pass to Ruby. Must be an array of strings starting with the Ruby program name.

      Returns void

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

      Parameters

      • instance: 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.

      Returns Promise<void>

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

      Parameters

      • imports: WebAssembly.Imports

        The import object to add to the WebAssembly instance

      Returns void

    Other

    • Wrap a JavaScript value into a Ruby JS::Object

      Parameters

      • value: any

        The value to convert to RbValue

      Returns RbValue

      the RbValue object representing the given JS value

      const hash = vm.eval(`Hash.new`)
      hash.call("store", vm.eval(`"key1"`), vm.wrap(new Object()));