MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / simple

Function simple

tests/all/component_model/import.rs:86–169  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

84
85#[test]
86fn simple() -> Result<()> {
87 let component = r#"
88 (component
89 (import "a" (func $log (param "a" string)))
90
91 (core module $libc
92 (memory (export "memory") 1)
93
94 (func (export "realloc") (param i32 i32 i32 i32) (result i32)
95 unreachable)
96 )
97 (core instance $libc (instantiate $libc))
98 (core func $log_lower
99 (canon lower (func $log) (memory $libc "memory") (realloc (func $libc "realloc")))
100 )
101 (core module $m
102 (import "libc" "memory" (memory 1))
103 (import "host" "log" (func $log (param i32 i32)))
104
105 (func (export "call")
106 i32.const 5
107 i32.const 11
108 call $log)
109
110 (data (i32.const 5) "hello world")
111 )
112 (core instance $i (instantiate $m
113 (with "libc" (instance $libc))
114 (with "host" (instance (export "log" (func $log_lower))))
115 ))
116 (func (export "call")
117 (canon lift (core func $i "call"))
118 )
119 )
120 "#;
121
122 let engine = super::engine();
123 let component = Component::new(&engine, component)?;
124 let mut store = Store::new(&engine, None);
125 assert!(store.data().is_none());
126
127 // First, test the static API
128
129 let mut linker = Linker::new(&engine);
130 linker.root().func_wrap(
131 "a",
132 |mut store: StoreContextMut<'_, Option<String>>, (arg,): (WasmStr,)| -> Result<_> {
133 let s = arg.to_str(&store)?.to_string();
134 assert!(store.data().is_none());
135 *store.data_mut() = Some(s);
136 Ok(())
137 },
138 )?;
139 let instance = linker.instantiate(&mut store, &component)?;
140 instance
141 .get_typed_func::<(), ()>(&mut store, "call")?
142 .call(&mut store, ())?;
143 assert_eq!(store.data().as_ref().unwrap(), "hello world");

Callers

nothing calls this directly

Calls 13

OkFunction · 0.85
to_strMethod · 0.80
engineFunction · 0.50
newFunction · 0.50
func_wrapMethod · 0.45
rootMethod · 0.45
to_stringMethod · 0.45
data_mutMethod · 0.45
instantiateMethod · 0.45
callMethod · 0.45
func_newMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected