forked from yuin/gopher-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debuglib.go
173 lines (158 loc) · 3.33 KB
/
debuglib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package lua
import (
"fmt"
"strings"
)
func OpenDebug(L *LState) int {
dbgmod := L.RegisterModule(DebugLibName, debugFuncs)
L.Push(dbgmod)
return 1
}
var debugFuncs = map[string]LGFunction{
"getfenv": debugGetFEnv,
"getinfo": debugGetInfo,
"getlocal": debugGetLocal,
"getmetatable": debugGetMetatable,
"getupvalue": debugGetUpvalue,
"setfenv": debugSetFEnv,
"setlocal": debugSetLocal,
"setmetatable": debugSetMetatable,
"setupvalue": debugSetUpvalue,
"traceback": debugTraceback,
}
func debugGetFEnv(L *LState) int {
L.Push(L.GetFEnv(L.CheckAny(1)))
return 1
}
func debugGetInfo(L *LState) int {
L.CheckTypes(1, LTFunction, LTNumber)
arg1 := L.Get(1)
what := L.OptString(2, "Slunf")
var dbg *Debug
var fn LValue
var err error
var ok bool
switch lv := arg1.(type) {
case *LFunction:
dbg = &Debug{}
fn, err = L.GetInfo(">"+what, dbg, lv)
case LNumber:
dbg, ok = L.GetStack(int(lv))
if !ok {
L.Push(LNil)
return 1
}
fn, err = L.GetInfo(what, dbg, LNil)
}
if err != nil {
L.Push(LNil)
return 1
}
tbl := L.NewTable()
if len(dbg.Name) > 0 {
tbl.RawSetString("name", LString(dbg.Name))
} else {
tbl.RawSetString("name", LNil)
}
tbl.RawSetString("what", LString(dbg.What))
tbl.RawSetString("source", LString(dbg.Source))
tbl.RawSetString("currentline", LNumber(dbg.CurrentLine))
tbl.RawSetString("nups", LNumber(dbg.NUpvalues))
tbl.RawSetString("linedefined", LNumber(dbg.LineDefined))
tbl.RawSetString("lastlinedefined", LNumber(dbg.LastLineDefined))
tbl.RawSetString("func", fn)
L.Push(tbl)
return 1
}
func debugGetLocal(L *LState) int {
level := L.CheckInt(1)
idx := L.CheckInt(2)
dbg, ok := L.GetStack(level)
if !ok {
L.ArgError(1, "level out of range")
}
name, value := L.GetLocal(dbg, idx)
if len(name) > 0 {
L.Push(LString(name))
L.Push(value)
return 2
}
L.Push(LNil)
return 1
}
func debugGetMetatable(L *LState) int {
L.Push(L.GetMetatable(L.CheckAny(1)))
return 1
}
func debugGetUpvalue(L *LState) int {
fn := L.CheckFunction(1)
idx := L.CheckInt(2)
name, value := L.GetUpvalue(fn, idx)
if len(name) > 0 {
L.Push(LString(name))
L.Push(value)
return 2
}
L.Push(LNil)
return 1
}
func debugSetFEnv(L *LState) int {
L.SetFEnv(L.CheckAny(1), L.CheckAny(2))
return 0
}
func debugSetLocal(L *LState) int {
level := L.CheckInt(1)
idx := L.CheckInt(2)
value := L.CheckAny(3)
dbg, ok := L.GetStack(level)
if !ok {
L.ArgError(1, "level out of range")
}
name := L.SetLocal(dbg, idx, value)
if len(name) > 0 {
L.Push(LString(name))
} else {
L.Push(LNil)
}
return 1
}
func debugSetMetatable(L *LState) int {
L.CheckTypes(2, LTNil, LTTable)
obj := L.Get(1)
mt := L.Get(2)
L.SetMetatable(obj, mt)
L.SetTop(1)
return 1
}
func debugSetUpvalue(L *LState) int {
fn := L.CheckFunction(1)
idx := L.CheckInt(2)
value := L.CheckAny(3)
name := L.SetUpvalue(fn, idx, value)
if len(name) > 0 {
L.Push(LString(name))
} else {
L.Push(LNil)
}
return 1
}
func debugTraceback(L *LState) int {
msg := ""
level := L.OptInt(2, 1)
ls := L
if L.GetTop() > 0 {
if s, ok := L.Get(1).(LString); ok {
msg = string(s)
}
if l, ok := L.Get(1).(*LState); ok {
ls = l
msg = ""
}
}
traceback := strings.TrimSpace(ls.stackTrace(level))
if len(msg) > 0 {
traceback = fmt.Sprintf("%s\n%s", msg, traceback)
}
L.Push(LString(traceback))
return 1
}