diff --git a/crates/wasmprinter/src/lib.rs b/crates/wasmprinter/src/lib.rs index 3333b11ea5..5ae939a14e 100644 --- a/crates/wasmprinter/src/lib.rs +++ b/crates/wasmprinter/src/lib.rs @@ -2944,24 +2944,18 @@ impl Printer { } CanonicalOption::Memory(idx) => { self.start_group("memory "); - self.start_group("core memory "); self.print_idx(&state.core.memory_names, *idx)?; self.end_group(); - self.end_group(); } CanonicalOption::Realloc(idx) => { self.start_group("realloc "); - self.start_group("core func "); self.print_idx(&state.core.func_names, *idx)?; self.end_group(); - self.end_group(); } CanonicalOption::PostReturn(idx) => { self.start_group("post-return "); - self.start_group("core func "); self.print_idx(&state.core.func_names, *idx)?; self.end_group(); - self.end_group(); } } } diff --git a/crates/wast/src/component/func.rs b/crates/wast/src/component/func.rs index f655d4b8b4..0ed28d6cbb 100644 --- a/crates/wast/src/component/func.rs +++ b/crates/wast/src/component/func.rs @@ -329,23 +329,21 @@ impl<'a> Parse<'a> for CanonOpt<'a> { parser.parens(|parser| { let mut l = parser.lookahead1(); if l.peek::() { - parser.parse::()?; - Ok(CanonOpt::Memory(parser.parens(|parser| { - parser.parse::()?; - parser.parse() - })?)) + let span = parser.parse::()?.0; + Ok(CanonOpt::Memory(parse_trailing_item_ref( + kw::memory(span), + parser, + )?)) } else if l.peek::() { parser.parse::()?; - Ok(CanonOpt::Realloc(parser.parens(|parser| { - parser.parse::()?; - parser.parse() - })?)) + Ok(CanonOpt::Realloc( + parser.parse::>()?.0, + )) } else if l.peek::() { parser.parse::()?; - Ok(CanonOpt::PostReturn(parser.parens(|parser| { - parser.parse::()?; - parser.parse() - })?)) + Ok(CanonOpt::PostReturn( + parser.parse::>()?.0, + )) } else { Err(l.error()) } @@ -356,6 +354,14 @@ impl<'a> Parse<'a> for CanonOpt<'a> { } } +fn parse_trailing_item_ref<'a, T>(kind: T, parser: Parser<'a>) -> Result> { + Ok(CoreItemRef { + kind, + idx: parser.parse()?, + export_name: parser.parse()?, + }) +} + impl<'a> Parse<'a> for Vec> { fn parse(parser: Parser<'a>) -> Result { let mut funcs = Vec::new(); diff --git a/crates/wast/src/component/item_ref.rs b/crates/wast/src/component/item_ref.rs index b89e25a7bd..c3bbf2f9f4 100644 --- a/crates/wast/src/component/item_ref.rs +++ b/crates/wast/src/component/item_ref.rs @@ -131,3 +131,24 @@ where } } } + +/// Convenience structure to parse `$f` or `(item $f)`. +#[derive(Clone, Debug)] +pub struct IndexOrCoreRef<'a, K>(pub CoreItemRef<'a, K>); + +impl<'a, K> Parse<'a> for IndexOrCoreRef<'a, K> +where + K: Parse<'a> + Default, +{ + fn parse(parser: Parser<'a>) -> Result { + if parser.peek::>() { + Ok(IndexOrCoreRef(CoreItemRef { + kind: K::default(), + idx: parser.parse()?, + export_name: None, + })) + } else { + Ok(IndexOrCoreRef(parser.parens(|p| p.parse())?)) + } + } +} diff --git a/tests/dump/bundled.wat b/tests/dump/bundled.wat index 8fb82f1308..4e17eac4d7 100644 --- a/tests/dump/bundled.wat +++ b/tests/dump/bundled.wat @@ -33,8 +33,8 @@ (core func $real-wasi-read (canon lower (func $real-wasi "read") - (memory (core memory $libc "mem")) - (realloc (core func $libc "realloc")) + (memory $libc "mem") + (realloc (func $libc "realloc")) ) ) @@ -42,8 +42,8 @@ (core instance $child (instantiate $CHILD (with "wasi_file" (instance $virt-wasi)))) (func (export "work") (canon lift (core func $child "play") - (memory (core memory $libc "mem")) - (realloc (core func $libc "realloc")) + (memory $libc "mem") + (realloc (func $libc "realloc")) ) ) ) diff --git a/tests/local/component-model/adapt.wast b/tests/local/component-model/adapt.wast index 3f873993f0..c95b9c88fa 100644 --- a/tests/local/component-model/adapt.wast +++ b/tests/local/component-model/adapt.wast @@ -30,9 +30,11 @@ (core instance $libc (instantiate $libc)) - (core func $log_lower_utf8 (canon lower (func $log) string-encoding=utf8 (memory (core memory $libc "memory")) (realloc (core func $libc "canonical_abi_realloc")))) - (core func $log_lower_utf16 (canon lower (func $log) string-encoding=utf16 (memory (core memory $libc "memory")) (realloc (core func $libc "canonical_abi_realloc")))) - (core func $log_lower_compact_utf16 (canon lower (func $log) string-encoding=latin1+utf16 (memory (core memory $libc "memory")) (realloc (core func $libc "canonical_abi_realloc")))) + (core alias export $libc "canonical_abi_realloc" (func $realloc)) + (core alias export $libc "memory" (memory $memory)) + (core func $log_lower_utf8 (canon lower (func $log) string-encoding=utf8 (memory $memory) (realloc $realloc))) + (core func $log_lower_utf16 (canon lower (func $log) string-encoding=utf16 (memory $memory) (realloc $realloc))) + (core func $log_lower_compact_utf16 (canon lower (func $log) string-encoding=latin1+utf16 (memory $memory) (realloc $realloc))) (core instance $my_instance (instantiate $my_module (with "libc" (instance $libc)) @@ -47,24 +49,24 @@ (canon lift (core func $my_instance "log-utf8") string-encoding=utf8 - (memory (core memory $libc "memory")) - (realloc (core func $libc "canonical_abi_realloc")) + (memory $memory) + (realloc $realloc) ) ) (func (export "log2") (param string) (canon lift (core func $my_instance "log-utf16") string-encoding=utf16 - (memory (core memory $libc "memory")) - (realloc (core func $libc "canonical_abi_realloc")) + (memory $memory) + (realloc $realloc) ) ) (func (export "log3") (param string) (canon lift (core func $my_instance "log-compact-utf16") string-encoding=latin1+utf16 - (memory (core memory $libc "memory")) - (realloc (core func $libc "canonical_abi_realloc")) + (memory $memory) + (realloc $realloc) ) ) ) @@ -93,7 +95,7 @@ (assert_invalid (component (import "" (func $f)) - (core func (canon lower (func $f) (memory (core memory 0)))) + (core func (canon lower (func $f) (memory 0))) ) "memory index out of bounds") @@ -102,7 +104,7 @@ (import "" (func $f)) (core module $m (memory (export "memory") 1)) (core instance $i (instantiate $m)) - (core func (canon lower (func $f) (memory (core memory $i "memory")) (memory (core memory $i "memory")))) + (core func (canon lower (func $f) (memory $i "memory") (memory $i "memory"))) ) "`memory` is specified more than once") @@ -125,7 +127,7 @@ (core instance $i (instantiate $m)) (func (param (list u8)) (canon lift (core func $i "f") - (memory (core memory $i "m")) + (memory $i "m") ) ) ) @@ -141,9 +143,9 @@ (core instance $i (instantiate $m)) (func (param (list u8)) (canon lift (core func $i "f") - (memory (core memory $i "m")) - (realloc (core func $i "r")) - (realloc (core func $i "r")) + (memory $i "m") + (realloc (func $i "r")) + (realloc (func $i "r")) ) ) ) @@ -159,8 +161,8 @@ (core instance $i (instantiate $m)) (func (param (list u8)) (canon lift (core func $i "f") - (memory (core memory $i "m")) - (realloc (core func $i "r")) + (memory $i "m") + (realloc (func $i "r")) ) ) ) @@ -177,9 +179,9 @@ (core instance $i (instantiate $m)) (func (result string) (canon lift (core func $i "f") - (memory (core memory $i "m")) - (realloc (core func $i "r")) - (post-return (core func $i "p")) + (memory $i "m") + (realloc (func $i "r")) + (post-return (func $i "p")) ) ) ) @@ -196,10 +198,10 @@ (core instance $i (instantiate $m)) (func (result string) (canon lift (core func $i "f") - (memory (core memory $i "m")) - (realloc (core func $i "r")) - (post-return (core func $i "p")) - (post-return (core func $i "p")) + (memory $i "m") + (realloc (func $i "r")) + (post-return (func $i "p")) + (post-return (func $i "p")) ) ) ) @@ -217,9 +219,9 @@ (core instance $i (instantiate $m)) (core func (canon lower (func $f) - (memory (core memory $i "m")) - (realloc (core func $i "r")) - (post-return (core func $i "p")) + (memory $i "m") + (realloc (func $i "r")) + (post-return (func $i "p")) ) ) ) @@ -235,9 +237,9 @@ (core instance $i (instantiate $m)) (func (result string) (canon lift (core func $i "f") - (memory (core memory $i "m")) - (realloc (core func $i "r")) - (post-return (core func $i "p")) + (memory $i "m") + (realloc (func $i "r")) + (post-return (func $i "p")) ) ) ) diff --git a/tests/local/component-model/big.wast b/tests/local/component-model/big.wast index 56c61f3d46..846f468b3e 100644 --- a/tests/local/component-model/big.wast +++ b/tests/local/component-model/big.wast @@ -9,7 +9,7 @@ (core instance $libc (instantiate $Libc)) (core func $log (canon lower (func $logging "log") - (memory (core memory $libc "memory")) (realloc (core func $libc "realloc")) + (memory $libc "memory") (realloc (func $libc "realloc")) )) (core module $Main (import "libc" "memory" (memory 1)) @@ -28,7 +28,7 @@ )) (func $run (param string) (result string) (canon lift (core func $main "run") - (memory (core memory $libc "memory")) (realloc (core func $libc "realloc")) + (memory $libc "memory") (realloc (func $libc "realloc")) )) (export "run" (func $run)) ) diff --git a/tests/local/component-model/func.wast b/tests/local/component-model/func.wast index 440efbdbd6..e32ecf4764 100644 --- a/tests/local/component-model/func.wast +++ b/tests/local/component-model/func.wast @@ -24,7 +24,7 @@ (memory (export "memory") 1) ) (core instance $libc (instantiate $libc)) - (core func (canon lower (func $log) (memory (core memory $libc "memory")))) + (core func (canon lower (func $log) (memory $libc "memory"))) ) (component @@ -35,7 +35,7 @@ (core instance $i (instantiate $m)) (func (export "ret-list") (result (list u8)) - (canon lift (core func $i "ret-list") (memory (core memory $i "memory"))) + (canon lift (core func $i "ret-list") (memory $i "memory")) ) ) @@ -46,7 +46,7 @@ (memory (export "memory") 1) ) (core instance $libc (instantiate $libc)) - (core func (canon lower (func $log) (memory (core memory $libc "memory")))) + (core func (canon lower (func $log) (memory $libc "memory"))) ) "canonical option `realloc` is required" ) @@ -60,7 +60,7 @@ (core instance $i (instantiate $m)) (func (export "param-list") (param (list u8)) - (canon lift (core func $i "param-list") (memory (core memory $i "memory"))) + (canon lift (core func $i "param-list") (memory $i "memory")) ) ) "canonical option `realloc` is required" diff --git a/tests/local/component-model/string.wast b/tests/local/component-model/string.wast index 19d0e0f6a7..9719012bdb 100644 --- a/tests/local/component-model/string.wast +++ b/tests/local/component-model/string.wast @@ -21,9 +21,9 @@ (core alias export $main "start" (func $main_func)) (func $start (param string) (result string) (canon lift (core func $main_func) - (memory (core memory $libc "memory")) - (realloc (core func $libc "canonical_abi_realloc")) - (post-return (core func $main "start-post-return")) + (memory $libc "memory") + (realloc (func $libc "canonical_abi_realloc")) + (post-return (func $main "start-post-return")) ) ) (start $start (value $name) (result (value $greeting))) diff --git a/tests/local/component-model/virtualize.wast b/tests/local/component-model/virtualize.wast index 538866fa27..0e24e92d5c 100644 --- a/tests/local/component-model/virtualize.wast +++ b/tests/local/component-model/virtualize.wast @@ -24,8 +24,8 @@ (core func $wasi_file_read (canon lower (func $wasi-file "read") - (memory (core memory $libc "mem")) - (realloc (core func $libc "realloc")) + (memory $libc "mem") + (realloc (func $libc "realloc")) ) ) @@ -100,8 +100,8 @@ (core func $real-wasi-read (canon lower (func $real-wasi "read") - (memory (core memory $libc "mem")) - (realloc (core func $libc "realloc")) + (memory $libc "mem") + (realloc (func $libc "realloc")) ) ) @@ -109,8 +109,8 @@ (core instance $child (instantiate $CHILD (with "wasi_file" (instance $virt-wasi)))) (func (export "work") (canon lift (core func $child "play") - (memory (core memory $libc "mem")) - (realloc (core func $libc "realloc")) + (memory $libc "mem") + (realloc (func $libc "realloc")) ) ) )