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

Function maps

tests/all/component_model/dynamic.rs:140–212  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

138
139#[test]
140fn maps() -> Result<()> {
141 let engine = super::map_engine();
142 let mut store = Store::new(&engine, ());
143
144 let component = Component::new(&engine, make_echo_component("(map u32 string)", 8))?;
145 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
146 let func = instance.get_func(&mut store, "echo").unwrap();
147
148 let input_map = vec![
149 (Val::U32(1), Val::String("one".into())),
150 (Val::U32(2), Val::String("two".into())),
151 (Val::U32(3), Val::String("three".into())),
152 ];
153 let input = Val::Map(input_map);
154
155 let mut output = [Val::Bool(false)];
156 func.call(&mut store, &[input.clone()], &mut output)?;
157
158 // Maps should round-trip correctly
159 match &output[0] {
160 Val::Map(output_map) => {
161 assert_eq!(output_map.len(), 3);
162 assert!(
163 output_map
164 .iter()
165 .any(|(k, v)| *k == Val::U32(1) && *v == Val::String("one".into()))
166 );
167 assert!(
168 output_map
169 .iter()
170 .any(|(k, v)| *k == Val::U32(2) && *v == Val::String("two".into()))
171 );
172 assert!(
173 output_map
174 .iter()
175 .any(|(k, v)| *k == Val::U32(3) && *v == Val::String("three".into()))
176 );
177 }
178 _ => panic!("expected map"),
179 }
180
181 // Sad path: type mismatch (wrong key type)
182 // Need to create a fresh instance because errors can leave the instance in a bad state
183 let mut store2 = Store::new(&engine, ());
184 let instance2 = Linker::new(&engine).instantiate(&mut store2, &component)?;
185 let func2 = instance2.get_func(&mut store2, "echo").unwrap();
186
187 let err_map = vec![(Val::String("key".into()), Val::String("value".into()))];
188 let err = Val::Map(err_map);
189 let err = func2.call(&mut store2, &[err], &mut output).unwrap_err();
190 assert!(err.to_string().contains("type mismatch"), "{err}");
191
192 // Sad path: type mismatch (wrong value type)
193 let mut store3 = Store::new(&engine, ());
194 let instance3 = Linker::new(&engine).instantiate(&mut store3, &component)?;
195 let func3 = instance3.get_func(&mut store3, "echo").unwrap();
196
197 let err_map2 = vec![(Val::U32(1), Val::U32(42))];

Callers

nothing calls this directly

Calls 10

map_engineFunction · 0.85
make_echo_componentFunction · 0.85
OkFunction · 0.85
newFunction · 0.50
MapClass · 0.50
instantiateMethod · 0.45
unwrapMethod · 0.45
get_funcMethod · 0.45
callMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected