Propagate optional import names to the wasmtime/C API

With the module linking proposal the field name on imports is now
optional, and only the module is required to be specified. This commit
propagates this API change to the boundary of wasmtime's API, ensuring
consumers are aware of what's optional with module linking and what
isn't. Note that it's expected that all existing users will either
update accordingly or unwrap the result since module linking is
presumably disabled.
This commit is contained in:
Alex Crichton
2020-11-23 15:17:34 -08:00
parent 1dd20b4371
commit 62be6841e4
18 changed files with 132 additions and 93 deletions

View File

@@ -116,7 +116,7 @@ pub extern "C" fn wasmtime_linker_get_default(
pub extern "C" fn wasmtime_linker_get_one_by_name(
linker: &wasmtime_linker_t,
module: &wasm_name_t,
name: &wasm_name_t,
name: Option<&wasm_name_t>,
item_ptr: &mut *mut wasm_extern_t,
) -> Option<Box<wasmtime_error_t>> {
let linker = &linker.linker;
@@ -124,9 +124,12 @@ pub extern "C" fn wasmtime_linker_get_one_by_name(
Ok(s) => s,
Err(_) => return bad_utf8(),
};
let name = match str::from_utf8(name.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 }))

View File

@@ -51,7 +51,13 @@ pub extern "C" fn wasmtime_module_new(
handle_result(Module::from_binary(&engine.engine, binary), |module| {
let imports = module
.imports()
.map(|i| wasm_importtype_t::new(i.module().to_owned(), i.name().to_owned(), i.ty()))
.map(|i| {
wasm_importtype_t::new(
i.module().to_owned(),
i.name().map(|s| s.to_owned()),
i.ty(),
)
})
.collect::<Vec<_>>();
let exports = module
.exports()
@@ -118,7 +124,13 @@ pub extern "C" fn wasm_module_obtain(
}
let imports = module
.imports()
.map(|i| wasm_importtype_t::new(i.module().to_owned(), i.name().to_owned(), i.ty()))
.map(|i| {
wasm_importtype_t::new(
i.module().to_owned(),
i.name().map(|s| s.to_owned()),
i.ty(),
)
})
.collect::<Vec<_>>();
let exports = module
.exports()
@@ -175,7 +187,13 @@ pub extern "C" fn wasmtime_module_deserialize(
|module| {
let imports = module
.imports()
.map(|i| wasm_importtype_t::new(i.module().to_owned(), i.name().to_owned(), i.ty()))
.map(|i| {
wasm_importtype_t::new(
i.module().to_owned(),
i.name().map(|s| s.to_owned()),
i.ty(),
)
})
.collect::<Vec<_>>();
let exports = module
.exports()

View File

@@ -6,7 +6,7 @@ use wasmtime::ExternType;
#[derive(Clone)]
pub struct wasm_importtype_t {
pub(crate) module: String,
pub(crate) name: String,
pub(crate) name: Option<String>,
pub(crate) ty: ExternType,
module_cache: OnceCell<wasm_name_t>,
name_cache: OnceCell<wasm_name_t>,
@@ -16,7 +16,7 @@ pub struct wasm_importtype_t {
wasmtime_c_api_macros::declare_ty!(wasm_importtype_t);
impl wasm_importtype_t {
pub(crate) fn new(module: String, name: String, ty: ExternType) -> wasm_importtype_t {
pub(crate) fn new(module: String, name: Option<String>, ty: ExternType) -> wasm_importtype_t {
wasm_importtype_t {
module,
name,
@@ -31,13 +31,16 @@ impl wasm_importtype_t {
#[no_mangle]
pub extern "C" fn wasm_importtype_new(
module: &mut wasm_name_t,
name: &mut wasm_name_t,
name: Option<&mut wasm_name_t>,
ty: Box<wasm_externtype_t>,
) -> Option<Box<wasm_importtype_t>> {
let module = module.take();
let name = name.take();
let name = name.map(|n| n.take());
let module = String::from_utf8(module).ok()?;
let name = String::from_utf8(name).ok()?;
let name = match name {
Some(name) => Some(String::from_utf8(name).ok()?),
None => None,
};
Some(Box::new(wasm_importtype_t::new(module, name, ty.ty())))
}
@@ -48,9 +51,12 @@ pub extern "C" fn wasm_importtype_module(it: &wasm_importtype_t) -> &wasm_name_t
}
#[no_mangle]
pub extern "C" fn wasm_importtype_name(it: &wasm_importtype_t) -> &wasm_name_t {
it.name_cache
.get_or_init(|| wasm_name_t::from_name(it.name.clone()))
pub extern "C" fn wasm_importtype_name(it: &wasm_importtype_t) -> Option<&wasm_name_t> {
let name = it.name.as_ref()?;
Some(
it.name_cache
.get_or_init(|| wasm_name_t::from_name(name.to_string())),
)
}
#[no_mangle]

View File

@@ -328,7 +328,7 @@ pub extern "C" fn wasi_instance_bind_import<'a>(
import: &wasm_importtype_t,
) -> Option<&'a wasm_extern_t> {
let module = &import.module;
let name = str::from_utf8(import.name.as_bytes()).ok()?;
let name = str::from_utf8(import.name.as_ref()?.as_bytes()).ok()?;
let export = match &instance.wasi {
WasiInstance::Preview1(wasi) => {