File: wasmexport2.go

package info (click to toggle)
golang-1.24 1.24.4-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid
  • size: 167,820 kB
  • sloc: asm: 154,901; ansic: 7,009; sh: 2,267; javascript: 1,705; perl: 1,052; python: 421; makefile: 110; cpp: 39; f90: 8; awk: 7; objc: 4
file content (92 lines) | stat: -rw-r--r-- 2,581 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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