wa-lang.org/wazero@v1.0.2/internal/wasm/binary/import_test.go (about)

     1  package binary
     2  
     3  import (
     4  	"testing"
     5  
     6  	"wa-lang.org/wazero/internal/testing/require"
     7  	"wa-lang.org/wazero/internal/wasm"
     8  )
     9  
    10  func TestEncodeImport(t *testing.T) {
    11  	ptrOfUint32 := func(v uint32) *uint32 {
    12  		return &v
    13  	}
    14  
    15  	tests := []struct {
    16  		name     string
    17  		input    *wasm.Import
    18  		expected []byte
    19  	}{
    20  		{
    21  			name: "func no module, no name, type index 0",
    22  			input: &wasm.Import{ // e.g. (import "" "" (func (type 0)))
    23  				Type:     wasm.ExternTypeFunc,
    24  				Module:   "",
    25  				Name:     "",
    26  				DescFunc: 0,
    27  			},
    28  			expected: []byte{wasm.ExternTypeFunc, 0x00, 0x00, 0x00},
    29  		},
    30  		{
    31  			name: "func module, no name, type index 0",
    32  			input: &wasm.Import{ // e.g. (import "$test" "" (func (type 0)))
    33  				Type:     wasm.ExternTypeFunc,
    34  				Module:   "test",
    35  				Name:     "",
    36  				DescFunc: 0,
    37  			},
    38  			expected: []byte{
    39  				0x04, 't', 'e', 's', 't',
    40  				0x00,
    41  				wasm.ExternTypeFunc,
    42  				0x00,
    43  			},
    44  		},
    45  		{
    46  			name: "func module, name, type index 0",
    47  			input: &wasm.Import{ // e.g. (import "$math" "$pi" (func (type 0)))
    48  				Type:     wasm.ExternTypeFunc,
    49  				Module:   "math",
    50  				Name:     "pi",
    51  				DescFunc: 0,
    52  			},
    53  			expected: []byte{
    54  				0x04, 'm', 'a', 't', 'h',
    55  				0x02, 'p', 'i',
    56  				wasm.ExternTypeFunc,
    57  				0x00,
    58  			},
    59  		},
    60  		{
    61  			name: "func module, name, type index 10",
    62  			input: &wasm.Import{ // e.g. (import "$math" "$pi" (func (type 10)))
    63  				Type:     wasm.ExternTypeFunc,
    64  				Module:   "math",
    65  				Name:     "pi",
    66  				DescFunc: 10,
    67  			},
    68  			expected: []byte{
    69  				0x04, 'm', 'a', 't', 'h',
    70  				0x02, 'p', 'i',
    71  				wasm.ExternTypeFunc,
    72  				0x0a,
    73  			},
    74  		},
    75  		{
    76  			name: "global const",
    77  			input: &wasm.Import{
    78  				Type:       wasm.ExternTypeGlobal,
    79  				Module:     "math",
    80  				Name:       "pi",
    81  				DescGlobal: &wasm.GlobalType{ValType: wasm.ValueTypeF64},
    82  			},
    83  			expected: []byte{
    84  				0x04, 'm', 'a', 't', 'h',
    85  				0x02, 'p', 'i',
    86  				wasm.ExternTypeGlobal,
    87  				wasm.ValueTypeF64, 0x00, // 0 == const
    88  			},
    89  		},
    90  		{
    91  			name: "global var",
    92  			input: &wasm.Import{
    93  				Type:       wasm.ExternTypeGlobal,
    94  				Module:     "math",
    95  				Name:       "pi",
    96  				DescGlobal: &wasm.GlobalType{ValType: wasm.ValueTypeF64, Mutable: true},
    97  			},
    98  			expected: []byte{
    99  				0x04, 'm', 'a', 't', 'h',
   100  				0x02, 'p', 'i',
   101  				wasm.ExternTypeGlobal,
   102  				wasm.ValueTypeF64, 0x01, // 1 == var
   103  			},
   104  		},
   105  		{
   106  			name: "table",
   107  			input: &wasm.Import{
   108  				Type:      wasm.ExternTypeTable,
   109  				Module:    "my",
   110  				Name:      "table",
   111  				DescTable: &wasm.Table{Min: 1, Max: ptrOfUint32(2)},
   112  			},
   113  			expected: []byte{
   114  				0x02, 'm', 'y',
   115  				0x05, 't', 'a', 'b', 'l', 'e',
   116  				wasm.ExternTypeTable,
   117  				wasm.RefTypeFuncref,
   118  				0x1, 0x1, 0x2, // Limit with max.
   119  			},
   120  		},
   121  		{
   122  			name: "memory",
   123  			input: &wasm.Import{
   124  				Type:    wasm.ExternTypeMemory,
   125  				Module:  "my",
   126  				Name:    "memory",
   127  				DescMem: &wasm.Memory{Min: 1, Max: 2, IsMaxEncoded: true},
   128  			},
   129  			expected: []byte{
   130  				0x02, 'm', 'y',
   131  				0x06, 'm', 'e', 'm', 'o', 'r', 'y',
   132  				wasm.ExternTypeMemory,
   133  				0x1, 0x1, 0x2, // Limit with max.
   134  			},
   135  		},
   136  		{
   137  			name: "memory - defaultt max",
   138  			input: &wasm.Import{
   139  				Type:    wasm.ExternTypeMemory,
   140  				Module:  "my",
   141  				Name:    "memory",
   142  				DescMem: &wasm.Memory{Min: 1, Max: wasm.MemoryLimitPages, IsMaxEncoded: false},
   143  			},
   144  			expected: []byte{
   145  				0x02, 'm', 'y',
   146  				0x06, 'm', 'e', 'm', 'o', 'r', 'y',
   147  				wasm.ExternTypeMemory,
   148  				0x0, 0x1, // Limit without max.
   149  			},
   150  		},
   151  	}
   152  
   153  	for _, tt := range tests {
   154  		tc := tt
   155  
   156  		t.Run(tc.name, func(t *testing.T) {
   157  			bytes := encodeImport(tc.input)
   158  			require.Equal(t, tc.expected, bytes)
   159  		})
   160  	}
   161  }