Files
wazero/config_test.go
Crypt Keeper cfb11f352a Adds ability to disable mutable globals and improves decode perf (#315)
This adds `RuntimeConfig.WithFeatureMutableGlobal(enabled bool)`, which
allows disabling of mutable globals. When disabled, any attempt to add a
mutable global, either explicitly or implicitly via decoding wasm will
fail.

To support this, there's a new `Features` bitflag that can allow up to
63 feature toggles without passing structs.

While here, I fixed a significant performance problem in decoding
binary:

Before
```
BenchmarkCodecExample/binary.DecodeModule-16         	  184243	      5623 ns/op	    3848 B/op	     184 allocs/op
```

Now
```
BenchmarkCodecExample/binary.DecodeModule-16         	  294084	      3520 ns/op	    2176 B/op	      91 allocs/op

```

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-03 10:31:10 +08:00

49 lines
1.1 KiB
Go

package wazero
import (
"testing"
"github.com/stretchr/testify/require"
internalwasm "github.com/tetratelabs/wazero/internal/wasm"
)
func TestRuntimeConfig_Features(t *testing.T) {
tests := []struct {
name string
feature internalwasm.Features
expectDefault bool
setFeature func(*RuntimeConfig, bool) *RuntimeConfig
}{
{
name: "mutable-global",
feature: internalwasm.FeatureMutableGlobal,
expectDefault: true,
setFeature: func(c *RuntimeConfig, v bool) *RuntimeConfig {
return c.WithFeatureMutableGlobal(v)
},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
c := NewRuntimeConfig()
require.Equal(t, tc.expectDefault, c.enabledFeatures.Get(tc.feature))
// Set to false even if it was initially false.
c = tc.setFeature(c, false)
require.False(t, c.enabledFeatures.Get(tc.feature))
// Set true makes it true
c = tc.setFeature(c, true)
require.True(t, c.enabledFeatures.Get(tc.feature))
// Set false makes it false again
c = tc.setFeature(c, false)
require.False(t, c.enabledFeatures.Get(tc.feature))
})
}
}