-
Notifications
You must be signed in to change notification settings - Fork 3
/
func.go
52 lines (42 loc) · 1.18 KB
/
func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package types
import (
"bytes"
"fmt"
"io"
"github.com/c0mm4nd/wasman/leb128decode"
)
// FuncType classify the signature of functions, mapping a vector of parameters to a vector of results, written as follows.
type FuncType struct {
InputTypes []ValueType
ReturnTypes []ValueType
}
// ReadFuncType will read a types.ReadFuncType from the io.Reader
func ReadFuncType(r *bytes.Reader) (*FuncType, error) {
b := make([]byte, 1)
if _, err := io.ReadFull(r, b); err != nil {
return nil, fmt.Errorf("read leading byte: %w", err)
}
if b[0] != 0x60 {
return nil, fmt.Errorf("%w: %#x != 0x60", ErrInvalidTypeByte, b[0])
}
s, _, err := leb128decode.DecodeUint32(r)
if err != nil {
return nil, fmt.Errorf("get the size of input value types: %w", err)
}
ip, err := ReadValueTypes(r, s)
if err != nil {
return nil, fmt.Errorf("read value types of inputs: %w", err)
}
s, _, err = leb128decode.DecodeUint32(r)
if err != nil {
return nil, fmt.Errorf("get the size of output value types: %w", err)
}
op, err := ReadValueTypes(r, s)
if err != nil {
return nil, fmt.Errorf("read value types of outputs: %w", err)
}
return &FuncType{
InputTypes: ip,
ReturnTypes: op,
}, nil
}