runtime: panic any custometype
This commit is contained in:
@@ -20,9 +20,7 @@ import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/bitcast"
|
||||
"github.com/goplus/llgo/c/pthread"
|
||||
"github.com/goplus/llgo/internal/abi"
|
||||
)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -48,6 +46,12 @@ func Recover() (ret any) {
|
||||
|
||||
// Panic panics with a value.
|
||||
func Panic(v any) {
|
||||
switch e := v.(type) {
|
||||
case error:
|
||||
v = e.Error()
|
||||
case interface{ String() string }:
|
||||
v = e.String()
|
||||
}
|
||||
ptr := c.Malloc(unsafe.Sizeof(v))
|
||||
*(*any)(ptr) = v
|
||||
excepKey.Set(ptr)
|
||||
@@ -85,54 +89,8 @@ func unpackEface(i any) *eface {
|
||||
// TracePanic prints panic message.
|
||||
func TracePanic(v any) {
|
||||
print("panic: ")
|
||||
switch e := v.(type) {
|
||||
case nil:
|
||||
println("nil")
|
||||
return
|
||||
case (interface{ Error() string }):
|
||||
println(e.Error())
|
||||
return
|
||||
case (interface{ String() string }):
|
||||
println(e.String())
|
||||
return
|
||||
}
|
||||
e := unpackEface(v)
|
||||
switch e.Kind() {
|
||||
case abi.Int, abi.Int8, abi.Int16, abi.Int32, abi.Int64:
|
||||
if isDirectIface(e._type) {
|
||||
println(int64(uintptr(e.data)))
|
||||
} else {
|
||||
println(*(*int64)(e.data))
|
||||
}
|
||||
case abi.Uint, abi.Uint8, abi.Uint16, abi.Uint32, abi.Uint64, abi.Uintptr:
|
||||
if isDirectIface(e._type) {
|
||||
println(uint64(uintptr(e.data)))
|
||||
} else {
|
||||
println(*(*uint64)(e.data))
|
||||
}
|
||||
case abi.Float32:
|
||||
if isDirectIface(e._type) {
|
||||
println(bitcast.ToFloat32((uintptr(e.data))))
|
||||
} else {
|
||||
println(*(*float32)(e.data))
|
||||
}
|
||||
case abi.Float64:
|
||||
if isDirectIface(e._type) {
|
||||
println(bitcast.ToFloat64(uintptr(e.data)))
|
||||
} else {
|
||||
println(*(*float64)(e.data))
|
||||
}
|
||||
case abi.Complex64:
|
||||
println(*(*complex64)(e.data))
|
||||
case abi.Complex128:
|
||||
println(*(*complex128)(e.data))
|
||||
case abi.String:
|
||||
println(*(*string)(e.data))
|
||||
default:
|
||||
// TODO kind to e._type.Str_
|
||||
print("(", e.Kind(), ") ")
|
||||
println(e.data)
|
||||
}
|
||||
printany(v)
|
||||
println("\n")
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user