From 7b4be6078640c67eb75b3191f8d440fa6c4e5ce8 Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 28 Jun 2021 20:15:59 +0200 Subject: [PATCH] runtime: print panic messages as gc does This change implements the commit golang/go@972df38445977cc04414c7b6f469e2a8e5a63861 in Scriggo. For #385 --- runtime/errors.go | 35 +++++++++++++++++++++-- runtime/errors_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 runtime/errors_test.go diff --git a/runtime/errors.go b/runtime/errors.go index 28251dc98..6aea83caa 100644 --- a/runtime/errors.go +++ b/runtime/errors.go @@ -338,9 +338,38 @@ func panicToString(msg interface{}) string { case stringer: return v.String() default: - typ := reflect.TypeOf(v).String() - iData := reflect.ValueOf(&v).Elem().InterfaceData() - return "(" + typ + ") (" + hex(iData[0]) + "," + hex(iData[1]) + ")" + rv := reflect.ValueOf(v) + rt := rv.Type().String() + var s string + switch rv.Kind() { + case reflect.Bool: + s = "false" + if rv.Bool() { + s = "true" + } + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + s = strconv.FormatInt(rv.Int(), 10) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + s = strconv.FormatUint(rv.Uint(), 10) + case reflect.Float32: + s = strconv.FormatFloat(rv.Float(), 'e', -1, 32) + case reflect.Float64: + s = strconv.FormatFloat(rv.Float(), 'e', -1, 64) + case reflect.Complex64: + c := rv.Complex() + s = strconv.FormatFloat(real(c), 'e', -1, 32) + + strconv.FormatFloat(imag(c), 'e', -1, 32) + case reflect.Complex128: + c := rv.Complex() + s = strconv.FormatFloat(real(c), 'e', 3, 64) + + strconv.FormatFloat(imag(c), 'e', 3, 64) + case reflect.String: + s = `"` + rv.String() + `"` + default: + iData := reflect.ValueOf(&v).Elem().InterfaceData() + return "(" + rt + ") (" + hex(iData[0]) + "," + hex(iData[1]) + ")" + } + return rt + "(" + s + ")" } } diff --git a/runtime/errors_test.go b/runtime/errors_test.go new file mode 100644 index 000000000..30f42eeb7 --- /dev/null +++ b/runtime/errors_test.go @@ -0,0 +1,65 @@ +// Copyright (c) 2021 Open2b Software Snc. All rights reserved. +// https://www.open2b.com + +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package runtime + +import ( + "strings" + "testing" +) + +type myBool bool +type myInt int +type myInt8 int8 +type myInt16 int16 +type myInt32 int32 +type myInt64 int64 +type myUint uint +type myUint8 uint8 +type myUint16 uint16 +type myUint32 uint32 +type myUint64 uint64 +type myUintptr uintptr +type myFloat32 float32 +type myFloat64 float64 +type myComplex64 complex64 +type myComplex128 complex128 +type myString string +type myStruct struct{} + +var tests = []struct { + val interface{} + str string +}{ + {myBool(true), "myBool(true)"}, + {myInt(73), "myInt(73)"}, + {myInt8(73), "myInt8(73)"}, + {myInt16(73), "myInt16(73)"}, + {myInt32(73), "myInt32(73)"}, + {myInt64(73), "myInt64(73)"}, + {myUint(73), "myUint(73)"}, + {myUint8(73), "myUint8(73)"}, + {myUint16(73), "myUint16(73)"}, + {myUint32(73), "myUint32(73)"}, + {myUint64(73), "myUint64(73)"}, + {myUintptr(73), "myUintptr(73)"}, + {myFloat32(12.639), "myFloat32(1.2639e+01)"}, + {myFloat64(12.639), "myFloat64(1.2639e+01)"}, + {myComplex64(6.2 + 2.7i), "myComplex64(6.2e+002.7e+00)"}, + {myComplex128(6.2 + 2.7i), "myComplex128(6.200e+002.700e+00)"}, + {myString("foo"), "myString(\"foo\")"}, +} + +func TestPanicToString(t *testing.T) { + for _, tt := range tests { + t.Run(tt.str, func(t *testing.T) { + s := panicToString(tt.val) + if !strings.HasPrefix(s, "runtime.") || s[len("runtime."):] != tt.str { + t.Fatalf("expecting %s, got %s", tt.str, s) + } + }) + } +}