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
.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'ext/src/ruby_api/func.rs', line 127
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 {
store: store.into(),
inner,
})
}
|
Instance Method Details
#call(*args) ⇒ nil, ...
Calls a Wasm function.
177 178 179 |
# File 'ext/src/ruby_api/func.rs', line 177
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.
187 188 189 190 191 192 193 194 195 |
# File 'ext/src/ruby_api/func.rs', line 187
pub fn params(&self) -> Result<RArray, Error> {
let ty = self.inner.ty(self.store.context()?);
let len = ty.params().len();
let mut params = ty.params();
params.try_fold(RArray::with_capacity(len), |array, p| {
array.push(p.to_sym()?)?;
Ok(array)
})
}
|
#results ⇒ Array<Symbol>
Returns The function’s result types.
199 200 201 202 203 204 205 206 207 |
# File 'ext/src/ruby_api/func.rs', line 199
pub fn results(&self) -> Result<RArray, Error> {
let ty = self.inner.ty(self.store.context()?);
let len = ty.results().len();
let mut results = ty.results();
results.try_fold(RArray::with_capacity(len), |array, r| {
array.push(r.to_sym()?)?;
Ok(array)
})
}
|