Add initial support for WebAssembly Interface Types (#282)

This commit adds initial support for [WebAssembly Interface
Types][proposal] to wasmtime. This is all intended to be quite
experimental, so experimental in fact that even the name of the
[proposal] is still in flux. (this has otherwise been known as "host
bindings" or "webidl bindings" or "wasm bindings").

The goal of this commit is to start adding support the wasmtime set of
crates for WebAssembly Interface Types. A new `wasmtime-interface-types`
crate has been added with very basic support for dynamically invoking
and inspecting the various bindings of a module. This is in turn powered
by the `wasm-webidl-bindings` crate which is shared with the
`wasm-bindgen` CLI tool as a producer of this section.

Currently the only integration in `wasmtime`-the-binary itself is that
when passed the `--invoke` argument the CLI will now attempt to invoke
the target function with arguments as parsed from the command line
itself. For example if you export a function like:

    fn render(&str) -> String

Then passing `--invoke render` will require one argument on the command
line, which is the first argument as a string, and the return value is
printed to the console. This differs from today's interpretation of
`--invoke` where it is a failure if the invoked function takes more than
one argument and the return values are currently ignored.

This is intended to also be the basis of embedding wasmtime in other
contexts which also want to consume WebAssembly interface types. A
Python extension is also added to this repository which implements the
`wasmtime` package on PyPI. This Python extension is intended to make it
as easy as `pip3 install wasmtime` to load a WebAssembly file with
WebAssembly Interface Types into Python. Extensions for other languages
is of course possible as well!

One of the major missing pieces from this is handling imported functions
with interface bindings. Currently the embedding support doesn't have
much ability to support handling imports ergonomically, so it's intended
that this will be included in a follow-up patch.

[proposal]: https://github.com/webassembly/webidl-bindings

Co-authored-by: Yury Delendik <ydelendik@mozilla.com>
This commit is contained in:
Alex Crichton
2019-08-19 06:32:13 -05:00
committed by Till Schneidereit
parent 7009c8dd73
commit af2b4e4946
38 changed files with 2277 additions and 84 deletions

145
misc/wasmtime-py/src/lib.rs Normal file
View File

@@ -0,0 +1,145 @@
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 std::cell::RefCell;
use std::rc::Rc;
use wasmtime_interface_types::ModuleData;
mod code_memory;
mod function;
mod import;
mod instance;
mod memory;
mod module;
mod value;
fn err2py(err: failure::Error) -> PyErr {
let mut desc = err.to_string();
for cause in err.iter_causes() {
desc.push_str("\n");
desc.push_str(" caused by: ");
desc.push_str(&cause.to_string());
}
PyErr::new::<Exception, _>(desc)
}
#[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);
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(())
}