@@ -45,12 +45,13 @@ export interface Vars {
45
45
[ name : string ] : Var | undefined ;
46
46
}
47
47
48
- export interface Local extends Var {
48
+ export interface IndexedVar extends Var {
49
49
index : number ;
50
50
}
51
51
52
52
export interface Locals {
53
- [ name : string ] : Local ;
53
+ vars : { [ name : string ] : IndexedVar } ;
54
+ varargs ?: IndexedVar [ ] ;
54
55
}
55
56
56
57
export namespace Debugger {
@@ -165,21 +166,21 @@ export namespace Debugger {
165
166
}
166
167
167
168
function getLocals ( level : number , thread ?: Thread ) : Locals {
168
- const locs : Locals = { } ;
169
+ const locs : Locals = { vars : { } } ;
169
170
170
171
if ( thread === mainThreadName ) {
171
172
return locs ; // Accessing locals for main thread, but we're in a coroutine right now
172
173
}
173
174
174
175
//Validate level
176
+ let info : debug . FunctionInfo | undefined ;
175
177
if ( thread ) {
176
- if ( ! debug . getinfo ( thread , level , "l" ) ) {
177
- return locs ;
178
- }
178
+ info = debug . getinfo ( thread , level , "u" ) ;
179
179
} else {
180
- if ( ! debug . getinfo ( level + 1 , "l" ) ) {
181
- return locs ;
182
- }
180
+ info = debug . getinfo ( level + 1 , "u" ) ;
181
+ }
182
+ if ( ! info ) {
183
+ return locs ;
183
184
}
184
185
185
186
let name : string | undefined ;
@@ -198,47 +199,49 @@ export namespace Debugger {
198
199
}
199
200
200
201
if ( isValidIdentifier ( name ) ) {
201
- locs [ name ] = { val, index, type : type ( val ) } ;
202
+ locs . vars [ name ] = { val, index, type : type ( val ) } ;
202
203
}
203
204
204
205
++ index ;
205
206
}
206
207
207
208
//Varargs
208
- index = - 1 ;
209
- while ( true ) {
210
- if ( thread ) {
211
- [ name , val ] = debug . getlocal ( thread , level , index ) ;
212
- } else {
213
- [ name , val ] = debug . getlocal ( level + 1 , index ) ;
209
+ const isVarArg = ( info as unknown as { isvararg : boolean | undefined } ) . isvararg ;
210
+ if ( isVarArg !== false ) {
211
+ if ( isVarArg ) {
212
+ locs . varargs = [ ] ;
214
213
}
215
- if ( ! name ) {
216
- break ;
217
- }
218
-
219
- [ name ] = name . gsub ( "[^a-zA-Z0-9_]+" , "_" ) ;
220
- let key = `${ name } _${ - index } ` ;
221
- while ( locs [ key ] ) {
222
- key = `${ key } _` ;
214
+ index = - 1 ;
215
+ while ( true ) {
216
+ if ( thread ) {
217
+ [ name , val ] = debug . getlocal ( thread , level , index ) ;
218
+ } else {
219
+ [ name , val ] = debug . getlocal ( level + 1 , index ) ;
220
+ }
221
+ if ( ! name ) {
222
+ break ;
223
+ }
224
+ if ( ! locs . varargs ) {
225
+ locs . varargs = [ ] ;
226
+ }
227
+ table . insert ( locs . varargs , { val, index, type : type ( val ) } ) ;
228
+ -- index ;
223
229
}
224
- locs [ key ] = { val, index, type : type ( val ) } ;
225
-
226
- -- index ;
227
230
}
228
231
229
232
return locs ;
230
233
}
231
234
232
235
function getUpvalues ( info : debug . FunctionInfo ) : Locals {
233
- const ups : Locals = { } ;
236
+ const ups : Locals = { vars : { } } ;
234
237
235
238
if ( ! info . nups || ! info . func ) {
236
239
return ups ;
237
240
}
238
241
239
242
for ( const index of $range ( 1 , info . nups ) ) {
240
243
const [ name , val ] = debug . getupvalue ( info . func , index ) ;
241
- ups [ luaAssert ( name ) ] = { val, index, type : type ( val ) } ;
244
+ ups . vars [ luaAssert ( name ) ] = { val, index, type : type ( val ) } ;
242
245
}
243
246
244
247
return ups ;
@@ -365,7 +368,7 @@ export namespace Debugger {
365
368
level : number ,
366
369
info : debug . FunctionInfo ,
367
370
thread ?: Thread
368
- ) : LuaMultiReturn < [ true , unknown ] | [ false , string ] > {
371
+ ) : LuaMultiReturn < [ true , ... unknown [ ] ] | [ false , string ] > {
369
372
if ( thread === mainThreadName ) {
370
373
return $multi ( false , "unable to access main thread while running in a coroutine" ) ;
371
374
}
@@ -381,15 +384,15 @@ export namespace Debugger {
381
384
{ } ,
382
385
{
383
386
__index ( this : unknown , name : string ) {
384
- const variable = locs [ name ] ?? ups [ name ] ;
387
+ const variable = locs . vars [ name ] ?? ups . vars [ name ] ;
385
388
if ( variable !== undefined ) {
386
389
return variable . val ;
387
390
} else {
388
391
return fenv [ name ] ;
389
392
}
390
393
} ,
391
394
__newindex ( this : unknown , name : string , val : unknown ) {
392
- const variable = locs [ name ] ?? ups [ name ] ;
395
+ const variable = locs . vars [ name ] ?? ups . vars [ name ] ;
393
396
if ( variable !== undefined ) {
394
397
variable . type = type ( val ) ;
395
398
variable . val = val ;
@@ -400,25 +403,34 @@ export namespace Debugger {
400
403
}
401
404
) ;
402
405
403
- const [ func , err ] = loadLuaString ( statement , env ) ;
406
+ const loadStringResult = loadLuaString ( statement , env ) ;
407
+ const func = loadStringResult [ 0 ] ;
404
408
if ( ! func ) {
405
- return $multi ( false , err as string ) ;
409
+ return $multi ( false , loadStringResult [ 1 ] ) ;
410
+ }
411
+
412
+ const varargs : unknown [ ] = [ ] ;
413
+ if ( locs . varargs ) {
414
+ for ( const vararg of locs . varargs ) {
415
+ table . insert ( varargs , vararg . val ) ;
416
+ }
406
417
}
407
418
408
- const [ success , result ] = pcall ( func ) ;
409
- if ( success ) {
410
- for ( const [ _ , loc ] of pairs ( locs ) ) {
419
+ const results = pcall < unknown [ ] , unknown [ ] > ( func , ... unpack ( varargs ) ) ;
420
+ if ( results [ 0 ] ) {
421
+ for ( const [ _ , loc ] of pairs ( locs . vars ) ) {
411
422
if ( thread ) {
412
423
debug . setlocal ( thread , level , loc . index , loc . val ) ;
413
424
} else {
414
425
debug . setlocal ( level , loc . index , loc . val ) ;
415
426
}
416
427
}
417
- for ( const [ _ , up ] of pairs ( ups ) ) {
428
+ for ( const [ _ , up ] of pairs ( ups . vars ) ) {
418
429
debug . setupvalue ( luaAssert ( info . func ) , up . index , up . val ) ;
419
430
}
431
+ return $multi ( true , ...unpack ( results , 2 ) ) ;
420
432
}
421
- return $multi ( success as true , result ) ;
433
+ return $multi ( false , results [ 1 ] ) ;
422
434
}
423
435
424
436
function getInput ( ) : string | undefined {
@@ -455,6 +467,8 @@ export namespace Debugger {
455
467
return stack ;
456
468
}
457
469
470
+ const varArgTable : LuaDebug . VarArgTable = "{...}" ;
471
+
458
472
let breakAtDepth = - 1 ;
459
473
let breakInThread : Thread | undefined ;
460
474
let updateHook : { ( ) : void } ;
@@ -603,13 +617,20 @@ export namespace Debugger {
603
617
604
618
} else if ( inp === "locals" ) {
605
619
const locs = getLocals ( frame + frameOffset , currentThread !== activeThread ? currentThread : undefined ) ;
606
- mapVarNames ( locs , sourceMap ) ;
607
- Send . vars ( locs ) ;
620
+ mapVarNames ( locs . vars , sourceMap ) ;
621
+ if ( locs . varargs ) {
622
+ const varargVals : unknown [ ] = [ ] ;
623
+ for ( const vararg of locs . varargs ) {
624
+ table . insert ( varargVals , vararg . val ) ;
625
+ }
626
+ locs . vars [ varArgTable ] = { val : varargVals , index : - 1 , type : "table" } ;
627
+ }
628
+ Send . vars ( locs . vars ) ;
608
629
609
630
} else if ( inp === "ups" ) {
610
631
const ups = getUpvalues ( info ) ;
611
- mapVarNames ( ups , sourceMap ) ;
612
- Send . vars ( ups ) ;
632
+ mapVarNames ( ups . vars , sourceMap ) ;
633
+ Send . vars ( ups . vars ) ;
613
634
614
635
} else if ( inp === "globals" ) {
615
636
const globs = getGlobals (
@@ -691,16 +712,16 @@ export namespace Debugger {
691
712
692
713
} else {
693
714
const mappedExpression = mapExpressionNames ( expression , sourceMap ) ;
694
- const [ s , r ] = execute (
715
+ const results = execute (
695
716
`return ${ mappedExpression } ` ,
696
717
frame + frameOffset ,
697
718
info ,
698
719
currentThread !== activeThread ? currentThread : undefined
699
720
) ;
700
- if ( s ) {
701
- Send . result ( r ) ;
721
+ if ( results [ 0 ] ) {
722
+ Send . result ( ... unpack ( results , 2 ) ) ;
702
723
} else {
703
- Send . error ( r as string ) ;
724
+ Send . error ( results [ 1 ] ) ;
704
725
}
705
726
}
706
727
@@ -737,16 +758,16 @@ export namespace Debugger {
737
758
Send . error ( "Bad statement" ) ;
738
759
739
760
} else {
740
- const [ s , r ] = execute (
761
+ const results = execute (
741
762
statement ,
742
763
frame + frameOffset ,
743
764
info ,
744
765
currentThread !== activeThread ? currentThread : undefined
745
766
) ;
746
- if ( s ) {
747
- Send . result ( r ) ;
767
+ if ( results [ 0 ] ) {
768
+ Send . result ( ... unpack ( results , 2 ) ) ;
748
769
} else {
749
- Send . error ( r as string ) ;
770
+ Send . error ( results [ 1 ] ) ;
750
771
}
751
772
}
752
773
0 commit comments