Path: blob/main/tests/all/component_model/nested.rs
1692 views
#![cfg(not(miri))]12use super::REALLOC_AND_FREE;3use anyhow::Result;4use wasmtime::component::*;5use wasmtime::{Module, Store, StoreContextMut};67#[test]8fn top_level_instance_two_level() -> Result<()> {9let component = r#"10(component11(import "c" (instance $i12(export "c" (instance13(export "m" (core module14(export "g" (global i32))15))16))17))18(component $c119(import "c" (instance $i20(export "c" (instance21(export "m" (core module22(export "g" (global i32))23))24))25))26(core module $verify27(import "" "g" (global i32))28(func $start29global.get 030i32.const 10131i32.ne32if unreachable end33)3435(start $start)36)37(core instance $m (instantiate (module $i "c" "m")))38(core instance (instantiate $verify (with "" (instance $m))))39)40(instance (instantiate $c1 (with "c" (instance $i))))41)42"#;43let module = r#"44(module45(global (export "g") i32 i32.const 101)46)47"#;4849let engine = super::engine();50let module = Module::new(&engine, module)?;51let component = Component::new(&engine, component)?;52let mut store = Store::new(&engine, ());53let mut linker = Linker::new(&engine);54linker.instance("c")?.instance("c")?.module("m", &module)?;55linker.instantiate(&mut store, &component)?;56Ok(())57}5859#[test]60fn nested_many_instantiations() -> Result<()> {61let component = r#"62(component63(import "count" (func $count))64(component $c165(import "count" (func $count))66(core func $count_lower (canon lower (func $count)))67(core module $m68(import "" "" (func $count))69(start $count)70)71(core instance (instantiate $m (with "" (instance (export "" (func $count_lower))))))72(core instance (instantiate $m (with "" (instance (export "" (func $count_lower))))))73)74(component $c275(import "count" (func $count))76(instance (instantiate $c1 (with "count" (func $count))))77(instance (instantiate $c1 (with "count" (func $count))))78)79(component $c380(import "count" (func $count))81(instance (instantiate $c2 (with "count" (func $count))))82(instance (instantiate $c2 (with "count" (func $count))))83)84(component $c485(import "count" (func $count))86(instance (instantiate $c3 (with "count" (func $count))))87(instance (instantiate $c3 (with "count" (func $count))))88)8990(instance (instantiate $c4 (with "count" (func $count))))91)92"#;93let engine = super::engine();94let component = Component::new(&engine, component)?;95let mut store = Store::new(&engine, 0);96let mut linker = Linker::new(&engine);97linker98.root()99.func_wrap("count", |mut store: StoreContextMut<'_, u32>, _: ()| {100*store.data_mut() += 1;101Ok(())102})?;103linker.instantiate(&mut store, &component)?;104assert_eq!(*store.data(), 16);105Ok(())106}107108#[test]109fn thread_options_through_inner() -> Result<()> {110let component = format!(111r#"112(component113(import "hostfn" (func $host (param "a" u32) (result string)))114115(component $c116(import "hostfn" (func $host (param "a" u32) (result string)))117118(core module $libc119(memory (export "memory") 1)120{REALLOC_AND_FREE}121)122(core instance $libc (instantiate $libc))123124(core func $host_lower125(canon lower126(func $host)127(memory $libc "memory")128(realloc (func $libc "realloc"))129)130)131132(core module $m133(import "" "host" (func $host (param i32 i32)))134(import "libc" "memory" (memory 1))135(func (export "run") (param i32) (result i32)136i32.const 42137i32.const 100138call $host139i32.const 100140)141(export "memory" (memory 0))142)143(core instance $m (instantiate $m144(with "" (instance (export "host" (func $host_lower))))145(with "libc" (instance $libc))146))147148(func (export "run") (param "a" u32) (result string)149(canon lift150(core func $m "run")151(memory $m "memory")152)153)154)155(instance $c (instantiate $c (with "hostfn" (func $host))))156(export "run" (func $c "run"))157)158"#159);160let engine = super::engine();161let component = Component::new(&engine, component)?;162let mut store = Store::new(&engine, 0);163let mut linker = Linker::new(&engine);164linker165.root()166.func_wrap("hostfn", |_, (param,): (u32,)| Ok((param.to_string(),)))?;167let instance = linker.instantiate(&mut store, &component)?;168let result = instance169.get_typed_func::<(u32,), (WasmStr,)>(&mut store, "run")?170.call(&mut store, (43,))?171.0;172assert_eq!(result.to_str(&store)?, "42");173Ok(())174}175176177