Skip to content

Commit 8bff065

Browse files
committed
runtime: use internal/reflectlite instead of reflect
This avoids a circular dependency.
1 parent 439dc7a commit 8bff065

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

src/reflect/value.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,6 @@ func (v Value) IsZero() bool {
132132
}
133133
}
134134

135-
// Internal function only, do not use.
136-
//
137-
// RawType returns the raw, underlying type code. It is used in the runtime
138-
// package and needs to be exported for the runtime package to access it.
139-
func (v Value) RawType() *rawType {
140-
return v.typecode
141-
}
142-
143135
func (v Value) Kind() Kind {
144136
return v.typecode.Kind()
145137
}

src/runtime/hashmap.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package runtime
66
// https://golang.org/src/runtime/map.go
77

88
import (
9-
"reflect"
9+
"internal/reflectlite"
1010
"tinygo"
1111
"unsafe"
1212
)
@@ -534,13 +534,13 @@ func hashmapStringDelete(m *hashmap, key string) {
534534

535535
// Hashmap with interface keys (for everything else).
536536

537-
// This is a method that is intentionally unexported in the reflect package. It
538-
// is identical to the Interface() method call, except it doesn't check whether
539-
// a field is exported and thus allows circumventing the type system.
537+
// This is a method that is intentionally unexported in the reflectlite package.
538+
// It is identical to the Interface() method call, except it doesn't check
539+
// whether a field is exported and thus allows circumventing the type system.
540540
// The hash function needs it as it also needs to hash unexported struct fields.
541541
//
542542
//go:linkname valueInterfaceUnsafe internal/reflectlite.valueInterfaceUnsafe
543-
func valueInterfaceUnsafe(v reflect.Value) interface{}
543+
func valueInterfaceUnsafe(v reflectlite.Value) interface{}
544544

545545
func hashmapFloat32Hash(ptr unsafe.Pointer, seed uintptr) uint32 {
546546
f := *(*uint32)(ptr)
@@ -561,7 +561,7 @@ func hashmapFloat64Hash(ptr unsafe.Pointer, seed uintptr) uint32 {
561561
}
562562

563563
func hashmapInterfaceHash(itf interface{}, seed uintptr) uint32 {
564-
x := reflect.ValueOf(itf)
564+
x := reflectlite.ValueOf(itf)
565565
if x.RawType() == nil {
566566
return 0 // nil interface
567567
}
@@ -574,39 +574,39 @@ func hashmapInterfaceHash(itf interface{}, seed uintptr) uint32 {
574574
}
575575

576576
switch x.RawType().Kind() {
577-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
577+
case reflectlite.Int, reflectlite.Int8, reflectlite.Int16, reflectlite.Int32, reflectlite.Int64:
578578
return hash32(ptr, x.RawType().Size(), seed)
579-
case reflect.Bool, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
579+
case reflectlite.Bool, reflectlite.Uint, reflectlite.Uint8, reflectlite.Uint16, reflectlite.Uint32, reflectlite.Uint64, reflectlite.Uintptr:
580580
return hash32(ptr, x.RawType().Size(), seed)
581-
case reflect.Float32:
581+
case reflectlite.Float32:
582582
// It should be possible to just has the contents. However, NaN != NaN
583583
// so if you're using lots of NaNs as map keys (you shouldn't) then hash
584584
// time may become exponential. To fix that, it would be better to
585585
// return a random number instead:
586586
// https://research.swtch.com/randhash
587587
return hashmapFloat32Hash(ptr, seed)
588-
case reflect.Float64:
588+
case reflectlite.Float64:
589589
return hashmapFloat64Hash(ptr, seed)
590-
case reflect.Complex64:
590+
case reflectlite.Complex64:
591591
rptr, iptr := ptr, unsafe.Add(ptr, 4)
592592
return hashmapFloat32Hash(rptr, seed) ^ hashmapFloat32Hash(iptr, seed)
593-
case reflect.Complex128:
593+
case reflectlite.Complex128:
594594
rptr, iptr := ptr, unsafe.Add(ptr, 8)
595595
return hashmapFloat64Hash(rptr, seed) ^ hashmapFloat64Hash(iptr, seed)
596-
case reflect.String:
596+
case reflectlite.String:
597597
return hashmapStringHash(x.String(), seed)
598-
case reflect.Chan, reflect.Ptr, reflect.UnsafePointer:
598+
case reflectlite.Chan, reflectlite.Ptr, reflectlite.UnsafePointer:
599599
// It might seem better to just return the pointer, but that won't
600600
// result in an evenly distributed hashmap. Instead, hash the pointer
601601
// like most other types.
602602
return hash32(ptr, x.RawType().Size(), seed)
603-
case reflect.Array:
603+
case reflectlite.Array:
604604
var hash uint32
605605
for i := 0; i < x.Len(); i++ {
606606
hash ^= hashmapInterfaceHash(valueInterfaceUnsafe(x.Index(i)), seed)
607607
}
608608
return hash
609-
case reflect.Struct:
609+
case reflectlite.Struct:
610610
var hash uint32
611611
for i := 0; i < x.NumField(); i++ {
612612
hash ^= hashmapInterfaceHash(valueInterfaceUnsafe(x.Field(i)), seed)

src/runtime/interface.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package runtime
66
// anything (including non-pointers).
77

88
import (
9+
"internal/reflectlite"
910
"reflect"
1011
"unsafe"
1112
)
@@ -27,10 +28,10 @@ func decomposeInterface(i _interface) (unsafe.Pointer, unsafe.Pointer) {
2728

2829
// Return true iff both interfaces are equal.
2930
func interfaceEqual(x, y interface{}) bool {
30-
return reflectValueEqual(reflect.ValueOf(x), reflect.ValueOf(y))
31+
return reflectValueEqual(reflectlite.ValueOf(x), reflectlite.ValueOf(y))
3132
}
3233

33-
func reflectValueEqual(x, y reflect.Value) bool {
34+
func reflectValueEqual(x, y reflectlite.Value) bool {
3435
// Note: doing a x.Type() == y.Type() comparison would not work here as that
3536
// would introduce an infinite recursion: comparing two reflect.Type values
3637
// is done with this reflectValueEqual runtime call.

0 commit comments

Comments
 (0)