Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion _glua-tests/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,52 @@ assert(string.match("あいうえお", ".*あ.*") == "あいうえお")
assert(string.match("あいうえお", "あいうえお") == "あいうえお")

-- issue 47
assert(string.gsub("A\nA", ".", "A") == "AAA")
assert(string.gsub("A\nA", ".", "A") == "AAA")

-- issue 62
local function level4() error("error!") end
local function level3() level4() end
local function level2() level3() end
local function level1() level2() end
local ok, result = xpcall(level1, function(err)
return debug.traceback("msg", 10)
end)
assert(result == [[msg
stack traceback:]])
ok, result = xpcall(level1, function(err)
return debug.traceback("msg", 9)
end)
assert(result == string.gsub([[msg
stack traceback:
@TAB@[G]: ?]], "@TAB@", "\t"))
local ok, result = xpcall(level1, function(err)
return debug.traceback("msg", 0)
end)

assert(result == string.gsub([[msg
stack traceback:
@TAB@[G]: in function 'traceback'
@TAB@issues.lua:87: in function <issues.lua:86>
@TAB@[G]: in function 'error'
@TAB@issues.lua:71: in function 'level4'
@TAB@issues.lua:72: in function 'level3'
@TAB@issues.lua:73: in function 'level2'
@TAB@issues.lua:74: in function <issues.lua:74>
@TAB@[G]: in function 'xpcall'
@TAB@issues.lua:86: in main chunk
@TAB@[G]: ?]], "@TAB@", "\t"))

local ok, result = xpcall(level1, function(err)
return debug.traceback("msg", 3)
end)

assert(result == string.gsub([[msg
stack traceback:
@TAB@issues.lua:71: in function 'level4'
@TAB@issues.lua:72: in function 'level3'
@TAB@issues.lua:73: in function 'level2'
@TAB@issues.lua:74: in function <issues.lua:74>
@TAB@[G]: in function 'xpcall'
@TAB@issues.lua:103: in main chunk
@TAB@[G]: ?]], "@TAB@", "\t"))

27 changes: 18 additions & 9 deletions _state.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ func newLState(options Options) *LState {
currentFrame: nil,
wrapped: false,
uvcache: nil,
hasErrorFunc: false,
}
ls.Env = ls.G.Global
return ls
Expand Down Expand Up @@ -379,7 +380,9 @@ func (ls *LState) closeAllUpvalues() { // +inline-start
} // +inline-end

func (ls *LState) raiseError(level int, format string, args ...interface{}) {
ls.closeAllUpvalues()
if !ls.hasErrorFunc {
ls.closeAllUpvalues()
}
message := format
if len(args) > 0 {
message = fmt.Sprintf(format, args...)
Expand Down Expand Up @@ -453,11 +456,11 @@ func (ls *LState) stackTrace(include bool) string {
}
}
buf = append(buf, fmt.Sprintf("\t%v: %v", "[G]", "?"))
if len(buf) > 10 {
if len(buf) > 20 {
newbuf := make([]string, 0, 20)
newbuf = append(newbuf, buf[0:7]...)
newbuf = append(newbuf, "\t...")
newbuf = append(newbuf, buf[len(buf)-7:len(buf)-1]...)
newbuf = append(newbuf, buf[len(buf)-7:len(buf)]...)
buf = newbuf
}
ret := strings.Join(buf, "\n")
Expand All @@ -467,12 +470,12 @@ func (ls *LState) stackTrace(include bool) string {
func (ls *LState) formattedFrameFuncName(fr *callFrame) string {
name, ischunk := ls.frameFuncName(fr)
if ischunk {
if name[0] != '(' && name[0] != '<' {
return fmt.Sprintf("function '%s'", name)
}
return fmt.Sprintf("function %s", name)
return name
}
return name
if name[0] != '(' && name[0] != '<' {
return fmt.Sprintf("function '%s'", name)
}
return fmt.Sprintf("function %s", name)
}

func (ls *LState) rawFrameFuncName(fr *callFrame) string {
Expand Down Expand Up @@ -1225,7 +1228,9 @@ func (ls *LState) Error(lv LValue, level int) {
if str, ok := lv.(LString); ok {
ls.raiseError(level, string(str))
} else {
ls.closeAllUpvalues()
if !ls.hasErrorFunc {
ls.closeAllUpvalues()
}
ls.Push(lv)
ls.Panic(ls)
}
Expand Down Expand Up @@ -1520,8 +1525,12 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
base := ls.reg.Top() - nargs - 1
oldpanic := ls.Panic
ls.Panic = panicWithoutTraceback
if errfunc != nil {
ls.hasErrorFunc = true
}
defer func() {
ls.Panic = oldpanic
ls.hasErrorFunc = false
rcv := recover()
if rcv != nil {
if _, ok := rcv.(*ApiError); !ok {
Expand Down
12 changes: 9 additions & 3 deletions debuglib.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lua

import (
"fmt"
"strings"
)

func OpenDebug(L *LState) int {
Expand Down Expand Up @@ -151,10 +152,15 @@ func debugSetUpvalue(L *LState) int {

func debugTraceback(L *LState) int {
msg := L.OptString(1, "")
level := L.OptInt(2, 1)
stacktrace := strings.TrimSpace(L.stackTrace(true))
lines := strings.Split(stacktrace, "\n")
header := lines[0]
lst := lines[intMax(intMin(len(lines)-1, level), 0)+1 : len(lines)]
traceback := fmt.Sprintf("%s\n%s", header, strings.Join(lst, "\n"))
if len(msg) > 0 {
L.Push(LString(fmt.Sprintf("%s\n%s\n", msg, L.stackTrace(false))))
} else {
L.Push(LString(L.stackTrace(false)))
traceback = fmt.Sprintf("%s\n%s", msg, traceback)
}
L.Push(LString(strings.TrimSpace(traceback)))
return 1
}
27 changes: 18 additions & 9 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ func newLState(options Options) *LState {
currentFrame: nil,
wrapped: false,
uvcache: nil,
hasErrorFunc: false,
}
ls.Env = ls.G.Global
return ls
Expand Down Expand Up @@ -383,7 +384,9 @@ func (ls *LState) closeAllUpvalues() { // +inline-start
} // +inline-end

func (ls *LState) raiseError(level int, format string, args ...interface{}) {
ls.closeAllUpvalues()
if !ls.hasErrorFunc {
ls.closeAllUpvalues()
}
message := format
if len(args) > 0 {
message = fmt.Sprintf(format, args...)
Expand Down Expand Up @@ -457,11 +460,11 @@ func (ls *LState) stackTrace(include bool) string {
}
}
buf = append(buf, fmt.Sprintf("\t%v: %v", "[G]", "?"))
if len(buf) > 10 {
if len(buf) > 20 {
newbuf := make([]string, 0, 20)
newbuf = append(newbuf, buf[0:7]...)
newbuf = append(newbuf, "\t...")
newbuf = append(newbuf, buf[len(buf)-7:len(buf)-1]...)
newbuf = append(newbuf, buf[len(buf)-7:len(buf)]...)
buf = newbuf
}
ret := strings.Join(buf, "\n")
Expand All @@ -471,12 +474,12 @@ func (ls *LState) stackTrace(include bool) string {
func (ls *LState) formattedFrameFuncName(fr *callFrame) string {
name, ischunk := ls.frameFuncName(fr)
if ischunk {
if name[0] != '(' && name[0] != '<' {
return fmt.Sprintf("function '%s'", name)
}
return fmt.Sprintf("function %s", name)
return name
}
return name
if name[0] != '(' && name[0] != '<' {
return fmt.Sprintf("function '%s'", name)
}
return fmt.Sprintf("function %s", name)
}

func (ls *LState) rawFrameFuncName(fr *callFrame) string {
Expand Down Expand Up @@ -1310,7 +1313,9 @@ func (ls *LState) Error(lv LValue, level int) {
if str, ok := lv.(LString); ok {
ls.raiseError(level, string(str))
} else {
ls.closeAllUpvalues()
if !ls.hasErrorFunc {
ls.closeAllUpvalues()
}
ls.Push(lv)
ls.Panic(ls)
}
Expand Down Expand Up @@ -1605,8 +1610,12 @@ func (ls *LState) PCall(nargs, nret int, errfunc *LFunction) (err error) {
base := ls.reg.Top() - nargs - 1
oldpanic := ls.Panic
ls.Panic = panicWithoutTraceback
if errfunc != nil {
ls.hasErrorFunc = true
}
defer func() {
ls.Panic = oldpanic
ls.hasErrorFunc = false
rcv := recover()
if rcv != nil {
if _, ok := rcv.(*ApiError); !ok {
Expand Down
1 change: 1 addition & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ type LState struct {
currentFrame *callFrame
wrapped bool
uvcache *Upvalue
hasErrorFunc bool
}

func (ls *LState) String() string { return fmt.Sprintf("thread: %p", ls) }
Expand Down