Skip to content

Commit

Permalink
runtime: print panic messages as gc does
Browse files Browse the repository at this point in the history
This change implements the commit
golang/go@972df38 in Scriggo.

For #385
  • Loading branch information
gazerro committed Jun 28, 2021
1 parent 08e8078 commit 7b4be60
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
35 changes: 32 additions & 3 deletions runtime/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ")"
}
}

Expand Down
65 changes: 65 additions & 0 deletions runtime/errors_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 7b4be60

Please sign in to comment.