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

Function get_set_externref_globals_via_api

tests/all/externals.rs:156–207  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

154
155#[test]
156fn get_set_externref_globals_via_api() -> wasmtime::Result<()> {
157 let mut cfg = Config::new();
158 cfg.wasm_reference_types(true);
159 let engine = Engine::new(&cfg)?;
160 let mut store = Store::new(&engine, ());
161
162 // Initialize with a null externref.
163
164 let global = Global::new(
165 &mut store,
166 GlobalType::new(ValType::EXTERNREF, Mutability::Var),
167 Val::ExternRef(None),
168 )?;
169 assert!(global.get(&mut store).unwrap_externref().is_none());
170
171 let hello = ExternRef::new(&mut store, "hello".to_string())?;
172 global.set(&mut store, hello.into())?;
173 let r = global.get(&mut store).unwrap_externref().cloned().unwrap();
174 assert!(
175 r.data(&store)?
176 .expect("should have host data")
177 .is::<String>()
178 );
179 assert_eq!(
180 r.data(&store)?
181 .expect("should have host data")
182 .downcast_ref::<String>()
183 .unwrap(),
184 "hello"
185 );
186
187 // Initialize with a non-null externref.
188
189 let externref = ExternRef::new(&mut store, 42_i32)?;
190 let global = Global::new(
191 &mut store,
192 GlobalType::new(ValType::EXTERNREF, Mutability::Const),
193 externref.into(),
194 )?;
195 let r = global.get(&mut store).unwrap_externref().cloned().unwrap();
196 assert!(r.data(&store)?.expect("should have host data").is::<i32>());
197 assert_eq!(
198 r.data(&store)?
199 .expect("should have host data")
200 .downcast_ref::<i32>()
201 .copied()
202 .unwrap(),
203 42
204 );
205
206 Ok(())
207}
208
209#[test]
210fn get_set_funcref_globals_via_api() -> wasmtime::Result<()> {

Callers

nothing calls this directly

Calls 9

OkFunction · 0.85
unwrap_externrefMethod · 0.80
newFunction · 0.50
ExternRefClass · 0.50
wasm_reference_typesMethod · 0.45
to_stringMethod · 0.45
setMethod · 0.45
unwrapMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected