1 package binaryencoding
2
3 import (
4 "testing"
5
6 "github.com/tetratelabs/wazero/internal/testing/require"
7 "github.com/tetratelabs/wazero/internal/wasm"
8 )
9
10 var addLocalZeroLocalTwo = []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 2, wasm.OpcodeI32Add, wasm.OpcodeEnd}
11
12 func TestEncodeCode(t *testing.T) {
13 addLocalZeroLocalOne := []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 1, wasm.OpcodeI32Add, wasm.OpcodeEnd}
14 tests := []struct {
15 name string
16 input *wasm.Code
17 expected []byte
18 }{
19 {
20 name: "smallest function body",
21 input: &wasm.Code{
22 Body: []byte{wasm.OpcodeEnd},
23 },
24 expected: []byte{
25 0x02,
26 0x00,
27 wasm.OpcodeEnd,
28 },
29 },
30 {
31 name: "params and instructions",
32 input: &wasm.Code{
33 Body: addLocalZeroLocalOne,
34 },
35 expected: append([]byte{
36 0x07,
37 0x00,
38 },
39 addLocalZeroLocalOne...,
40 ),
41 },
42 {
43 name: "locals and instructions",
44 input: &wasm.Code{
45 LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
46 Body: addLocalZeroLocalOne,
47 },
48 expected: append([]byte{
49 0x09,
50 0x01,
51 0x02, wasm.ValueTypeI32,
52 },
53 addLocalZeroLocalOne...,
54 ),
55 },
56 {
57 name: "mixed locals and instructions",
58 input: &wasm.Code{
59 LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeI32},
60 Body: addLocalZeroLocalTwo,
61 },
62 expected: append([]byte{
63 0x0d,
64 0x03,
65 0x01, wasm.ValueTypeI32,
66 0x01, wasm.ValueTypeI64,
67 0x01, wasm.ValueTypeI32,
68 },
69 addLocalZeroLocalTwo...,
70 ),
71 },
72 }
73
74 for _, tt := range tests {
75 tc := tt
76
77 t.Run(tc.name, func(t *testing.T) {
78 bytes := encodeCode(tc.input)
79 require.Equal(t, tc.expected, bytes)
80 })
81 }
82 }
83
84 func BenchmarkEncodeCode(b *testing.B) {
85 input := &wasm.Code{
86 LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeI32},
87 Body: addLocalZeroLocalTwo,
88 }
89
90 b.ReportAllocs()
91 for i := 0; i < b.N; i++ {
92 if bytes := encodeCode(input); len(bytes) == 0 {
93 b.Fatal("didn't encode anything")
94 }
95 }
96 }
97
View as plain text