Skip to content

Commit

Permalink
cm: extract into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Dec 8, 2024
1 parent f4ad832 commit e5290ff
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 29 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ updates:
interval: weekly
open-pull-requests-limit: 10

- package-ecosystem: gomod
directory: "/cm"
schedule:
interval: weekly
open-pull-requests-limit: 10

- package-ecosystem: gomod
directory: "/tests"
schedule:
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
go-version-file: go.mod

- name: Vet Go code
run: go vet ./...
run: go vet ./... ./cm/... ./tests/...

# Test with Go
test-go:
Expand All @@ -61,15 +61,15 @@ jobs:
version: ${{ env.wasm-tools-version }}

- name: Run Go tests
run: go test -v ./...
run: go test -v ./... ./cm/...

- name: Run Go tests with race detector
run: go test -v -race ./...
run: go test -v -race ./... ./cm/...

- name: Test Go without cgo
env:
CGO_ENABLED: 0
run: go test -v ./...
run: go test -v ./... ./cm/...

- name: Verify repo is unchanged
run: git diff --exit-code HEAD
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
version: ${{ env.wasm-tools-version }}

- name: Test with TinyGo
run: tinygo test -v ./...
run: tinygo test -v ./... ./cm/...

- name: Verify repo is unchanged
run: git diff --exit-code HEAD
Expand Down Expand Up @@ -164,19 +164,19 @@ jobs:
env:
GOARCH: wasm
GOOS: wasip1
run: go test -v ./...
run: go test -v ./... ./cm/...

- name: Test wasm/wasip1 with TinyGo 0.32.0
if: ${{ matrix.tinygo-version == '0.32.0' }}
run: tinygo test -v -target=wasi ./...
run: tinygo test -v -target=wasi ./... ./cm/...

- name: Test wasm/wasip1 with TinyGo >= 0.33.0
if: ${{ matrix.tinygo-version != '0.32.0' }}
run: tinygo test -v -target=wasip1 ./...
run: tinygo test -v -target=wasip1 ./... ./cm/...

- name: Test wasm/wasip2 with TinyGo >= 0.33.0
if: ${{ matrix.tinygo-version != '0.32.0' }}
run: tinygo test -v -target=wasip2 ./...
run: tinygo test -v -target=wasip2 ./... ./cm/...

- name: Test generated Go with TinyGo >= 0.34.0
if: ${{ matrix.tinygo-version != '0.32.0' && matrix.tinygo-version != '0.33.0' }}
Expand Down
11 changes: 7 additions & 4 deletions cm/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"reflect"
"strings"
"unsafe"

"go.bytecodealliance.org/internal/tinyunsafe"
)

func typeName(v any) string {
Expand Down Expand Up @@ -33,6 +31,11 @@ func zeroPtr[T any]() *T {
return &zero
}

// TODO: remove this when TinyGo supports unsafe.Offsetof
func offsetOf[Struct any, Field any](s *Struct, f *Field) uintptr {
return uintptr(unsafe.Pointer(f)) - uintptr(unsafe.Pointer(s))
}

// VariantDebug is an interface used in tests to validate layout of variant types.
type VariantDebug interface {
Size() uintptr
Expand All @@ -42,7 +45,7 @@ type VariantDebug interface {

func (v variant[Disc, Shape, Align]) Size() uintptr { return unsafe.Sizeof(v) }
func (v variant[Disc, Shape, Align]) DataAlign() uintptr { return unsafe.Alignof(v.data) }
func (v variant[Disc, Shape, Align]) DataOffset() uintptr { return tinyunsafe.OffsetOf(&v, &v.data) }
func (v variant[Disc, Shape, Align]) DataOffset() uintptr { return offsetOf(&v, &v.data) }

// ResultDebug is an interface used in tests to validate layout of result types.
type ResultDebug interface {
Expand All @@ -55,4 +58,4 @@ func (r BoolResult) DataOffset() uintptr { return 0 }

func (r result[Shape, OK, Err]) Size() uintptr { return unsafe.Sizeof(r) }
func (r result[Shape, OK, Err]) DataAlign() uintptr { return unsafe.Alignof(r.data) }
func (r result[Shape, OK, Err]) DataOffset() uintptr { return tinyunsafe.OffsetOf(&r, &r.data) }
func (r result[Shape, OK, Err]) DataOffset() uintptr { return offsetOf(&r, &r.data) }
3 changes: 3 additions & 0 deletions cm/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go.bytecodealliance.org/cm

go 1.22.0
10 changes: 4 additions & 6 deletions cm/hostlayout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"runtime"
"testing"
"unsafe"

"go.bytecodealliance.org/internal/tinyunsafe"
)

func TestFieldAlignment(t *testing.T) {
Expand All @@ -18,7 +16,7 @@ func TestFieldAlignment(t *testing.T) {
if got, want := unsafe.Sizeof(v1), uintptr(16); got != want {
t.Errorf("unsafe.Sizeof(v1): %d, expected %d", got, want)
}
if got, want := tinyunsafe.OffsetOf(&v1, &v1.u64), uintptr(8); got != want {
if got, want := offsetOf(&v1, &v1.u64), uintptr(8); got != want {
t.Errorf("unsafe.Offsetof(v1.u64): %d, expected %d", got, want)
}

Expand All @@ -36,7 +34,7 @@ func TestFieldAlignment(t *testing.T) {
if got, want := unsafe.Sizeof(v2), uintptr(16); got != want {
t.Errorf("unsafe.Sizeof(v2): %d, expected %d", got, want)
}
if got, want := tinyunsafe.OffsetOf(&v2, &v2.u64), uintptr(8); got != want {
if got, want := offsetOf(&v2, &v2.u64), uintptr(8); got != want {
t.Errorf("unsafe.Offsetof(v2.u64): %d, expected %d", got, want)
}

Expand All @@ -49,7 +47,7 @@ func TestFieldAlignment(t *testing.T) {
if got, want := unsafe.Sizeof(v3), uintptr(1); got != want {
t.Errorf("unsafe.Sizeof(v3): %d, expected %d", got, want)
}
if got, want := tinyunsafe.OffsetOf(&v3, &v3.b), uintptr(0); got != want {
if got, want := offsetOf(&v3, &v3.b), uintptr(0); got != want {
t.Errorf("unsafe.Offsetof(v3.b): %d, expected %d", got, want)
}

Expand All @@ -62,7 +60,7 @@ func TestFieldAlignment(t *testing.T) {
if got, want := unsafe.Sizeof(v4), uintptr(4); got != want {
t.Errorf("unsafe.Sizeof(v4): %d, expected %d", got, want)
}
if got, want := tinyunsafe.OffsetOf(&v4, &v4.b), uintptr(0); got != want {
if got, want := offsetOf(&v4, &v4.b), uintptr(0); got != want {
t.Errorf("unsafe.Offsetof(v4.b): %d, expected %d", got, want)
}
}
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ go 1.22.0

use (
.
./cm
./tests
)
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240521205824-bda55230c457/go.mod h1:pRgIJT+bRLFKnoM1ldnzKoxTIn14Yxz928LQRYYgIN0=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
Expand Down
10 changes: 0 additions & 10 deletions internal/tinyunsafe/tinyunsafe.go

This file was deleted.

0 comments on commit e5290ff

Please sign in to comment.