Skip to content

Commit e69a9b4

Browse files
committed
Add an 'IncludeGoStackTrace' option
1 parent 3d4297f commit e69a9b4

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

_state.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ type Options struct {
9191
RegistrySize int
9292
// Controls whether or not libraries are opened by default
9393
SkipOpenLibs bool
94+
// Tells whether a Go stacktrace should be included in a Lua stacktrace when panics occur.
95+
IncludeGoStackTrace bool
9496
}
9597

9698
/* }}} */
@@ -1523,10 +1525,12 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
15231525
rcv := recover()
15241526
if rcv != nil {
15251527
if _, ok := rcv.(*ApiError); !ok {
1526-
buf := make([]byte, 4096)
1527-
runtime.Stack(buf, false)
15281528
err = newApiErrorS(ApiErrorPanic, fmt.Sprint(rcv))
1529-
err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000")
1529+
if ls.Options.IncludeGoStackTrace {
1530+
buf := make([]byte, 4096)
1531+
runtime.Stack(buf, false)
1532+
err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000") + "\n" + ls.stackTrace(true)
1533+
}
15301534
} else {
15311535
err = rcv.(*ApiError)
15321536
}
@@ -1539,10 +1543,12 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
15391543
rcv := recover()
15401544
if rcv != nil {
15411545
if _, ok := rcv.(*ApiError); !ok {
1542-
buf := make([]byte, 4096)
1543-
runtime.Stack(buf, false)
15441546
err = newApiErrorS(ApiErrorPanic, fmt.Sprint(rcv))
1545-
err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000")
1547+
if ls.Options.IncludeGoStackTrace {
1548+
buf := make([]byte, 4096)
1549+
runtime.Stack(buf, false)
1550+
err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000") + ls.stackTrace(true)
1551+
}
15461552
} else {
15471553
err = rcv.(*ApiError)
15481554
err.(*ApiError).StackTrace = ls.stackTrace(true)
@@ -1551,7 +1557,7 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
15511557
}()
15521558
ls.Call(1, 1)
15531559
err = newApiError(ApiErrorError, ls.Get(-1))
1554-
} else {
1560+
} else if len(err.(*ApiError).StackTrace) == 0 {
15551561
err.(*ApiError).StackTrace = ls.stackTrace(true)
15561562
}
15571563
ls.reg.SetTop(base)

state.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ type Options struct {
9292
RegistrySize int
9393
// Controls whether or not libraries are opened by default
9494
SkipOpenLibs bool
95+
// Tells whether a Go stacktrace should be included in a Lua stacktrace when panics occur.
96+
IncludeGoStackTrace bool
9597
}
9698

9799
/* }}} */
@@ -1605,10 +1607,12 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
16051607
rcv := recover()
16061608
if rcv != nil {
16071609
if _, ok := rcv.(*ApiError); !ok {
1608-
buf := make([]byte, 4096)
1609-
runtime.Stack(buf, false)
16101610
err = newApiErrorS(ApiErrorPanic, fmt.Sprint(rcv))
1611-
err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000")
1611+
if ls.Options.IncludeGoStackTrace {
1612+
buf := make([]byte, 4096)
1613+
runtime.Stack(buf, false)
1614+
err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000") + "\n" + ls.stackTrace(true)
1615+
}
16121616
} else {
16131617
err = rcv.(*ApiError)
16141618
}
@@ -1621,10 +1625,12 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
16211625
rcv := recover()
16221626
if rcv != nil {
16231627
if _, ok := rcv.(*ApiError); !ok {
1624-
buf := make([]byte, 4096)
1625-
runtime.Stack(buf, false)
16261628
err = newApiErrorS(ApiErrorPanic, fmt.Sprint(rcv))
1627-
err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000")
1629+
if ls.Options.IncludeGoStackTrace {
1630+
buf := make([]byte, 4096)
1631+
runtime.Stack(buf, false)
1632+
err.(*ApiError).StackTrace = strings.Trim(string(buf), "\000") + ls.stackTrace(true)
1633+
}
16281634
} else {
16291635
err = rcv.(*ApiError)
16301636
err.(*ApiError).StackTrace = ls.stackTrace(true)
@@ -1633,7 +1639,7 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
16331639
}()
16341640
ls.Call(1, 1)
16351641
err = newApiError(ApiErrorError, ls.Get(-1))
1636-
} else {
1642+
} else if len(err.(*ApiError).StackTrace) == 0 {
16371643
err.(*ApiError).StackTrace = ls.stackTrace(true)
16381644
}
16391645
ls.reg.SetTop(base)

0 commit comments

Comments
 (0)