Source file test/wasmexport2.go

     1  // errorcheck
     2  
     3  // Copyright 2024 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Verify that wasmexport supports allowed types and rejects
     8  // unallowed types.
     9  
    10  //go:build wasm
    11  
    12  package p
    13  
    14  import (
    15  	"structs"
    16  	"unsafe"
    17  )
    18  
    19  //go:wasmexport good1
    20  func good1(int32, uint32, int64, uint64, float32, float64, unsafe.Pointer) {} // allowed types
    21  
    22  type MyInt32 int32
    23  
    24  //go:wasmexport good2
    25  func good2(MyInt32) {} // named type is ok
    26  
    27  //go:wasmexport good3
    28  func good3() int32 { return 0 } // one result is ok
    29  
    30  //go:wasmexport good4
    31  func good4() unsafe.Pointer { return nil } // one result is ok
    32  
    33  //go:wasmexport good5
    34  func good5(string, uintptr) bool { return false } // bool, string, and uintptr are allowed
    35  
    36  //go:wasmexport bad1
    37  func bad1(any) {} // ERROR "go:wasmexport: unsupported parameter type"
    38  
    39  //go:wasmexport bad2
    40  func bad2(func()) {} // ERROR "go:wasmexport: unsupported parameter type"
    41  
    42  //go:wasmexport bad3
    43  func bad3(uint8) {} // ERROR "go:wasmexport: unsupported parameter type"
    44  
    45  //go:wasmexport bad4
    46  func bad4(int) {} // ERROR "go:wasmexport: unsupported parameter type"
    47  
    48  // Struct and array types are also not allowed.
    49  
    50  type S struct { x, y int32 }
    51  
    52  type H struct { _ structs.HostLayout; x, y int32 }
    53  
    54  type A = structs.HostLayout
    55  
    56  type AH struct { _ A; x, y int32 }
    57  
    58  //go:wasmexport bad5
    59  func bad5(S) {} // ERROR "go:wasmexport: unsupported parameter type"
    60  
    61  //go:wasmexport bad6
    62  func bad6(H) {} // ERROR "go:wasmexport: unsupported parameter type"
    63  
    64  //go:wasmexport bad7
    65  func bad7([4]int32) {} // ERROR "go:wasmexport: unsupported parameter type"
    66  
    67  // Pointer types are not allowed, with resitrictions on
    68  // the element type.
    69  
    70  //go:wasmexport good6
    71  func good6(*int32, *uint8, *bool) {}
    72  
    73  //go:wasmexport bad8
    74  func bad8(*S) {} // ERROR "go:wasmexport: unsupported parameter type" // without HostLayout, not allowed
    75  
    76  //go:wasmexport bad9
    77  func bad9() *S { return nil } // ERROR "go:wasmexport: unsupported result type"
    78  
    79  //go:wasmexport good7
    80  func good7(*H, *AH) {} // pointer to struct with HostLayout is allowed
    81  
    82  //go:wasmexport good8
    83  func good8(*struct{}) {} // pointer to empty struct is allowed
    84  
    85  //go:wasmexport good9
    86  func good9(*[4]int32, *[2]H) {} // pointer to array is allowed, if the element type is okay
    87  
    88  //go:wasmexport toomanyresults
    89  func toomanyresults() (int32, int32) { return 0, 0 } // ERROR "go:wasmexport: too many return values"
    90  
    91  //go:wasmexport bad10
    92  func bad10() string { return "" } // ERROR "go:wasmexport: unsupported result type" // string cannot be a result
    93  

View as plain text