1c0bb341dSAlex Crichton #![cfg(not(miri))]
2c0bb341dSAlex Crichton
3651f4085SAlex Crichton use super::REALLOC_AND_FREE;
4*94740588SNick Fitzgerald use wasmtime::Result;
5651f4085SAlex Crichton use wasmtime::component::*;
6651f4085SAlex Crichton use wasmtime::{Module, Store, StoreContextMut};
7651f4085SAlex Crichton
8651f4085SAlex Crichton #[test]
top_level_instance_two_level() -> Result<()>9651f4085SAlex Crichton fn top_level_instance_two_level() -> Result<()> {
10651f4085SAlex Crichton let component = r#"
11651f4085SAlex Crichton (component
12651f4085SAlex Crichton (import "c" (instance $i
13651f4085SAlex Crichton (export "c" (instance
14651f4085SAlex Crichton (export "m" (core module
15651f4085SAlex Crichton (export "g" (global i32))
16651f4085SAlex Crichton ))
17651f4085SAlex Crichton ))
18651f4085SAlex Crichton ))
19651f4085SAlex Crichton (component $c1
20651f4085SAlex Crichton (import "c" (instance $i
21651f4085SAlex Crichton (export "c" (instance
22651f4085SAlex Crichton (export "m" (core module
23651f4085SAlex Crichton (export "g" (global i32))
24651f4085SAlex Crichton ))
25651f4085SAlex Crichton ))
26651f4085SAlex Crichton ))
27651f4085SAlex Crichton (core module $verify
28651f4085SAlex Crichton (import "" "g" (global i32))
29651f4085SAlex Crichton (func $start
30651f4085SAlex Crichton global.get 0
31651f4085SAlex Crichton i32.const 101
32651f4085SAlex Crichton i32.ne
33651f4085SAlex Crichton if unreachable end
34651f4085SAlex Crichton )
35651f4085SAlex Crichton
36651f4085SAlex Crichton (start $start)
37651f4085SAlex Crichton )
38651f4085SAlex Crichton (core instance $m (instantiate (module $i "c" "m")))
39651f4085SAlex Crichton (core instance (instantiate $verify (with "" (instance $m))))
40651f4085SAlex Crichton )
41651f4085SAlex Crichton (instance (instantiate $c1 (with "c" (instance $i))))
42651f4085SAlex Crichton )
43651f4085SAlex Crichton "#;
44651f4085SAlex Crichton let module = r#"
45651f4085SAlex Crichton (module
46651f4085SAlex Crichton (global (export "g") i32 i32.const 101)
47651f4085SAlex Crichton )
48651f4085SAlex Crichton "#;
49651f4085SAlex Crichton
50651f4085SAlex Crichton let engine = super::engine();
51651f4085SAlex Crichton let module = Module::new(&engine, module)?;
52651f4085SAlex Crichton let component = Component::new(&engine, component)?;
53651f4085SAlex Crichton let mut store = Store::new(&engine, ());
54651f4085SAlex Crichton let mut linker = Linker::new(&engine);
55651f4085SAlex Crichton linker.instance("c")?.instance("c")?.module("m", &module)?;
56651f4085SAlex Crichton linker.instantiate(&mut store, &component)?;
57651f4085SAlex Crichton Ok(())
58651f4085SAlex Crichton }
59651f4085SAlex Crichton
60651f4085SAlex Crichton #[test]
nested_many_instantiations() -> Result<()>61651f4085SAlex Crichton fn nested_many_instantiations() -> Result<()> {
62651f4085SAlex Crichton let component = r#"
63651f4085SAlex Crichton (component
64651f4085SAlex Crichton (import "count" (func $count))
65651f4085SAlex Crichton (component $c1
66651f4085SAlex Crichton (import "count" (func $count))
67651f4085SAlex Crichton (core func $count_lower (canon lower (func $count)))
68651f4085SAlex Crichton (core module $m
69651f4085SAlex Crichton (import "" "" (func $count))
70651f4085SAlex Crichton (start $count)
71651f4085SAlex Crichton )
72651f4085SAlex Crichton (core instance (instantiate $m (with "" (instance (export "" (func $count_lower))))))
73651f4085SAlex Crichton (core instance (instantiate $m (with "" (instance (export "" (func $count_lower))))))
74651f4085SAlex Crichton )
75651f4085SAlex Crichton (component $c2
76651f4085SAlex Crichton (import "count" (func $count))
77651f4085SAlex Crichton (instance (instantiate $c1 (with "count" (func $count))))
78651f4085SAlex Crichton (instance (instantiate $c1 (with "count" (func $count))))
79651f4085SAlex Crichton )
80651f4085SAlex Crichton (component $c3
81651f4085SAlex Crichton (import "count" (func $count))
82651f4085SAlex Crichton (instance (instantiate $c2 (with "count" (func $count))))
83651f4085SAlex Crichton (instance (instantiate $c2 (with "count" (func $count))))
84651f4085SAlex Crichton )
85651f4085SAlex Crichton (component $c4
86651f4085SAlex Crichton (import "count" (func $count))
87651f4085SAlex Crichton (instance (instantiate $c3 (with "count" (func $count))))
88651f4085SAlex Crichton (instance (instantiate $c3 (with "count" (func $count))))
89651f4085SAlex Crichton )
90651f4085SAlex Crichton
91651f4085SAlex Crichton (instance (instantiate $c4 (with "count" (func $count))))
92651f4085SAlex Crichton )
93651f4085SAlex Crichton "#;
94651f4085SAlex Crichton let engine = super::engine();
95651f4085SAlex Crichton let component = Component::new(&engine, component)?;
96651f4085SAlex Crichton let mut store = Store::new(&engine, 0);
97651f4085SAlex Crichton let mut linker = Linker::new(&engine);
98651f4085SAlex Crichton linker
99651f4085SAlex Crichton .root()
10078ecc17dSPat Hickey .func_wrap("count", |mut store: StoreContextMut<'_, u32>, _: ()| {
101651f4085SAlex Crichton *store.data_mut() += 1;
102651f4085SAlex Crichton Ok(())
103651f4085SAlex Crichton })?;
104651f4085SAlex Crichton linker.instantiate(&mut store, &component)?;
105651f4085SAlex Crichton assert_eq!(*store.data(), 16);
106651f4085SAlex Crichton Ok(())
107651f4085SAlex Crichton }
108651f4085SAlex Crichton
109651f4085SAlex Crichton #[test]
thread_options_through_inner() -> Result<()>110651f4085SAlex Crichton fn thread_options_through_inner() -> Result<()> {
111651f4085SAlex Crichton let component = format!(
112651f4085SAlex Crichton r#"
113651f4085SAlex Crichton (component
11429c7de73SAlex Crichton (import "hostfn" (func $host (param "a" u32) (result string)))
115651f4085SAlex Crichton
116651f4085SAlex Crichton (component $c
11729c7de73SAlex Crichton (import "hostfn" (func $host (param "a" u32) (result string)))
118651f4085SAlex Crichton
119651f4085SAlex Crichton (core module $libc
120651f4085SAlex Crichton (memory (export "memory") 1)
121651f4085SAlex Crichton {REALLOC_AND_FREE}
122651f4085SAlex Crichton )
123651f4085SAlex Crichton (core instance $libc (instantiate $libc))
124651f4085SAlex Crichton
125651f4085SAlex Crichton (core func $host_lower
126651f4085SAlex Crichton (canon lower
127651f4085SAlex Crichton (func $host)
128651f4085SAlex Crichton (memory $libc "memory")
129651f4085SAlex Crichton (realloc (func $libc "realloc"))
130651f4085SAlex Crichton )
131651f4085SAlex Crichton )
132651f4085SAlex Crichton
133651f4085SAlex Crichton (core module $m
134651f4085SAlex Crichton (import "" "host" (func $host (param i32 i32)))
135651f4085SAlex Crichton (import "libc" "memory" (memory 1))
136651f4085SAlex Crichton (func (export "run") (param i32) (result i32)
137651f4085SAlex Crichton i32.const 42
138651f4085SAlex Crichton i32.const 100
139651f4085SAlex Crichton call $host
140651f4085SAlex Crichton i32.const 100
141651f4085SAlex Crichton )
142651f4085SAlex Crichton (export "memory" (memory 0))
143651f4085SAlex Crichton )
144651f4085SAlex Crichton (core instance $m (instantiate $m
145651f4085SAlex Crichton (with "" (instance (export "host" (func $host_lower))))
146651f4085SAlex Crichton (with "libc" (instance $libc))
147651f4085SAlex Crichton ))
148651f4085SAlex Crichton
14929c7de73SAlex Crichton (func (export "run") (param "a" u32) (result string)
150651f4085SAlex Crichton (canon lift
151651f4085SAlex Crichton (core func $m "run")
152651f4085SAlex Crichton (memory $m "memory")
153651f4085SAlex Crichton )
154651f4085SAlex Crichton )
155651f4085SAlex Crichton )
156651f4085SAlex Crichton (instance $c (instantiate $c (with "hostfn" (func $host))))
157651f4085SAlex Crichton (export "run" (func $c "run"))
158651f4085SAlex Crichton )
159651f4085SAlex Crichton "#
160651f4085SAlex Crichton );
161651f4085SAlex Crichton let engine = super::engine();
162651f4085SAlex Crichton let component = Component::new(&engine, component)?;
163651f4085SAlex Crichton let mut store = Store::new(&engine, 0);
164651f4085SAlex Crichton let mut linker = Linker::new(&engine);
165651f4085SAlex Crichton linker
166651f4085SAlex Crichton .root()
16778ecc17dSPat Hickey .func_wrap("hostfn", |_, (param,): (u32,)| Ok((param.to_string(),)))?;
168651f4085SAlex Crichton let instance = linker.instantiate(&mut store, &component)?;
169651f4085SAlex Crichton let result = instance
170284fec12SAlex Crichton .get_typed_func::<(u32,), (WasmStr,)>(&mut store, "run")?
17157dca934SAlex Crichton .call(&mut store, (43,))?
17257dca934SAlex Crichton .0;
173651f4085SAlex Crichton assert_eq!(result.to_str(&store)?, "42");
174651f4085SAlex Crichton Ok(())
175651f4085SAlex Crichton }
176