Skip to content

Commit

Permalink
PCall 仅在 REPL 输出错误
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 3, 2022
1 parent abcf643 commit e30dba6
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api/lua_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type BasicAPI interface {
/* 'load' and 'call' functions (load and run Lua code) */
Load(chunk []byte, chunkName, mode string) int
Call(nArgs, nResults int)
PCall(nArgs, nResults, msgh int) int
PCall(nArgs, nResults, msgh int, print bool) int
/* miscellaneous functions */
Len(idx int)
Concat(n int)
Expand Down
2 changes: 1 addition & 1 deletion repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func repl() {

// 加载line,调用
protectedLoadString(&ls, cmd)
ls.PCall(0, -1, 0)
ls.PCall(0, -1, 0, true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion state/api_arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (self *luaState) Arith(op ArithOp) {

self.stack.push(a)
self.stack.push(b)
panic(fmt.Sprintf("invalid arith: %T<%#v> %s %T<%#v>", a, a, opSymbol(mm), b, b))
panic(fmt.Sprintf("invalid arith: %T %s %T", a, opSymbol(mm), b))
}

func _arith(a, b any, op operator) any {
Expand Down
6 changes: 4 additions & 2 deletions state/api_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (self *luaState) runLuaClosure() {

// Calls a function in protected mode.
// http://www.lua.org/manual/5.3/manual.html#lua_pcall
func (self *luaState) PCall(nArgs, nResults, msgh int) (status int) {
func (self *luaState) PCall(nArgs, nResults, msgh int, print bool) (status int) {
caller := self.stack
status = LUA_ERRRUN

Expand All @@ -139,7 +139,9 @@ func (self *luaState) PCall(nArgs, nResults, msgh int) (status int) {
self.popLuaStack()
}
self.stack.push(err)
fmt.Printf("%v\n", err)
if print {
fmt.Printf("%v\n", err)
}
}
}()

Expand Down
2 changes: 1 addition & 1 deletion state/api_coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (self *luaState) Resume(from LkState, nArgs int) int {
self.coChan = make(chan int)
self.coCaller = lsFrom
go func() {
self.coStatus = self.PCall(nArgs, -1, 0)
self.coStatus = self.PCall(nArgs, -1, 0, false)
lsFrom.coChan <- 1
}()
} else {
Expand Down
4 changes: 2 additions & 2 deletions state/auxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ func (self *luaState) OptBool(arg int, def bool) bool {
// http://www.lua.org/manual/5.3/manual.html#luaL_dofile
func (self *luaState) DoFile(filename string) bool {
return self.LoadFile(filename) != LUA_OK ||
self.PCall(0, LUA_MULTRET, 0) != LUA_OK
self.PCall(0, LUA_MULTRET, 0, false) != LUA_OK
}

// [-0, +?, –]
// http://www.lua.org/manual/5.3/manual.html#luaL_dostring
func (self *luaState) DoString(str, source string) bool {
return self.LoadString(str, source) != LUA_OK ||
self.PCall(0, LUA_MULTRET, 0) != LUA_OK
self.PCall(0, LUA_MULTRET, 0, false) != LUA_OK
}

// [-0, +1, m]
Expand Down
2 changes: 1 addition & 1 deletion stdlib/lib_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func baseDoFile(ls LkState) int {
// http://www.lua.org/manual/5.3/manual.html#pdf-pcall
func basePCall(ls LkState) int {
nArgs := ls.GetTop() - 1
status := ls.PCall(nArgs, -1, 0)
status := ls.PCall(nArgs, -1, 0, false)
ls.PushBoolean(status == LUA_OK)
ls.Insert(1)
return ls.GetTop()
Expand Down

0 comments on commit e30dba6

Please sign in to comment.