Skip to content

Commit

Permalink
refactor: some suggestion
Browse files Browse the repository at this point in the history
Co-authored-by: Morgan <[email protected]>
  • Loading branch information
omarsy and thehowl committed Jul 24, 2024
1 parent 51202f2 commit 44793f4
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 37 deletions.
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Check warning on line 131 in gnovm/pkg/gnolang/frame.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/frame.go#L129-L131

Added lines #L129 - L131 were not covered by tests
}
Expand Down
41 changes: 23 additions & 18 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func (m *Machine) Stacktrace() (stacktrace Stacktrace) {
return

Check warning on line 466 in gnovm/pkg/gnolang/machine.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/machine.go#L466

Added line #L466 was not covered by tests
}

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() {
Expand All @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/panic0.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ func main() {
}

// Stacktrace:
// panic wtf
// panic: wtf
// main<VPBlock(1,0)>()
// main/files/panic0.gno:4

Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/panic0a.gno
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
}

// Stacktrace:
// panic wtf
// panic: wtf
// f<VPBlock(3,1)>([](const-type string)<len=2>,S<VPBlock(3,0)><len=1>,map[(const-type string)] (const-type string)<len=2>,func(s (const-type string)) (const-type string){ ... })
// main/files/panic0a.gno:8
// main<VPBlock(1,2)>()
Expand Down
4 changes: 2 additions & 2 deletions gnovm/tests/files/panic0b.gno
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func f() {
}

// Stacktrace:
// panic first
// panic: first
// f<VPBlock(3,1)>()
// main/files/panic0b.gno:14
// main<VPBlock(1,0)>()
// main/files/panic0b.gno:4
// ... 1 panic(s) elided ...
// panic third
// panic: third
// f<VPBlock(3,1)>()
// main/files/panic0b.gno:9
// main<VPBlock(1,0)>()
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/panic0c.gno
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
}

// Stacktrace:
// panic wtf
// panic: wtf
// f<VPBlock(3,1)>([]string<len=2>,main.S,map[string]string<len=2>,func(s string)( string))
// main/files/panic0c.gno:8
// main<VPBlock(1,2)>()
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/panic1.gno
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
}

// Stacktrace:
// panic here
// panic: here
// main<VPBlock(1,2)>()
// main/files/panic1.gno:22

Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/panic2a.gno
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func main() {
}

// Stacktrace:
// panic here
// panic: here
// p<VPBlock(3,0)>(i<VPBlock(1,0)> + 1)
// main/files/panic2a.gno:5
// p<VPBlock(3,0)>(i<VPBlock(1,0)> + 1)
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/panic2b.gno
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
}

// Stacktrace:
// panic here
// panic: here

// Error:
// here
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/recover10.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func main() {
}

// Stacktrace:
// panic ahhhhh
// panic: ahhhhh
// main<VPBlock(1,0)>()
// main/files/recover10.gno:6

Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/recover1b.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {
}

// Stacktrace:
// panic other panic
// panic: other panic
// main<VPBlock(1,0)>()
// main/files/recover1b.gno:8

Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/recover8.gno
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
}

// Stacktrace:
// panic do something panic
// panic: do something panic
// doSomething<VPBlock(3,0)>()
// main/files/recover8.gno:7
// main<VPBlock(1,2)>()
Expand Down
3 changes: 1 addition & 2 deletions gnovm/tests/files/std5_stdlibs.gno
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ func main() {
}

// Stacktrace:

// panic frame not found
// panic: frame not found
// callerAt<VPBlock(3,44)>(n<VPBlock(1,0)>)
// gonative:std.callerAt
// std<VPBlock(2,0)>.GetCallerAt(2)
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/std8_stdlibs.gno
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
}

// Stacktrace:
// panic frame not found
// panic: frame not found
// callerAt<VPBlock(3,44)>(n<VPBlock(1,0)>)
// gonative:std.callerAt
// std<VPBlock(2,0)>.GetCallerAt(4)
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/typeassert1.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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<VPBlock(1,1)>()
// main/files/typeassert1.gno:9

Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/typeassert2a.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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<VPBlock(1,1)>()
// main/files/typeassert2a.gno:10

Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/typeassert9.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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<VPBlock(1,2)>()
// main/files/typeassert9.gno:16

Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/zrealm_panic.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
}

// Stacktrace:
// panic
// panic: panic
// ms<VPBlock(1,0)>.Panic()
// gno.land/r/test/main.gno:7
// main<VPBlock(1,1)>()
Expand Down

0 comments on commit 44793f4

Please sign in to comment.