* Migrate from failure to thiserror and anyhow The failure crate invents its own traits that don't use std::error::Error (because failure predates certain features added to Error); this prevents using ? on an error from failure in a function using Error. The thiserror and anyhow crates integrate with the standard Error trait instead. This change does not attempt to semantically change or refactor the approach to error-handling in any portion of the code, to ensure that the change remains straightforward to review. Modules using specific differentiated error types move from failure_derive and derive(Fail) to thiserror and derive(Error). Modules boxing all errors opaquely move from failure::Error to anyhow. Modules using String as an error type continue to do so. Code using unwrap or expect continues to do so. Drop Display implementations when thiserror can easily derive an identical instance. Drop manual traversal of iter_causes; anyhow's Debug instance prints the chain of causes by default. Use anyhow's type alias anyhow::Result<T> in place of std::result::Result<T, anyhow::Error> whenever possible. * wasm2obj: Simplify error handling using existing messages handle_module in wasm2obj manually maps cranelift_codegen::isa::LookupError values to strings, but LookupError values already have strings that say almost exactly the same thing. Rely on the strings from cranelift. * wasmtime: Rely on question-mark-in-main The main() wrapper around rmain() completely matches the behavior of question-mark-in-main (print error to stderr and return 1), so switch to question-mark-in-main. * Update to walrus 0.13 and wasm-webidl-bindings 0.6 Both crates switched from failure to anyhow; updating lets us avoid a translation from failure to anyhow within wasmtime-interface-types.
139 lines
3.8 KiB
Rust
139 lines
3.8 KiB
Rust
use pyo3::exceptions::Exception;
|
|
use pyo3::prelude::*;
|
|
use pyo3::types::{PyBytes, PyDict, PySet};
|
|
use pyo3::wrap_pyfunction;
|
|
|
|
use crate::import::into_instance_from_obj;
|
|
use crate::instance::Instance;
|
|
use crate::memory::Memory;
|
|
use crate::module::Module;
|
|
use core::cell::RefCell;
|
|
use std::rc::Rc;
|
|
use wasmtime_interface_types::ModuleData;
|
|
|
|
mod function;
|
|
mod import;
|
|
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))
|
|
}
|
|
}
|
|
|
|
/// 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 generate_debug_info = false;
|
|
|
|
let isa = {
|
|
let isa_builder = cranelift_native::builder().map_err(|s| PyErr::new::<Exception, _>(s))?;
|
|
let flag_builder = cranelift_codegen::settings::builder();
|
|
isa_builder.finish(cranelift_codegen::settings::Flags::new(flag_builder))
|
|
};
|
|
|
|
let mut context = wasmtime_jit::Context::with_isa(isa, wasmtime_jit::CompilationStrategy::Auto);
|
|
context.set_debug_info(generate_debug_info);
|
|
let global_exports = context.get_global_exports();
|
|
|
|
for (name, obj) in import_obj.iter() {
|
|
context.name_instance(
|
|
name.to_string(),
|
|
into_instance_from_obj(py, global_exports.clone(), obj)?,
|
|
)
|
|
}
|
|
|
|
let data = Rc::new(ModuleData::new(wasm_data).map_err(err2py)?);
|
|
let instance = context
|
|
.instantiate_module(None, wasm_data)
|
|
.map_err(|e| err2py(e.into()))?;
|
|
|
|
let module = Py::new(
|
|
py,
|
|
Module {
|
|
module: instance.module(),
|
|
},
|
|
)?;
|
|
|
|
let instance = Py::new(
|
|
py,
|
|
Instance {
|
|
context: Rc::new(RefCell::new(context)),
|
|
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();
|
|
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(())
|
|
}
|