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

Function chars

tests/all/component_model/func.rs:576–635  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

574
575#[test]
576fn chars() -> Result<()> {
577 let component = r#"
578 (component
579 (core module $m
580 (func (export "pass") (param i32) (result i32) local.get 0)
581 )
582 (core instance $i (instantiate $m))
583
584 (func (export "u32-to-char") (param "a" u32) (result char)
585 (canon lift (core func $i "pass"))
586 )
587 (func (export "char-to-u32") (param "a" char) (result u32)
588 (canon lift (core func $i "pass"))
589 )
590 )
591 "#;
592
593 let engine = super::engine();
594 let component = Component::new(&engine, component)?;
595 let mut store = Store::new(&engine, ());
596 let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
597 let u32_to_char = instance.get_typed_func::<(u32,), (char,)>(&mut store, "u32-to-char")?;
598 let char_to_u32 = instance.get_typed_func::<(char,), (u32,)>(&mut store, "char-to-u32")?;
599
600 let mut roundtrip = |x: char| -> Result<()> {
601 assert_eq!(char_to_u32.call(&mut store, (x,))?, (x as u32,));
602 assert_eq!(u32_to_char.call(&mut store, (x as u32,))?, (x,));
603 Ok(())
604 };
605
606 roundtrip('x')?;
607 roundtrip('a')?;
608 roundtrip('\0')?;
609 roundtrip('\n')?;
610 roundtrip('💝')?;
611
612 let u32_to_char = |store: &mut Store<()>| {
613 Linker::new(&engine)
614 .instantiate(&mut *store, &component)?
615 .get_typed_func::<(u32,), (char,)>(&mut *store, "u32-to-char")
616 };
617 let err = u32_to_char(&mut store)?
618 .call(&mut store, (0xd800,))
619 .unwrap_err();
620 assert!(err.to_string().contains("integer out of range"), "{}", err);
621 let err = u32_to_char(&mut store)?
622 .call(&mut store, (0xdfff,))
623 .unwrap_err();
624 assert!(err.to_string().contains("integer out of range"), "{}", err);
625 let err = u32_to_char(&mut store)?
626 .call(&mut store, (0x110000,))
627 .unwrap_err();
628 assert!(err.to_string().contains("integer out of range"), "{}", err);
629 let err = u32_to_char(&mut store)?
630 .call(&mut store, (u32::MAX,))
631 .unwrap_err();
632 assert!(err.to_string().contains("integer out of range"), "{}", err);
633

Callers

nothing calls this directly

Calls 6

OkFunction · 0.85
roundtripFunction · 0.70
engineFunction · 0.50
newFunction · 0.50
instantiateMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected