Skip to content

Commit fe27b67

Browse files
authored
reflect: use int in StringHeader and SliceHeader on non-AVR platforms (#4156)
1 parent c78dbcd commit fe27b67

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

src/reflect/intw.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build !avr
2+
3+
package reflect
4+
5+
// intw is an integer type, used in places where an int is typically required,
6+
// except architectures where the size of an int != word size.
7+
// See https://github.com/tinygo-org/tinygo/issues/1284.
8+
type intw = int

src/reflect/intw_avr.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build avr
2+
3+
package reflect
4+
5+
// intw is an integer type, used in places where an int is typically required,
6+
// except architectures where the size of an int != word size.
7+
// See https://github.com/tinygo-org/tinygo/issues/1284.
8+
type intw = uintptr

src/reflect/intw_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build !avr
2+
3+
package reflect_test
4+
5+
import (
6+
"reflect"
7+
"testing"
8+
"unsafe"
9+
)
10+
11+
// Verify that SliceHeader is the same size as a slice.
12+
var _ [unsafe.Sizeof([]byte{})]byte = [unsafe.Sizeof(reflect.SliceHeader{})]byte{}
13+
14+
// TestSliceHeaderIntegerSize verifies that SliceHeader.Len and Cap are type int on non-AVR platforms.
15+
// See https://github.com/tinygo-org/tinygo/issues/1284.
16+
func TestSliceHeaderIntegerSize(t *testing.T) {
17+
var h reflect.SliceHeader
18+
h.Len = int(0)
19+
h.Cap = int(0)
20+
}
21+
22+
// Verify that StringHeader is the same size as a string.
23+
var _ [unsafe.Sizeof("hello")]byte = [unsafe.Sizeof(reflect.StringHeader{})]byte{}
24+
25+
// TestStringHeaderIntegerSize verifies that StringHeader.Len and Cap are type int on non-AVR platforms.
26+
// See https://github.com/tinygo-org/tinygo/issues/1284.
27+
func TestStringHeaderIntegerSize(t *testing.T) {
28+
var h reflect.StringHeader
29+
h.Len = int(0)
30+
}

src/reflect/value.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,8 +1578,8 @@ type funcHeader struct {
15781578

15791579
type SliceHeader struct {
15801580
Data uintptr
1581-
Len uintptr
1582-
Cap uintptr
1581+
Len intw
1582+
Cap intw
15831583
}
15841584

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

15931593
type StringHeader struct {
15941594
Data uintptr
1595-
Len uintptr
1595+
Len intw
15961596
}
15971597

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

1605+
// Verify SliceHeader and StringHeader sizes.
1606+
// See https://github.com/tinygo-org/tinygo/pull/4156
1607+
// and https://github.com/tinygo-org/tinygo/issues/1284.
1608+
var (
1609+
_ [unsafe.Sizeof([]byte{})]byte = [unsafe.Sizeof(SliceHeader{})]byte{}
1610+
_ [unsafe.Sizeof([]byte{})]byte = [unsafe.Sizeof(sliceHeader{})]byte{}
1611+
_ [unsafe.Sizeof("")]byte = [unsafe.Sizeof(StringHeader{})]byte{}
1612+
_ [unsafe.Sizeof("")]byte = [unsafe.Sizeof(stringHeader{})]byte{}
1613+
)
1614+
16051615
type ValueError struct {
16061616
Method string
16071617
Kind Kind

tests/runtime_wasi/malloc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func checkFilledBuffer(t *testing.T, ptr uintptr, content string) {
6767
t.Helper()
6868
buf := *(*string)(unsafe.Pointer(&reflect.StringHeader{
6969
Data: ptr,
70-
Len: uintptr(len(content)),
70+
Len: len(content),
7171
}))
7272
if buf != content {
7373
t.Errorf("expected %q, got %q", content, buf)

0 commit comments

Comments
 (0)