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

View File

@@ -0,0 +1,106 @@
//! WebAssembly Instance API object.
use pyo3::prelude::*;
use pyo3::types::PyDict;
use crate::function::Function;
use crate::memory::Memory;
use std::cell::RefCell;
use std::rc::Rc;
use cranelift_codegen::ir;
use cranelift_codegen::ir::types;
use wasmtime_environ::Export;
use wasmtime_interface_types::ModuleData;
use wasmtime_jit::{Context, InstanceHandle};
use wasmtime_runtime::Export as RuntimeExport;
#[pyclass]
pub struct Instance {
pub context: Rc<RefCell<Context>>,
pub instance: InstanceHandle,
pub data: Rc<ModuleData>,
}
fn get_type_annot(ty: ir::Type) -> &'static str {
match ty {
types::I32 => "i32",
types::I64 => "i64",
types::F32 => "f32",
types::F64 => "f64",
_ => panic!("unknown type"),
}
}
#[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 mut function_exports = Vec::new();
let mut memory_exports = Vec::new();
for (name, export) in self.instance.exports() {
match export {
Export::Function(_) => function_exports.push(name.to_string()),
Export::Memory(_) => memory_exports.push(name.to_string()),
_ => {
// Skip unknown export type.
continue;
}
}
}
for name in memory_exports {
if let Some(RuntimeExport::Memory { .. }) = self.instance.lookup(&name) {
let f = Py::new(
py,
Memory {
context: self.context.clone(),
instance: self.instance.clone(),
export_name: name.clone(),
},
)?;
exports.set_item(name, f)?;
} else {
panic!("memory");
}
}
for name in function_exports {
if let Some(RuntimeExport::Function { signature, .. }) = self.instance.lookup(&name) {
let annot = PyDict::new(py);
let mut args_types = Vec::new();
for index in 1..signature.params.len() {
let ty = signature.params[index].value_type;
args_types.push(ty);
annot.set_item(format!("param{}", index - 1), get_type_annot(ty))?;
}
match signature.returns.len() {
0 => (),
1 => {
annot
.set_item("return", get_type_annot(signature.returns[0].value_type))?;
}
_ => panic!("multi-return"),
}
let f = Py::new(
py,
Function {
context: self.context.clone(),
instance: self.instance.clone(),
data: self.data.clone(),
export_name: name.clone(),
args_types,
},
)?;
// FIXME set the f object the `__annotations__` attribute somehow?
let _ = annot;
exports.set_item(name, f)?;
} else {
panic!("function");
}
}
Ok(exports.to_object(py))
}
}