internal/lib/reflect: Value.Float Value.Bool

This commit is contained in:
visualfc
2024-11-13 21:21:32 +08:00
parent 1f757270d9
commit 38f1585ac6

View File

@@ -24,11 +24,11 @@ import (
"errors" "errors"
"unsafe" "unsafe"
"github.com/goplus/llgo/x/ffi" "github.com/goplus/llgo/c/bitcast"
"github.com/goplus/llgo/internal/abi" "github.com/goplus/llgo/internal/abi"
"github.com/goplus/llgo/internal/runtime" "github.com/goplus/llgo/internal/runtime"
"github.com/goplus/llgo/internal/runtime/goarch" "github.com/goplus/llgo/internal/runtime/goarch"
"github.com/goplus/llgo/x/ffi"
) )
// Value is the reflection interface to a Go value. // Value is the reflection interface to a Go value.
@@ -316,8 +316,11 @@ func (v Value) Bool() bool {
if v.kind() != Bool { if v.kind() != Bool {
v.panicNotBool() v.panicNotBool()
} }
if v.flag&flagAddr != 0 {
return *(*bool)(v.ptr) return *(*bool)(v.ptr)
} }
return uintptr(v.ptr) != 0
}
func (v Value) panicNotBool() { func (v Value) panicNotBool() {
v.mustBe(Bool) v.mustBe(Bool)
@@ -627,12 +630,21 @@ func (v Value) CanFloat() bool {
// It panics if v's Kind is not Float32 or Float64 // It panics if v's Kind is not Float32 or Float64
func (v Value) Float() float64 { func (v Value) Float() float64 {
k := v.kind() k := v.kind()
if v.flag&flagAddr != 0 {
switch k { switch k {
case Float32: case Float32:
return float64(*(*float32)(v.ptr)) return float64(*(*float32)(v.ptr))
case Float64: case Float64:
return *(*float64)(v.ptr) return *(*float64)(v.ptr)
} }
} else {
switch k {
case Float32:
return float64(bitcast.ToFloat32(uintptr(v.ptr)))
case Float64:
return bitcast.ToFloat64(uintptr(v.ptr))
}
}
panic(&ValueError{"reflect.Value.Float", v.kind()}) panic(&ValueError{"reflect.Value.Float", v.kind()})
} }