diff --git a/ast/capabilities.go b/ast/capabilities.go index ff25c427b3..161fdac7f5 100644 --- a/ast/capabilities.go +++ b/ast/capabilities.go @@ -15,16 +15,23 @@ import ( // Capabilities defines a structure containing data that describes the capablilities // or features supported by a particular version of OPA. type Capabilities struct { - Builtins []*Builtin `json:"builtins"` // builtins is a set of built-in functions that are supported. - WasmABIVersions []int `json:"wasm_abi_versions"` + Builtins []*Builtin `json:"builtins"` // builtins is a set of built-in functions that are supported. + WasmABIVersions []WasmABIVersion `json:"wasm_abi_versions"` +} + +// WasmABIVersion captures the Wasm ABI version. Its `Minor` version is indicating +// backwards-compatible changes. +type WasmABIVersion struct { + Version int `json:"version"` + Minor int `json:"minor_version"` } // CapabilitiesForThisVersion returns the capabilities of this version of OPA. func CapabilitiesForThisVersion() *Capabilities { + f := &Capabilities{} - f := &Capabilities{ - Builtins: []*Builtin{}, - WasmABIVersions: capabilities.ABIVersions(), + for _, vers := range capabilities.ABIVersions() { + f.WasmABIVersions = append(f.WasmABIVersions, WasmABIVersion{Version: vers[0], Minor: vers[1]}) } for _, bi := range Builtins { diff --git a/capabilities.json b/capabilities.json index e3b3afca09..78e9abc869 100644 --- a/capabilities.json +++ b/capabilities.json @@ -3381,6 +3381,9 @@ } ], "wasm_abi_versions": [ - 1 + { + "version": 1, + "minor_version": 0 + } ] } diff --git a/compile/compile.go b/compile/compile.go index de61ba1e1b..e8e18a6b09 100644 --- a/compile/compile.go +++ b/compile/compile.go @@ -432,8 +432,9 @@ func (c *Compiler) compileWasm(ctx context.Context) error { compiler := wasm.New() found := false + have := compiler.ABIVersion() for _, v := range c.capabilities.WasmABIVersions { - if v == compiler.ABIVersion() { + if v.Version == have.Version && v.Minor <= have.Minor { found = true break } diff --git a/compile/compile_test.go b/compile/compile_test.go index ac2d7a7061..fb06ce1515 100644 --- a/compile/compile_test.go +++ b/compile/compile_test.go @@ -428,7 +428,7 @@ func TestCompilerOptimizationL2(t *testing.T) { } // NOTE(sr): we override this to not depend on build tags in tests -func wasmABIVersions(vs ...int) *ast.Capabilities { +func wasmABIVersions(vs ...ast.WasmABIVersion) *ast.Capabilities { caps := ast.CapabilitiesForThisVersion() caps.WasmABIVersions = vs return caps @@ -445,7 +445,7 @@ func TestCompilerWasmTarget(t *testing.T) { test.WithTempFS(files, func(root string) { compiler := New().WithPaths(root).WithTarget("wasm").WithEntrypoints("test/p", "test/q"). - WithCapabilities(wasmABIVersions(1)) + WithCapabilities(wasmABIVersions(ast.WasmABIVersion{Version: 1})) err := compiler.Build(context.Background()) if err != nil { t.Fatal(err) @@ -474,7 +474,7 @@ func TestCompilerWasmTargetWithCapabilitiesMismatch(t *testing.T) { test.WithTempFS(files, func(root string) { compiler := New().WithPaths(root).WithTarget("wasm").WithEntrypoints("test/p", "test/q"). - WithCapabilities(wasmABIVersions(0, 2)) + WithCapabilities(wasmABIVersions(ast.WasmABIVersion{Version: 0}, ast.WasmABIVersion{Version: 1, Minor: 2}, ast.WasmABIVersion{Version: 2})) err := compiler.Build(context.Background()) if err == nil { t.Fatal("expected err, got nil") @@ -498,7 +498,7 @@ func TestCompilerWasmTargetMultipleEntrypoints(t *testing.T) { test.WithTempFS(files, func(root string) { compiler := New().WithPaths(root).WithTarget("wasm").WithEntrypoints("test/p", "policy/authz"). - WithCapabilities(wasmABIVersions(1)) + WithCapabilities(wasmABIVersions(ast.WasmABIVersion{Version: 1})) err := compiler.Build(context.Background()) if err != nil { t.Fatal(err) @@ -543,7 +543,7 @@ func TestCompilerWasmTargetEntrypointDependents(t *testing.T) { test.WithTempFS(files, func(root string) { compiler := New().WithPaths(root).WithTarget("wasm").WithEntrypoints("test/r", "test/z"). - WithCapabilities(wasmABIVersions(1)) + WithCapabilities(wasmABIVersions(ast.WasmABIVersion{Version: 1})) err := compiler.Build(context.Background()) if err != nil { t.Fatal(err) @@ -595,7 +595,7 @@ func TestCompilerWasmTargetLazyCompile(t *testing.T) { test.WithTempFS(files, func(root string) { compiler := New().WithPaths(root).WithTarget("wasm").WithEntrypoints("test/p").WithOptimizationLevel(1). - WithCapabilities(wasmABIVersions(1)) + WithCapabilities(wasmABIVersions(ast.WasmABIVersion{Version: 1})) err := compiler.Build(context.Background()) if err != nil { t.Fatal(err) diff --git a/internal/compiler/wasm/wasm.go b/internal/compiler/wasm/wasm.go index 251b767b5d..238d74e2c7 100644 --- a/internal/compiler/wasm/wasm.go +++ b/internal/compiler/wasm/wasm.go @@ -225,14 +225,11 @@ func New() *Compiler { // ABIVersion returns the Wasm ABI version this compiler // emits. -func (*Compiler) ABIVersion() int { - return opaWasmABIVersionVal -} - -// ABIMinorVersion returns the Wasm ABI mior version this -// compiler emits. -func (*Compiler) ABIMinorVersion() int { - return opaWasmABIMinorVersionVal +func (*Compiler) ABIVersion() ast.WasmABIVersion { + return ast.WasmABIVersion{ + Version: opaWasmABIVersionVal, + Minor: opaWasmABIMinorVersionVal, + } } // WithPolicy sets the policy to compile. diff --git a/internal/wasm/sdk/opa/capabilities/capabilities.go b/internal/wasm/sdk/opa/capabilities/capabilities.go index 6d4adf843b..11e9016d40 100644 --- a/internal/wasm/sdk/opa/capabilities/capabilities.go +++ b/internal/wasm/sdk/opa/capabilities/capabilities.go @@ -6,9 +6,10 @@ package capabilities -var wasmABIVersions = [...]int{1} +const abiVersion = 1 +const abiMinorVersion = 0 // ABIVersions returns the ABI versions that this SDK supports -func ABIVersions() []int { - return wasmABIVersions[:] +func ABIVersions() [][2]int { + return [][2]int{{abiVersion, abiMinorVersion}} } diff --git a/internal/wasm/sdk/opa/capabilities/capabilities_nowasm.go b/internal/wasm/sdk/opa/capabilities/capabilities_nowasm.go index 94ecefb099..cb3ed90126 100644 --- a/internal/wasm/sdk/opa/capabilities/capabilities_nowasm.go +++ b/internal/wasm/sdk/opa/capabilities/capabilities_nowasm.go @@ -8,6 +8,6 @@ package capabilities // ABIVersions returns the supported Wasm ABI versions for this // build: none -func ABIVersions() []int { +func ABIVersions() [][2]int { return nil }