* Update the C API with module linking support
This commit does everything necessary (ideally) to support the module
linking proposal in the C API. The changes here are:
* New `wasm_{module,instance}type_t` types and accessors
* New `wasm_{module,instance}_type` functions
* Conversions between `wasm_extern_t` and `wasm_{instance,module}_t`, as
well as `wasm_externtype_t` and the new types.
* Addition of `WASM_EXTERN_{MODULE,INSTANCE}` constants
* New `wasm_config_t` modifier to enable/disable module linking
With these functions it should be possible to pass instances/modules to
instances and also acquire them from exports. Altogether this should
enable everything for module linking.
An important point for this is that I've opted to add all these items
under the `wasm_*` name prefix instead of `wasmtime_*`. I've done this
since they're all following the idioms of existing APIs and while not
standard the intention would be to standardize them (unlike many other
Wasmtime-specific APIs).
cc #2094
* Appease doxygen
138 lines
3.9 KiB
Rust
138 lines
3.9 KiB
Rust
use crate::{bad_utf8, handle_result, wasmtime_error_t};
|
|
use crate::{wasm_extern_t, wasm_store_t};
|
|
use crate::{wasm_func_t, wasm_instance_t, wasm_module_t, wasm_name_t, wasm_trap_t};
|
|
use std::str;
|
|
use wasmtime::Linker;
|
|
|
|
#[repr(C)]
|
|
pub struct wasmtime_linker_t {
|
|
linker: Linker,
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_new(store: &wasm_store_t) -> Box<wasmtime_linker_t> {
|
|
Box::new(wasmtime_linker_t {
|
|
linker: Linker::new(&store.store),
|
|
})
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_allow_shadowing(
|
|
linker: &mut wasmtime_linker_t,
|
|
allow_shadowing: bool,
|
|
) {
|
|
linker.linker.allow_shadowing(allow_shadowing);
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_delete(_linker: Box<wasmtime_linker_t>) {}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_define(
|
|
linker: &mut wasmtime_linker_t,
|
|
module: &wasm_name_t,
|
|
name: &wasm_name_t,
|
|
item: &wasm_extern_t,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
let linker = &mut linker.linker;
|
|
let module = match str::from_utf8(module.as_slice()) {
|
|
Ok(s) => s,
|
|
Err(_) => return bad_utf8(),
|
|
};
|
|
let name = match str::from_utf8(name.as_slice()) {
|
|
Ok(s) => s,
|
|
Err(_) => return bad_utf8(),
|
|
};
|
|
let item = item.which.clone();
|
|
handle_result(linker.define(module, name, item), |_linker| ())
|
|
}
|
|
|
|
#[cfg(feature = "wasi")]
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_define_wasi(
|
|
linker: &mut wasmtime_linker_t,
|
|
instance: &crate::wasi_instance_t,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
let linker = &mut linker.linker;
|
|
handle_result(instance.add_to_linker(linker), |_linker| ())
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_define_instance(
|
|
linker: &mut wasmtime_linker_t,
|
|
name: &wasm_name_t,
|
|
instance: &wasm_instance_t,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
let linker = &mut linker.linker;
|
|
let name = match str::from_utf8(name.as_slice()) {
|
|
Ok(s) => s,
|
|
Err(_) => return bad_utf8(),
|
|
};
|
|
handle_result(linker.instance(name, instance.instance()), |_linker| ())
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_instantiate(
|
|
linker: &wasmtime_linker_t,
|
|
module: &wasm_module_t,
|
|
instance_ptr: &mut *mut wasm_instance_t,
|
|
trap_ptr: &mut *mut wasm_trap_t,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
let result = linker.linker.instantiate(module.module());
|
|
super::instance::handle_instantiate(result, instance_ptr, trap_ptr)
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_module(
|
|
linker: &mut wasmtime_linker_t,
|
|
name: &wasm_name_t,
|
|
module: &wasm_module_t,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
let linker = &mut linker.linker;
|
|
let name = match str::from_utf8(name.as_slice()) {
|
|
Ok(s) => s,
|
|
Err(_) => return bad_utf8(),
|
|
};
|
|
handle_result(linker.module(name, module.module()), |_linker| ())
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_get_default(
|
|
linker: &wasmtime_linker_t,
|
|
name: &wasm_name_t,
|
|
func: &mut *mut wasm_func_t,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
let linker = &linker.linker;
|
|
let name = match str::from_utf8(name.as_slice()) {
|
|
Ok(s) => s,
|
|
Err(_) => return bad_utf8(),
|
|
};
|
|
handle_result(linker.get_default(name), |f| {
|
|
*func = Box::into_raw(Box::new(f.into()))
|
|
})
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_linker_get_one_by_name(
|
|
linker: &wasmtime_linker_t,
|
|
module: &wasm_name_t,
|
|
name: Option<&wasm_name_t>,
|
|
item_ptr: &mut *mut wasm_extern_t,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
let linker = &linker.linker;
|
|
let module = match str::from_utf8(module.as_slice()) {
|
|
Ok(s) => s,
|
|
Err(_) => return bad_utf8(),
|
|
};
|
|
let name = match name {
|
|
Some(name) => match str::from_utf8(name.as_slice()) {
|
|
Ok(s) => Some(s),
|
|
Err(_) => return bad_utf8(),
|
|
},
|
|
None => None,
|
|
};
|
|
handle_result(linker.get_one_by_name(module, name), |which| {
|
|
*item_ptr = Box::into_raw(Box::new(wasm_extern_t { which }))
|
|
})
|
|
}
|