Skip to content

Commit 87a8aaf

Browse files
ydnardeadprogram
authored andcommitted
all: simplify wasm-tools-go dependency
- add internal/wasm-tools/go.mod file to depend on wasm-tools-go - copy package cm into src/internal/cm - remove wasm-tools-go "vendor" submodule internal/tools: fix typo go.{mod,sum}, internal/tools: add wit-bindgen-go to tools GNUmakefile: use go run for wit-bindgen-go GNUmakefile: add tools target to go:generate tools binaries in internal/tools GNUmakefile: add .PHONY for lint and spell GNUmakefile, internal/cm: vendor package cm into internal/cm go.{mod,sum}: update wasm-tools-go to v0.1.4 internal/wasi: use internal/cm package remove submodule src/vendor/github.com/ydnar/wasm-tools-go GNUmakefile: add comment documenting what wasi-cm target does go.{mod,sum}: remove toolchain; go mod tidy go.mod: revert to Go 1.19 go.mod: go 1.19 go.{mod,sum}, internal/{tools,wasm-tools}: revert root go.mod file to go1.19 Create a wasm-tools specific module that can require go1.22 for wasm-tools-go.
1 parent f026422 commit 87a8aaf

File tree

50 files changed

+760
-46
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+760
-46
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,3 @@
4242
[submodule "lib/wasi-cli"]
4343
path = lib/wasi-cli
4444
url = https://github.com/WebAssembly/wasi-cli
45-
[submodule "src/vendor/github.com/ydnar/wasm-tools-go"]
46-
path = src/vendor/github.com/ydnar/wasm-tools-go
47-
url = https://github.com/ydnar/wasm-tools-go.git

GNUmakefile

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,16 @@ lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a:
269269
cd lib/wasi-libc && $(MAKE) -j4 EXTRA_CFLAGS="-O2 -g -DNDEBUG -mnontrapping-fptoint -msign-ext" MALLOC_IMPL=none CC="$(CLANG)" AR=$(LLVM_AR) NM=$(LLVM_NM)
270270

271271
# Generate WASI syscall bindings
272+
WASM_TOOLS_MODULE=github.com/ydnar/wasm-tools-go
272273
.PHONY: wasi-syscall
273-
wasi-syscall:
274-
wit-bindgen-go generate -o ./src/internal -p internal --versioned ./lib/wasi-cli/wit
274+
wasi-syscall: wasi-cm
275+
go run -modfile ./internal/wasm-tools/go.mod $(WASM_TOOLS_MODULE)/cmd/wit-bindgen-go generate --versioned -o ./src/internal -p internal --cm internal/cm ./lib/wasi-cli/wit
276+
277+
# Copy package cm into src/internal/cm
278+
.PHONY: wasi-cm
279+
wasi-cm:
280+
# rm -rf ./src/internal/cm
281+
rsync -rv --delete --exclude '*_test.go' $(shell go list -modfile ./internal/wasm-tools/go.mod -m -f {{.Dir}} $(WASM_TOOLS_MODULE))/cm ./src/internal/
275282

276283
# Check for Node.js used during WASM tests.
277284
NODEJS_VERSION := $(word 1,$(subst ., ,$(shell node -v | cut -c 2-)))
@@ -946,6 +953,11 @@ release: build/release
946953
deb: build/release
947954
endif
948955

956+
.PHONY: tools
957+
tools:
958+
go generate -C ./internal/tools -tags tools ./
959+
960+
.PHONY: lint
949961
lint:
950962
go run github.com/mgechev/revive -version
951963
# TODO: lint more directories!
@@ -954,6 +966,7 @@ lint:
954966
# Use 'grep .' to get rid of stray blank line
955967
go run github.com/mgechev/revive -config revive.toml compiler/... src/{os,reflect}/*.go | grep -v "should have comment or be unexported" | grep '.' | awk '{print}; END {exit NR>0}'
956968

969+
.PHONY: spell
957970
spell:
958-
# Check for typos in comments. Skip git submodules etc.
971+
# Check for typos in comments. Skip git submodules etc.
959972
go run github.com/client9/misspell/cmd/misspell -i 'ackward,devided,extint,inbetween,programmmer,rela' $$( find . -depth 1 -type d | egrep -w -v 'lib|llvm|src/net' )

internal/tools/tools.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//go:build tools
22

3-
// Install linter versions specified in go.mod
4-
// See https://marcofranssen.nl/manage-go-tools-via-go-modules for idom
3+
// Install tools specified in go.mod.
4+
// See https://marcofranssen.nl/manage-go-tools-via-go-modules for idiom.
55
package tools
66

77
import (
88
_ "github.com/client9/misspell"
99
_ "github.com/mgechev/revive"
1010
)
11+
12+
//go:generate go install github.com/client9/misspell/cmd/misspell
13+
//go:generate go install github.com/mgechev/revive

internal/wasm-tools/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# wasm-tools directory
2+
3+
This directory has a separate `go.mod` file because the `wasm-tools-go` module requires Go 1.22, while TinyGo itself supports Go 1.19.
4+
5+
When the minimum Go version for TinyGo is 1.22, this directory can be folded into `internal/tools` and the `go.mod` and `go.sum` files deleted.

internal/wasm-tools/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/tinygo-org/tinygo/internal/tools
2+
3+
go 1.22.4
4+
5+
require github.com/ydnar/wasm-tools-go v0.1.4
6+
7+
require (
8+
github.com/coreos/go-semver v0.3.1 // indirect
9+
github.com/urfave/cli/v3 v3.0.0-alpha9 // indirect
10+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
11+
golang.org/x/mod v0.19.0 // indirect
12+
)

internal/wasm-tools/go.sum

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
2+
github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
8+
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
9+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
10+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
11+
github.com/urfave/cli/v3 v3.0.0-alpha9 h1:P0RMy5fQm1AslQS+XCmy9UknDXctOmG/q/FZkUFnJSo=
12+
github.com/urfave/cli/v3 v3.0.0-alpha9/go.mod h1:0kK/RUFHyh+yIKSfWxwheGndfnrvYSmYFVeKCh03ZUc=
13+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
14+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
15+
github.com/ydnar/wasm-tools-go v0.1.4 h1:+25WqBj0AhLx8OFvZvrs7bQO6L3WtQ7t6JzQEYsXQb8=
16+
github.com/ydnar/wasm-tools-go v0.1.4/go.mod h1:lQfv2Tde3tRgZDSYriro0EmdSHzP1mrHPMmYNahSS/g=
17+
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
18+
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
19+
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
20+
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
21+
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
22+
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
23+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
24+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
25+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/wasm-tools/tools.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build tools
2+
3+
// Install tools specified in go.mod.
4+
// See https://marcofranssen.nl/manage-go-tools-via-go-modules for idiom.
5+
package tools
6+
7+
import (
8+
_ "github.com/ydnar/wasm-tools-go/cmd/wit-bindgen-go"
9+
)
10+
11+
//go:generate go install github.com/ydnar/wasm-tools-go/cmd/wit-bindgen-go

loader/goroot.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func pathsToOverride(goMinor int, needsSyscallPackage bool) map[string]bool {
238238
"internal/": true,
239239
"internal/binary/": false,
240240
"internal/bytealg/": false,
241+
"internal/cm/": false,
241242
"internal/fuzz/": false,
242243
"internal/reflectlite/": false,
243244
"internal/task/": false,
@@ -251,8 +252,6 @@ func pathsToOverride(goMinor int, needsSyscallPackage bool) map[string]bool {
251252
"runtime/": false,
252253
"sync/": true,
253254
"testing/": true,
254-
"vendor/": true,
255-
"vendor/github.com/": false,
256255
}
257256

258257
if goMinor >= 19 {

src/internal/cm/abi.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package cm
2+
3+
import "unsafe"
4+
5+
// Reinterpret reinterprets the bits of type From into type T.
6+
// Will panic if the size of From is smaller than the size of To.
7+
func Reinterpret[T, From any](from From) (to T) {
8+
if unsafe.Sizeof(to) > unsafe.Sizeof(from) {
9+
panic("reinterpret: size of to > from")
10+
}
11+
return *(*T)(unsafe.Pointer(&from))
12+
}
13+
14+
// LowerString lowers a [string] into a pair of Core WebAssembly types.
15+
//
16+
// [string]: https://pkg.go.dev/builtin#string
17+
func LowerString[S ~string](s S) (*byte, uint32) {
18+
return unsafe.StringData(string(s)), uint32(len(s))
19+
}
20+
21+
// LiftString lifts Core WebAssembly types into a [string].
22+
func LiftString[T ~string, Data unsafe.Pointer | uintptr | *uint8, Len uint | uintptr | uint32 | uint64](data Data, len Len) T {
23+
return T(unsafe.String((*uint8)(unsafe.Pointer(data)), int(len)))
24+
}
25+
26+
// LowerList lowers a [List] into a pair of Core WebAssembly types.
27+
func LowerList[L ~struct{ list[T] }, T any](list L) (*T, uint32) {
28+
l := (*List[T])(unsafe.Pointer(&list))
29+
return l.data, uint32(l.len)
30+
}
31+
32+
// LiftList lifts Core WebAssembly types into a [List].
33+
func LiftList[L List[T], T any, Data unsafe.Pointer | uintptr | *T, Len uint | uintptr | uint32 | uint64](data Data, len Len) L {
34+
return L(NewList((*T)(unsafe.Pointer(data)), uint(len)))
35+
}
36+
37+
// BoolToU32 converts a value whose underlying type is [bool] into a [uint32].
38+
// Used to lower a [bool] into a Core WebAssembly i32 as specified in the [Canonical ABI].
39+
//
40+
// [bool]: https://pkg.go.dev/builtin#bool
41+
// [uint32]: https://pkg.go.dev/builtin#uint32
42+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
43+
func BoolToU32[B ~bool](v B) uint32 { return uint32(*(*uint8)(unsafe.Pointer(&v))) }
44+
45+
// U32ToBool converts a [uint32] into a [bool].
46+
// Used to lift a Core WebAssembly i32 into a [bool] as specified in the [Canonical ABI].
47+
//
48+
// [uint32]: https://pkg.go.dev/builtin#uint32
49+
// [bool]: https://pkg.go.dev/builtin#bool
50+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
51+
func U32ToBool(v uint32) bool { tmp := uint8(v); return *(*bool)(unsafe.Pointer(&tmp)) }
52+
53+
// F32ToU32 maps the bits of a [float32] into a [uint32].
54+
// Used to lower a [float32] into a Core WebAssembly i32 as specified in the [Canonical ABI].
55+
//
56+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
57+
// [float32]: https://pkg.go.dev/builtin#float32
58+
// [uint32]: https://pkg.go.dev/builtin#uint32
59+
func F32ToU32(v float32) uint32 { return *(*uint32)(unsafe.Pointer(&v)) }
60+
61+
// U32ToF32 maps the bits of a [uint32] into a [float32].
62+
// Used to lift a Core WebAssembly i32 into a [float32] as specified in the [Canonical ABI].
63+
//
64+
// [uint32]: https://pkg.go.dev/builtin#uint32
65+
// [float32]: https://pkg.go.dev/builtin#float32
66+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
67+
func U32ToF32(v uint32) float32 { return *(*float32)(unsafe.Pointer(&v)) }
68+
69+
// F64ToU64 maps the bits of a [float64] into a [uint64].
70+
// Used to lower a [float64] into a Core WebAssembly i64 as specified in the [Canonical ABI].
71+
//
72+
// [float64]: https://pkg.go.dev/builtin#float64
73+
// [uint64]: https://pkg.go.dev/builtin#uint64
74+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
75+
//
76+
// [uint32]: https://pkg.go.dev/builtin#uint32
77+
func F64ToU64(v float64) uint64 { return *(*uint64)(unsafe.Pointer(&v)) }
78+
79+
// U64ToF64 maps the bits of a [uint64] into a [float64].
80+
// Used to lift a Core WebAssembly i64 into a [float64] as specified in the [Canonical ABI].
81+
//
82+
// [uint64]: https://pkg.go.dev/builtin#uint64
83+
// [float64]: https://pkg.go.dev/builtin#float64
84+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
85+
func U64ToF64(v uint64) float64 { return *(*float64)(unsafe.Pointer(&v)) }
86+
87+
// PointerToU32 converts a pointer of type *T into a [uint32].
88+
// Used to lower a pointer into a Core WebAssembly i32 as specified in the [Canonical ABI].
89+
//
90+
// [uint32]: https://pkg.go.dev/builtin#uint32
91+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
92+
func PointerToU32[T any](v *T) uint32 { return uint32(uintptr(unsafe.Pointer(v))) }
93+
94+
// U32ToPointer converts a [uint32] into a pointer of type *T.
95+
// Used to lift a Core WebAssembly i32 into a pointer as specified in the [Canonical ABI].
96+
//
97+
// [uint32]: https://pkg.go.dev/builtin#uint32
98+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
99+
func U32ToPointer[T any](v uint32) *T { return (*T)(unsafePointer(uintptr(v))) }
100+
101+
// PointerToU64 converts a pointer of type *T into a [uint64].
102+
// Used to lower a pointer into a Core WebAssembly i64 as specified in the [Canonical ABI].
103+
//
104+
// [uint64]: https://pkg.go.dev/builtin#uint64
105+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
106+
func PointerToU64[T any](v *T) uint64 { return uint64(uintptr(unsafe.Pointer(v))) }
107+
108+
// U64ToPointer converts a [uint64] into a pointer of type *T.
109+
// Used to lift a Core WebAssembly i64 into a pointer as specified in the [Canonical ABI].
110+
//
111+
// [uint64]: https://pkg.go.dev/builtin#uint64
112+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
113+
func U64ToPointer[T any](v uint64) *T { return (*T)(unsafePointer(uintptr(v))) }
114+
115+
// Appease vet, see https://github.com/golang/go/issues/58625
116+
func unsafePointer(p uintptr) unsafe.Pointer {
117+
return *(*unsafe.Pointer)(unsafe.Pointer(&p))
118+
}

src/internal/cm/docs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Package cm contains types and functions for interfacing with the WebAssembly Component Model.
2+
//
3+
// The types in this package (such as [List], [Option], [Result], and [Variant]) are designed to match the memory layout
4+
// of [Component Model] types as specified in the [Canonical ABI].
5+
//
6+
// [Component Model]: https://component-model.bytecodealliance.org/introduction.html
7+
// [Canonical ABI]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#alignment
8+
package cm

0 commit comments

Comments
 (0)