| 1 |
package wasm |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"math" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/tetratelabs/wazero/api" |
| 9 |
"github.com/tetratelabs/wazero/internal/internalapi" |
| 10 |
"github.com/tetratelabs/wazero/internal/testing/require" |
| 11 |
"github.com/tetratelabs/wazero/internal/u32" |
| 12 |
"github.com/tetratelabs/wazero/internal/u64" |
| 13 |
) |
| 14 |
|
| 15 |
func TestGlobalTypes(t *testing.T) { |
| 16 |
tests := []struct { |
| 17 |
name string |
| 18 |
global api.Global |
| 19 |
expectedType api.ValueType |
| 20 |
expectedVal uint64 |
| 21 |
expectedString string |
| 22 |
expectedMutable bool |
| 23 |
}{ |
| 24 |
{ |
| 25 |
name: "i32 - immutable", |
| 26 |
global: constantGlobal{g: &GlobalInstance{ |
| 27 |
Type: GlobalType{ValType: ValueTypeI32}, |
| 28 |
Val: 1, |
| 29 |
}}, |
| 30 |
expectedType: ValueTypeI32.Kind(), |
| 31 |
expectedVal: 1, |
| 32 |
expectedString: "global(1)", |
| 33 |
}, |
| 34 |
{ |
| 35 |
name: "i32 - immutable - max", |
| 36 |
global: constantGlobal{g: &GlobalInstance{ |
| 37 |
Type: GlobalType{ValType: ValueTypeI32}, |
| 38 |
Val: math.MaxInt32, |
| 39 |
}}, |
| 40 |
expectedType: ValueTypeI32.Kind(), |
| 41 |
expectedVal: math.MaxInt32, |
| 42 |
expectedString: "global(2147483647)", |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "i64 - immutable", |
| 46 |
global: constantGlobal{g: &GlobalInstance{ |
| 47 |
Type: GlobalType{ValType: ValueTypeI64}, |
| 48 |
Val: 1, |
| 49 |
}}, |
| 50 |
expectedType: ValueTypeI64.Kind(), |
| 51 |
expectedVal: 1, |
| 52 |
expectedString: "global(1)", |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "i64 - immutable - max", |
| 56 |
global: constantGlobal{g: &GlobalInstance{ |
| 57 |
Type: GlobalType{ValType: ValueTypeI64}, |
| 58 |
Val: math.MaxInt64, |
| 59 |
}}, |
| 60 |
expectedType: ValueTypeI64.Kind(), |
| 61 |
expectedVal: math.MaxInt64, |
| 62 |
expectedString: "global(9223372036854775807)", |
| 63 |
}, |
| 64 |
{ |
| 65 |
name: "f32 - immutable", |
| 66 |
global: constantGlobal{g: &GlobalInstance{ |
| 67 |
Type: GlobalType{ValType: ValueTypeF32}, |
| 68 |
Val: api.EncodeF32(1.0), |
| 69 |
}}, |
| 70 |
expectedType: ValueTypeF32.Kind(), |
| 71 |
expectedVal: api.EncodeF32(1.0), |
| 72 |
expectedString: "global(1.000000)", |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "f32 - immutable - max", |
| 76 |
global: constantGlobal{g: &GlobalInstance{ |
| 77 |
Type: GlobalType{ValType: ValueTypeF32}, |
| 78 |
Val: api.EncodeF32(math.MaxFloat32), |
| 79 |
}}, |
| 80 |
expectedType: ValueTypeF32.Kind(), |
| 81 |
expectedVal: api.EncodeF32(math.MaxFloat32), |
| 82 |
expectedString: "global(340282346638528859811704183484516925440.000000)", |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: "f64 - immutable", |
| 86 |
global: constantGlobal{g: &GlobalInstance{ |
| 87 |
Type: GlobalType{ValType: ValueTypeF64}, |
| 88 |
Val: api.EncodeF64(1.0), |
| 89 |
}}, |
| 90 |
expectedType: ValueTypeF64.Kind(), |
| 91 |
expectedVal: api.EncodeF64(1.0), |
| 92 |
expectedString: "global(1.000000)", |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "f64 - immutable - max", |
| 96 |
global: constantGlobal{g: &GlobalInstance{ |
| 97 |
Type: GlobalType{ValType: ValueTypeF64}, |
| 98 |
Val: api.EncodeF64(math.MaxFloat64), |
| 99 |
}}, |
| 100 |
expectedType: ValueTypeF64.Kind(), |
| 101 |
expectedVal: api.EncodeF64(math.MaxFloat64), |
| 102 |
expectedString: "global(179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000)", |
| 103 |
}, |
| 104 |
{ |
| 105 |
name: "i32 - mutable", |
| 106 |
global: mutableGlobal{internalapi.WazeroOnlyType{}, &GlobalInstance{ |
| 107 |
Type: GlobalType{ValType: ValueTypeI32, Mutable: true}, |
| 108 |
Val: 1, |
| 109 |
}}, |
| 110 |
expectedType: ValueTypeI32.Kind(), |
| 111 |
expectedVal: 1, |
| 112 |
expectedString: "global(1)", |
| 113 |
expectedMutable: true, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "i64 - mutable", |
| 117 |
global: mutableGlobal{internalapi.WazeroOnlyType{}, &GlobalInstance{ |
| 118 |
Type: GlobalType{ValType: ValueTypeI64, Mutable: true}, |
| 119 |
Val: 1, |
| 120 |
}}, |
| 121 |
expectedType: ValueTypeI64.Kind(), |
| 122 |
expectedVal: 1, |
| 123 |
expectedString: "global(1)", |
| 124 |
expectedMutable: true, |
| 125 |
}, |
| 126 |
{ |
| 127 |
name: "f32 - mutable", |
| 128 |
global: mutableGlobal{internalapi.WazeroOnlyType{}, &GlobalInstance{ |
| 129 |
Type: GlobalType{ValType: ValueTypeF32, Mutable: true}, |
| 130 |
Val: api.EncodeF32(1.0), |
| 131 |
}}, |
| 132 |
expectedType: ValueTypeF32.Kind(), |
| 133 |
expectedVal: api.EncodeF32(1.0), |
| 134 |
expectedString: "global(1.000000)", |
| 135 |
expectedMutable: true, |
| 136 |
}, |
| 137 |
{ |
| 138 |
name: "f64 - mutable", |
| 139 |
global: mutableGlobal{internalapi.WazeroOnlyType{}, &GlobalInstance{ |
| 140 |
Type: GlobalType{ValType: ValueTypeF64, Mutable: true}, |
| 141 |
Val: api.EncodeF64(1.0), |
| 142 |
}}, |
| 143 |
expectedType: ValueTypeF64.Kind(), |
| 144 |
expectedVal: api.EncodeF64(1.0), |
| 145 |
expectedString: "global(1.000000)", |
| 146 |
expectedMutable: true, |
| 147 |
}, |
| 148 |
} |
| 149 |
|
| 150 |
for _, tt := range tests { |
| 151 |
tc := tt |
| 152 |
|
| 153 |
t.Run(tc.name, func(t *testing.T) { |
| 154 |
require.Equal(t, tc.expectedType, tc.global.Type()) |
| 155 |
require.Equal(t, tc.expectedVal, tc.global.Get()) |
| 156 |
require.Equal(t, tc.expectedString, tc.global.String()) |
| 157 |
|
| 158 |
mutable, ok := tc.global.(api.MutableGlobal) |
| 159 |
require.Equal(t, tc.expectedMutable, ok) |
| 160 |
if ok { |
| 161 |
mutable.Set(2) |
| 162 |
require.Equal(t, uint64(2), tc.global.Get()) |
| 163 |
|
| 164 |
mutable.Set(tc.expectedVal) |
| 165 |
require.Equal(t, tc.expectedVal, tc.global.Get()) |
| 166 |
} |
| 167 |
}) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestPublicModule_Global(t *testing.T) { |
| 172 |
tests := []struct { |
| 173 |
name string |
| 174 |
module *Module |
| 175 |
expected api.Global |
| 176 |
}{ |
| 177 |
{ |
| 178 |
name: "no global", |
| 179 |
module: &Module{}, |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "global not exported", |
| 183 |
module: &Module{ |
| 184 |
GlobalSection: []Global{ |
| 185 |
{ |
| 186 |
Type: GlobalType{ValType: ValueTypeI32}, |
| 187 |
Init: NewConstantExpressionFromI32(1), |
| 188 |
}, |
| 189 |
}, |
| 190 |
}, |
| 191 |
}, |
| 192 |
{ |
| 193 |
name: "global exported - immutable I32", |
| 194 |
module: &Module{ |
| 195 |
GlobalSection: []Global{ |
| 196 |
{ |
| 197 |
Type: GlobalType{ValType: ValueTypeI32}, |
| 198 |
Init: NewConstantExpressionFromI32(1), |
| 199 |
}, |
| 200 |
}, |
| 201 |
Exports: map[string]*Export{"global": {Type: ExternTypeGlobal, Name: "global"}}, |
| 202 |
}, |
| 203 |
expected: constantGlobal{g: &GlobalInstance{ |
| 204 |
Type: GlobalType{ValType: ValueTypeI32}, |
| 205 |
Val: 1, |
| 206 |
}}, |
| 207 |
}, |
| 208 |
{ |
| 209 |
name: "global exported - immutable I64", |
| 210 |
module: &Module{ |
| 211 |
GlobalSection: []Global{ |
| 212 |
{ |
| 213 |
Type: GlobalType{ValType: ValueTypeI64}, |
| 214 |
Init: NewConstantExpressionFromI64(1), |
| 215 |
}, |
| 216 |
}, |
| 217 |
Exports: map[string]*Export{"global": {Type: ExternTypeGlobal, Name: "global"}}, |
| 218 |
}, |
| 219 |
expected: constantGlobal{g: &GlobalInstance{ |
| 220 |
Type: GlobalType{ValType: ValueTypeI64}, |
| 221 |
Val: 1, |
| 222 |
}}, |
| 223 |
}, |
| 224 |
{ |
| 225 |
name: "global exported - immutable F32", |
| 226 |
module: &Module{ |
| 227 |
GlobalSection: []Global{ |
| 228 |
{ |
| 229 |
Type: GlobalType{ValType: ValueTypeF32}, |
| 230 |
Init: NewConstantExpressionFromOpcode(OpcodeF32Const, u32.LeBytes(uint32(api.EncodeF32(1.0)))), |
| 231 |
}, |
| 232 |
}, |
| 233 |
Exports: map[string]*Export{"global": {Type: ExternTypeGlobal, Name: "global"}}, |
| 234 |
}, |
| 235 |
expected: constantGlobal{g: &GlobalInstance{ |
| 236 |
Type: GlobalType{ValType: ValueTypeF32}, |
| 237 |
Val: api.EncodeF32(1.0), |
| 238 |
}}, |
| 239 |
}, |
| 240 |
{ |
| 241 |
name: "global exported - immutable F64", |
| 242 |
module: &Module{ |
| 243 |
GlobalSection: []Global{ |
| 244 |
{ |
| 245 |
Type: GlobalType{ValType: ValueTypeF64}, |
| 246 |
Init: NewConstantExpressionFromOpcode(OpcodeF64Const, u64.LeBytes(api.EncodeF64(1.0))), |
| 247 |
}, |
| 248 |
}, |
| 249 |
Exports: map[string]*Export{"global": {Type: ExternTypeGlobal, Name: "global"}}, |
| 250 |
}, |
| 251 |
expected: constantGlobal{g: &GlobalInstance{ |
| 252 |
Type: GlobalType{ValType: ValueTypeF64}, |
| 253 |
Val: api.EncodeF64(1.0), |
| 254 |
}}, |
| 255 |
}, |
| 256 |
{ |
| 257 |
name: "global exported - mutable I32", |
| 258 |
module: &Module{ |
| 259 |
GlobalSection: []Global{ |
| 260 |
{ |
| 261 |
Type: GlobalType{ValType: ValueTypeI32, Mutable: true}, |
| 262 |
Init: NewConstantExpressionFromI32(1), |
| 263 |
}, |
| 264 |
}, |
| 265 |
Exports: map[string]*Export{"global": {Type: ExternTypeGlobal, Name: "global"}}, |
| 266 |
}, |
| 267 |
expected: mutableGlobal{ |
| 268 |
internalapi.WazeroOnlyType{}, &GlobalInstance{ |
| 269 |
Type: GlobalType{ValType: ValueTypeI32, Mutable: true}, Val: 1, |
| 270 |
}, |
| 271 |
}, |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: "global exported - mutable I64", |
| 275 |
module: &Module{ |
| 276 |
GlobalSection: []Global{ |
| 277 |
{ |
| 278 |
Type: GlobalType{ValType: ValueTypeI64, Mutable: true}, |
| 279 |
Init: NewConstantExpressionFromI64(1), |
| 280 |
}, |
| 281 |
}, |
| 282 |
Exports: map[string]*Export{"global": {Type: ExternTypeGlobal, Name: "global"}}, |
| 283 |
}, |
| 284 |
expected: mutableGlobal{ |
| 285 |
internalapi.WazeroOnlyType{}, &GlobalInstance{ |
| 286 |
Type: GlobalType{ValType: ValueTypeI64, Mutable: true}, Val: 1, |
| 287 |
}, |
| 288 |
}, |
| 289 |
}, |
| 290 |
{ |
| 291 |
name: "global exported - mutable F32", |
| 292 |
module: &Module{ |
| 293 |
GlobalSection: []Global{ |
| 294 |
{ |
| 295 |
Type: GlobalType{ValType: ValueTypeF32, Mutable: true}, |
| 296 |
Init: NewConstantExpressionFromOpcode(OpcodeF32Const, u32.LeBytes(uint32(api.EncodeF32(1.0)))), |
| 297 |
}, |
| 298 |
}, |
| 299 |
Exports: map[string]*Export{"global": {Type: ExternTypeGlobal, Name: "global"}}, |
| 300 |
}, |
| 301 |
expected: mutableGlobal{ |
| 302 |
internalapi.WazeroOnlyType{}, &GlobalInstance{ |
| 303 |
Type: GlobalType{ValType: ValueTypeF32, Mutable: true}, Val: api.EncodeF32(1.0), |
| 304 |
}, |
| 305 |
}, |
| 306 |
}, |
| 307 |
{ |
| 308 |
name: "global exported - mutable F64", |
| 309 |
module: &Module{ |
| 310 |
GlobalSection: []Global{ |
| 311 |
{ |
| 312 |
Type: GlobalType{ValType: ValueTypeF64, Mutable: true}, |
| 313 |
Init: NewConstantExpressionFromOpcode(OpcodeF64Const, u64.LeBytes(api.EncodeF64(1.0))), |
| 314 |
}, |
| 315 |
}, |
| 316 |
Exports: map[string]*Export{"global": {Type: ExternTypeGlobal, Name: "global"}}, |
| 317 |
}, |
| 318 |
expected: mutableGlobal{ |
| 319 |
internalapi.WazeroOnlyType{}, &GlobalInstance{ |
| 320 |
Type: GlobalType{ValType: ValueTypeF64, Mutable: true}, Val: api.EncodeF64(1.0), |
| 321 |
}, |
| 322 |
}, |
| 323 |
}, |
| 324 |
} |
| 325 |
|
| 326 |
for _, tt := range tests { |
| 327 |
tc := tt |
| 328 |
|
| 329 |
s := newStore() |
| 330 |
t.Run(tc.name, func(t *testing.T) { |
| 331 |
|
| 332 |
module, err := s.Instantiate(context.Background(), tc.module, t.Name(), nil, nil) |
| 333 |
require.NoError(t, err) |
| 334 |
|
| 335 |
if global := module.ExportedGlobal("global"); tc.expected != nil { |
| 336 |
require.Equal(t, tc.expected, global) |
| 337 |
} else { |
| 338 |
require.Nil(t, global) |
| 339 |
} |
| 340 |
}) |
| 341 |
} |
| 342 |
} |