Make wasmtime_environ::Module serializable (#2005)

* Define WasmType/WasmFuncType in the Cranelift
* Make `Module` serializable
This commit is contained in:
Yury Delendik
2020-07-10 15:56:43 -05:00
committed by GitHub
parent c3d385e935
commit b2551bb4d0
17 changed files with 205 additions and 62 deletions

View File

@@ -202,13 +202,11 @@ impl Global {
// The original export is coming from wasmtime_runtime itself we should
// support all the types coming out of it, so assert such here.
GlobalType::from_wasmtime_global(&self.wasmtime_export.global)
.expect("core wasm global type should be supported")
}
/// Returns the value type of this `global`.
pub fn val_type(&self) -> ValType {
ValType::from_wasm_type(&self.wasmtime_export.global.wasm_ty)
.expect("core wasm type should be supported")
}
/// Returns the underlying mutability of this `global`.

View File

@@ -545,7 +545,7 @@ impl Func {
// This is only called with `Export::Function`, and since it's coming
// from wasmtime_runtime itself we should support all the types coming
// out of it, so assert such here.
FuncType::from_wasm_func_type(&wft).expect("core wasm signature should be supported")
FuncType::from_wasm_func_type(&wft)
}
/// Returns the number of parameters that this function takes.

View File

@@ -123,16 +123,15 @@ impl ValType {
}
}
pub(crate) fn from_wasm_type(ty: &wasm::WasmType) -> Option<Self> {
pub(crate) fn from_wasm_type(ty: &wasm::WasmType) -> Self {
match ty {
wasm::WasmType::I32 => Some(Self::I32),
wasm::WasmType::I64 => Some(Self::I64),
wasm::WasmType::F32 => Some(Self::F32),
wasm::WasmType::F64 => Some(Self::F64),
wasm::WasmType::V128 => Some(Self::V128),
wasm::WasmType::FuncRef => Some(Self::FuncRef),
wasm::WasmType::ExternRef => Some(Self::ExternRef),
wasm::WasmType::Func | wasm::WasmType::EmptyBlockType => None,
wasm::WasmType::I32 => Self::I32,
wasm::WasmType::I64 => Self::I64,
wasm::WasmType::F32 => Self::F32,
wasm::WasmType::F64 => Self::F64,
wasm::WasmType::V128 => Self::V128,
wasm::WasmType::FuncRef => Self::FuncRef,
wasm::WasmType::ExternRef => Self::ExternRef,
}
}
}
@@ -279,21 +278,21 @@ impl FuncType {
/// Returns `None` if any types in the signature can't be converted to the
/// types in this crate, but that should very rarely happen and largely only
/// indicate a bug in our cranelift integration.
pub(crate) fn from_wasm_func_type(signature: &wasm::WasmFuncType) -> Option<FuncType> {
pub(crate) fn from_wasm_func_type(signature: &wasm::WasmFuncType) -> FuncType {
let params = signature
.params
.iter()
.map(|p| ValType::from_wasm_type(p))
.collect::<Option<Vec<_>>>()?;
.collect::<Vec<_>>();
let results = signature
.returns
.iter()
.map(|r| ValType::from_wasm_type(r))
.collect::<Option<Vec<_>>>()?;
Some(FuncType {
.collect::<Vec<_>>();
FuncType {
params: params.into_boxed_slice(),
results: results.into_boxed_slice(),
})
}
}
}
@@ -332,14 +331,14 @@ impl GlobalType {
/// Returns `None` if the wasmtime global has a type that we can't
/// represent, but that should only very rarely happen and indicate a bug.
pub(crate) fn from_wasmtime_global(global: &wasm::Global) -> Option<GlobalType> {
let ty = ValType::from_wasm_type(&global.wasm_ty)?;
pub(crate) fn from_wasmtime_global(global: &wasm::Global) -> GlobalType {
let ty = ValType::from_wasm_type(&global.wasm_ty);
let mutability = if global.mutability {
Mutability::Var
} else {
Mutability::Const
};
Some(GlobalType::new(ty, mutability))
GlobalType::new(ty, mutability)
}
}
@@ -451,14 +450,10 @@ impl<'module> EntityType<'module> {
/// Convert this `EntityType` to an `ExternType`.
pub(crate) fn extern_type(&self) -> ExternType {
match self {
EntityType::Function(sig) => FuncType::from_wasm_func_type(sig)
.expect("core wasm function type should be supported")
.into(),
EntityType::Function(sig) => FuncType::from_wasm_func_type(sig).into(),
EntityType::Table(table) => TableType::from_wasmtime_table(table).into(),
EntityType::Memory(memory) => MemoryType::from_wasmtime_memory(memory).into(),
EntityType::Global(global) => GlobalType::from_wasmtime_global(global)
.expect("core wasm global type should be supported")
.into(),
EntityType::Global(global) => GlobalType::from_wasmtime_global(global).into(),
}
}
}