Path: blob/main/tests/all/component_model/aot.rs
1691 views
use anyhow::Result;1use wasmtime::component::types::ComponentItem;2use wasmtime::component::{Component, Linker, Type};3use wasmtime::{Engine, Module, Precompiled, Store};45#[test]6fn module_component_mismatch() -> Result<()> {7let engine = super::engine();8let module = Module::new(&engine, "(module)")?.serialize()?;9let component = Component::new(&engine, "(component)")?.serialize()?;1011unsafe {12assert!(Module::deserialize(&engine, &component).is_err());13assert!(Component::deserialize(&engine, &module).is_err());14}1516Ok(())17}1819#[test]20fn bare_bones() -> Result<()> {21let engine = super::engine();22let component = Component::new(&engine, "(component)")?.serialize()?;23assert_eq!(component, engine.precompile_component(b"(component)")?);2425let component = unsafe { Component::deserialize(&engine, &component)? };26let mut store = Store::new(&engine, ());27Linker::new(&engine).instantiate(&mut store, &component)?;2829Ok(())30}3132#[test]33#[cfg_attr(miri, ignore)]34fn mildly_more_interesting() -> Result<()> {35let engine = super::engine();36let component = Component::new(37&engine,38r#"39(component40(core module $a41(func (export "a") (result i32)42i32.const 100)43)44(core instance $a (instantiate $a))4546(core module $b47(import "a" "a" (func $import (result i32)))48(func (export "a") (result i32)49call $import50i32.const 351i32.add)52)53(core instance $b (instantiate $b (with "a" (instance $a))))5455(func (export "a") (result u32)56(canon lift (core func $b "a"))57)58)59"#,60)?61.serialize()?;6263let component = unsafe { Component::deserialize(&engine, &component)? };64let mut store = Store::new(&engine, ());65let instance = Linker::new(&engine).instantiate(&mut store, &component)?;66let func = instance.get_typed_func::<(), (u32,)>(&mut store, "a")?;67assert_eq!(func.call(&mut store, ())?, (103,));6869Ok(())70}7172#[test]73fn deserialize_from_serialized() -> Result<()> {74let engine = super::engine();75let buffer1 = Component::new(&engine, "(component (core module))")?.serialize()?;76let buffer2 = unsafe { Component::deserialize(&engine, &buffer1)?.serialize()? };77assert!(buffer1 == buffer2);78Ok(())79}8081// This specifically tests the current behavior that it's an error, but this can82// be made to work if necessary in the future. Currently the implementation of83// `serialize` is not conducive to easily implementing this feature and84// otherwise it's not seen as too important to implement.85#[test]86fn cannot_serialize_exported_module() -> Result<()> {87let engine = super::engine();88let component = Component::new(89&engine,90r#"(component91(core module $m)92(export "a" (core module $m))93)"#,94)?;95let mut store = Store::new(&engine, ());96let instance = Linker::new(&engine).instantiate(&mut store, &component)?;97let module = instance.get_module(&mut store, "a").unwrap();98assert!(module.serialize().is_err());99Ok(())100}101102#[test]103#[cfg_attr(miri, ignore)]104fn usable_exported_modules() -> Result<()> {105let engine = super::engine();106let component = Component::new(107&engine,108r#"(component109(core module $m)110(core module $m1 (export "a")111(import "" "" (func (param i32)))112)113)"#,114)?;115let mut store = Store::new(&engine, ());116let instance = Linker::new(&engine).instantiate(&mut store, &component)?;117let module = instance.get_module(&mut store, "a").unwrap();118let mut core_linker = wasmtime::Linker::new(&engine);119core_linker.func_wrap("", "", |_: u32| {})?;120core_linker.instantiate(&mut store, &module)?;121Ok(())122}123124#[test]125#[cfg_attr(miri, ignore)]126fn detect_precompiled() -> Result<()> {127let engine = super::engine();128let buffer = Component::new(&engine, "(component)")?.serialize()?;129assert_eq!(Engine::detect_precompiled(&[]), None);130assert_eq!(Engine::detect_precompiled(&buffer[..5]), None);131assert_eq!(132Engine::detect_precompiled(&buffer),133Some(Precompiled::Component)134);135Ok(())136}137138#[test]139fn reflect_resource_import() -> Result<()> {140let engine = super::engine();141let c = Component::new(142&engine,143r#"144(component145(import "x" (type $x (sub resource)))146(import "y" (func (result (own $x))))147)148"#,149)?;150let ty = c.component_type();151let mut imports = ty.imports(&engine);152let (_, x) = imports.next().unwrap();153let (_, y) = imports.next().unwrap();154let x = match x {155ComponentItem::Resource(t) => t,156_ => unreachable!(),157};158let y = match y {159ComponentItem::ComponentFunc(t) => t,160_ => unreachable!(),161};162let result = y.results().next().unwrap();163assert_eq!(result, Type::Own(x));164165Ok(())166}167168#[test]169#[cfg_attr(miri, ignore)]170fn truncated_component_binaries_dont_panic() -> Result<()> {171let engine = super::engine();172173let binary = wat::parse_str(174r#"175(component176(import "a" (core module $m0177(import "" "" (func))178))179180(core module $m1181(func (export ""))182)183(core instance $i1 (instantiate (module $m1)))184(func $f (canon lift (core func $i1 "f")))185186(component $c1187(import "f" (func))188(core module $m2189(func (export "g"))190)191(core instance $i2 (instantiate $m2))192(func (export "g")193(canon lift (core func $i2 "g"))194)195)196(instance $i3 (instantiate $c1 (with "f" (func $f))))197(func (export "g") (alias export $i3 "g"))198)199"#,200)?;201202// Check that if we feed each truncation of the component binary into203// `Component::new` we don't get any panics.204for i in 1..binary.len() - 1 {205let _ = Component::from_binary(&engine, &binary[0..i]);206}207208Ok(())209}210211212