components: Fix support for 0-sized flags (#4560)
This commit goes through and updates support in the various argument passing routines to support 0-sized flags. A bit of a degenerate case but clarified in WebAssembly/component-model#76 as intentional.
This commit is contained in:
@@ -210,7 +210,9 @@ fn make_echo_component(type_definition: &str, type_size: u32) -> String {
|
||||
}
|
||||
|
||||
fn make_echo_component_with_params(type_definition: &str, params: &[Param]) -> String {
|
||||
let func = if params.len() == 1 || params.len() > 16 {
|
||||
let func = if params.len() == 0 {
|
||||
format!("(func (export \"echo\"))")
|
||||
} else if params.len() == 1 || params.len() > 16 {
|
||||
let primitive = if params.len() == 1 {
|
||||
params[0].0.primitive()
|
||||
} else {
|
||||
|
||||
@@ -426,6 +426,22 @@ fn enum_derive() -> Result<()> {
|
||||
|
||||
#[test]
|
||||
fn flags() -> Result<()> {
|
||||
let engine = super::engine();
|
||||
let mut store = Store::new(&engine, ());
|
||||
|
||||
// Edge case of 0 flags
|
||||
wasmtime::component::flags! {
|
||||
Flags0 {}
|
||||
}
|
||||
assert_eq!(Flags0::default(), Flags0::default());
|
||||
|
||||
let component = Component::new(&engine, make_echo_component(r#"(flags)"#, 0))?;
|
||||
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
|
||||
let func = instance.get_typed_func::<(Flags0,), Flags0, _>(&mut store, "echo")?;
|
||||
let output = func.call_and_post_return(&mut store, (Flags0::default(),))?;
|
||||
assert_eq!(output, Flags0::default());
|
||||
|
||||
// Simple 8-bit flags
|
||||
wasmtime::component::flags! {
|
||||
Foo {
|
||||
#[component(name = "foo-bar-baz")]
|
||||
@@ -442,9 +458,6 @@ fn flags() -> Result<()> {
|
||||
assert_eq!(Foo::default(), Foo::A ^ Foo::A);
|
||||
assert_eq!(Foo::B | Foo::C, !Foo::A);
|
||||
|
||||
let engine = super::engine();
|
||||
let mut store = Store::new(&engine, ());
|
||||
|
||||
// Happy path: component type matches flag count and names
|
||||
|
||||
let component = Component::new(
|
||||
|
||||
@@ -1228,6 +1228,7 @@
|
||||
|
||||
;; test that flags get their upper bits all masked off
|
||||
(component
|
||||
(type $f0 (flags))
|
||||
(type $f1 (flags "f1"))
|
||||
(type $f8 (flags "f1" "f2" "f3" "f4" "f5" "f6" "f7" "f8"))
|
||||
(type $f9 (flags "f1" "f2" "f3" "f4" "f5" "f6" "f7" "f8" "f9"))
|
||||
@@ -1277,6 +1278,7 @@
|
||||
|
||||
(component $c1
|
||||
(core module $m
|
||||
(func (export "f0"))
|
||||
(func (export "f1") (param i32)
|
||||
(if (i32.ne (local.get 0) (i32.const 0x1)) (unreachable))
|
||||
)
|
||||
@@ -1310,6 +1312,7 @@
|
||||
)
|
||||
)
|
||||
(core instance $m (instantiate $m))
|
||||
(func (export "f0") (param $f0) (canon lift (core func $m "f0")))
|
||||
(func (export "f1") (param $f1) (canon lift (core func $m "f1")))
|
||||
(func (export "f8") (param $f8) (canon lift (core func $m "f8")))
|
||||
(func (export "f9") (param $f9) (canon lift (core func $m "f9")))
|
||||
@@ -1324,6 +1327,7 @@
|
||||
|
||||
(component $c2
|
||||
(import "" (instance $i
|
||||
(export "f0" (func (param $f0)))
|
||||
(export "f1" (func (param $f1)))
|
||||
(export "f8" (func (param $f8)))
|
||||
(export "f9" (func (param $f9)))
|
||||
@@ -1334,6 +1338,7 @@
|
||||
(export "f64" (func (param $f64)))
|
||||
(export "f65" (func (param $f65)))
|
||||
))
|
||||
(core func $f0 (canon lower (func $i "f0")))
|
||||
(core func $f1 (canon lower (func $i "f1")))
|
||||
(core func $f8 (canon lower (func $i "f8")))
|
||||
(core func $f9 (canon lower (func $i "f9")))
|
||||
@@ -1345,6 +1350,7 @@
|
||||
(core func $f65 (canon lower (func $i "f65")))
|
||||
|
||||
(core module $m
|
||||
(import "" "f0" (func $f0))
|
||||
(import "" "f1" (func $f1 (param i32)))
|
||||
(import "" "f8" (func $f8 (param i32)))
|
||||
(import "" "f9" (func $f9 (param i32)))
|
||||
@@ -1356,6 +1362,7 @@
|
||||
(import "" "f65" (func $f65 (param i32 i32 i32)))
|
||||
|
||||
(func $start
|
||||
(call $f0)
|
||||
(call $f1 (i32.const 0xffffff01))
|
||||
(call $f8 (i32.const 0xffffff11))
|
||||
(call $f9 (i32.const 0xffffff11))
|
||||
@@ -1371,6 +1378,7 @@
|
||||
)
|
||||
(core instance $m (instantiate $m
|
||||
(with "" (instance
|
||||
(export "f0" (func $f0))
|
||||
(export "f1" (func $f1))
|
||||
(export "f8" (func $f8))
|
||||
(export "f9" (func $f9))
|
||||
|
||||
Reference in New Issue
Block a user