@@ -91,6 +91,8 @@ type Options struct {
91
91
RegistrySize int
92
92
// Controls whether or not libraries are opened by default
93
93
SkipOpenLibs bool
94
+ // Tells whether a Go stacktrace should be included in a Lua stacktrace when panics occur.
95
+ IncludeGoStackTrace bool
94
96
}
95
97
96
98
/* }}} */
@@ -1523,10 +1525,12 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
1523
1525
rcv := recover ()
1524
1526
if rcv != nil {
1525
1527
if _ , ok := rcv .(* ApiError ); ! ok {
1526
- buf := make ([]byte , 4096 )
1527
- runtime .Stack (buf , false )
1528
1528
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
+ }
1530
1534
} else {
1531
1535
err = rcv .(* ApiError )
1532
1536
}
@@ -1539,10 +1543,12 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
1539
1543
rcv := recover ()
1540
1544
if rcv != nil {
1541
1545
if _ , ok := rcv .(* ApiError ); ! ok {
1542
- buf := make ([]byte , 4096 )
1543
- runtime .Stack (buf , false )
1544
1546
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
+ }
1546
1552
} else {
1547
1553
err = rcv .(* ApiError )
1548
1554
err .(* ApiError ).StackTrace = ls .stackTrace (true )
@@ -1551,7 +1557,7 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
1551
1557
}()
1552
1558
ls .Call (1 , 1 )
1553
1559
err = newApiError (ApiErrorError , ls .Get (- 1 ))
1554
- } else {
1560
+ } else if len ( err .( * ApiError ). StackTrace ) == 0 {
1555
1561
err .(* ApiError ).StackTrace = ls .stackTrace (true )
1556
1562
}
1557
1563
ls .reg .SetTop (base )
0 commit comments