This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
luadebug.pir
455 lines (342 loc) · 10.5 KB
/
luadebug.pir
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
# Copyright (C) 2006-2009, Parrot Foundation.
# $Id$
=head1 Lua Debug Library
=head2 Description
This library provides the functionality of the debug interface to Lua programs.
You should exert care when using this library. The functions provided here
should be used exclusively for debugging and similar tasks, such as profiling.
Please resist the temptation to use them as a usual programming tool:
They can be very slow.
Moreover, several of its functions violate some assumptions about Lua code
(e.g., that variables local to a function cannot be accessed from outside
or that userdata metatables cannot be changed by Lua code) and therefore can
compromise otherwise secure code.
All functions in this library are provided inside the C<debug> table.
See "Lua 5.1 Reference Manual", section 5.9 "The Debug Library",
L<http://www.lua.org/manual/5.1/manual.html#5.9>.
=head3 Functions
=over 4
=cut
.HLL 'lua'
.loadlib 'lua_group'
.namespace [ 'debug' ]
.sub 'luaopen_debug'
# print "init Lua Debug\n"
.local pmc _lua__GLOBAL
_lua__GLOBAL = get_hll_global '_G'
new $P1, 'LuaString'
.local pmc _debug
new _debug, 'LuaTable'
set $P1, 'debug'
_lua__GLOBAL[$P1] = _debug
$P2 = split "\n", <<'LIST'
debug
getfenv
gethook
getinfo
getlocal
getmetatable
getregistry
getupvalue
setfenv
sethook
setlocal
setmetatable
setupvalue
traceback
LIST
lua_register($P1, _debug, $P2)
.end
=item C<debug.debug ()>
Enters an interactive mode with the user, running each string that the user
enters. Using simple commands and other debug facilities, the user can
inspect global and local variables, change their values, evaluate expressions,
and so on. A line containing only the word C<cont> finishes this function,
so that the caller continues its execution.
Note that commands for C<debug.debug> are not lexically nested within any
function, and so have no direct access to local variables.
=cut
.include 'stdio.pasm'
.sub 'debug'
.param pmc extra :slurpy
.local string buffer
.local pmc stdin
$P0 = getinterp
stdin = $P0.'stdhandle'(.PIO_STDIN_FILENO)
L1:
printerr 'lua_debug> '
buffer = readline stdin
if buffer == '' goto L2
$I0 = index buffer, "cont"
if $I0 == 0 goto L2
($P0, $S0) = lua_loadbuffer(buffer, '=(debug command)')
unless null $P0 goto L3
printerr $S0
printerr "\n"
goto L1
L3:
push_eh _handler
$P0()
pop_eh
goto L1
_handler:
.local pmc e
.local string msg
.get_results (e)
msg = e
printerr msg
printerr "\n"
goto L1
L2:
.end
=item C<debug.getfenv (o)>
Returns the environment of object C<o>.
=cut
.sub 'getfenv'
.param pmc o :optional
.param pmc extra :slurpy
.tailcall lua_getfenv(o)
.end
=item C<debug.gethook ()>
Returns the current hook settings, as three values: the current hook function,
the current hook mask, and the current hook count (as set by the
C<debug.sethook> function).
NOT YET IMPLEMENTED.
=cut
.sub 'gethook'
not_implemented()
.end
=item C<debug.getinfo (function [, what])>
Returns a table with information about a function. You can give the function
directly, or you can give a number as the value of C<function>, which means
the function running at level C<function> of the call stack: Level 0 is the
current function (C<getinfo> itself); level 1 is the function that called
C<getinfo>; and so on. If C<function> is a number larger than the number of
active functions, then C<getinfo> returns B<nil>.
The returned table contains all the fields returned by C<lua_getinfo>, with
the string C<what> describing which fields to fill in. The default for
C<what> is to get all information available. If present, the option `C<f>´
adds a field named C<func> with the function itself.
For instance, the expression C<debug.getinfo(1,"n").name> returns a name of
the current function, if a reasonable name can be found, and
C<debug.getinfo(print)> returns a table with all available information about
the C<print> function.
STILL INCOMPLETE.
=cut
.sub 'getinfo'
.param pmc func :optional
.param pmc what :optional
.param pmc extra :slurpy
$S2 = lua_optstring(2, what, 'flnSu')
$I0 = isa func, 'LuaNumber'
unless $I0 goto L1
$I1 = func
$P0 = getinterp
push_eh _handler
func = $P0['sub'; $I1]
pop_eh
goto L2
L1:
$I0 = isa func, 'LuaFunction'
if $I0 goto L2
lua_argerror(1, "function or level expected")
L2:
.local pmc res
res = new 'LuaTable'
$P1 = new 'LuaString'
# S : source, short_src, linedefined, lastlinedefined, what
# l : currentline
# u : nups
# n : name, namewhat
# L : activelines
# f : func
$I0 = index $S2, 'f'
if $I0 < 0 goto L3
set $P1, 'func'
res[$P1] = func
L3:
.return (res)
_handler:
res = new 'LuaNil'
.return (res)
.end
=item C<debug.getlocal (level, local)>
This function returns the name and the value of the local variable with index
C<local> of the function at level C<level> of the stack. (The first parameter
or local variable has index 1, and so on, until the last active local
variable.) The function returns B<nil> if there is no local variable with the
given index, and raises an error when called with a C<level> out of range.
(You can call C<debug.getinfo> to check whether the level is valid.)
Variable names starting with `C<(>´ (open parentheses) represent internal
variables (loop control variables, temporaries, and C function locals).
NOT YET IMPLEMENTED.
=cut
.sub 'getlocal'
not_implemented()
.end
=item C<debug.getmetatable (object)>
Returns the metatable of the given C<object> or B<nil> if it does not have a
metatable.
=cut
.sub 'getmetatable'
.param pmc obj :optional
.param pmc extra :slurpy
.local pmc res
lua_checkany(1, obj)
res = obj.'get_metatable'()
.return (res)
.end
=item C<debug.getregistry ()>
Returns the registry table.
=cut
.sub 'getregistry'
.param pmc extra :slurpy
.local pmc res
res = get_hll_global '_REGISTRY'
.return (res)
.end
=item C<debug.getupvalue (func, up)>
This function returns the name and the value of the upvalue with index C<up>
of the function C<func>. The function returns B<nil> if there is no upvalue
with the given index.
NOT YET IMPLEMENTED.
=cut
.sub 'getupvalue'
not_implemented()
.end
=item C<debug.setfenv (object, table)>
Sets the environment of the given C<object> to the given C<table>.
=cut
.sub 'setfenv'
.param pmc o :optional
.param pmc table :optional
.param pmc extra :slurpy
lua_checktype(2, table, 'table')
$I0 = lua_setfenv(o, table)
unless $I0 goto L1
.return (o)
L1:
lua_error("'setfenv' cannot change environment of given object")
.end
=item C<debug.sethook (hook, mask [, count])>
Sets the given function as a hook. The string C<mask> and the number C<count>
describe when the hook will be called. The string mask may have the following
characters, with the given meaning:
=over 4
=item C<"c">
The hook is called every time Lua calls a function;
=item C<"r">
The hook is called every time Lua returns from a function;
=item C<"l">
The hook is called every time Lua enters a new line of code.
=back
With a C<count> different from zero, the hook is called after every C<count>
instructions.
When called without arguments, C<debug.sethook> turns off the hook.
When the hook is called, its first parameter is a string describing the event
that has triggered its call: C<"call">, C<"return"> (or C<"tail return">),
C<"line">, and C<"count">. For line events, the hook also gets the new line
number as its second parameter. Inside a hook, you can call C<getinfo> with
level 2 to get more information about the running function (level 0 is the
C<getinfo> function, and level 1 is the hook function), unless the event is
C<"tail return">. In this case, Lua is only simulating the return, and a call
to C<getinfo> will return invalid data.
NOT YET IMPLEMENTED.
=cut
.sub 'sethook'
not_implemented()
.end
=item C<debug.setlocal (level, local, value)>
This function assigns the value C<value> to the local variable with index
C<local> of the function at level C<level> of the stack. The function returns
B<nil> if there is no local variable with the given index, and raises an error
when called with a C<level> out of range. (You can call C<getinfo> to check
whether the level is valid.) Otherwise, it returns the name of the local
variable.
NOT YET IMPLEMENTED.
=cut
.sub 'setlocal'
not_implemented()
.end
=item C<debug.setmetatable (object, table)>
Sets the metatable for the given C<object> to the given C<table> (which can
be B<nil>).
=cut
.sub 'setmetatable'
.param pmc table :optional
.param pmc metatable :optional
.param pmc extra :slurpy
.local pmc res
lua_checktype(1, table, 'table')
if null metatable goto L1
$I0 = isa metatable, 'LuaNil'
if $I0 goto L2
$I0 = isa metatable, 'LuaTable'
if $I0 goto L2
L1:
lua_argerror(2, "nil or table expected")
L2:
table.'set_metatable'(metatable)
new res, 'LuaBoolean'
set res, 1
.return (res)
.end
=item C<debug.setupvalue (func, up, value)>
This function assigns the value C<value> to the upvalue with index C<up> of
the function C<func>. The function returns B<nil> if there is no upvalue with
the given index. Otherwise, it returns the name of the upvalue.
NOT YET IMPLEMENTED.
=cut
.sub 'setupvalue'
not_implemented()
.end
=item C<debug.traceback ([message] [, level])>
Returns a string with a traceback of the call stack. An optional C<message>
string is appended at the beginning of the traceback. This function is
typically used with C<xpcall> to produce better error messages.
STILL INCOMPLETE.
=cut
.sub 'traceback'
.param pmc message :optional
.param pmc level :optional
.param pmc extra :slurpy
.local pmc res
$S1 = lua_optstring(1, message, '')
$I2 = lua_optint(2, level, 1)
unless $S1 goto L1
$S1 .= "\n"
L1:
$P0 = _traceback($I2)
$S0 = str_traceback($P0)
$S1 .= $S0
new res, 'LuaString'
set res, $S1
.return (res)
.end
.sub '_traceback' :anon
.param int level
$P0 = getinterp
.local pmc res, hash, sub, annos
new res, 'ResizablePMCArray'
$I0 = 0
L1:
inc $I0
push_eh _handler
sub = $P0['sub'; $I0]
annos = $P0['annotations'; $I0]
pop_eh
new hash, 'Hash'
hash['sub'] = sub
hash['annotations'] = annos
push res, hash
goto L1
_handler:
.return (res)
.end
=back
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: