reflect: implement New function

This is very important for some use cases, for example for Vecty.
This commit is contained in:
Ayke van Laethem
2021-04-11 13:50:44 +02:00
committed by Ron Evans
parent 57271d7eaa
commit e587b1d1b4
8 changed files with 87 additions and 25 deletions
+3
View File
@@ -322,6 +322,9 @@ func TypeOf(i interface{}) Type {
}
func PtrTo(t Type) Type {
if t.Kind() == Ptr {
panic("reflect: cannot make **T type")
}
ptrType := t.(rawType)<<5 | 5 // 0b0101 == 5
if ptrType>>5 != t {
panic("reflect: PtrTo type does not fit")
+10 -1
View File
@@ -724,8 +724,14 @@ func Zero(typ Type) Value {
panic("unimplemented: reflect.Zero()")
}
// New is the reflect equivalent of the new(T) keyword, returning a pointer to a
// new value of the given type.
func New(typ Type) Value {
panic("unimplemented: reflect.New()")
return Value{
typecode: PtrTo(typ).(rawType),
value: alloc(typ.Size()),
flags: valueFlagExported,
}
}
type funcHeader struct {
@@ -756,6 +762,9 @@ func (e *ValueError) Error() string {
// llvm.memcpy.p0i8.p0i8.i32().
func memcpy(dst, src unsafe.Pointer, size uintptr)
//go:linkname alloc runtime.alloc
func alloc(size uintptr) unsafe.Pointer
// Copy copies the contents of src into dst until either
// dst has been filled or src has been exhausted.
func Copy(dst, src Value) int {
+5
View File
@@ -112,6 +112,11 @@ type typecodeID struct {
length uintptr
methodSet *interfaceMethodInfo // nil or a GEP of an array
// The type that's a pointer to this type, nil if it is already a pointer.
// Keeping the type struct alive here is important so that values from
// reflect.New (which uses reflect.PtrTo) can be used in type asserts etc.
ptrTo *typecodeID
}
// structField is used by the compiler to pass information to the interface