From 44793f45f640f6032ada0225803d98e35cc650e6 Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Wed, 24 Jul 2024 18:07:07 +0200 Subject: [PATCH] refactor: some suggestion Co-authored-by: Morgan --- gnovm/pkg/gnolang/frame.go | 2 +- gnovm/pkg/gnolang/machine.go | 41 +++++++++++++++++------------- gnovm/tests/files/panic0.gno | 2 +- gnovm/tests/files/panic0a.gno | 2 +- gnovm/tests/files/panic0b.gno | 4 +-- gnovm/tests/files/panic0c.gno | 2 +- gnovm/tests/files/panic1.gno | 2 +- gnovm/tests/files/panic2a.gno | 2 +- gnovm/tests/files/panic2b.gno | 2 +- gnovm/tests/files/recover10.gno | 2 +- gnovm/tests/files/recover1b.gno | 2 +- gnovm/tests/files/recover8.gno | 2 +- gnovm/tests/files/std5_stdlibs.gno | 3 +-- gnovm/tests/files/std8_stdlibs.gno | 2 +- gnovm/tests/files/typeassert1.gno | 2 +- gnovm/tests/files/typeassert2a.gno | 2 +- gnovm/tests/files/typeassert9.gno | 2 +- gnovm/tests/files/zrealm_panic.gno | 2 +- 18 files changed, 41 insertions(+), 37 deletions(-) diff --git a/gnovm/pkg/gnolang/frame.go b/gnovm/pkg/gnolang/frame.go index a362f3fb278..cefa121317b 100644 --- a/gnovm/pkg/gnolang/frame.go +++ b/gnovm/pkg/gnolang/frame.go @@ -126,7 +126,7 @@ func (s Stacktrace) String() string { case call.Frame.Func != nil: fmt.Fprintf(&builder, "%s\n", toExprTrace(cx)) fmt.Fprintf(&builder, " %s/%s:%d\n", call.Frame.Func.PkgPath, call.Frame.Func.FileName, call.Stmt.GetLine()) - default: + case call.Frame.GoFunc != nil: fmt.Fprintf(&builder, "%s\n", toExprTrace(cx)) fmt.Fprintf(&builder, " %s\n", call.Frame.GoFunc.Value.Type()) } diff --git a/gnovm/pkg/gnolang/machine.go b/gnovm/pkg/gnolang/machine.go index b22be1ad4bd..24299dd1442 100644 --- a/gnovm/pkg/gnolang/machine.go +++ b/gnovm/pkg/gnolang/machine.go @@ -466,7 +466,7 @@ func (m *Machine) Stacktrace() (stacktrace Stacktrace) { return } - var calls []StacktraceCall + calls := make([]StacktraceCall, 0, len(m.Stmts)) nextStmtIndex := len(m.Stmts) - 1 for i := len(m.Frames) - 1; i >= 0; i-- { if m.Frames[i].IsCall() { @@ -482,15 +482,17 @@ func (m *Machine) Stacktrace() (stacktrace Stacktrace) { nextStmtIndex = m.Frames[i].NumStmts - 1 } - stacktrace.Calls = calls - // if the stacktrace is too long, we trim it down to maxStacktraceSize if len(calls) > maxStacktraceSize { - stacktrace.Calls = calls[:maxStacktraceSize/2] - stacktrace.Calls = append(stacktrace.Calls, calls[len(calls)-maxStacktraceSize/2:]...) + const halfMax = maxStacktraceSize / 2 + stacktrace.NumFramesElided = len(calls) - maxStacktraceSize + calls = append(calls[:halfMax], calls[len(calls)-halfMax:]...) + calls = calls[:len(calls):len(calls)] // makes remaining part of "calls" GC'able } + stacktrace.Calls = calls + return } @@ -2268,21 +2270,24 @@ func (m *Machine) String() string { } func (m *Machine) ExceptionsStacktrace() string { - var builder strings.Builder - - i := 0 - for i < len(m.Exceptions) { - ex := m.Exceptions[i] - builder.WriteString(fmt.Sprintf("panic %s\n", ex.Sprint(m))) - builder.WriteString(fmt.Sprintf("%s", ex.Stacktrace.String())) + if len(m.Exceptions) == 0 { + return "" + } - if i == 0 && len(m.Exceptions) > 2 { - builder.WriteString(fmt.Sprintf("... %d panic(s) elided ...\n", len(m.Exceptions)-2)) - i = len(m.Exceptions) - 1 - continue - } + var builder strings.Builder - i++ + ex := m.Exceptions[0] + builder.WriteString("panic: " + ex.Sprint(m) + "\n") + builder.WriteString(ex.Stacktrace.String()) + + switch { + case len(m.Exceptions) > 2: + fmt.Fprintf(&builder, "... %d panic(s) elided ...\n", len(m.Exceptions)-2) + fallthrough // to print last exception + case len(m.Exceptions) == 2: + ex = m.Exceptions[len(m.Exceptions)-1] + builder.WriteString("panic: " + ex.Sprint(m) + "\n") + builder.WriteString(ex.Stacktrace.String()) } return builder.String() diff --git a/gnovm/tests/files/panic0.gno b/gnovm/tests/files/panic0.gno index ffa1a5aad0c..06460ca9d07 100644 --- a/gnovm/tests/files/panic0.gno +++ b/gnovm/tests/files/panic0.gno @@ -5,7 +5,7 @@ func main() { } // Stacktrace: -// panic wtf +// panic: wtf // main() // main/files/panic0.gno:4 diff --git a/gnovm/tests/files/panic0a.gno b/gnovm/tests/files/panic0a.gno index 0b98763479e..044d1a44ca3 100644 --- a/gnovm/tests/files/panic0a.gno +++ b/gnovm/tests/files/panic0a.gno @@ -19,7 +19,7 @@ func main() { } // Stacktrace: -// panic wtf +// panic: wtf // f([](const-type string),S,map[(const-type string)] (const-type string),func(s (const-type string)) (const-type string){ ... }) // main/files/panic0a.gno:8 // main() diff --git a/gnovm/tests/files/panic0b.gno b/gnovm/tests/files/panic0b.gno index 55205cd508a..55a7b21015a 100644 --- a/gnovm/tests/files/panic0b.gno +++ b/gnovm/tests/files/panic0b.gno @@ -15,13 +15,13 @@ func f() { } // Stacktrace: -// panic first +// panic: first // f() // main/files/panic0b.gno:14 // main() // main/files/panic0b.gno:4 // ... 1 panic(s) elided ... -// panic third +// panic: third // f() // main/files/panic0b.gno:9 // main() diff --git a/gnovm/tests/files/panic0c.gno b/gnovm/tests/files/panic0c.gno index ba48587bb23..aa4ecd410da 100644 --- a/gnovm/tests/files/panic0c.gno +++ b/gnovm/tests/files/panic0c.gno @@ -19,7 +19,7 @@ func main() { } // Stacktrace: -// panic wtf +// panic: wtf // f([]string,main.S,map[string]string,func(s string)( string)) // main/files/panic0c.gno:8 // main() diff --git a/gnovm/tests/files/panic1.gno b/gnovm/tests/files/panic1.gno index 251e53766e7..235ba4a0b34 100644 --- a/gnovm/tests/files/panic1.gno +++ b/gnovm/tests/files/panic1.gno @@ -23,7 +23,7 @@ func main() { } // Stacktrace: -// panic here +// panic: here // main() // main/files/panic1.gno:22 diff --git a/gnovm/tests/files/panic2a.gno b/gnovm/tests/files/panic2a.gno index 09936568902..7310d6cce71 100644 --- a/gnovm/tests/files/panic2a.gno +++ b/gnovm/tests/files/panic2a.gno @@ -12,7 +12,7 @@ func main() { } // Stacktrace: -// panic here +// panic: here // p(i + 1) // main/files/panic2a.gno:5 // p(i + 1) diff --git a/gnovm/tests/files/panic2b.gno b/gnovm/tests/files/panic2b.gno index ff3ace31789..4ad4968ace8 100644 --- a/gnovm/tests/files/panic2b.gno +++ b/gnovm/tests/files/panic2b.gno @@ -15,7 +15,7 @@ func main() { } // Stacktrace: -// panic here +// panic: here // Error: // here diff --git a/gnovm/tests/files/recover10.gno b/gnovm/tests/files/recover10.gno index ea3d4afcd22..de083a322a4 100644 --- a/gnovm/tests/files/recover10.gno +++ b/gnovm/tests/files/recover10.gno @@ -7,7 +7,7 @@ func main() { } // Stacktrace: -// panic ahhhhh +// panic: ahhhhh // main() // main/files/recover10.gno:6 diff --git a/gnovm/tests/files/recover1b.gno b/gnovm/tests/files/recover1b.gno index 7392e8b87c3..978b4988329 100644 --- a/gnovm/tests/files/recover1b.gno +++ b/gnovm/tests/files/recover1b.gno @@ -11,7 +11,7 @@ func main() { } // Stacktrace: -// panic other panic +// panic: other panic // main() // main/files/recover1b.gno:8 diff --git a/gnovm/tests/files/recover8.gno b/gnovm/tests/files/recover8.gno index d8f6a6a62b8..d144a4f986f 100644 --- a/gnovm/tests/files/recover8.gno +++ b/gnovm/tests/files/recover8.gno @@ -21,7 +21,7 @@ func main() { } // Stacktrace: -// panic do something panic +// panic: do something panic // doSomething() // main/files/recover8.gno:7 // main() diff --git a/gnovm/tests/files/std5_stdlibs.gno b/gnovm/tests/files/std5_stdlibs.gno index c9be65af496..4afa09da8d3 100644 --- a/gnovm/tests/files/std5_stdlibs.gno +++ b/gnovm/tests/files/std5_stdlibs.gno @@ -12,8 +12,7 @@ func main() { } // Stacktrace: - -// panic frame not found +// panic: frame not found // callerAt(n) // gonative:std.callerAt // std.GetCallerAt(2) diff --git a/gnovm/tests/files/std8_stdlibs.gno b/gnovm/tests/files/std8_stdlibs.gno index e746bba468f..ab5e15bd618 100644 --- a/gnovm/tests/files/std8_stdlibs.gno +++ b/gnovm/tests/files/std8_stdlibs.gno @@ -22,7 +22,7 @@ func main() { } // Stacktrace: -// panic frame not found +// panic: frame not found // callerAt(n) // gonative:std.callerAt // std.GetCallerAt(4) diff --git a/gnovm/tests/files/typeassert1.gno b/gnovm/tests/files/typeassert1.gno index 8bb5b64da8d..f6609a3d18c 100644 --- a/gnovm/tests/files/typeassert1.gno +++ b/gnovm/tests/files/typeassert1.gno @@ -10,7 +10,7 @@ func main() { } // Stacktrace: -// panic interface conversion: interface is nil, not main.A +// panic: interface conversion: interface is nil, not main.A // main() // main/files/typeassert1.gno:9 diff --git a/gnovm/tests/files/typeassert2a.gno b/gnovm/tests/files/typeassert2a.gno index 3bcbc6ad68e..bfbd24d38bd 100644 --- a/gnovm/tests/files/typeassert2a.gno +++ b/gnovm/tests/files/typeassert2a.gno @@ -12,7 +12,7 @@ func main() { } // Stacktrace: -// panic interface conversion: interface is nil, not main.A +// panic: interface conversion: interface is nil, not main.A // main() // main/files/typeassert2a.gno:10 diff --git a/gnovm/tests/files/typeassert9.gno b/gnovm/tests/files/typeassert9.gno index a5e74627f54..6ea072661c1 100644 --- a/gnovm/tests/files/typeassert9.gno +++ b/gnovm/tests/files/typeassert9.gno @@ -17,7 +17,7 @@ func main() { } // Stacktrace: -// panic interface conversion: interface is nil, not main.Writer +// panic: interface conversion: interface is nil, not main.Writer // main() // main/files/typeassert9.gno:16 diff --git a/gnovm/tests/files/zrealm_panic.gno b/gnovm/tests/files/zrealm_panic.gno index e46d7b7ae2a..3864e2a7f7f 100644 --- a/gnovm/tests/files/zrealm_panic.gno +++ b/gnovm/tests/files/zrealm_panic.gno @@ -13,7 +13,7 @@ func main() { } // Stacktrace: -// panic +// panic: panic // ms.Panic() // gno.land/r/test/main.gno:7 // main()