reflect: implement Sizeof and Alignof for func values

This is a small change that appears to be necessary for encoding/json
support. It's simple enough to implement.
This commit is contained in:
Ayke van Laethem
2021-03-28 22:31:27 +02:00
committed by Ron Evans
parent 16e7dd83a3
commit 4f6d598ea8
3 changed files with 14 additions and 4 deletions
+10 -4
View File
@@ -533,13 +533,16 @@ func (t rawType) Size() uintptr {
case Complex128:
return 16
case String:
return unsafe.Sizeof(StringHeader{})
return unsafe.Sizeof("")
case UnsafePointer, Chan, Map, Ptr:
return unsafe.Sizeof(uintptr(0))
case Slice:
return unsafe.Sizeof(SliceHeader{})
return unsafe.Sizeof([]int{})
case Interface:
return unsafe.Sizeof(interface{}(nil))
case Func:
var f func()
return unsafe.Sizeof(f)
case Array:
return t.elem().Size() * uintptr(t.Len())
case Struct:
@@ -579,13 +582,16 @@ func (t rawType) Align() int {
case Complex128:
return int(unsafe.Alignof(complex128(0)))
case String:
return int(unsafe.Alignof(StringHeader{}))
return int(unsafe.Alignof(""))
case UnsafePointer, Chan, Map, Ptr:
return int(unsafe.Alignof(uintptr(0)))
case Slice:
return int(unsafe.Alignof(SliceHeader{}))
return int(unsafe.Alignof([]int(nil)))
case Interface:
return int(unsafe.Alignof(interface{}(nil)))
case Func:
var f func()
return int(unsafe.Alignof(f))
case Struct:
numField := t.NumField()
alignment := 1