Allow using WASI APIs in the Python extension (#533)

* Allow using WASI APIs in the Python extension

This commit adds support to the Python extension to load the WASI
implementation when a WASI module is seen allowing Python to load
WebAssembly modules that use WASI. This is pretty primitive right now
because there's no way to configure the environment/args/preopens/etc,
but it's hoped to be at least a start!

* rustfmt

* Refactor checks for the wasi module name

* Move the check into `wasmtime-wasi` itself
* Make it conservative for now and match anything that says `wasi*`
* Leave a `FIXME` for improving this later on

* Enable missing feature of winapi for `winx`
This commit is contained in:
Alex Crichton
2019-11-11 13:09:45 -06:00
committed by Dan Gohman
parent 0006a2af95
commit d9edb95218
6 changed files with 54 additions and 16 deletions

View File

@@ -73,6 +73,15 @@ pub fn instantiate(
}
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.
if let Some(module_name) = data.find_wasi_module_name() {
let wasi_handle =
wasmtime_wasi::instantiate_wasi("", context.get_global_exports(), &[], &[], &[])
.map_err(|e| err2py(e.into()))?;
context.name_instance(module_name, wasi_handle);
}
let instance = context
.instantiate_module(None, wasm_data)
.map_err(|e| err2py(e.into()))?;
@@ -111,6 +120,11 @@ pub fn imported_modules<'p>(py: Python<'p>, buffer_source: &PyBytes) -> PyResult
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 => {