Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions crates/wasmprinter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
32 changes: 19 additions & 13 deletions crates/wast/src/component/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,21 @@ impl<'a> Parse<'a> for CanonOpt<'a> {
parser.parens(|parser| {
let mut l = parser.lookahead1();
if l.peek::<kw::memory>() {
parser.parse::<kw::memory>()?;
Ok(CanonOpt::Memory(parser.parens(|parser| {
parser.parse::<kw::core>()?;
parser.parse()
})?))
let span = parser.parse::<kw::memory>()?.0;
Ok(CanonOpt::Memory(parse_trailing_item_ref(
kw::memory(span),
parser,
)?))
} else if l.peek::<kw::realloc>() {
parser.parse::<kw::realloc>()?;
Ok(CanonOpt::Realloc(parser.parens(|parser| {
parser.parse::<kw::core>()?;
parser.parse()
})?))
Ok(CanonOpt::Realloc(
parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
))
} else if l.peek::<kw::post_return>() {
parser.parse::<kw::post_return>()?;
Ok(CanonOpt::PostReturn(parser.parens(|parser| {
parser.parse::<kw::core>()?;
parser.parse()
})?))
Ok(CanonOpt::PostReturn(
parser.parse::<IndexOrCoreRef<'_, _>>()?.0,
))
} else {
Err(l.error())
}
Expand All @@ -356,6 +354,14 @@ impl<'a> Parse<'a> for CanonOpt<'a> {
}
}

fn parse_trailing_item_ref<'a, T>(kind: T, parser: Parser<'a>) -> Result<CoreItemRef<'a, T>> {
Ok(CoreItemRef {
kind,
idx: parser.parse()?,
export_name: parser.parse()?,
})
}

impl<'a> Parse<'a> for Vec<CanonOpt<'a>> {
fn parse(parser: Parser<'a>) -> Result<Self> {
let mut funcs = Vec::new();
Expand Down
21 changes: 21 additions & 0 deletions crates/wast/src/component/item_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
if parser.peek::<Index<'_>>() {
Ok(IndexOrCoreRef(CoreItemRef {
kind: K::default(),
idx: parser.parse()?,
export_name: None,
}))
} else {
Ok(IndexOrCoreRef(parser.parens(|p| p.parse())?))
}
}
}
8 changes: 4 additions & 4 deletions tests/dump/bundled.wat
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@

(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"))
)
)

(core instance $virt-wasi (instantiate $VIRTUALIZE (with "wasi_file" (instance (export "read" (func $real-wasi-read))))))
(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"))
)
)
)
62 changes: 32 additions & 30 deletions tests/local/component-model/adapt.wast
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
)
)
)
Expand Down Expand Up @@ -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")

Expand All @@ -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")

Expand All @@ -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")
)
)
)
Expand All @@ -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"))
)
)
)
Expand All @@ -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"))
)
)
)
Expand All @@ -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"))
)
)
)
Expand All @@ -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"))
)
)
)
Expand All @@ -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"))
)
)
)
Expand All @@ -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"))
)
)
)
Expand Down
4 changes: 2 additions & 2 deletions tests/local/component-model/big.wast
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
)
8 changes: 4 additions & 4 deletions tests/local/component-model/func.wast
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
)
)

Expand All @@ -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"
)
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions tests/local/component-model/string.wast
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
12 changes: 6 additions & 6 deletions tests/local/component-model/virtualize.wast
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
)
)

Expand Down Expand Up @@ -100,17 +100,17 @@

(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"))
)
)

(core instance $virt-wasi (instantiate $VIRTUALIZE (with "wasi_file" (instance (export "read" (func $real-wasi-read))))))
(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"))
)
)
)
Expand Down