A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wave: support component map values (#14843)
Randolf J.2e67c0bc9804
diff --git a/crates/wasmtime/src/runtime/wave/component.rs b/crates/wasmtime/src/runtime/wave/component.rsindex 7c7b50013b..ca2b676574 100644--- a/crates/wasmtime/src/runtime/wave/component.rs+++ b/crates/wasmtime/src/runtime/wave/component.rs@@ -41,13 +41,13 @@ impl WasmType for component::Type {Self::Result(_) => WasmTypeKind::Result,Self::Flags(_) => WasmTypeKind::Flags,Self::FixedLengthList(_) => WasmTypeKind::FixedLengthList,+ Self::Map(_) => WasmTypeKind::Map,Self::Own(_)| Self::Borrow(_)| Self::Stream(_)| Self::Future(_)- | Self::ErrorContext- | Self::Map(_) => WasmTypeKind::Unsupported,+ | Self::ErrorContext => WasmTypeKind::Unsupported,}}@@ -98,6 +98,11 @@ impl WasmType for component::Type {};Box::new(flags.names().map(Into::into))}++ fn map_key_value_types(&self) -> Option<(Self, Self)> {+ let map = maybe_unwrap_type!(self, Self::Map)?;+ Some((map.key(), map.value()))+ }}macro_rules! impl_primitives {@@ -140,11 +145,10 @@ impl WasmValue for component::Val {Self::Option(_) => WasmTypeKind::Option,Self::Result(_) => WasmTypeKind::Result,Self::Flags(_) => WasmTypeKind::Flags,- Self::Resource(_)- | Self::Stream(_)- | Self::Future(_)- | Self::ErrorContext(_)- | Self::Map(_) => WasmTypeKind::Unsupported,+ Self::Map(_) => WasmTypeKind::Map,+ Self::Resource(_) | Self::Stream(_) | Self::Future(_) | Self::ErrorContext(_) => {+ WasmTypeKind::Unsupported+ }Self::FixedLengthList(_) => WasmTypeKind::FixedLengthList,}}@@ -248,6 +252,15 @@ impl WasmValue for component::Val {ensure_type_val(ty, &val)?;Ok(val)}+ fn make_map(+ ty: &Self::Type,+ entries: impl IntoIterator<Item = (Self, Self)>,+ ) -> Result<Self, WasmValueError> {+ ensure_type_kind(ty, WasmTypeKind::Map)?;+ let val = Self::Map(entries.into_iter().collect());+ ensure_type_val(ty, &val)?;+ Ok(val)+ }fn unwrap_f32(&self) -> f32 {let val = *unwrap_val!(self, Self::Float32, "f32");@@ -297,6 +310,10 @@ impl WasmValue for component::Val {let flags = unwrap_val!(self, Self::Flags, "flags");Box::new(flags.iter().map(Into::into))}+ fn unwrap_map(&self) -> Box<dyn Iterator<Item = (Cow<'_, Self>, Cow<'_, Self>)> + '_> {+ let map = unwrap_val!(self, Self::Map, "map");+ Box::new(map.iter().map(|(key, value)| (cow(key), cow(value))))+ }}// Returns an error if the given component::Val is not of the given component::Type.@@ -321,6 +338,16 @@ fn ensure_type_val(ty: &component::Type, val: &component::Val) -> Result<(), Wasensure_type_val(&list_type, val)?;}}+ component::Val::Map(entries) => {+ let component::Type::Map(map_type) = ty else {+ return wrong_value_type();+ };+ let (key_type, value_type) = (map_type.key(), map_type.value());+ for (key, value) in entries {+ ensure_type_val(&key_type, key)?;+ ensure_type_val(&value_type, value)?;+ }+ }component::Val::Record(vals) => {let record_handle = ty.unwrap_record();// Check that every non option field type is found in the Vecdiff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rsindex 9892903209..f9337994b3 100644--- a/tests/all/cli_tests.rs+++ b/tests/all/cli_tests.rs@@ -686,6 +686,22 @@ fn component_invoke_multiple_run_exports() -> Result<()> {Ok(())}+#[test]+#[cfg_attr(not(feature = "component-model"), ignore)]+fn component_invoke_map() -> Result<()> {+ let path = "tests/all/cli_tests/component-map.wat";+ let invoke =+ |call: &str| run_wasmtime(&["run", "-Wcomponent-model-map", "--invoke", call, path]);++ // Map arguments are parsed and map results are printed in WAVE, which+ // merges duplicate keys.+ assert_eq!(invoke("echo({})")?, "{}\n");+ assert_eq!(invoke("echo({3: 4, 1: 2, 3: 5,})")?, "{3: 5, 1: 2}\n");+ assert_eq!(invoke("duplicates()")?, "{1: 30, 2: 20}\n");++ Ok(())+}+#[test]#[cfg_attr(not(feature = "component-model"), ignore)]fn component_unknown_imports_default() -> Result<()> {diff --git a/tests/all/cli_tests/component-map.wat b/tests/all/cli_tests/component-map.watnew file mode 100644index 0000000000..4f472b89b8--- /dev/null+++ b/tests/all/cli_tests/component-map.wat@@ -0,0 +1,41 @@+;; A component exporting functions that pass `map<u32, u32>` values in and out.+(component+ (core module $m+ (memory (export "memory") 1)++ ;; Bump allocator for lowering arguments, starting past the return area.+ (global $next (mut i32) (i32.const 16))+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (local $ptr i32)+ ;; Align up `$next` to the requested alignment.+ (local.set $ptr+ (i32.and+ (i32.add (global.get $next) (i32.sub (local.get 2) (i32.const 1)))+ (i32.sub (i32.const 0) (local.get 2))))+ (global.set $next (i32.add (local.get $ptr) (local.get 3)))+ (local.get $ptr))++ ;; Returns its map argument unchanged.+ (func (export "echo") (param $ptr i32) (param $len i32) (result i32)+ (i32.store (i32.const 0) (local.get $ptr))+ (i32.store (i32.const 4) (local.get $len))+ (i32.const 0))++ ;; Returns the entries 1: 10, 2: 20 and 1: 30, with a duplicate key.+ (data (i32.const 8192) "\01\00\00\00\0a\00\00\00\02\00\00\00\14\00\00\00\01\00\00\00\1e\00\00\00")+ (func (export "duplicates") (result i32)+ (i32.store (i32.const 0) (i32.const 8192))+ (i32.store (i32.const 4) (i32.const 3))+ (i32.const 0))+ )+ (core instance $i (instantiate $m))++ (type $map (map u32 u32))+ (func (export "echo") (param "m" $map) (result $map)+ (canon lift (core func $i "echo")+ (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+ (func (export "duplicates") (result $map)+ (canon lift (core func $i "duplicates")+ (memory (core memory $i "memory"))))+)diff --git a/tests/misc_testsuite/component-model/map-types.wast b/tests/misc_testsuite/component-model/map-types.wastindex 9a821110a0..f67b5502fb 100644--- a/tests/misc_testsuite/component-model/map-types.wast+++ b/tests/misc_testsuite/component-model/map-types.wast@@ -60,3 +60,59 @@(type $t (resource (rep i32)))(type (map (borrow $t) u32)))"invalid map key type")++;; Map values keep duplicate keys when lifted from guest memory and lowered+;; into it.+(component+ (core module $m+ (memory (export "memory") 1)++ ;; Returns (ptr = 16, len = 3) pointing at the entries+ ;; (1, 10) (1, 20) (2, 30).+ (data (i32.const 8) "\10\00\00\00\03\00\00\00")+ (data (i32.const 16)+ "\01\00\00\00\0a\00\00\00"+ "\01\00\00\00\14\00\00\00"+ "\02\00\00\00\1e\00\00\00")+ (func (export "dups") (result i32) i32.const 8)++ ;; Returns the map it was given.+ (func (export "echo") (param i32 i32) (result i32)+ (i32.store (i32.const 0) (local.get 0))+ (i32.store (i32.const 4) (local.get 1))+ i32.const 0)++ ;; Bump allocator for the lowered argument, after the static data.+ (global $next (mut i32) (i32.const 64))+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (local $ret i32)+ (local.set $ret (global.get $next))+ (global.set $next (i32.add (local.get $ret) (local.get 3)))+ (local.get $ret))+ )+ (core instance $i (instantiate $m))++ (type $t (map u32 u32))+ (func (export "dups") (result $t)+ (canon lift (core func $i "dups") (memory (core memory $i "memory"))))+ (func (export "echo") (param "m" $t) (result $t)+ (canon lift (core func $i "echo") (memory (core memory $i "memory"))+ (realloc (core func $i "realloc"))))+)++(assert_return (invoke "dups")+ (map.const+ (entry (u32.const 1) (u32.const 10))+ (entry (u32.const 1) (u32.const 20))+ (entry (u32.const 2) (u32.const 30))))++(assert_return+ (invoke "echo"+ (map.const+ (entry (u32.const 2) (u32.const 1))+ (entry (u32.const 1) (u32.const 2))+ (entry (u32.const 2) (u32.const 3))))+ (map.const+ (entry (u32.const 2) (u32.const 1))+ (entry (u32.const 1) (u32.const 2))+ (entry (u32.const 2) (u32.const 3))))