Files
wasmtime/crates/misc/py/src/instance.rs
Alex Crichton e134505b90 Refactor the types.rs types and structures (#681)
* Refactor the `types.rs` types and structures

A few changes applied along the way:

* Documentation added to most methods and types.
* Limits are now stored with the maximum as optional rather than a
  sentinel u32 value for `None`.
* The `Name` type was removed in favor of just using a bare `String`.
* The `Extern` prefix in the varaints of `ExternType` has been removed
  since it was redundant.
* Accessors of `ExternType` variants no longer panic, and unwrapping
  versions were added with "unwrap" in the name.
* Fields and methods named `r#type` were renamed to `ty` to avoid
  requiring a raw identifier to use them.

* Remove `fail-fast: false`

This was left around since the development of GitHub Actions for
wasmtime, but they're no longer needed!

* Fix compilation of the test-programs code

* Fix compilation of wasmtime-py package

* Run rustfmt
2019-12-06 16:19:55 -06:00

64 lines
2.0 KiB
Rust

//! WebAssembly Instance API object.
use crate::function::Function;
use crate::memory::Memory;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use std::rc::Rc;
use wasmtime_interface_types::ModuleData;
#[pyclass]
pub struct Instance {
pub instance: wasmtime::HostRef<wasmtime::Instance>,
pub data: Rc<ModuleData>,
}
#[pymethods]
impl Instance {
#[getter(exports)]
fn get_exports(&mut self) -> PyResult<PyObject> {
let gil = Python::acquire_gil();
let py = gil.python();
let exports = PyDict::new(py);
let module = self.instance.borrow().module().clone();
for (i, e) in module.borrow().exports().iter().enumerate() {
match e.ty() {
wasmtime::ExternType::Func(ft) => {
let mut args_types = Vec::new();
for ty in ft.params().iter() {
args_types.push(ty.clone());
}
let f = Py::new(
py,
Function {
instance: self.instance.clone(),
data: self.data.clone(),
export_name: e.name().to_string(),
args_types,
},
)?;
exports.set_item(e.name().to_string(), f)?;
}
wasmtime::ExternType::Memory(_) => {
let f = Py::new(
py,
Memory {
memory: self.instance.borrow().exports()[i]
.memory()
.unwrap()
.clone(),
},
)?;
exports.set_item(e.name().to_string(), f)?;
}
_ => {
// Skip unknown export type.
continue;
}
}
}
Ok(exports.to_object(py))
}
}