Add *.wast support for invoking components (#4526)

This commit builds on bytecodealliance/wasm-tools#690 to add support to
testing of the component model to execute functions when running
`*.wast` files. This support is all built on #4442 as functions are
invoked through a "dynamic" API. Right now the testing and integration
is fairly crude but I'm hoping that we can try to improve it over time
as necessary. For now this should provide a hopefully more convenient
syntax for unit tests and the like.
This commit is contained in:
Alex Crichton
2022-07-27 16:02:16 -05:00
committed by GitHub
parent 0508932174
commit 174b60dcf7
25 changed files with 1154 additions and 468 deletions

View File

@@ -160,9 +160,17 @@
(component $c
(import "roundtrip" (func $roundtrip (type $roundtrip)))
(core module $libc (memory (export "memory") 1))
(core module $libc
(memory (export "memory") 1)
(func (export "realloc") (param i32 i32 i32 i32) (result i32) unreachable)
)
(core instance $libc (instantiate $libc))
(core func $roundtrip (canon lower (func $roundtrip) (memory $libc "memory")))
(core func $roundtrip
(canon lower (func $roundtrip)
(memory $libc "memory")
(realloc (func $libc "realloc")) ;; FIXME(wasm-tools#693) should not be necessary
)
)
(core module $m2
(import "libc" "memory" (memory 1))
@@ -312,9 +320,18 @@
(component $c
(import "foo" (func $foo (param $tuple20)))
(core module $libc (memory (export "memory") 1))
(core module $libc
(memory (export "memory") 1)
(func (export "realloc") (param i32 i32 i32 i32) (result i32)
unreachable)
)
(core instance $libc (instantiate $libc))
(core func $foo (canon lower (func $foo) (memory $libc "memory")))
(core func $foo
(canon lower (func $foo)
(memory $libc "memory")
(realloc (func $libc "realloc")) ;; FIXME(wasm-tools#693) should not be necessary
)
)
(core module $something
(import "" "foo" (func (param i32)))
)
@@ -620,9 +637,17 @@
)
(component $c2
(import "r" (func $r (param $big)))
(core module $libc (memory (export "memory") 1))
(core module $libc
(memory (export "memory") 1)
(func (export "realloc") (param i32 i32 i32 i32) (result i32) unreachable)
)
(core instance $libc (instantiate $libc))
(core func $r (canon lower (func $r) (memory $libc "memory")))
(core func $r
(canon lower (func $r)
(memory $libc "memory")
(realloc (func $libc "realloc")) ;; FIXME(wasm-tools#693) should not be necessary
)
)
(core module $m
(import "" "r" (func $r (param i32)))
@@ -660,9 +685,18 @@
)
(component $c2
(import "r" (func $r (param $big)))
(core module $libc (memory (export "memory") 1))
(core module $libc
(memory (export "memory") 1)
(func (export "realloc") (param i32 i32 i32 i32) (result i32) unreachable)
)
(core instance $libc (instantiate $libc))
(core func $r (canon lower (func $r) (memory $libc "memory")))
(core func $r
(canon lower (func $r)
(memory $libc "memory")
(realloc (func $libc "realloc")) ;; FIXME(wasm-tools#693) should not be necessary
)
)
(core module $m
(import "" "r" (func $r (param i32)))

View File

@@ -32,3 +32,11 @@
(component (export ""))
)
"exporting a component from the root component is not supported")
(component
(core module $m (func (export "")))
(core instance $m (instantiate $m))
(func (export "") (canon lift (core func $m "")))
)
(assert_return (invoke "") (unit.const))

View File

@@ -101,3 +101,28 @@
))
)
)
(component
(core module $m (func (export "") (param i32) (result i32) local.get 0))
(core instance $m (instantiate $m))
(func (export "i-to-b") (param u32) (result bool) (canon lift (core func $m "")))
(func (export "i-to-u8") (param u32) (result u8) (canon lift (core func $m "")))
(func (export "i-to-s8") (param u32) (result s8) (canon lift (core func $m "")))
(func (export "i-to-u16") (param u32) (result u16) (canon lift (core func $m "")))
(func (export "i-to-s16") (param u32) (result s16) (canon lift (core func $m "")))
)
(assert_return (invoke "i-to-b" (u32.const 0)) (bool.const false))
(assert_return (invoke "i-to-b" (u32.const 1)) (bool.const true))
(assert_return (invoke "i-to-b" (u32.const 2)) (bool.const true))
(assert_return (invoke "i-to-u8" (u32.const 0x00)) (u8.const 0))
(assert_return (invoke "i-to-u8" (u32.const 0x01)) (u8.const 1))
(assert_return (invoke "i-to-u8" (u32.const 0xf01)) (u8.const 1))
(assert_return (invoke "i-to-u8" (u32.const 0xf00)) (u8.const 0))
(assert_return (invoke "i-to-s8" (u32.const 0xffffffff)) (s8.const -1))
(assert_return (invoke "i-to-s8" (u32.const 127)) (s8.const 127))
(assert_return (invoke "i-to-u16" (u32.const 0)) (u16.const 0))
(assert_return (invoke "i-to-u16" (u32.const 1)) (u16.const 1))
(assert_return (invoke "i-to-u16" (u32.const 0xffffffff)) (u16.const 0xffff))
(assert_return (invoke "i-to-s16" (u32.const 0)) (s16.const 0))
(assert_return (invoke "i-to-s16" (u32.const 1)) (s16.const 1))
(assert_return (invoke "i-to-s16" (u32.const 0xffffffff)) (s16.const -1))