Remove the module linking implementation in Wasmtime (#3958)
* Remove the module linking implementation in Wasmtime This commit removes the experimental implementation of the module linking WebAssembly proposal from Wasmtime. The module linking is no longer intended for core WebAssembly but is instead incorporated into the component model now at this point. This means that very large parts of Wasmtime's implementation of module linking are no longer applicable and would change greatly with an implementation of the component model. The main purpose of this is to remove Wasmtime's reliance on the support for module-linking in `wasmparser` and tooling crates. With this reliance removed we can move over to the `component-model` branch of `wasmparser` and use the updated support for the component model. Additionally given the trajectory of the component model proposal the embedding API of Wasmtime will not look like what it looks like today for WebAssembly. For example the core wasm `Instance` will not change and instead a `Component` is likely to be added instead. Some more rationale for this is in #3941, but the basic idea is that I feel that it's not going to be viable to develop support for the component model on a non-`main` branch of Wasmtime. Additionaly I don't think it's viable, for the same reasons as `wasm-tools`, to support the old module linking proposal and the new component model at the same time. This commit takes a moment to not only delete the existing module linking implementation but some abstractions are also simplified. For example module serialization is a bit simpler that there's only one module. Additionally instantiation is much simpler since the only initializer we have to deal with are imports and nothing else. Closes #3941 * Fix doc link * Update comments
This commit is contained in:
@@ -94,11 +94,6 @@ pub extern "C" fn wasmtime_config_wasm_multi_memory_set(c: &mut wasm_config_t, e
|
||||
c.config.wasm_multi_memory(enable);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_config_wasm_module_linking_set(c: &mut wasm_config_t, enable: bool) {
|
||||
c.config.wasm_module_linking(enable);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_config_wasm_memory64_set(c: &mut wasm_config_t, enable: bool) {
|
||||
c.config.wasm_memory64(enable);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::{
|
||||
wasm_externkind_t, wasm_externtype_t, wasm_func_t, wasm_global_t, wasm_instance_t,
|
||||
wasm_memory_t, wasm_module_t, wasm_table_t, wasmtime_module_t, CStoreContext, StoreRef,
|
||||
wasm_externkind_t, wasm_externtype_t, wasm_func_t, wasm_global_t, wasm_memory_t, wasm_table_t,
|
||||
CStoreContext, StoreRef,
|
||||
};
|
||||
use std::mem::ManuallyDrop;
|
||||
use wasmtime::{Extern, Func, Global, Instance, Memory, Table};
|
||||
use wasmtime::{Extern, Func, Global, Memory, Table};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_extern_t {
|
||||
@@ -20,8 +20,6 @@ pub extern "C" fn wasm_extern_kind(e: &wasm_extern_t) -> wasm_externkind_t {
|
||||
Extern::Global(_) => crate::WASM_EXTERN_GLOBAL,
|
||||
Extern::Table(_) => crate::WASM_EXTERN_TABLE,
|
||||
Extern::Memory(_) => crate::WASM_EXTERN_MEMORY,
|
||||
Extern::Instance(_) => crate::WASM_EXTERN_INSTANCE,
|
||||
Extern::Module(_) => crate::WASM_EXTERN_MODULE,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,26 +68,6 @@ pub extern "C" fn wasm_extern_as_memory_const(e: &wasm_extern_t) -> Option<&wasm
|
||||
wasm_extern_as_memory(e)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_extern_as_module(e: &wasm_extern_t) -> Option<&wasm_module_t> {
|
||||
wasm_module_t::try_from(e)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_extern_as_module_const(e: &wasm_extern_t) -> Option<&wasm_module_t> {
|
||||
wasm_extern_as_module(e)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_extern_as_instance(e: &wasm_extern_t) -> Option<&wasm_instance_t> {
|
||||
wasm_instance_t::try_from(e)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_extern_as_instance_const(e: &wasm_extern_t) -> Option<&wasm_instance_t> {
|
||||
wasm_extern_as_instance(e)
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct wasmtime_extern_t {
|
||||
pub kind: wasmtime_extern_kind_t,
|
||||
@@ -101,17 +79,13 @@ pub const WASMTIME_EXTERN_FUNC: wasmtime_extern_kind_t = 0;
|
||||
pub const WASMTIME_EXTERN_GLOBAL: wasmtime_extern_kind_t = 1;
|
||||
pub const WASMTIME_EXTERN_TABLE: wasmtime_extern_kind_t = 2;
|
||||
pub const WASMTIME_EXTERN_MEMORY: wasmtime_extern_kind_t = 3;
|
||||
pub const WASMTIME_EXTERN_INSTANCE: wasmtime_extern_kind_t = 4;
|
||||
pub const WASMTIME_EXTERN_MODULE: wasmtime_extern_kind_t = 5;
|
||||
|
||||
#[repr(C)]
|
||||
pub union wasmtime_extern_union {
|
||||
pub func: Func,
|
||||
pub table: Table,
|
||||
pub global: Global,
|
||||
pub instance: Instance,
|
||||
pub memory: Memory,
|
||||
pub module: ManuallyDrop<Box<wasmtime_module_t>>,
|
||||
}
|
||||
|
||||
impl wasmtime_extern_t {
|
||||
@@ -121,8 +95,6 @@ impl wasmtime_extern_t {
|
||||
WASMTIME_EXTERN_GLOBAL => Extern::Global(self.of.global),
|
||||
WASMTIME_EXTERN_TABLE => Extern::Table(self.of.table),
|
||||
WASMTIME_EXTERN_MEMORY => Extern::Memory(self.of.memory),
|
||||
WASMTIME_EXTERN_INSTANCE => Extern::Instance(self.of.instance),
|
||||
WASMTIME_EXTERN_MODULE => Extern::Module(self.of.module.module.clone()),
|
||||
other => panic!("unknown wasm_extern_kind_t: {}", other),
|
||||
}
|
||||
}
|
||||
@@ -147,26 +119,6 @@ impl From<Extern> for wasmtime_extern_t {
|
||||
kind: WASMTIME_EXTERN_MEMORY,
|
||||
of: wasmtime_extern_union { memory },
|
||||
},
|
||||
Extern::Instance(instance) => wasmtime_extern_t {
|
||||
kind: WASMTIME_EXTERN_INSTANCE,
|
||||
of: wasmtime_extern_union { instance },
|
||||
},
|
||||
Extern::Module(module) => wasmtime_extern_t {
|
||||
kind: WASMTIME_EXTERN_MODULE,
|
||||
of: wasmtime_extern_union {
|
||||
module: ManuallyDrop::new(Box::new(wasmtime_module_t { module })),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for wasmtime_extern_t {
|
||||
fn drop(&mut self) {
|
||||
if self.kind == WASMTIME_EXTERN_MODULE {
|
||||
unsafe {
|
||||
ManuallyDrop::drop(&mut self.of.module);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,21 @@
|
||||
use crate::{
|
||||
wasm_extern_t, wasm_extern_vec_t, wasm_module_t, wasm_store_t, wasm_trap_t, wasmtime_error_t,
|
||||
wasmtime_extern_t, wasmtime_instancetype_t, wasmtime_module_t, CStoreContext, CStoreContextMut,
|
||||
StoreRef,
|
||||
wasmtime_extern_t, wasmtime_module_t, CStoreContextMut, StoreRef,
|
||||
};
|
||||
use std::mem::MaybeUninit;
|
||||
use wasmtime::{Extern, Instance, Trap};
|
||||
use wasmtime::{Instance, Trap};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(transparent)]
|
||||
pub struct wasm_instance_t {
|
||||
ext: wasm_extern_t,
|
||||
store: StoreRef,
|
||||
instance: Instance,
|
||||
}
|
||||
|
||||
wasmtime_c_api_macros::declare_ref!(wasm_instance_t);
|
||||
|
||||
impl wasm_instance_t {
|
||||
pub(crate) fn new(store: StoreRef, instance: Instance) -> wasm_instance_t {
|
||||
wasm_instance_t {
|
||||
ext: wasm_extern_t {
|
||||
store: store,
|
||||
which: instance.into(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_instance_t> {
|
||||
match &e.which {
|
||||
Extern::Instance(_) => Some(unsafe { &*(e as *const _ as *const _) }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn instance(&self) -> Instance {
|
||||
match self.ext.which {
|
||||
Extern::Instance(i) => i,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
wasm_instance_t { store, instance }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +34,7 @@ pub unsafe extern "C" fn wasm_instance_new(
|
||||
None => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
match Instance::new(store.store.context_mut(), wasm_module.module(), &imports) {
|
||||
match Instance::new(store.store.context_mut(), &wasm_module.module, &imports) {
|
||||
Ok(instance) => Some(Box::new(wasm_instance_t::new(
|
||||
store.store.clone(),
|
||||
instance,
|
||||
@@ -68,21 +48,16 @@ pub unsafe extern "C" fn wasm_instance_new(
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_instance_as_extern(m: &wasm_instance_t) -> &wasm_extern_t {
|
||||
&m.ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_instance_exports(
|
||||
instance: &mut wasm_instance_t,
|
||||
out: &mut wasm_extern_vec_t,
|
||||
) {
|
||||
let store = instance.ext.store.clone();
|
||||
let store = instance.store.clone();
|
||||
out.set_buffer(
|
||||
instance
|
||||
.instance()
|
||||
.exports(instance.ext.store.context_mut())
|
||||
.instance
|
||||
.exports(instance.store.context_mut())
|
||||
.map(|e| {
|
||||
Some(Box::new(wasm_extern_t {
|
||||
which: e.into_extern(),
|
||||
@@ -133,14 +108,6 @@ pub(crate) fn handle_instantiate(
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_instance_type(
|
||||
store: CStoreContext<'_>,
|
||||
instance: &Instance,
|
||||
) -> Box<wasmtime_instancetype_t> {
|
||||
Box::new(wasmtime_instancetype_t::new(instance.ty(store)))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_instance_export_get(
|
||||
store: CStoreContextMut<'_>,
|
||||
|
||||
@@ -173,13 +173,9 @@ pub unsafe extern "C" fn wasmtime_linker_get(
|
||||
Ok(s) => s,
|
||||
Err(_) => return false,
|
||||
};
|
||||
let name = if name.is_null() {
|
||||
None
|
||||
} else {
|
||||
match str::from_utf8(crate::slice_from_raw_parts(name, name_len)) {
|
||||
Ok(s) => Some(s),
|
||||
Err(_) => return false,
|
||||
}
|
||||
let name = match str::from_utf8(crate::slice_from_raw_parts(name, name_len)) {
|
||||
Ok(s) => s,
|
||||
Err(_) => return false,
|
||||
};
|
||||
match linker.get(store, module, name) {
|
||||
Some(which) => {
|
||||
|
||||
@@ -1,43 +1,22 @@
|
||||
use crate::{
|
||||
handle_result, wasm_byte_vec_t, wasm_engine_t, wasm_exporttype_t, wasm_exporttype_vec_t,
|
||||
wasm_extern_t, wasm_importtype_t, wasm_importtype_vec_t, wasm_store_t, wasmtime_error_t,
|
||||
wasmtime_moduletype_t, StoreRef,
|
||||
wasm_importtype_t, wasm_importtype_vec_t, wasm_store_t, wasmtime_error_t,
|
||||
};
|
||||
use anyhow::Context;
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::c_char;
|
||||
use wasmtime::{Engine, Extern, Module};
|
||||
use wasmtime::{Engine, Module};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(transparent)]
|
||||
pub struct wasm_module_t {
|
||||
ext: wasm_extern_t,
|
||||
pub(crate) module: Module,
|
||||
}
|
||||
|
||||
wasmtime_c_api_macros::declare_ref!(wasm_module_t);
|
||||
|
||||
impl wasm_module_t {
|
||||
pub(crate) fn new(store: StoreRef, module: Module) -> wasm_module_t {
|
||||
wasm_module_t {
|
||||
ext: wasm_extern_t {
|
||||
store: store,
|
||||
which: module.into(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn try_from(e: &wasm_extern_t) -> Option<&wasm_module_t> {
|
||||
match &e.which {
|
||||
Extern::Module(_) => Some(unsafe { &*(e as *const _ as *const _) }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn module(&self) -> &Module {
|
||||
match &self.ext.which {
|
||||
Extern::Module(i) => i,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
pub(crate) fn new(module: Module) -> wasm_module_t {
|
||||
wasm_module_t { module }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +34,7 @@ pub unsafe extern "C" fn wasm_module_new(
|
||||
binary: &wasm_byte_vec_t,
|
||||
) -> Option<Box<wasm_module_t>> {
|
||||
match Module::from_binary(store.store.context().engine(), binary.as_slice()) {
|
||||
Ok(module) => Some(Box::new(wasm_module_t::new(store.store.clone(), module))),
|
||||
Ok(module) => Some(Box::new(wasm_module_t::new(module))),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
@@ -68,15 +47,10 @@ pub unsafe extern "C" fn wasm_module_validate(
|
||||
Module::validate(store.store.context().engine(), binary.as_slice()).is_ok()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_module_as_extern(m: &wasm_module_t) -> &wasm_extern_t {
|
||||
&m.ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_module_exports(module: &wasm_module_t, out: &mut wasm_exporttype_vec_t) {
|
||||
let exports = module
|
||||
.module()
|
||||
.module
|
||||
.exports()
|
||||
.map(|e| {
|
||||
Some(Box::new(wasm_exporttype_t::new(
|
||||
@@ -91,12 +65,12 @@ pub extern "C" fn wasm_module_exports(module: &wasm_module_t, out: &mut wasm_exp
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_module_imports(module: &wasm_module_t, out: &mut wasm_importtype_vec_t) {
|
||||
let imports = module
|
||||
.module()
|
||||
.module
|
||||
.imports()
|
||||
.map(|i| {
|
||||
Some(Box::new(wasm_importtype_t::new(
|
||||
i.module().to_owned(),
|
||||
i.name().map(|s| s.to_owned()),
|
||||
i.name().to_owned(),
|
||||
i.ty(),
|
||||
)))
|
||||
})
|
||||
@@ -107,7 +81,7 @@ pub extern "C" fn wasm_module_imports(module: &wasm_module_t, out: &mut wasm_imp
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_module_share(module: &wasm_module_t) -> Box<wasm_shared_module_t> {
|
||||
Box::new(wasm_shared_module_t {
|
||||
module: module.module().clone(),
|
||||
module: module.module.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -118,7 +92,7 @@ pub unsafe extern "C" fn wasm_module_obtain(
|
||||
) -> Option<Box<wasm_module_t>> {
|
||||
let module = shared_module.module.clone();
|
||||
if Engine::same(store.store.context().engine(), module.engine()) {
|
||||
Some(Box::new(wasm_module_t::new(store.store.clone(), module)))
|
||||
Some(Box::new(wasm_module_t::new(module)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -126,7 +100,7 @@ pub unsafe extern "C" fn wasm_module_obtain(
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_module_serialize(module: &wasm_module_t, ret: &mut wasm_byte_vec_t) {
|
||||
if let Ok(buf) = module.module().serialize() {
|
||||
if let Ok(buf) = module.module.serialize() {
|
||||
ret.set_buffer(buf);
|
||||
}
|
||||
}
|
||||
@@ -137,7 +111,7 @@ pub unsafe extern "C" fn wasm_module_deserialize(
|
||||
binary: &wasm_byte_vec_t,
|
||||
) -> Option<Box<wasm_module_t>> {
|
||||
match Module::deserialize(store.store.context().engine(), binary.as_slice()) {
|
||||
Ok(module) => Some(Box::new(wasm_module_t::new(store.store.clone(), module))),
|
||||
Ok(module) => Some(Box::new(wasm_module_t::new(module))),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
@@ -180,11 +154,6 @@ pub unsafe extern "C" fn wasmtime_module_validate(
|
||||
handle_result(Module::validate(&engine.engine, binary), |()| {})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_module_type(m: &wasmtime_module_t) -> Box<wasmtime_moduletype_t> {
|
||||
Box::new(wasmtime_moduletype_t::new(m.module.ty()))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_module_serialize(
|
||||
module: &wasmtime_module_t,
|
||||
|
||||
@@ -20,18 +20,14 @@ mod r#extern;
|
||||
mod func;
|
||||
mod global;
|
||||
mod import;
|
||||
mod instance;
|
||||
mod memory;
|
||||
mod module;
|
||||
mod table;
|
||||
mod val;
|
||||
pub use self::export::*;
|
||||
pub use self::func::*;
|
||||
pub use self::global::*;
|
||||
pub use self::import::*;
|
||||
pub use self::instance::*;
|
||||
pub use self::memory::*;
|
||||
pub use self::module::*;
|
||||
pub use self::r#extern::*;
|
||||
pub use self::table::*;
|
||||
pub use self::val::*;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::{wasm_functype_t, wasm_globaltype_t, wasm_memorytype_t, wasm_tabletype_t};
|
||||
use crate::{wasmtime_instancetype_t, wasmtime_moduletype_t};
|
||||
use crate::{CFuncType, CGlobalType, CInstanceType, CMemoryType, CModuleType, CTableType};
|
||||
use crate::{CFuncType, CGlobalType, CMemoryType, CTableType};
|
||||
use wasmtime::ExternType;
|
||||
|
||||
#[repr(C)]
|
||||
@@ -17,8 +16,6 @@ pub(crate) enum CExternType {
|
||||
Global(CGlobalType),
|
||||
Memory(CMemoryType),
|
||||
Table(CTableType),
|
||||
Instance(CInstanceType),
|
||||
Module(CModuleType),
|
||||
}
|
||||
|
||||
pub type wasm_externkind_t = u8;
|
||||
@@ -27,8 +24,6 @@ pub const WASM_EXTERN_FUNC: wasm_externkind_t = 0;
|
||||
pub const WASM_EXTERN_GLOBAL: wasm_externkind_t = 1;
|
||||
pub const WASM_EXTERN_TABLE: wasm_externkind_t = 2;
|
||||
pub const WASM_EXTERN_MEMORY: wasm_externkind_t = 3;
|
||||
pub const WASM_EXTERN_MODULE: wasm_externkind_t = 4;
|
||||
pub const WASM_EXTERN_INSTANCE: wasm_externkind_t = 5;
|
||||
|
||||
impl wasm_externtype_t {
|
||||
pub(crate) fn new(ty: ExternType) -> wasm_externtype_t {
|
||||
@@ -38,8 +33,6 @@ impl wasm_externtype_t {
|
||||
ExternType::Global(f) => CExternType::Global(CGlobalType::new(f)),
|
||||
ExternType::Memory(f) => CExternType::Memory(CMemoryType::new(f)),
|
||||
ExternType::Table(f) => CExternType::Table(CTableType::new(f)),
|
||||
ExternType::Instance(f) => CExternType::Instance(CInstanceType::new(f)),
|
||||
ExternType::Module(f) => CExternType::Module(CModuleType::new(f)),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -50,8 +43,6 @@ impl wasm_externtype_t {
|
||||
CExternType::Table(f) => ExternType::Table(f.ty.clone()),
|
||||
CExternType::Global(f) => ExternType::Global(f.ty.clone()),
|
||||
CExternType::Memory(f) => ExternType::Memory(f.ty.clone()),
|
||||
CExternType::Instance(f) => ExternType::Instance(f.ty.clone()),
|
||||
CExternType::Module(f) => ExternType::Module(f.ty.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,8 +54,6 @@ pub extern "C" fn wasm_externtype_kind(et: &wasm_externtype_t) -> wasm_externkin
|
||||
CExternType::Table(_) => WASM_EXTERN_TABLE,
|
||||
CExternType::Global(_) => WASM_EXTERN_GLOBAL,
|
||||
CExternType::Memory(_) => WASM_EXTERN_MEMORY,
|
||||
CExternType::Instance(_) => WASM_EXTERN_INSTANCE,
|
||||
CExternType::Module(_) => WASM_EXTERN_MODULE,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,17 +110,3 @@ pub extern "C" fn wasm_externtype_as_memorytype_const(
|
||||
) -> Option<&wasm_memorytype_t> {
|
||||
wasm_memorytype_t::try_from(et)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_externtype_as_moduletype(
|
||||
et: &wasm_externtype_t,
|
||||
) -> Option<&wasmtime_moduletype_t> {
|
||||
wasmtime_moduletype_t::try_from(et)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_externtype_as_instancetype(
|
||||
et: &wasm_externtype_t,
|
||||
) -> Option<&wasmtime_instancetype_t> {
|
||||
wasmtime_instancetype_t::try_from(et)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use wasmtime::ExternType;
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_importtype_t {
|
||||
pub(crate) module: String,
|
||||
pub(crate) name: Option<String>,
|
||||
pub(crate) name: 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: Option<String>, ty: ExternType) -> wasm_importtype_t {
|
||||
pub(crate) fn new(module: String, name: String, ty: ExternType) -> wasm_importtype_t {
|
||||
wasm_importtype_t {
|
||||
module,
|
||||
name,
|
||||
@@ -31,16 +31,13 @@ impl wasm_importtype_t {
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_importtype_new(
|
||||
module: &mut wasm_name_t,
|
||||
name: Option<&mut wasm_name_t>,
|
||||
name: &mut wasm_name_t,
|
||||
ty: Box<wasm_externtype_t>,
|
||||
) -> Option<Box<wasm_importtype_t>> {
|
||||
let module = module.take();
|
||||
let name = name.map(|n| n.take());
|
||||
let name = name.take();
|
||||
let module = String::from_utf8(module).ok()?;
|
||||
let name = match name {
|
||||
Some(name) => Some(String::from_utf8(name).ok()?),
|
||||
None => None,
|
||||
};
|
||||
let name = String::from_utf8(name).ok()?;
|
||||
Some(Box::new(wasm_importtype_t::new(module, name, ty.ty())))
|
||||
}
|
||||
|
||||
@@ -51,12 +48,9 @@ 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) -> 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())),
|
||||
)
|
||||
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.to_string()))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
use crate::{wasm_exporttype_t, wasm_exporttype_vec_t, wasm_externtype_t, CExternType};
|
||||
use wasmtime::InstanceType;
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone)]
|
||||
pub struct wasmtime_instancetype_t {
|
||||
ext: wasm_externtype_t,
|
||||
}
|
||||
|
||||
wasmtime_c_api_macros::declare_ty!(wasmtime_instancetype_t);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct CInstanceType {
|
||||
pub(crate) ty: InstanceType,
|
||||
}
|
||||
|
||||
impl wasmtime_instancetype_t {
|
||||
pub(crate) fn new(ty: InstanceType) -> wasmtime_instancetype_t {
|
||||
wasmtime_instancetype_t {
|
||||
ext: wasm_externtype_t::new(ty.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasmtime_instancetype_t> {
|
||||
match &e.which {
|
||||
CExternType::Instance(_) => Some(unsafe { &*(e as *const _ as *const _) }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn ty(&self) -> &CInstanceType {
|
||||
match &self.ext.which {
|
||||
CExternType::Instance(f) => &f,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CInstanceType {
|
||||
pub(crate) fn new(ty: InstanceType) -> CInstanceType {
|
||||
CInstanceType { ty }
|
||||
}
|
||||
}
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_instancetype_as_externtype(
|
||||
ty: &wasmtime_instancetype_t,
|
||||
) -> &wasm_externtype_t {
|
||||
&ty.ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_instancetype_exports(
|
||||
instance: &wasmtime_instancetype_t,
|
||||
out: &mut wasm_exporttype_vec_t,
|
||||
) {
|
||||
let exports = instance
|
||||
.ty()
|
||||
.ty
|
||||
.exports()
|
||||
.map(|e| {
|
||||
Some(Box::new(wasm_exporttype_t::new(
|
||||
e.name().to_owned(),
|
||||
e.ty(),
|
||||
)))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
out.set_buffer(exports);
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
use crate::{
|
||||
wasm_exporttype_t, wasm_exporttype_vec_t, wasm_externtype_t, wasm_importtype_t,
|
||||
wasm_importtype_vec_t, CExternType,
|
||||
};
|
||||
use wasmtime::ModuleType;
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Clone)]
|
||||
pub struct wasmtime_moduletype_t {
|
||||
ext: wasm_externtype_t,
|
||||
}
|
||||
|
||||
wasmtime_c_api_macros::declare_ty!(wasmtime_moduletype_t);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct CModuleType {
|
||||
pub(crate) ty: ModuleType,
|
||||
}
|
||||
|
||||
impl wasmtime_moduletype_t {
|
||||
pub(crate) fn new(ty: ModuleType) -> wasmtime_moduletype_t {
|
||||
wasmtime_moduletype_t {
|
||||
ext: wasm_externtype_t::new(ty.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasmtime_moduletype_t> {
|
||||
match &e.which {
|
||||
CExternType::Module(_) => Some(unsafe { &*(e as *const _ as *const _) }),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn ty(&self) -> &CModuleType {
|
||||
match &self.ext.which {
|
||||
CExternType::Module(f) => &f,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CModuleType {
|
||||
pub(crate) fn new(ty: ModuleType) -> CModuleType {
|
||||
CModuleType { ty }
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_moduletype_as_externtype(
|
||||
ty: &wasmtime_moduletype_t,
|
||||
) -> &wasm_externtype_t {
|
||||
&ty.ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_moduletype_exports(
|
||||
module: &wasmtime_moduletype_t,
|
||||
out: &mut wasm_exporttype_vec_t,
|
||||
) {
|
||||
let exports = module
|
||||
.ty()
|
||||
.ty
|
||||
.exports()
|
||||
.map(|e| {
|
||||
Some(Box::new(wasm_exporttype_t::new(
|
||||
e.name().to_owned(),
|
||||
e.ty(),
|
||||
)))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
out.set_buffer(exports);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasmtime_moduletype_imports(
|
||||
module: &wasmtime_moduletype_t,
|
||||
out: &mut wasm_importtype_vec_t,
|
||||
) {
|
||||
let imports = module
|
||||
.ty()
|
||||
.ty
|
||||
.imports()
|
||||
.map(|i| {
|
||||
Some(Box::new(wasm_importtype_t::new(
|
||||
i.module().to_owned(),
|
||||
i.name().map(|s| s.to_owned()),
|
||||
i.ty(),
|
||||
)))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
out.set_buffer(imports);
|
||||
}
|
||||
Reference in New Issue
Block a user