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

Function primitives

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

Source from the content-addressed store, hash-verified

9
10#[test]
11fn primitives() -> Result<()> {
12 let engine = super::engine();
13 let mut store = Store::new(&engine, ());
14 let mut output = [Val::Bool(false)];
15
16 for (input, ty, param) in [
17 (Val::Bool(true), "bool", Param(Type::U8, Some(0))),
18 (Val::S8(-42), "s8", Param(Type::S8, Some(0))),
19 (Val::U8(42), "u8", Param(Type::U8, Some(0))),
20 (Val::S16(-4242), "s16", Param(Type::S16, Some(0))),
21 (Val::U16(4242), "u16", Param(Type::U16, Some(0))),
22 (Val::S32(-314159265), "s32", Param(Type::I32, Some(0))),
23 (Val::U32(314159265), "u32", Param(Type::I32, Some(0))),
24 (Val::S64(-31415926535897), "s64", Param(Type::I64, Some(0))),
25 (Val::U64(31415926535897), "u64", Param(Type::I64, Some(0))),
26 (
27 Val::Float32(3.14159265),
28 "float32",
29 Param(Type::F32, Some(0)),
30 ),
31 (
32 Val::Float64(3.14159265),
33 "float64",
34 Param(Type::F64, Some(0)),
35 ),
36 (Val::Char('🦀'), "char", Param(Type::I32, Some(0))),
37 ] {
38 let component = Component::new(&engine, make_echo_component_with_params(ty, &[param]))?;
39 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
40 let func = instance.get_func(&mut store, "echo").unwrap();
41 func.call(&mut store, &[input.clone()], &mut output)?;
42
43 assert_eq!(input, output[0]);
44 }
45
46 // Sad path: type mismatch
47
48 let component = Component::new(
49 &engine,
50 make_echo_component_with_params("float64", &[Param(Type::F64, Some(0))]),
51 )?;
52 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
53 let func = instance.get_func(&mut store, "echo").unwrap();
54 let err = func
55 .call(&mut store, &[Val::U64(42)], &mut output)
56 .unwrap_err();
57
58 assert!(err.to_string().contains("type mismatch"), "{err}");
59
60 // Sad path: arity mismatch (too many)
61
62 let err = func
63 .call(
64 &mut store,
65 &[Val::Float64(3.14159265), Val::Float64(3.14159265)],
66 &mut output,
67 )
68 .unwrap_err();

Callers

nothing calls this directly

Calls 12

ParamClass · 0.85
Float32Class · 0.85
Float64Class · 0.85
OkFunction · 0.85
engineFunction · 0.50
newFunction · 0.50
instantiateMethod · 0.45
unwrapMethod · 0.45
get_funcMethod · 0.45
callMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected