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>
67 lines
1.8 KiB
Rust
67 lines
1.8 KiB
Rust
//! Support for a calling of a bounds (exported) function.
|
|
|
|
use pyo3::prelude::*;
|
|
use pyo3::types::PyTuple;
|
|
|
|
use crate::value::{pyobj_to_value, value_to_pyobj};
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
|
|
use cranelift_codegen::ir;
|
|
use wasmtime_interface_types::ModuleData;
|
|
use wasmtime_jit::{Context, InstanceHandle};
|
|
use wasmtime_runtime::Export;
|
|
|
|
// TODO support non-export functions
|
|
#[pyclass]
|
|
pub struct Function {
|
|
pub context: Rc<RefCell<Context>>,
|
|
pub instance: InstanceHandle,
|
|
pub export_name: String,
|
|
pub args_types: Vec<ir::Type>,
|
|
pub data: Rc<ModuleData>,
|
|
}
|
|
|
|
impl Function {
|
|
pub fn get_signature(&self) -> ir::Signature {
|
|
let mut instance = self.instance.clone();
|
|
if let Some(Export::Function { signature, .. }) = instance.lookup(&self.export_name) {
|
|
signature
|
|
} else {
|
|
panic!()
|
|
}
|
|
}
|
|
}
|
|
|
|
#[pymethods]
|
|
impl Function {
|
|
#[__call__]
|
|
#[args(args = "*")]
|
|
fn call(&self, py: Python, args: &PyTuple) -> PyResult<PyObject> {
|
|
let mut runtime_args = Vec::new();
|
|
for item in args.iter() {
|
|
runtime_args.push(pyobj_to_value(py, item)?);
|
|
}
|
|
let mut instance = self.instance.clone();
|
|
let mut cx = self.context.borrow_mut();
|
|
let results = self
|
|
.data
|
|
.invoke(
|
|
&mut cx,
|
|
&mut instance,
|
|
self.export_name.as_str(),
|
|
&runtime_args,
|
|
)
|
|
.map_err(crate::err2py)?;
|
|
let mut py_results = Vec::new();
|
|
for result in results {
|
|
py_results.push(value_to_pyobj(py, result)?);
|
|
}
|
|
if py_results.len() == 1 {
|
|
Ok(py_results[0].clone_ref(py))
|
|
} else {
|
|
Ok(PyTuple::new(py, py_results).to_object(py))
|
|
}
|
|
}
|
|
}
|