* Reimplement `wasmtime-wasi` on top of `wasmtime` This commit reimplements the `wasmtime-wasi` crate on top of the `wasmtime` API crate, instead of being placed on top of the `wasmtime-*` family of internal crates. The purpose here is to continue to exercise the API as well as avoid usage of internals wherever possible and instead use the safe API as much as possible. The `wasmtime-wasi` crate's API has been updated as part of this PR as well. The general outline of it is now: * Each module snapshot has a `WasiCtxBuilder`, `WasiCtx`, and `Wasi` type. * The `WasiCtx*` types are reexported from `wasi-common`. * The `Wasi` type is synthesized by the `wig` crate's procedural macro * The `Wasi` type exposes one constructor which takes a `Store` and a `WasiCtx`, and produces a `Wasi` * Each `Wasi` struct fields for all the exported functions in that wasi module. They're all public an they all have type `wasmtime::Func` * The `Wasi` type has a `get_export` method to fetch an struct field by name. The intention here is that we can continue to make progress on #727 by integrating WASI construction into the `Instance::new` experience, but it requires everything to be part of the same system! The main oddity required by the `wasmtime-wasi` crate is that it needs access to the caller's `memory` export, if any. This is currently done with a bit of a hack and is expected to go away once interface types are more fully baked in. * Remove now no-longer-necessary APIs from `wasmtime` * rustfmt * Rename to from_abi
179 lines
5.6 KiB
Rust
179 lines
5.6 KiB
Rust
use crate::function::{wrap_into_pyfunction, Function};
|
|
use crate::instance::Instance;
|
|
use crate::memory::Memory;
|
|
use crate::module::Module;
|
|
use pyo3::exceptions::Exception;
|
|
use pyo3::prelude::*;
|
|
use pyo3::types::{PyAny, PyBytes, PyDict, PySet};
|
|
use pyo3::wrap_pyfunction;
|
|
use std::rc::Rc;
|
|
use wasmtime_interface_types::ModuleData;
|
|
|
|
mod function;
|
|
mod instance;
|
|
mod memory;
|
|
mod module;
|
|
mod value;
|
|
|
|
fn err2py(err: anyhow::Error) -> PyErr {
|
|
PyErr::new::<Exception, _>(format!("{:?}", err))
|
|
}
|
|
|
|
#[pyclass]
|
|
pub struct InstantiateResultObject {
|
|
instance: Py<Instance>,
|
|
module: Py<Module>,
|
|
}
|
|
|
|
#[pymethods]
|
|
impl InstantiateResultObject {
|
|
#[getter(instance)]
|
|
fn get_instance(&self) -> PyResult<Py<Instance>> {
|
|
let gil = Python::acquire_gil();
|
|
let py = gil.python();
|
|
Ok(self.instance.clone_ref(py))
|
|
}
|
|
|
|
#[getter(module)]
|
|
fn get_module(&self) -> PyResult<Py<Module>> {
|
|
let gil = Python::acquire_gil();
|
|
let py = gil.python();
|
|
Ok(self.module.clone_ref(py))
|
|
}
|
|
}
|
|
|
|
fn find_export_in(obj: &PyAny, store: &wasmtime::Store, name: &str) -> PyResult<wasmtime::Extern> {
|
|
let obj = obj.cast_as::<PyDict>()?;
|
|
|
|
Ok(if let Some(item) = obj.get_item(name) {
|
|
if item.is_callable() {
|
|
if item.get_type().is_subclass::<Function>()? {
|
|
let wasm_fn = item.cast_as::<Function>()?;
|
|
wasm_fn.func().into()
|
|
} else {
|
|
wrap_into_pyfunction(store, item)?.into()
|
|
}
|
|
} else if item.get_type().is_subclass::<Memory>()? {
|
|
let wasm_mem = item.cast_as::<Memory>()?;
|
|
wasm_mem.memory.clone().into()
|
|
} else {
|
|
return Err(PyErr::new::<Exception, _>(format!(
|
|
"unsupported import type {}",
|
|
name
|
|
)));
|
|
}
|
|
} else {
|
|
return Err(PyErr::new::<Exception, _>(format!(
|
|
"import {} is not found",
|
|
name
|
|
)));
|
|
})
|
|
}
|
|
|
|
/// WebAssembly instantiate API method.
|
|
#[pyfunction]
|
|
pub fn instantiate(
|
|
py: Python,
|
|
buffer_source: &PyBytes,
|
|
import_obj: &PyDict,
|
|
) -> PyResult<Py<InstantiateResultObject>> {
|
|
let wasm_data = buffer_source.as_bytes();
|
|
|
|
let engine = wasmtime::Engine::new(&wasmtime::Config::new().wasm_multi_value(true));
|
|
let store = wasmtime::Store::new(&engine);
|
|
|
|
let module = wasmtime::Module::new(&store, wasm_data).map_err(err2py)?;
|
|
|
|
let data = Rc::new(ModuleData::new(wasm_data).map_err(err2py)?);
|
|
|
|
// If this module expects to be able to use wasi then go ahead and hook
|
|
// that up into the imported crates.
|
|
let wasi = if let Some(module_name) = data.find_wasi_module_name() {
|
|
let cx = wasmtime_wasi::WasiCtxBuilder::new()
|
|
.build()
|
|
.map_err(|e| err2py(e.into()))?;
|
|
let wasi = wasmtime_wasi::Wasi::new(&store, cx);
|
|
Some((module_name, wasi))
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let mut imports: Vec<wasmtime::Extern> = Vec::new();
|
|
for i in module.imports() {
|
|
let module_name = i.module();
|
|
if let Some(m) = import_obj.get_item(module_name) {
|
|
let e = find_export_in(m, &store, i.name())?;
|
|
imports.push(e);
|
|
} else if wasi.is_some() && module_name == wasi.as_ref().unwrap().0 {
|
|
let e = wasi
|
|
.as_ref()
|
|
.unwrap()
|
|
.1
|
|
.get_export(i.name())
|
|
.ok_or_else(|| {
|
|
PyErr::new::<Exception, _>(format!("wasi export {} is not found", i.name(),))
|
|
})?;
|
|
imports.push(e.clone().into());
|
|
} else {
|
|
return Err(PyErr::new::<Exception, _>(format!(
|
|
"imported module {} is not found",
|
|
module_name
|
|
)));
|
|
}
|
|
}
|
|
|
|
let instance = wasmtime::Instance::new(&module, &imports)
|
|
.map_err(|t| PyErr::new::<Exception, _>(format!("instantiated with trap {:?}", t)))?;
|
|
|
|
let module = Py::new(py, Module { module })?;
|
|
|
|
let instance = Py::new(py, Instance { instance, data })?;
|
|
|
|
Py::new(py, InstantiateResultObject { instance, module })
|
|
}
|
|
|
|
#[pyfunction]
|
|
pub fn imported_modules<'p>(py: Python<'p>, buffer_source: &PyBytes) -> PyResult<&'p PyDict> {
|
|
let wasm_data = buffer_source.as_bytes();
|
|
let dict = PyDict::new(py);
|
|
// TODO: error handling
|
|
let mut parser = wasmparser::ModuleReader::new(wasm_data).unwrap();
|
|
while !parser.eof() {
|
|
let section = parser.read().unwrap();
|
|
match section.code {
|
|
wasmparser::SectionCode::Import => {}
|
|
_ => continue,
|
|
};
|
|
let reader = section.get_import_section_reader().unwrap();
|
|
for import in reader {
|
|
let import = import.unwrap();
|
|
// Skip over wasi-looking imports since those aren't imported from
|
|
// Python but rather they're implemented natively.
|
|
if wasmtime_wasi::is_wasi_module(import.module) {
|
|
continue;
|
|
}
|
|
let set = match dict.get_item(import.module) {
|
|
Some(set) => set.downcast_ref::<PySet>().unwrap(),
|
|
None => {
|
|
let set = PySet::new::<PyObject>(py, &[])?;
|
|
dict.set_item(import.module, set)?;
|
|
set
|
|
}
|
|
};
|
|
set.add(import.field)?;
|
|
}
|
|
}
|
|
Ok(dict)
|
|
}
|
|
|
|
#[pymodule]
|
|
fn lib_wasmtime(_: Python, m: &PyModule) -> PyResult<()> {
|
|
m.add_class::<Instance>()?;
|
|
m.add_class::<Memory>()?;
|
|
m.add_class::<Module>()?;
|
|
m.add_class::<InstantiateResultObject>()?;
|
|
m.add_wrapped(wrap_pyfunction!(instantiate))?;
|
|
m.add_wrapped(wrap_pyfunction!(imported_modules))?;
|
|
Ok(())
|
|
}
|