runtime: panic any custometype

This commit is contained in:
visualfc
2024-06-23 21:15:20 +08:00
parent df39b66e11
commit c090c34491
16 changed files with 798 additions and 532 deletions

View File

@@ -16,6 +16,13 @@
package runtime
import (
"unsafe"
"github.com/goplus/llgo/c/bitcast"
"github.com/goplus/llgo/internal/abi"
)
type errorString string
func (e errorString) RuntimeError() {}
@@ -41,3 +48,99 @@ func AssertIndexRange(b bool) {
panic(errorString("index out of range").Error())
}
}
// printany prints an argument passed to panic.
// If panic is called with a value that has a String or Error method,
// it has already been converted into a string by preprintpanics.
func printany(i any) {
switch v := i.(type) {
case nil:
print("nil")
case bool:
print(v)
case int:
print(v)
case int8:
print(v)
case int16:
print(v)
case int32:
print(v)
case int64:
print(v)
case uint:
print(v)
case uint8:
print(v)
case uint16:
print(v)
case uint32:
print(v)
case uint64:
print(v)
case uintptr:
print(v)
case float32:
print(v)
case float64:
print(v)
case complex64:
print(v)
case complex128:
print(v)
case string:
print(v)
default:
printanycustomtype(i)
}
}
func efaceOf(ep *any) *eface {
return (*eface)(unsafe.Pointer(ep))
}
func printanycustomtype(i any) {
e := efaceOf(&i)
typestring := e._type.String()
switch e._type.Kind() {
case abi.String:
print(typestring, `("`, *(*string)(e.data), `")`)
case abi.Bool:
if isDirectIface(e._type) {
print(typestring, "(", uintptr(e.data) != 0, ")")
} else {
print(typestring, "(", *(*bool)(e.data), ")")
}
case abi.Int, abi.Int8, abi.Int16, abi.Int32, abi.Int64:
if isDirectIface(e._type) {
print(typestring, "(", int64(uintptr(e.data)), ")")
} else {
print(typestring, "(", *(*int64)(e.data), ")")
}
case abi.Uint, abi.Uint8, abi.Uint16, abi.Uint32, abi.Uint64, abi.Uintptr:
if isDirectIface(e._type) {
print(typestring, "(", uint64(uintptr(e.data)), ")")
} else {
print(typestring, "(", *(*uint64)(e.data), ")")
}
case abi.Float32:
if isDirectIface(e._type) {
print(typestring, "(", bitcast.ToFloat32((uintptr(e.data))), ")")
} else {
print(typestring, "(", *(*float32)(e.data), ")")
}
case abi.Float64:
if isDirectIface(e._type) {
print(typestring, "(", bitcast.ToFloat64(uintptr(e.data)), ")")
} else {
print(typestring, "(", *(*float64)(e.data), ")")
}
case abi.Complex64:
println(typestring, *(*complex64)(e.data))
case abi.Complex128:
println(typestring, *(*complex128)(e.data))
default:
print("(", typestring, ") ", e.data)
}
}

View File

@@ -113,11 +113,24 @@ func NewNamed(kind abi.Kind, methods, ptrMethods int) *Type {
return ret
}
func lastSlash(s string) int {
i := len(s) - 1
for i >= 0 && s[i] != '/' {
i--
}
return i
}
func pkgName(path string) string {
i := lastSlash(path)
return path[i+1:]
}
// InitNamed initializes an uninitialized named type.
func InitNamed(ret *Type, pkgPath, name string, underlying *Type, methods, ptrMethods []Method) {
ptr := ret.PtrToThis_
if pkgPath != "" {
name = pkgPath + "." + name
name = pkgName(pkgPath) + "." + name
}
doInitNamed(ret, pkgPath, name, underlying, methods)
doInitNamed(ptr, pkgPath, name, newPointer(ret), ptrMethods)

View File

@@ -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")
}
/*