Supports functions with multiple results (multi-value) (#446)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-04-13 09:22:39 +08:00
committed by GitHub
parent 927914ffb0
commit c3ff16d596
68 changed files with 5555 additions and 1155 deletions

View File

@@ -248,6 +248,16 @@ type Memory interface {
Write(offset uint32, v []byte) bool
}
// EncodeI32 encodes the input as a ValueTypeI32.
func EncodeI32(input int32) uint64 {
return uint64(uint32(input))
}
// EncodeI64 encodes the input as a ValueTypeI64.
func EncodeI64(input int64) uint64 {
return uint64(input)
}
// EncodeF32 encodes the input as a ValueTypeF32.
// See DecodeF32
func EncodeF32(input float32) uint64 {

View File

@@ -41,6 +41,7 @@ func TestEncodeDecodeF32(t *testing.T) {
t.Run(fmt.Sprintf("%f", v), func(t *testing.T) {
encoded := EncodeF32(v)
binary := DecodeF32(encoded)
require.Zero(t, encoded>>32) // Ensures high bits aren't set
if math.IsNaN(float64(binary)) { // NaN cannot be compared with themselves, so we have to use IsNaN
require.True(t, math.IsNaN(float64(binary)))
} else {
@@ -73,3 +74,32 @@ func TestEncodeDecodeF64(t *testing.T) {
})
}
}
func TestEncodeCastI32(t *testing.T) {
for _, v := range []int32{
0, 100, -100, 1, -1,
math.MaxInt32,
math.MinInt32,
} {
t.Run(fmt.Sprintf("%d", v), func(t *testing.T) {
encoded := EncodeI32(v)
require.Zero(t, encoded>>32) // Ensures high bits aren't set
binary := int32(encoded)
require.Equal(t, v, binary)
})
}
}
func TestEncodeCastI64(t *testing.T) {
for _, v := range []int64{
0, 100, -100, 1, -1,
math.MaxInt64,
math.MinInt64,
} {
t.Run(fmt.Sprintf("%d", v), func(t *testing.T) {
encoded := EncodeI64(v)
binary := int64(encoded)
require.Equal(t, v, binary)
})
}
}