reflect: add converting complex

This commit is contained in:
Damian Gryski
2026-05-25 11:14:02 -07:00
committed by Damian Gryski
parent a93640662e
commit d29e8e5f22
2 changed files with 33 additions and 8 deletions
+32 -7
View File
@@ -1485,13 +1485,11 @@ func convertOp(src Value, typ Type) (Value, bool) {
return cvtFloat(src, rtype), true
}
/*
case Complex64, Complex128:
switch src.Kind() {
case Complex64, Complex128:
return cvtComplex
}
*/
case Complex64, Complex128:
switch src.Kind() {
case Complex64, Complex128:
return cvtComplex(src, typ.(*RawType)), true
}
case Slice:
switch rtype := typ.(*RawType); rtype.Kind() {
@@ -1578,6 +1576,10 @@ func cvtFloat(v Value, t *RawType) Value {
return makeFloat(v.flags, v.Float(), t)
}
func cvtComplex(v Value, t *RawType) Value {
return makeComplex(v.flags, v.Complex(), t)
}
//go:linkname stringToBytes runtime.stringToBytes
func stringToBytes(x string) []byte
@@ -1661,6 +1663,29 @@ func makeFloat32(flags valueFlags, f float32, t *RawType) Value {
return v
}
func makeComplex(flags valueFlags, f complex128, t *RawType) Value {
size := t.Size()
v := Value{
typecode: t,
flags: flags,
}
ptr := unsafe.Pointer(&v.value)
if size > unsafe.Sizeof(uintptr(0)) {
ptr = alloc(size, nil)
v.value = ptr
}
switch size {
case 8:
*(*complex64)(ptr) = complex64(f)
case 16:
*(*complex128)(ptr) = f
}
return v
}
func cvtIntString(src Value, t *RawType) Value {
panic("cvtUintString: unimplemented")
}
+1 -1
View File
@@ -402,7 +402,7 @@ func (t *rawType) CanSeq2() bool {
}
func (t *rawType) ConvertibleTo(u Type) bool {
return t.RawType.Convertibleto(u.(*rawType).RawType)
return t.RawType.ConvertibleTo(&(u.(*rawType).RawType))
}
func (t *rawType) Elem() Type {