-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_body.go
More file actions
executable file
·75 lines (67 loc) · 1.86 KB
/
function_body.go
File metadata and controls
executable file
·75 lines (67 loc) · 1.86 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package wax
/*
Function bodies consist of a sequence of local variable declarations followed by bytecode instructions.
Instructions are encoded as an opcode followed by zero or more immediates as defined by the tables below.
Each function body must end with the end opcode.
| Field | Type | Description
| body_size | varuint32 | size of function body to follow, in bytes
| local_count | varuint32 | number of local entries
| locals | local_entry* | local variables
| code | byte* | bytecode of the function
| end | byte | 0x0b, indicating the end of the body
*/
/*
type FunctionBody struct {
BodySize uint32
LocalCount uint32
Locals []*LocalEntry
CodeBytes CodeBytes
//Code Code
End byte
}
func ParseFunctionBody(ber *BinaryEncodingReader) (*FunctionBody, error) {
bs64, _, err := ber.ReadVaruintN(32)
if err != nil {
return nil, err
}
bs := uint32(bs64)
bodyBytes := make([]byte, bs)
n, err := ber.Read(bodyBytes)
if err != nil {
return nil, err
}
if uint32(n) != bs {
return nil, errors.New("insufficient data")
}
consumedBytes := []byte{}
br := NewBinaryEncodingReader(bytes.NewReader(bodyBytes))
lc, c, err := br.ReadVaruintN(32)
if err != nil {
return nil, err
}
consumedBytes = append(consumedBytes, c...)
var locals []*LocalEntry
if lc > 0 {
locals = make([]*LocalEntry, 0, lc)
for i := uint64(0); i < lc; i++ {
l, c, err := ParseLocalEntry(br)
if err != nil {
return nil, err
}
locals = append(locals, l)
consumedBytes = append(consumedBytes, c...)
}
}
codeBytes := bodyBytes[len(consumedBytes):]
end := codeBytes[len(codeBytes)-1:][0]
code := codeBytes[0 : len(codeBytes)-1]
return &FunctionBody{
BodySize: bs,
LocalCount: uint32(lc),
Locals: locals,
CodeBytes: code,
//Code: code,
End: end,
}, nil
}
*/