mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 20:43:40 +00:00
reflect: implement Value.Set*() for basic types
This commit is contained in:
+1
-2
@@ -199,8 +199,7 @@ func (t Type) Size() uintptr {
|
||||
case Slice:
|
||||
return unsafe.Sizeof(SliceHeader{})
|
||||
default:
|
||||
// Size unknown.
|
||||
return 0
|
||||
panic("unimplemented: size of type")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+219
-71
@@ -4,22 +4,42 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// This is the same thing as an interface{}.
|
||||
type Value struct {
|
||||
typecode Type
|
||||
value unsafe.Pointer
|
||||
indirect bool
|
||||
}
|
||||
|
||||
func Indirect(v Value) Value {
|
||||
return v
|
||||
if v.Kind() != Ptr {
|
||||
return v
|
||||
}
|
||||
return v.Elem()
|
||||
}
|
||||
|
||||
func ValueOf(i interface{}) Value {
|
||||
return *(*Value)(unsafe.Pointer(&i))
|
||||
v := (*interfaceHeader)(unsafe.Pointer(&i))
|
||||
return Value{
|
||||
typecode: v.typecode,
|
||||
value: v.value,
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Interface() interface{} {
|
||||
return *(*interface{})(unsafe.Pointer(&v))
|
||||
i := interfaceHeader{
|
||||
typecode: v.typecode,
|
||||
value: v.value,
|
||||
}
|
||||
if v.indirect && v.Type().Size() <= unsafe.Sizeof(uintptr(0)) {
|
||||
// Value was indirect but must be put back directly in the interface
|
||||
// value.
|
||||
var value uintptr
|
||||
for j := v.Type().Size(); j != 0; j-- {
|
||||
value = (value << 8) | uintptr(*(*uint8)(unsafe.Pointer(uintptr(v.value) + j - 1)))
|
||||
}
|
||||
i.value = unsafe.Pointer(value)
|
||||
}
|
||||
return *(*interface{})(unsafe.Pointer(&i))
|
||||
}
|
||||
|
||||
func (v Value) Type() Type {
|
||||
@@ -47,7 +67,11 @@ func (v Value) IsNil() bool {
|
||||
slice := (*SliceHeader)(v.value)
|
||||
return slice.Data == 0
|
||||
case Interface:
|
||||
panic("unimplemented: (reflect.Value).IsNil()")
|
||||
if v.value == nil {
|
||||
return true
|
||||
}
|
||||
itf := (*interfaceHeader)(v.value)
|
||||
return itf.value == nil
|
||||
default:
|
||||
panic(&ValueError{"IsNil"})
|
||||
}
|
||||
@@ -85,13 +109,17 @@ func (v Value) Addr() Value {
|
||||
}
|
||||
|
||||
func (v Value) CanSet() bool {
|
||||
panic("unimplemented: (reflect.Value).CanSet()")
|
||||
return v.indirect
|
||||
}
|
||||
|
||||
func (v Value) Bool() bool {
|
||||
switch v.Kind() {
|
||||
case Bool:
|
||||
return uintptr(v.value) != 0
|
||||
if v.indirect {
|
||||
return *((*bool)(v.value))
|
||||
} else {
|
||||
return uintptr(v.value) != 0
|
||||
}
|
||||
default:
|
||||
panic(&ValueError{"Bool"})
|
||||
}
|
||||
@@ -100,27 +128,34 @@ func (v Value) Bool() bool {
|
||||
func (v Value) Int() int64 {
|
||||
switch v.Kind() {
|
||||
case Int:
|
||||
if unsafe.Sizeof(int(0)) <= unsafe.Sizeof(uintptr(0)) {
|
||||
return int64(int(uintptr(v.value)))
|
||||
} else {
|
||||
if v.indirect || unsafe.Sizeof(int(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return int64(*(*int)(v.value))
|
||||
} else {
|
||||
return int64(int(uintptr(v.value)))
|
||||
}
|
||||
case Int8:
|
||||
return int64(int8(uintptr(v.value)))
|
||||
case Int16:
|
||||
return int64(int16(uintptr(v.value)))
|
||||
case Int32:
|
||||
if unsafe.Sizeof(int32(0)) <= unsafe.Sizeof(uintptr(0)) {
|
||||
return int64(int32(uintptr(v.value)))
|
||||
if v.indirect {
|
||||
return int64(*(*int8)(v.value))
|
||||
} else {
|
||||
return int64(*(*int32)(v.value))
|
||||
return int64(int8(uintptr(v.value)))
|
||||
}
|
||||
return int64(uintptr(v.value))
|
||||
case Int64:
|
||||
if unsafe.Sizeof(int64(0)) <= unsafe.Sizeof(uintptr(0)) {
|
||||
return int64(uintptr(v.value))
|
||||
case Int16:
|
||||
if v.indirect {
|
||||
return int64(*(*int16)(v.value))
|
||||
} else {
|
||||
return *(*int64)(v.value)
|
||||
return int64(int16(uintptr(v.value)))
|
||||
}
|
||||
case Int32:
|
||||
if v.indirect || unsafe.Sizeof(int32(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return int64(*(*int32)(v.value))
|
||||
} else {
|
||||
return int64(int32(uintptr(v.value)))
|
||||
}
|
||||
case Int64:
|
||||
if v.indirect || unsafe.Sizeof(int64(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return int64(*(*int64)(v.value))
|
||||
} else {
|
||||
return int64(int64(uintptr(v.value)))
|
||||
}
|
||||
default:
|
||||
panic(&ValueError{"Int"})
|
||||
@@ -129,28 +164,41 @@ func (v Value) Int() int64 {
|
||||
|
||||
func (v Value) Uint() uint64 {
|
||||
switch v.Kind() {
|
||||
case Uintptr, Uint8, Uint16:
|
||||
return uint64(uintptr(v.value))
|
||||
case Uint:
|
||||
if unsafe.Sizeof(uint(0)) <= unsafe.Sizeof(uintptr(0)) {
|
||||
return uint64(uintptr(v.value))
|
||||
case Uintptr:
|
||||
if v.indirect {
|
||||
return uint64(*(*uintptr)(v.value))
|
||||
} else {
|
||||
// For systems with 16-bit pointers.
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint8:
|
||||
if v.indirect {
|
||||
return uint64(*(*uint8)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint16:
|
||||
if v.indirect {
|
||||
return uint64(*(*uint16)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint:
|
||||
if v.indirect || unsafe.Sizeof(uint(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return uint64(*(*uint)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint32:
|
||||
if unsafe.Sizeof(uint32(0)) <= unsafe.Sizeof(uintptr(0)) {
|
||||
return uint64(uintptr(v.value))
|
||||
} else {
|
||||
// For systems with 16-bit pointers.
|
||||
if v.indirect || unsafe.Sizeof(uint32(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return uint64(*(*uint32)(v.value))
|
||||
} else {
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
case Uint64:
|
||||
if unsafe.Sizeof(uint64(0)) <= unsafe.Sizeof(uintptr(0)) {
|
||||
return uint64(uintptr(v.value))
|
||||
if v.indirect || unsafe.Sizeof(uint64(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
return uint64(*(*uint64)(v.value))
|
||||
} else {
|
||||
// For systems with 16-bit or 32-bit pointers.
|
||||
return *(*uint64)(v.value)
|
||||
return uint64(uintptr(v.value))
|
||||
}
|
||||
default:
|
||||
panic(&ValueError{"Uint"})
|
||||
@@ -160,23 +208,23 @@ func (v Value) Uint() uint64 {
|
||||
func (v Value) Float() float64 {
|
||||
switch v.Kind() {
|
||||
case Float32:
|
||||
if unsafe.Sizeof(float32(0)) <= unsafe.Sizeof(uintptr(0)) {
|
||||
// The float is directly stored in the interface value on systems
|
||||
// with 32-bit and 64-bit pointers.
|
||||
return float64(*(*float32)(unsafe.Pointer(&v.value)))
|
||||
} else {
|
||||
if v.indirect || unsafe.Sizeof(float32(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
// The float is stored as an external value on systems with 16-bit
|
||||
// pointers.
|
||||
return float64(*(*float32)(v.value))
|
||||
} else {
|
||||
// The float is directly stored in the interface value on systems
|
||||
// with 32-bit and 64-bit pointers.
|
||||
return float64(*(*float32)(unsafe.Pointer(&v.value)))
|
||||
}
|
||||
case Float64:
|
||||
if unsafe.Sizeof(float64(0)) <= unsafe.Sizeof(uintptr(0)) {
|
||||
if v.indirect || unsafe.Sizeof(float64(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
// For systems with 16-bit and 32-bit pointers.
|
||||
return *(*float64)(v.value)
|
||||
} else {
|
||||
// The float is directly stored in the interface value on systems
|
||||
// with 64-bit pointers.
|
||||
return *(*float64)(unsafe.Pointer(&v.value))
|
||||
} else {
|
||||
// For systems with 16-bit and 32-bit pointers.
|
||||
return *(*float64)(v.value)
|
||||
}
|
||||
default:
|
||||
panic(&ValueError{"Float"})
|
||||
@@ -186,14 +234,14 @@ func (v Value) Float() float64 {
|
||||
func (v Value) Complex() complex128 {
|
||||
switch v.Kind() {
|
||||
case Complex64:
|
||||
if unsafe.Sizeof(complex64(0)) <= unsafe.Sizeof(uintptr(0)) {
|
||||
// The complex number is directly stored in the interface value on
|
||||
// systems with 64-bit pointers.
|
||||
return complex128(*(*complex64)(unsafe.Pointer(&v.value)))
|
||||
} else {
|
||||
if v.indirect || unsafe.Sizeof(complex64(0)) > unsafe.Sizeof(uintptr(0)) {
|
||||
// The complex number is stored as an external value on systems with
|
||||
// 16-bit and 32-bit pointers.
|
||||
return complex128(*(*complex64)(v.value))
|
||||
} else {
|
||||
// The complex number is directly stored in the interface value on
|
||||
// systems with 64-bit pointers.
|
||||
return complex128(*(*complex64)(unsafe.Pointer(&v.value)))
|
||||
}
|
||||
case Complex128:
|
||||
// This is a 128-bit value, which is always stored as an external value.
|
||||
@@ -252,7 +300,23 @@ func (v Value) NumField() int {
|
||||
}
|
||||
|
||||
func (v Value) Elem() Value {
|
||||
panic("unimplemented: (reflect.Value).Elem()")
|
||||
switch v.Kind() {
|
||||
case Ptr:
|
||||
ptr := v.value
|
||||
if v.indirect {
|
||||
ptr = *(*unsafe.Pointer)(ptr)
|
||||
}
|
||||
if ptr == nil {
|
||||
return Value{}
|
||||
}
|
||||
return Value{
|
||||
typecode: v.Type().Elem(),
|
||||
value: ptr,
|
||||
indirect: true,
|
||||
}
|
||||
default: // not implemented: Interface
|
||||
panic(&ValueError{"Elem"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) Field(i int) Value {
|
||||
@@ -269,21 +333,10 @@ func (v Value) Index(i int) Value {
|
||||
}
|
||||
elem := Value{
|
||||
typecode: v.Type().Elem(),
|
||||
indirect: true,
|
||||
}
|
||||
addr := uintptr(slice.Data) + elem.Type().Size() * uintptr(i) // pointer to new value
|
||||
if elem.Type().Size() <= unsafe.Sizeof(uintptr(0)) {
|
||||
// Value fits inside interface value.
|
||||
// Make sure to copy it from the slice to the interface value.
|
||||
var value uintptr
|
||||
for j := elem.Type().Size(); j != 0; j-- {
|
||||
value = (value << 8) | uintptr(*(*uint8)(unsafe.Pointer(addr + j - 1)))
|
||||
}
|
||||
elem.value = unsafe.Pointer(value)
|
||||
} else {
|
||||
// Value doesn't fit in the interface.
|
||||
// Store a pointer to the element in the interface.
|
||||
elem.value = unsafe.Pointer(addr)
|
||||
}
|
||||
elem.value = unsafe.Pointer(addr)
|
||||
return elem
|
||||
case String:
|
||||
// Extract a character from a string.
|
||||
@@ -313,31 +366,117 @@ func (v Value) MapIndex(key Value) Value {
|
||||
}
|
||||
|
||||
func (v Value) Set(x Value) {
|
||||
panic("unimplemented: (reflect.Value).Set()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
if v.Type() != x.Type() {
|
||||
if v.Kind() == Interface {
|
||||
panic("reflect: unimplemented: assigning to interface of different type")
|
||||
} else {
|
||||
panic("reflect: cannot assign")
|
||||
}
|
||||
}
|
||||
size := v.Type().Size()
|
||||
xptr := x.value
|
||||
if size <= unsafe.Sizeof(uintptr(0)) && !x.indirect {
|
||||
value := x.value
|
||||
xptr = unsafe.Pointer(&value)
|
||||
}
|
||||
memcpy(v.value, xptr, size)
|
||||
}
|
||||
|
||||
func (v Value) SetBool(x bool) {
|
||||
panic("unimplemented: (reflect.Value).SetBool()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Bool:
|
||||
*(*bool)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetBool"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetInt(x int64) {
|
||||
panic("unimplemented: (reflect.Value).SetInt()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Int:
|
||||
*(*int)(v.value) = int(x)
|
||||
case Int8:
|
||||
*(*int8)(v.value) = int8(x)
|
||||
case Int16:
|
||||
*(*int16)(v.value) = int16(x)
|
||||
case Int32:
|
||||
*(*int32)(v.value) = int32(x)
|
||||
case Int64:
|
||||
*(*int64)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetInt"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetUint(x uint64) {
|
||||
panic("unimplemented: (reflect.Value).SetUint()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Uint:
|
||||
*(*uint)(v.value) = uint(x)
|
||||
case Uint8:
|
||||
*(*uint8)(v.value) = uint8(x)
|
||||
case Uint16:
|
||||
*(*uint16)(v.value) = uint16(x)
|
||||
case Uint32:
|
||||
*(*uint32)(v.value) = uint32(x)
|
||||
case Uint64:
|
||||
*(*uint64)(v.value) = x
|
||||
case Uintptr:
|
||||
*(*uintptr)(v.value) = uintptr(x)
|
||||
default:
|
||||
panic(&ValueError{"SetUint"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetFloat(x float64) {
|
||||
panic("unimplemented: (reflect.Value).SetFloat()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Float32:
|
||||
*(*float32)(v.value) = float32(x)
|
||||
case Float64:
|
||||
*(*float64)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetFloat"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetComplex(x complex128) {
|
||||
panic("unimplemented: (reflect.Value).SetComplex()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case Complex64:
|
||||
*(*complex64)(v.value) = complex64(x)
|
||||
case Complex128:
|
||||
*(*complex128)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetComplex"})
|
||||
}
|
||||
}
|
||||
|
||||
func (v Value) SetString(x string) {
|
||||
panic("unimplemented: (reflect.Value).SetString()")
|
||||
if !v.indirect {
|
||||
panic("reflect: value is not addressable")
|
||||
}
|
||||
switch v.Kind() {
|
||||
case String:
|
||||
*(*string)(v.value) = x
|
||||
default:
|
||||
panic(&ValueError{"SetString"})
|
||||
}
|
||||
}
|
||||
|
||||
func MakeSlice(typ Type, len, cap int) Value {
|
||||
@@ -349,6 +488,12 @@ type funcHeader struct {
|
||||
Code unsafe.Pointer
|
||||
}
|
||||
|
||||
// This is the same thing as an interface{}.
|
||||
type interfaceHeader struct {
|
||||
typecode Type
|
||||
value unsafe.Pointer
|
||||
}
|
||||
|
||||
type SliceHeader struct {
|
||||
Data uintptr
|
||||
Len uintptr
|
||||
@@ -367,3 +512,6 @@ type ValueError struct {
|
||||
func (e *ValueError) Error() string {
|
||||
return "reflect: call of reflect.Value." + e.Method + " on invalid type"
|
||||
}
|
||||
|
||||
//go:linkname memcpy runtime.memcpy
|
||||
func memcpy(dst, src unsafe.Pointer, size uintptr)
|
||||
|
||||
Reference in New Issue
Block a user