forked from yuin/gopher-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebuglib.go
More file actions
528 lines (469 loc) · 11.2 KB
/
Copy pathdebuglib.go
File metadata and controls
528 lines (469 loc) · 11.2 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package lua
import (
"fmt"
"os"
"strings"
)
// Debug hook constants (Lua 5.3 compatible)
const (
HookMaskCall = 1 << iota // 'c'
HookMaskReturn // 'r'
HookMaskLine // 'l'
HookMaskCount // 'n'
)
// HookEvent represents the type of hook event
type HookEvent int
const (
HookEventCall HookEvent = iota // "call"
HookEventReturn // "return"
HookEventLine // "line"
HookEventCount // "count"
HookEventTailReturn // "tail return"
)
// DebugHook represents a debug hook
type DebugHook struct {
Function *LFunction
Mask int
Count int
}
// callHook calls the debug hook with the given event
func callHook(L *LState, event HookEvent, line int) {
if L.G.Hook == nil {
return
}
// Prevent recursive hook calls
if L.G.InHook {
return
}
L.G.InHook = true
defer func() { L.G.InHook = false }()
// Push hook function
L.Push(L.G.Hook)
// Push event string
var eventStr string
switch event {
case HookEventCall:
eventStr = "call"
case HookEventReturn:
eventStr = "return"
case HookEventLine:
eventStr = "line"
case HookEventCount:
eventStr = "count"
case HookEventTailReturn:
eventStr = "tail return"
}
L.Push(LString(eventStr))
// Push line number (or nil)
if line > 0 {
L.Push(LNumberInt(int64(line)))
} else {
L.Push(LNil)
}
// Call hook (no return values expected)
if err := L.PCall(2, 0, nil); err != nil {
// Ignore hook errors
}
}
func OpenDebug(L *LState) int {
dbgmod := L.RegisterModule(DebugLibName, debugFuncs)
L.Push(dbgmod)
return 1
}
var debugFuncs = map[string]LGFunction{
"debug": debugDebug,
"gethook": debugGetHook,
"getinfo": debugGetInfo,
"getlocal": debugGetLocal,
"getmetatable": debugGetMetatable,
"getregistry": debugGetRegistry,
"getupvalue": debugGetUpvalue,
"getuservalue": debugGetUserValue,
"sethook": debugSetHook,
"setlocal": debugSetLocal,
"setmetatable": debugSetMetatable,
"setupvalue": debugSetUpvalue,
"setuservalue": debugSetUserValue,
"traceback": debugTraceback,
"upvalueid": debugUpvalueID,
"upvaluejoin": debugUpvalueJoin,
}
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.Int64()))
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", LNumberInt(int64(dbg.CurrentLine)))
tbl.RawSetString("nups", LNumberInt(int64(dbg.NUpvalues)))
tbl.RawSetString("linedefined", LNumberInt(int64(dbg.LineDefined)))
tbl.RawSetString("lastlinedefined", LNumberInt(int64(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 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
}
// debug.getuservalue(udata)
// Returns the user value associated with the userdata.
// In Lua 5.3+, this returns any value (not just tables).
func debugGetUserValue(L *LState) int {
ud := L.CheckUserData(1)
// In GopherLua, LUserData has an Env field that serves as uservalue
if ud.Env != nil {
// Check if this is a wrapper table for a non-table value
// Wrapper tables have exactly one element at index 1 and no metatable
if ud.Env.Len() == 1 && ud.Env.Metatable == LNil {
val := ud.Env.RawGetInt(1)
// Return the wrapped value (could be any type including nil)
L.Push(val)
return 1
}
L.Push(ud.Env)
} else {
L.Push(LNil)
}
return 1
}
// debug.setuservalue(udata, value)
// Sets the user value associated with the userdata.
// In Lua 5.3+, the value can be any Lua value (not just tables).
// Returns the userdata.
func debugSetUserValue(L *LState) int {
ud := L.CheckUserData(1)
value := L.CheckAny(2)
// In Lua 5.3+, uservalue can be any value (not just table or nil)
// Store the value directly in the Env field
// For non-table values, we wrap them in a table with a special key
if tb, ok := value.(*LTable); ok {
ud.Env = tb
} else {
// For non-table values, create a wrapper table
// This maintains compatibility with existing code that expects Env to be a table
wrapper := L.NewTable()
wrapper.RawSetInt(1, value) // Store value at index 1
ud.Env = wrapper
}
// Return the userdata
L.Push(ud)
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
}
// debug.debug([prompt[, env]])
// Enters an interactive debug console/REPL.
// In GopherLua, this is a simplified implementation that executes code from stdin.
func debugDebug(L *LState) int {
prompt := L.OptString(1, "debug> ")
// Get environment table (optional second argument)
var env *LTable
if L.GetTop() >= 2 {
if tb, ok := L.Get(2).(*LTable); ok {
env = tb
} else {
L.ArgError(2, "table expected")
return 0
}
}
// Save original environment
originalEnv := L.Env
if env != nil {
L.Env = env
}
// Print welcome message
fmt.Fprintf(os.Stdout, "Lua 5.3 Debug Console (GopherLua)\n")
fmt.Fprintf(os.Stdout, "Type 'exit' or 'quit' to leave, 'help' for help.\n\n")
// Interactive REPL loop
for {
// Print prompt
fmt.Fprintf(os.Stdout, "%s", prompt)
// Read line from stdin
var line string
_, err := fmt.Scanln(&line)
if err != nil {
fmt.Fprintf(os.Stdout, "\n")
break
}
// Check for exit commands
line = strings.TrimSpace(line)
if line == "exit" || line == "quit" || line == "cont" {
break
}
// Check for help command
if line == "help" {
fmt.Fprintf(os.Stdout, "Debug Console Commands:\n")
fmt.Fprintf(os.Stdout, " exit, quit, cont - Exit debug console\n")
fmt.Fprintf(os.Stdout, " help - Show this help\n")
fmt.Fprintf(os.Stdout, " <lua code> - Execute Lua code\n")
fmt.Fprintf(os.Stdout, " = <expr> - Evaluate and print expression\n")
fmt.Fprintf(os.Stdout, "\n")
continue
}
// Check for print shortcut (= expr)
if strings.HasPrefix(line, "=") {
line = "return " + strings.TrimPrefix(line, "=")
}
// Skip empty lines
if line == "" {
continue
}
// Execute the code
if err := L.DoString(line); err != nil {
fmt.Fprintf(os.Stdout, "Error: %v\n", err)
} else {
// Print any return values
top := L.GetTop()
if top > 0 {
for i := 1; i <= top; i++ {
val := L.Get(i)
if i > 1 {
fmt.Fprintf(os.Stdout, "\t")
}
fmt.Fprintf(os.Stdout, "%v", val)
}
fmt.Fprintf(os.Stdout, "\n")
L.SetTop(0)
}
}
}
// Restore original environment
L.Env = originalEnv
return 0
}
// debug.sethook(hook, mask[, count])
func debugSetHook(L *LState) int {
hook := L.Get(1)
// If hook is nil, disable hooks
if hook == LNil {
L.G.Hook = nil
L.G.HookMask = 0
L.G.HookCount = 0
return 0
}
maskStr := L.CheckString(2)
count := L.OptInt(3, 0)
mask := 0
for _, c := range maskStr {
switch c {
case 'c':
mask |= HookMaskCall
case 'r':
mask |= HookMaskReturn
case 'l':
mask |= HookMaskLine
case 'n':
mask |= HookMaskCount
}
}
if fn, ok := hook.(*LFunction); ok {
L.G.Hook = fn
L.G.HookMask = mask
L.G.HookCount = count
} else {
L.ArgError(1, "hook must be a function or nil")
}
return 0
}
// debug.gethook([thread])
func debugGetHook(L *LState) int {
if L.G.Hook == nil {
L.Push(LNil)
} else {
L.Push(L.G.Hook)
}
// Build mask string
mask := ""
if L.G.HookMask&HookMaskCall != 0 {
mask += "c"
}
if L.G.HookMask&HookMaskReturn != 0 {
mask += "r"
}
if L.G.HookMask&HookMaskLine != 0 {
mask += "l"
}
if L.G.HookMask&HookMaskCount != 0 {
mask += "n"
}
L.Push(LString(mask))
L.Push(LNumberInt(int64(L.G.HookCount)))
return 3
}
// debug.getregistry()
func debugGetRegistry(L *LState) int {
L.Push(L.Get(RegistryIndex))
return 1
}
// debug.upvalueid(f, n)
// Returns a unique identifier for the upvalue (like a light userdata in Lua 5.3)
// The identifier is based on the upvalue's address and can be compared for equality
// Works for both Lua functions and C (Go) functions
// Raises an error if the index is out of range (Lua 5.3 behavior)
func debugUpvalueID(L *LState) int {
fn := L.CheckFunction(1)
n := L.CheckInt(2)
if n < 1 {
L.ArgError(2, "invalid upvalue index")
return 0
}
// For Lua functions
if !fn.IsG {
if n > len(fn.Upvalues) {
L.ArgError(2, "invalid upvalue index")
return 0
}
uv := fn.Upvalues[n-1]
if uv == nil {
L.Push(LNil)
return 1
}
// In Lua 5.3, this returns a light userdata with the upvalue's address
// We simulate this by returning a unique string based on the upvalue pointer
L.Push(LString(fmt.Sprintf("upvalue_%p", uv)))
return 1
}
// For C (Go) functions - they can also have upvalues
if n > len(fn.Upvalues) {
L.ArgError(2, "invalid upvalue index")
return 0
}
uv := fn.Upvalues[n-1]
if uv == nil {
L.Push(LNil)
return 1
}
L.Push(LString(fmt.Sprintf("upvalue_%p", uv)))
return 1
}
// debug.upvaluejoin(f1, n1, f2, n2)
func debugUpvalueJoin(L *LState) int {
f1 := L.CheckFunction(1)
n1 := L.CheckInt(2)
f2 := L.CheckFunction(3)
n2 := L.CheckInt(4)
if f1.IsG || f2.IsG {
L.ArgError(1, "Lua function expected")
return 0
}
if n1 < 1 || n1 > len(f1.Upvalues) {
L.ArgError(2, "invalid upvalue index")
return 0
}
if n2 < 1 || n2 > len(f2.Upvalues) {
L.ArgError(4, "invalid upvalue index")
return 0
}
// Share the upvalue
f1.Upvalues[n1-1] = f2.Upvalues[n2-1]
return 0
}