Propagate module-linking types to wasmtime (#2115)

This commit adds lots of plumbing to get the type section from the
module linking proposal plumbed all the way through to the `wasmtime`
crate and the `wasmtime-c-api` crate. This isn't all that useful right
now because Wasmtime doesn't support imported/exported
modules/instances, but this is all necessary groundwork to getting that
exported at some point. I've added some light tests but I suspect the
bulk of the testing will come in a future commit.

One major change in this commit is that `SignatureIndex` no longer
follows type type index space in a wasm module. Instead a new
`TypeIndex` type is used to track that. Function signatures, still
indexed by `SignatureIndex`, are then packed together tightly.
This commit is contained in:
Alex Crichton
2020-11-06 14:48:09 -06:00
committed by GitHub
parent 77827a48a9
commit 73cda83548
27 changed files with 782 additions and 213 deletions

View File

@@ -23,14 +23,18 @@ 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::*;

View File

@@ -1,5 +1,6 @@
use crate::{wasm_functype_t, wasm_globaltype_t, wasm_memorytype_t, wasm_tabletype_t};
use crate::{CFuncType, CGlobalType, CMemoryType, CTableType};
use crate::{wasm_instancetype_t, wasm_moduletype_t};
use crate::{CFuncType, CGlobalType, CInstanceType, CMemoryType, CModuleType, CTableType};
use wasmtime::ExternType;
#[repr(C)]
@@ -16,6 +17,8 @@ pub(crate) enum CExternType {
Global(CGlobalType),
Memory(CMemoryType),
Table(CTableType),
Instance(CInstanceType),
Module(CModuleType),
}
pub type wasm_externkind_t = u8;
@@ -24,6 +27,8 @@ 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 WASMTIME_EXTERN_MODULE: wasm_externkind_t = 4;
pub const WASMTIME_EXTERN_INSTANCE: wasm_externkind_t = 5;
impl wasm_externtype_t {
pub(crate) fn new(ty: ExternType) -> wasm_externtype_t {
@@ -33,6 +38,8 @@ 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)),
},
}
}
@@ -43,6 +50,8 @@ 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()),
}
}
}
@@ -54,6 +63,8 @@ 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(_) => WASMTIME_EXTERN_INSTANCE,
CExternType::Module(_) => WASMTIME_EXTERN_MODULE,
}
}
@@ -110,3 +121,31 @@ 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 wasm_externtype_as_moduletype(
et: &wasm_externtype_t,
) -> Option<&wasm_moduletype_t> {
wasm_externtype_as_moduletype_const(et)
}
#[no_mangle]
pub extern "C" fn wasm_externtype_as_moduletype_const(
et: &wasm_externtype_t,
) -> Option<&wasm_moduletype_t> {
wasm_moduletype_t::try_from(et)
}
#[no_mangle]
pub extern "C" fn wasm_externtype_as_instancetype(
et: &wasm_externtype_t,
) -> Option<&wasm_instancetype_t> {
wasm_externtype_as_instancetype_const(et)
}
#[no_mangle]
pub extern "C" fn wasm_externtype_as_instancetype_const(
et: &wasm_externtype_t,
) -> Option<&wasm_instancetype_t> {
wasm_instancetype_t::try_from(et)
}

View File

@@ -0,0 +1,46 @@
use crate::{wasm_externtype_t, wasm_limits_t, CExternType};
use once_cell::unsync::OnceCell;
use wasmtime::InstanceType;
#[repr(transparent)]
#[derive(Clone)]
pub struct wasm_instancetype_t {
ext: wasm_externtype_t,
}
wasmtime_c_api_macros::declare_ty!(wasm_instancetype_t);
#[derive(Clone)]
pub(crate) struct CInstanceType {
pub(crate) ty: InstanceType,
limits_cache: OnceCell<wasm_limits_t>,
}
impl wasm_instancetype_t {
pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasm_instancetype_t> {
match &e.which {
CExternType::Instance(_) => Some(unsafe { &*(e as *const _ as *const _) }),
_ => None,
}
}
}
impl CInstanceType {
pub(crate) fn new(ty: InstanceType) -> CInstanceType {
CInstanceType {
ty,
limits_cache: OnceCell::new(),
}
}
}
#[no_mangle]
pub extern "C" fn wasm_instancetype_as_externtype(ty: &wasm_instancetype_t) -> &wasm_externtype_t {
&ty.ext
}
#[no_mangle]
pub extern "C" fn wasm_instancetype_as_externtype_const(
ty: &wasm_instancetype_t,
) -> &wasm_externtype_t {
&ty.ext
}

View File

@@ -0,0 +1,47 @@
use crate::{wasm_externtype_t, wasm_limits_t, CExternType};
use once_cell::unsync::OnceCell;
use wasmtime::ModuleType;
#[repr(transparent)]
#[derive(Clone)]
pub struct wasm_moduletype_t {
ext: wasm_externtype_t,
}
wasmtime_c_api_macros::declare_ty!(wasm_moduletype_t);
#[derive(Clone)]
pub(crate) struct CModuleType {
pub(crate) ty: ModuleType,
limits_cache: OnceCell<wasm_limits_t>,
}
impl wasm_moduletype_t {
pub(crate) fn try_from(e: &wasm_externtype_t) -> Option<&wasm_moduletype_t> {
match &e.which {
CExternType::Module(_) => Some(unsafe { &*(e as *const _ as *const _) }),
_ => None,
}
}
}
impl CModuleType {
pub(crate) fn new(ty: ModuleType) -> CModuleType {
CModuleType {
ty,
limits_cache: OnceCell::new(),
}
}
}
#[no_mangle]
pub extern "C" fn wasm_moduletype_as_externtype(ty: &wasm_moduletype_t) -> &wasm_externtype_t {
&ty.ext
}
#[no_mangle]
pub extern "C" fn wasm_moduletype_as_externtype_const(
ty: &wasm_moduletype_t,
) -> &wasm_externtype_t {
&ty.ext
}