github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/testing/binaryencoding/export_test.go (about) 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 func TestEncodeExport(t *testing.T) { 11 tests := []struct { 12 name string 13 input *wasm.Export 14 expected []byte 15 }{ 16 { 17 name: "func no name, index 0", 18 input: &wasm.Export{ // e.g. (export "" (func 0))) 19 Type: wasm.ExternTypeFunc, 20 Name: "", 21 Index: 0, 22 }, 23 expected: []byte{wasm.ExternTypeFunc, 0x00, 0x00}, 24 }, 25 { 26 name: "func name, func index 0", 27 input: &wasm.Export{ // e.g. (export "pi" (func 0)) 28 Type: wasm.ExternTypeFunc, 29 Name: "pi", 30 Index: 0, 31 }, 32 expected: []byte{ 33 0x02, 'p', 'i', 34 wasm.ExternTypeFunc, 35 0x00, 36 }, 37 }, 38 { 39 name: "func name, index 10", 40 input: &wasm.Export{ // e.g. (export "pi" (func 10)) 41 Type: wasm.ExternTypeFunc, 42 Name: "pi", 43 Index: 10, 44 }, 45 expected: []byte{ 46 0x02, 'p', 'i', 47 wasm.ExternTypeFunc, 48 0x0a, 49 }, 50 }, 51 { 52 name: "global no name, index 0", 53 input: &wasm.Export{ // e.g. (export "" (global 0))) 54 Type: wasm.ExternTypeGlobal, 55 Name: "", 56 Index: 0, 57 }, 58 expected: []byte{0x00, wasm.ExternTypeGlobal, 0x00}, 59 }, 60 { 61 name: "global name, global index 0", 62 input: &wasm.Export{ // e.g. (export "pi" (global 0)) 63 Type: wasm.ExternTypeGlobal, 64 Name: "pi", 65 Index: 0, 66 }, 67 expected: []byte{ 68 0x02, 'p', 'i', 69 wasm.ExternTypeGlobal, 70 0x00, 71 }, 72 }, 73 { 74 name: "global name, index 10", 75 input: &wasm.Export{ // e.g. (export "pi" (global 10)) 76 Type: wasm.ExternTypeGlobal, 77 Name: "pi", 78 Index: 10, 79 }, 80 expected: []byte{ 81 0x02, 'p', 'i', 82 wasm.ExternTypeGlobal, 83 0x0a, 84 }, 85 }, 86 { 87 name: "memory no name, index 0", 88 input: &wasm.Export{ // e.g. (export "" (memory 0))) 89 Type: wasm.ExternTypeMemory, 90 Name: "", 91 Index: 0, 92 }, 93 expected: []byte{0x00, wasm.ExternTypeMemory, 0x00}, 94 }, 95 { 96 name: "memory name, memory index 0", 97 input: &wasm.Export{ // e.g. (export "mem" (memory 0)) 98 Type: wasm.ExternTypeMemory, 99 Name: "mem", 100 Index: 0, 101 }, 102 expected: []byte{ 103 0x03, 'm', 'e', 'm', 104 wasm.ExternTypeMemory, 105 0x00, 106 }, 107 }, 108 { 109 name: "memory name, index 10", 110 input: &wasm.Export{ // e.g. (export "mem" (memory 10)) 111 Type: wasm.ExternTypeMemory, 112 Name: "mem", 113 Index: 10, 114 }, 115 expected: []byte{ 116 0x03, 'm', 'e', 'm', 117 wasm.ExternTypeMemory, 118 0x0a, 119 }, 120 }, 121 } 122 123 for _, tt := range tests { 124 tc := tt 125 126 t.Run(tc.name, func(t *testing.T) { 127 bytes := encodeExport(tc.input) 128 require.Equal(t, tc.expected, bytes) 129 }) 130 } 131 }