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

Function import_works

tests/all/host_funcs.rs:232–330  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

230#[test]
231#[cfg_attr(miri, ignore)]
232fn import_works() -> Result<()> {
233 static HITS: AtomicUsize = AtomicUsize::new(0);
234
235 let wasm = wat::parse_str(
236 r#"
237 (import "" "f1" (func))
238 (import "" "f2" (func (param i32) (result i32)))
239 (import "" "f3" (func (param i32) (param i64)))
240 (import "" "f4" (func (param i32 i64 i32 f32 f64 externref funcref)))
241
242 (func (export "run") (param externref funcref)
243 call 0
244 i32.const 0
245 call 1
246 i32.const 1
247 i32.add
248 i64.const 3
249 call 2
250
251 i32.const 100
252 i64.const 200
253 i32.const 300
254 f32.const 400
255 f64.const 500
256 local.get 0
257 local.get 1
258 call 3
259 )
260 "#,
261 )?;
262
263 let engine = Engine::default();
264 let mut linker = Linker::<()>::new(&engine);
265
266 linker.func_wrap("", "f1", || {
267 assert_eq!(HITS.fetch_add(1, SeqCst), 0);
268 })?;
269
270 linker.func_wrap("", "f2", |x: i32| -> i32 {
271 assert_eq!(x, 0);
272 assert_eq!(HITS.fetch_add(1, SeqCst), 1);
273 1
274 })?;
275
276 linker.func_wrap("", "f3", |x: i32, y: i64| {
277 assert_eq!(x, 2);
278 assert_eq!(y, 3);
279 assert_eq!(HITS.fetch_add(1, SeqCst), 2);
280 })?;
281
282 linker.func_wrap(
283 "",
284 "f4",
285 |mut caller: Caller<'_, _>,
286 a: i32,
287 b: i64,
288 c: i32,
289 d: f32,

Callers

nothing calls this directly

Calls 11

FuncRefClass · 0.85
OkFunction · 0.85
newFunction · 0.50
ExternRefClass · 0.50
func_wrapMethod · 0.45
unwrapMethod · 0.45
callMethod · 0.45
as_refMethod · 0.45
instantiateMethod · 0.45
get_funcMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected