Class: Wasmtime::Instance
- Inherits:
-
Object
- Object
- Wasmtime::Instance
- Defined in:
- ext/src/ruby_api/instance.rs
Overview
Represents a WebAssembly instance.
Class Method Summary collapse
Instance Method Summary collapse
-
#export(name) ⇒ Extern?
Get an export by name.
-
#exports ⇒ Hash{String => Extern}
Returns a
Hash
of exports where keys are export names as Strings and values are Externs. -
#invoke(name, *args) ⇒ nil, ...
Retrieves a Wasm function from the instance and calls it.
Class Method Details
.new(store, mod, imports = []) ⇒ Instance
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'ext/src/ruby_api/instance.rs', line 41
pub fn new(ruby: &Ruby, args: &[Value]) -> Result<Self, Error> {
let args =
scan_args::scan_args::<(Obj<Store>, &Module), (Option<Value>,), (), (), (), ()>(args)?;
let (wrapped_store, module) = args.required;
let mut context = wrapped_store.context_mut();
let imports = args
.optional
.0
.and_then(|v| if v.is_nil() { None } else { Some(v) });
let imports: Vec<Extern> = match imports {
Some(arr) => {
let arr = RArray::try_convert(arr)?;
let mut imports = Vec::with_capacity(arr.len());
// SAFETY: arr won't get gc'd (it's on the stack) and we don't mutate it.
for import in unsafe { arr.as_slice() } {
context.data_mut().retain(*import);
imports.push(import.to_extern(ruby)?);
}
imports
}
None => vec![],
};
let module = module.get();
let inner = InstanceImpl::new(context, module, &imports)
.map_err(|e| StoreContextValue::from(wrapped_store).handle_wasm_error(e))?;
Ok(Self {
inner,
store: wrapped_store,
})
}
|
Instance Method Details
#export(name) ⇒ Extern?
Get an export by name.
111 112 113 114 115 116 117 118 119 |
# File 'ext/src/ruby_api/instance.rs', line 111
pub fn export(&self, str: RString) -> Result<Option<super::externals::Extern>, Error> {
let export = self
.inner
.get_export(self.store.context_mut(), unsafe { str.as_str()? });
match export {
Some(export) => export.wrap_wasmtime_type(self.store.into()).map(Some),
None => Ok(None),
}
}
|
#exports ⇒ Hash{String => Extern}
Returns a Hash
of exports where keys are export names as Strings and values are Externs.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'ext/src/ruby_api/instance.rs', line 89
pub fn exports(&self) -> Result<RHash, Error> {
let mut ctx = self.store.context_mut();
let hash = RHash::new();
for export in self.inner.exports(&mut ctx) {
let export_name = RString::new(export.name());
let wrapped_store = self.store;
let wrapped_export = export
.into_extern()
.wrap_wasmtime_type(wrapped_store.into())?;
hash.aset(export_name, wrapped_export)?;
}
Ok(hash)
}
|
#invoke(name, *args) ⇒ nil, ...
Retrieves a Wasm function from the instance and calls it. Essentially a shortcut for instance.export(name).call(…).
130 131 132 133 134 135 136 137 138 139 140 |
# File 'ext/src/ruby_api/instance.rs', line 130
pub fn invoke(&self, args: &[Value]) -> Result<Value, Error> {
let name = RString::try_convert(*args.first().ok_or_else(|| {
Error::new(
magnus::exception::type_error(),
"wrong number of arguments (given 0, expected 1+)",
)
})?)?;
let func = self.get_func(self.store.context_mut(), unsafe { name.as_str()? })?;
Func::invoke(&self.store.into(), &func, &args[1..])
}
|