Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reflect: use int in StringHeader and SliceHeader on non-AVR platforms #4156

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/reflect/intw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !avr

package reflect

// intw is an integer type, used in places where an int is typically required,
// except architectures where the size of an int != word size.
// See https://github.com/tinygo-org/tinygo/issues/1284.
type intw = int
8 changes: 8 additions & 0 deletions src/reflect/intw_avr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build avr

package reflect

// intw is an integer type, used in places where an int is typically required,
// except architectures where the size of an int != word size.
// See https://github.com/tinygo-org/tinygo/issues/1284.
type intw = uintptr
30 changes: 30 additions & 0 deletions src/reflect/intw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build !avr

package reflect_test

import (
"reflect"
"testing"
"unsafe"
)

// Verify that SliceHeader is the same size as a slice.
var _ [unsafe.Sizeof([]byte{})]byte = [unsafe.Sizeof(reflect.SliceHeader{})]byte{}

// TestSliceHeaderIntegerSize verifies that SliceHeader.Len and Cap are type int on non-AVR platforms.
// See https://github.com/tinygo-org/tinygo/issues/1284.
func TestSliceHeaderIntegerSize(t *testing.T) {
var h reflect.SliceHeader
h.Len = int(0)
h.Cap = int(0)
}

// Verify that StringHeader is the same size as a string.
var _ [unsafe.Sizeof("hello")]byte = [unsafe.Sizeof(reflect.StringHeader{})]byte{}

// TestStringHeaderIntegerSize verifies that StringHeader.Len and Cap are type int on non-AVR platforms.
// See https://github.com/tinygo-org/tinygo/issues/1284.
func TestStringHeaderIntegerSize(t *testing.T) {
var h reflect.StringHeader
h.Len = int(0)
}
16 changes: 13 additions & 3 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1578,8 +1578,8 @@ type funcHeader struct {

type SliceHeader struct {
Data uintptr
Len uintptr
Cap uintptr
Len intw
Cap intw
}

// Slice header that matches the underlying structure. Used for when we switch
Expand All @@ -1592,7 +1592,7 @@ type sliceHeader struct {

type StringHeader struct {
Data uintptr
Len uintptr
Len intw
}

// Like sliceHeader, this type is used internally to make sure pointer and
Expand All @@ -1602,6 +1602,16 @@ type stringHeader struct {
len uintptr
}

// Verify SliceHeader and StringHeader sizes.
// See https://github.com/tinygo-org/tinygo/pull/4156
// and https://github.com/tinygo-org/tinygo/issues/1284.
var (
_ [unsafe.Sizeof([]byte{})]byte = [unsafe.Sizeof(SliceHeader{})]byte{}
_ [unsafe.Sizeof([]byte{})]byte = [unsafe.Sizeof(sliceHeader{})]byte{}
_ [unsafe.Sizeof("")]byte = [unsafe.Sizeof(StringHeader{})]byte{}
_ [unsafe.Sizeof("")]byte = [unsafe.Sizeof(stringHeader{})]byte{}
)

type ValueError struct {
Method string
Kind Kind
Expand Down
2 changes: 1 addition & 1 deletion tests/runtime_wasi/malloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func checkFilledBuffer(t *testing.T, ptr uintptr, content string) {
t.Helper()
buf := *(*string)(unsafe.Pointer(&reflect.StringHeader{
Data: ptr,
Len: uintptr(len(content)),
Len: len(content),
}))
if buf != content {
t.Errorf("expected %q, got %q", content, buf)
Expand Down