Lines
100 %
Functions
Branches
use wasm_encoder::{Function, Instruction};
use wasmparser::{CompositeInnerType, Parser, Payload, SubType};
use super::CompileContext;
use crate::ast::StructId;
use crate::runtime::StructLayout;
/// Every type of `wasm` with the size of the recursion group it sits in.
fn types_of(wasm: &[u8]) -> Vec<(usize, SubType)> {
let mut found = Vec::new();
for payload in Parser::new(0).parse_all(wasm) {
if let Payload::TypeSection(reader) = payload.expect("payload") {
for group in reader {
let types: Vec<SubType> = group.expect("group").into_types().collect();
let size = types.len();
found.extend(types.into_iter().map(|ty| (size, ty)));
}
found
fn finished(mut ctx: CompileContext) -> Vec<u8> {
ctx.add_should_apply(CompileContext::default_should_apply(), usize::MAX);
let mut process = Function::new([]);
process.instruction(&Instruction::End);
ctx.add_process(process);
ctx.finish()
#[test]
fn the_struct_shapes_are_distinct_from_every_other_registered_type() {
let ctx = CompileContext::new_with_host_fns(&[]).unwrap();
let (slots, instance) = (ctx.ids.ty_slots as usize, ctx.ids.ty_struct as usize);
let types = types_of(&finished(ctx));
for index in [slots, instance] {
let (group, ty) = &types[index];
assert_eq!(*group, 1, "type {index} shares a recursion group");
let twins = types
.iter()
.filter(|(size, other)| *size == 1 && other.composite_type == ty.composite_type)
.count();
assert_eq!(twins, 1, "type {index} is structurally equal to another");
assert!(matches!(
types[slots].1.composite_type.inner,
CompositeInnerType::Array(_)
));
types[instance].1.composite_type.inner,
CompositeInnerType::Struct(_)
fn a_layout_global_is_built_once_and_the_module_validates() {
let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
let layout = StructLayout {
name: "PT".to_string(),
fields: vec!["X".to_string(), "Y".to_string()],
};
let first = ctx.struct_layout_global(StructId(3), &layout).unwrap();
let again = ctx.struct_layout_global(StructId(3), &layout).unwrap();
let other = ctx.struct_layout_global(StructId(4), &layout).unwrap();
assert_eq!(first, again);
assert_ne!(first, other);
assert!(wasmparser::validate(&finished(ctx)).is_ok());
fn a_reused_id_with_another_layout_gets_a_global_of_its_own() {
let layout = |name: &str, fields: &[&str]| StructLayout {
name: name.to_string(),
fields: fields.iter().map(ToString::to_string).collect(),
let rolled_back = ctx
.struct_layout_global(StructId(0), &layout("PT", &["X"]))
.unwrap();
let reused = layout("BOX", &["P", "Q"]);
let global = ctx.struct_layout_global(StructId(0), &reused).unwrap();
assert_ne!(rolled_back, global);
assert_eq!(
ctx.struct_layout_global(StructId(0), &reused).unwrap(),
global
);
fn the_struct_helpers_are_registered_in_both_module_modes() {
let script = CompileContext::new_with_host_fns(&[]).unwrap();
let mut eval = CompileContext::new_eval_with_host_fns(&[]).unwrap();
let mut body = Function::new([]);
body.instruction(&Instruction::RefNull(wasm_encoder::HeapType::ANY));
body.instruction(&Instruction::End);
eval.add_nomi_eval(body);
assert_ne!(script.ids.struct_new, u32::MAX);
assert_ne!(eval.ids.struct_copy, u32::MAX);
assert!(wasmparser::validate(&finished(script)).is_ok());
assert!(wasmparser::validate(&eval.finish()).is_ok());