1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:14:40 -07:00
go/test/wasmexport2.go
Cherry Mui 5a9aeef9d5 cmd/compile: allow more types for wasmimport/wasmexport parameters and results
As proposed on #66984, this CL allows more types to be used as
wasmimport/wasmexport function parameters and results.
Specifically, bool, string, and uintptr are now allowed, and also
pointer types that point to allowed element types. Allowed element
types includes sized integer and floating point types (including
small integer types like uint8 which are not directly allowed as
a parameter type), bool, array whose element type is allowed, and
struct whose fields are allowed element type and also include a
struct.HostLayout field.

For #66984.

Change-Id: Ie5452a1eda21c089780dfb4d4246de6008655c84
Reviewed-on: https://go-review.googlesource.com/c/go/+/626615
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-11-11 17:11:50 +00:00

93 lines
2.5 KiB
Go

// errorcheck
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Verify that wasmexport supports allowed types and rejects
// unallowed types.
//go:build wasm
package p
import (
"structs"
"unsafe"
)
//go:wasmexport good1
func good1(int32, uint32, int64, uint64, float32, float64, unsafe.Pointer) {} // allowed types
type MyInt32 int32
//go:wasmexport good2
func good2(MyInt32) {} // named type is ok
//go:wasmexport good3
func good3() int32 { return 0 } // one result is ok
//go:wasmexport good4
func good4() unsafe.Pointer { return nil } // one result is ok
//go:wasmexport good5
func good5(string, uintptr) bool { return false } // bool, string, and uintptr are allowed
//go:wasmexport bad1
func bad1(any) {} // ERROR "go:wasmexport: unsupported parameter type"
//go:wasmexport bad2
func bad2(func()) {} // ERROR "go:wasmexport: unsupported parameter type"
//go:wasmexport bad3
func bad3(uint8) {} // ERROR "go:wasmexport: unsupported parameter type"
//go:wasmexport bad4
func bad4(int) {} // ERROR "go:wasmexport: unsupported parameter type"
// Struct and array types are also not allowed.
type S struct { x, y int32 }
type H struct { _ structs.HostLayout; x, y int32 }
type A = structs.HostLayout
type AH struct { _ A; x, y int32 }
//go:wasmexport bad5
func bad5(S) {} // ERROR "go:wasmexport: unsupported parameter type"
//go:wasmexport bad6
func bad6(H) {} // ERROR "go:wasmexport: unsupported parameter type"
//go:wasmexport bad7
func bad7([4]int32) {} // ERROR "go:wasmexport: unsupported parameter type"
// Pointer types are not allowed, with resitrictions on
// the element type.
//go:wasmexport good6
func good6(*int32, *uint8, *bool) {}
//go:wasmexport bad8
func bad8(*S) {} // ERROR "go:wasmexport: unsupported parameter type" // without HostLayout, not allowed
//go:wasmexport bad9
func bad9() *S { return nil } // ERROR "go:wasmexport: unsupported result type"
//go:wasmexport good7
func good7(*H, *AH) {} // pointer to struct with HostLayout is allowed
//go:wasmexport good8
func good8(*struct{}) {} // pointer to empty struct is allowed
//go:wasmexport good9
func good9(*[4]int32, *[2]H) {} // pointer to array is allowed, if the element type is okay
//go:wasmexport toomanyresults
func toomanyresults() (int32, int32) { return 0, 0 } // ERROR "go:wasmexport: too many return values"
//go:wasmexport bad10
func bad10() string { return "" } // ERROR "go:wasmexport: unsupported result type" // string cannot be a result