Class: Wasmtime::Func

Inherits:
Object
  • Object
show all
Defined in:
ext/src/ruby_api/func.rs

Overview

Represents a WebAssembly Function

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(store, params, results, &block) {|caller, *args| ... } ⇒ Func

Creates a WebAssembly function from a Ruby block. WebAssembly functions can have 0 or more parameters and results. Each param and result must be a valid WebAssembly type represented as a symbol. The valid symbols are: :i32, :i64, :f32, :f64, :v128, :funcref, :externref.

Examples:

Function that increments an i32:

store = Wasmtime::Store.new(Wasmtime::Engine.new)
Wasmtime::Func.new(store, [:i32], [:i32]) do |_caller, arg1|
  arg1.succ
end

Function with 2 params and 2 results:

store = Wasmtime::Store.new(Wasmtime::Engine.new)
Wasmtime::Func.new(store, [:i32, :i32], [:i32, :i32]) do |_caller, arg1, arg2|
  [arg1.succ, arg2.succ]
end

Parameters:

  • store (Store)
  • params (Array<Symbol>)

    The function's parameters.

  • results (Array<Symbol>)

    The function's results.

  • block (Block)

    The function's implementation.

Yields:

  • (caller, *args)

    The function's body

Yield Parameters:

  • caller (Caller)

    Caller which can be used to interact with the Store.

  • *args (Object)

    Splat of Ruby objects matching the function’s params arity.

Yield Returns:

  • (nil, Object, Array<Object>)

    The return type depends on function’s results arity:

    • 0 => nil
    • 1 => Object
    • 1 => Array<Object>

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/src/ruby_api/func.rs', line 132

pub fn new(args: &[Value]) -> Result<Self, Error> {
    let args = scan_args::<(Obj<Store>, RArray, RArray), (), (), (), (), Proc>(args)?;
    let (store, params, results) = args.required;
    let callable = args.block;

    store.retain(callable.as_value());

    let context = store.context_mut();
    let engine = context.engine();
    let ty = wasmtime::FuncType::new(
        engine,
        params.to_val_type_vec()?,
        results.to_val_type_vec()?,
    );
    let func_closure = make_func_closure(&ty, callable.into());
    let inner = wasmtime::Func::new(context, ty, func_closure);

    Ok(Self::from_inner(store.into(), inner))
}

Instance Method Details

#call(*args) ⇒ nil, ...

Calls a Wasm function.

Examples:

store = Wasmtime::Store.new(Wasmtime::Engine.new)
func = Wasmtime::Func.new(store, [:i32, :i32], [:i32, :i32]) do |_caller, arg1, arg2|
  [arg1.succ, arg2.succ]
end
func.call(1, 2) # => [2, 3]

Parameters:

  • args (Object)

    The arguments to send to the Wasm function. Raises if the arguments do not conform to the Wasm function's parameters.

Returns:

  • (nil, Object, Array<Object>)

    The return type depends on the function's results arity:

    • 0 => nil
    • 1 => Object
    • 1 => Array<Object>



191
192
193
194
# File 'ext/src/ruby_api/func.rs', line 191

pub fn call(&self, args: &[Value]) -> Result<Value, Error> {
    let ruby = Ruby::get().unwrap();
    Self::invoke(&ruby, &self.store, &self.inner, self.gvl, args)
}

#paramsArray<Symbol>

Returns The function's parameter types.

Returns:

  • (Array<Symbol>)

    The function's parameter types.



202
203
204
205
206
207
208
209
210
# File 'ext/src/ruby_api/func.rs', line 202

pub fn params(ruby: &Ruby, rb_self: Obj<Self>) -> Result<RArray, Error> {
    let ty = rb_self.inner.ty(rb_self.store.context()?);
    let len = ty.params().len();
    let mut params = ty.params();
    params.try_fold(ruby.ary_new_capa(len), |array, p| {
        array.push(p.to_sym()?)?;
        Ok(array)
    })
}

#resultsArray<Symbol>

Returns The function's result types.

Returns:

  • (Array<Symbol>)

    The function's result types.



214
215
216
217
218
219
220
221
222
# File 'ext/src/ruby_api/func.rs', line 214

pub fn results(ruby: &Ruby, rb_self: Obj<Self>) -> Result<RArray, Error> {
    let ty = rb_self.inner.ty(rb_self.store.context()?);
    let len = ty.results().len();
    let mut results = ty.results();
    results.try_fold(ruby.ary_new_capa(len), |array, r| {
        array.push(r.to_sym()?)?;
        Ok(array)
    })
}