Class: Wasmtime::Func
- Inherits:
-
Object
- Object
- Wasmtime::Func
- Defined in:
- ext/src/ruby_api/func.rs
Overview
Represents a WebAssembly Function
Class Method Summary collapse
-
.new(store, params, results, &block) {|caller, *args| ... } ⇒ Func
Creates a WebAssembly function from a Ruby block.
Instance Method Summary collapse
-
#call(*args) ⇒ nil, ...
Calls a Wasm function.
-
#params ⇒ Array<Symbol>
The function’s parameter types.
-
#results ⇒ Array<Symbol>
The function’s result types.
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
.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'ext/src/ruby_api/func.rs', line 93
pub fn new(args: &[Value]) -> Result<Self, Error> {
let args = scan_args::<(Value, RArray, RArray), (), (), (), (), Proc>(args)?;
let (s, params, results) = args.required;
let callable = args.block;
let wrapped_store: Obj<Store> = s.try_convert()?;
let store = wrapped_store.get();
store.retain(callable.into());
let context = store.context_mut();
let ty = wasmtime::FuncType::new(params.to_val_type_vec()?, results.to_val_type_vec()?);
let func_closure = make_func_closure(&ty, callable);
let inner = wasmtime::Func::new(context, ty, func_closure);
Ok(Self {
store: wrapped_store.into(),
inner,
})
}
|
Instance Method Details
#call(*args) ⇒ nil, ...
Calls a Wasm function.
140 141 142 |
# File 'ext/src/ruby_api/func.rs', line 140
pub fn call(&self, args: &[Value]) -> Result<Value, Error> {
Self::invoke(&self.store, &self.inner, args)
}
|
#params ⇒ Array<Symbol>
Returns The function’s parameter types.
150 151 152 153 154 155 156 157 158 |
# File 'ext/src/ruby_api/func.rs', line 150
pub fn params(&self) -> Result<RArray, Error> {
let params = self
.inner
.ty(self.store.context()?)
.params()
.map(ToSym::to_sym)
.collect();
Ok(params)
}
|
#results ⇒ Array<Symbol>
Returns The function’s result types.
162 163 164 165 166 167 168 169 170 |
# File 'ext/src/ruby_api/func.rs', line 162
pub fn results(&self) -> Result<RArray, Error> {
let results = self
.inner
.ty(self.store.context()?)
.results()
.map(ToSym::to_sym)
.collect();
Ok(results)
}
|