mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 22:43:40 +00:00
reflect: add Zero()
This commit is contained in:
committed by
Damian Gryski
parent
960a0b79bf
commit
bbc79ee40a
+32
-1
@@ -1017,8 +1017,39 @@ func MakeSlice(typ Type, len, cap int) Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var zerobuffer unsafe.Pointer
|
||||||
|
|
||||||
|
const zerobufferLen = 32
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// 32 characters of zero bytes
|
||||||
|
zerobufferStr := "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
|
||||||
|
s := (*stringHeader)(unsafe.Pointer(&zerobufferStr))
|
||||||
|
zerobuffer = s.data
|
||||||
|
}
|
||||||
|
|
||||||
func Zero(typ Type) Value {
|
func Zero(typ Type) Value {
|
||||||
panic("unimplemented: reflect.Zero()")
|
if typ.Size() < unsafe.Sizeof(uintptr(0)) {
|
||||||
|
return Value{
|
||||||
|
typecode: typ.(*rawType),
|
||||||
|
value: nil,
|
||||||
|
flags: valueFlagExported,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if typ.Size() <= zerobufferLen {
|
||||||
|
return Value{
|
||||||
|
typecode: typ.(*rawType),
|
||||||
|
value: unsafe.Pointer(zerobuffer),
|
||||||
|
flags: valueFlagExported,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Value{
|
||||||
|
typecode: typ.(*rawType),
|
||||||
|
value: alloc(typ.Size(), nil),
|
||||||
|
flags: valueFlagExported,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// New is the reflect equivalent of the new(T) keyword, returning a pointer to a
|
// New is the reflect equivalent of the new(T) keyword, returning a pointer to a
|
||||||
|
|||||||
Reference in New Issue
Block a user