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
Hashof 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(ruby, 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 120 121 122 |
# 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()? });
let ruby = Ruby::get_with(str);
match export {
Some(export) => export
.wrap_wasmtime_type(&ruby, 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(ruby: &Ruby, rb_self: Obj<Self>) -> Result<RHash, Error> {
let mut ctx = rb_self.store.context_mut();
let hash = ruby.hash_new();
for export in rb_self.inner.exports(&mut ctx) {
let export_name = ruby.str_new(export.name());
let wrapped_store = rb_self.store;
let wrapped_export = export
.into_extern()
.wrap_wasmtime_type(ruby, 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(…).
133 134 135 136 137 138 139 140 141 142 143 |
# File 'ext/src/ruby_api/instance.rs', line 133
pub fn invoke(ruby: &Ruby, rb_self: Obj<Self>, args: &[Value]) -> Result<Value, Error> {
let name = RString::try_convert(*args.first().ok_or_else(|| {
Error::new(
ruby.exception_type_error(),
"wrong number of arguments (given 0, expected 1+)",
)
})?)?;
let func = rb_self.get_func(rb_self.store.context_mut(), unsafe { name.as_str()? })?;
Func::invoke(ruby, &rb_self.store.into(), &func, &args[1..])
}
|