-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCiderDebugger.lua
1788 lines (1581 loc) · 49.8 KB
/
CiderDebugger.lua
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--v2.0.1
--[[
Glider Debugger Library
Author: M.Y. Developers LLC
Copyright (C) 2013 M.Y. Developers LLC All Rights Reserved
Support: [email protected]
Website: http://www.mydevelopersgames.com/
License: Many hours of genuine hard work have gone into this project and we kindly ask you not to redistribute or illegally sell this package.
We are constantly developing this software to provide you with a better development experience and any suggestions are welcome. Thanks for you support.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
--]]
local preFrameTimer,postFrameTimer,timeInFrame,frameTime,enterFrame,profilerRunning,profilerHook
local profilerPeriod = 1
local profilerTimer,reporter,systemTime,removeHook,debugloop,socketRecieveLoop,handleError
local socket = require "socket"
local tcpSocket,master,resolveName, tableToID, idToTable, lastKnownPC
local CiderRunMode = {};CiderRunMode.runmode = 'RUN';CiderRunMode.assertImage = true;CiderRunMode.userdir = "/Volumes/Macintosh HD/Users/dgross/Library/Application Support/luaglider2/dev";local SOCKET_PORT=49751;local GLIDER_MAIN_FOLDER= "/Volumes/Macintosh HD/Users/dgross/Corona Projects/textrender";local useNativePrint= false;local snapshotInterval= -1;local snapshotInterval= -1;local fileFilters= {"CiderDebugger.lua",};local startupMode= "require";local function shouldDebug()
local env = system.getInfo( "environment" )
if(env~="simulator") then
native.showAlert(
"Glider Debugger Warning!", "Glider debugger libraries are "
.."still included on the device! You probably meant to click "
.."build instead of debug/run. Please click the hammer icon when you "
.."wish to deploy on the device. ", {"OK"} )
return false
end
return true
end
local function gliderDebuggerErrorListener( event )
handleError(2, event.errorMessage )
return false
end
Runtime:addEventListener("unhandledError", gliderDebuggerErrorListener)
--in order for the profiler to work properly it must be synced to your the
--frame timer of your sdk.
local function setEnterframeCallback(func)
Runtime:addEventListener( "enterFrame" , func)
end
--this function will be called when an event is recieved from the IDE
local function ultimoteEventRecieved(evt)
Runtime:dispatchEvent(evt)
end
local function initializeUltimote()
local supportedEvents = {
orientation = true,
accelerometer = true,
gyroscope = true,
heading = true,
collision = true,
preCollision = true,
postCollision = true,
}
system.hasEventSource = function(evt)
return supportedEvents[evt]
end
end
local function setEnterframeCallback(func)
Runtime:addEventListener( "enterFrame" , func)
end
--DEBUG HEADERS HERE--
if(shouldDebug and not shouldDebug()) then
return;
end
io.stdout:setvbuf("no")
local json = {}
local function loadJson()
local string = string
local math = math
local table = table
local error = error
local tonumber = tonumber
local tostring = tostring
local type = type
local setmetatable = setmetatable
local pairs = pairs
local ipairs = ipairs
local assert = assert
local Chipmunk = Chipmunk
local function Null()
return Null
end
local StringBuilder = {
buffer = {}
}
function StringBuilder:New()
local o = {}
setmetatable(o, self)
self.__index = self
o.buffer = {}
return o
end
function StringBuilder:Append(s)
self.buffer[#self.buffer+1] = s
end
function StringBuilder:ToString()
return table.concat(self.buffer)
end
local JsonWriter = {
backslashes = {
['\b'] = "\\b",
['\t'] = "\\t",
['\n'] = "\\n",
['\f'] = "\\f",
['\r'] = "\\r",
['"'] = "\\\"",
['\\'] = "\\\\",
['/'] = "\\/"
}
}
function JsonWriter:New()
local o = {}
o.writer = StringBuilder:New()
setmetatable(o, self)
self.__index = self
return o
end
function JsonWriter:Append(s)
self.writer:Append(s)
end
function JsonWriter:ToString()
return self.writer:ToString()
end
function JsonWriter:Write(o)
local t = type(o)
if t == "nil" then
self:WriteNil()
elseif t == "boolean" then
self:WriteString(o)
elseif t == "number" then
self:WriteString(o)
elseif t == "string" then
self:ParseString(o)
elseif t == "table" then
self:WriteTable(o)
elseif t == "function" then
self:WriteFunction(o)
elseif t == "thread" then
self:WriteTable{}
elseif t == "userdata" then
self:WriteTable{}
end
end
function JsonWriter:WriteNil()
self:Append("null")
end
function JsonWriter:WriteString(o)
self:Append(tostring(o))
end
function JsonWriter:ParseString(s)
self:Append('"')
self:Append(string.gsub(s, "[%z%c\\\"/]", function(n)
local c = self.backslashes[n]
if c then return c end
return string.format("\\u%.4X", string.byte(n))
end))
self:Append('"')
end
function JsonWriter:IsArray(t)
local count = 0
local isindex = function(k)
if type(k) == "number" and k > 0 then
if math.floor(k) == k then
return true
end
end
return false
end
for k,v in pairs(t) do
if not isindex(k) then
return false, '{', '}'
else
count = math.max(count, k)
end
end
return true, '[', ']', count
end
function JsonWriter:WriteTable(t)
local ba, st, et, n = self:IsArray(t)
self:Append(st)
if ba then
for i = 1, n do
self:Write(t[i])
if i < n then
self:Append(',')
end
end
else
local first = true;
for k, v in pairs(t) do
if not first then
self:Append(',')
end
first = false;
self:ParseString(k)
self:Append(':')
self:Write(v)
end
end
self:Append(et)
end
function JsonWriter:WriteError(o)
error(string.format(
"Encoding of %s unsupported",
tostring(o)))
end
function JsonWriter:WriteFunction(o)
if o == Null then
self:WriteNil()
else
self:WriteTable{}
end
end
local StringReader = {
s = "",
i = 0
}
function StringReader:New(s)
local o = {}
setmetatable(o, self)
self.__index = self
o.s = s or o.s
return o
end
function StringReader:Peek()
local i = self.i + 1
if i <= #self.s then
return string.sub(self.s, i, i)
end
return nil
end
function StringReader:Next()
self.i = self.i+1
if self.i <= #self.s then
return string.sub(self.s, self.i, self.i)
end
return nil
end
function StringReader:All()
return self.s
end
local JsonReader = {
escapes = {
['t'] = '\t',
['n'] = '\n',
['f'] = '\f',
['r'] = '\r',
['b'] = '\b',
}
}
function JsonReader:New(s)
local o = {}
o.reader = StringReader:New(s)
setmetatable(o, self)
self.__index = self
return o;
end
function JsonReader:Read()
self:SkipWhiteSpace()
local peek = self:Peek()
if peek == nil then
error(string.format(
"Nil string: '%s'",
self:All()))
elseif peek == '{' then
return self:ReadObject()
elseif peek == '[' then
return self:ReadArray()
elseif peek == '"' then
return self:ReadString()
elseif string.find(peek, "[%+%-%d]") then
return self:ReadNumber()
elseif peek == 't' then
return self:ReadTrue()
elseif peek == 'f' then
return self:ReadFalse()
elseif peek == 'n' then
return self:ReadNull()
elseif peek == '/' then
self:ReadComment()
return self:Read()
else
error(string.format(
"Invalid input: '%s'",
self:All()))
end
end
function JsonReader:ReadTrue()
self:TestReservedWord{'t','r','u','e'}
return true
end
function JsonReader:ReadFalse()
self:TestReservedWord{'f','a','l','s','e'}
return false
end
function JsonReader:ReadNull()
self:TestReservedWord{'n','u','l','l'}
return nil
end
function JsonReader:TestReservedWord(t)
for i, v in ipairs(t) do
if self:Next() ~= v then
error(string.format(
"Error reading '%s': %s",
table.concat(t),
self:All()))
end
end
end
function JsonReader:ReadNumber()
local result = self:Next()
local peek = self:Peek()
while peek ~= nil and string.find(
peek,
"[%+%-%d%.eE]") do
result = result .. self:Next()
peek = self:Peek()
end
result = tonumber(result)
if result == nil then
error(string.format(
"Invalid number: '%s'",
result))
else
return result
end
end
function JsonReader:ReadString()
local result = ""
assert(self:Next() == '"')
while self:Peek() ~= '"' do
local ch = self:Next()
if ch == '\\' then
ch = self:Next()
if self.escapes[ch] then
ch = self.escapes[ch]
end
end
result = result .. ch
end
assert(self:Next() == '"')
local fromunicode = function(m)
return string.char(tonumber(m, 16))
end
return string.gsub(
result,
"u%x%x(%x%x)",
fromunicode)
end
function JsonReader:ReadComment()
assert(self:Next() == '/')
local second = self:Next()
if second == '/' then
self:ReadSingleLineComment()
elseif second == '*' then
self:ReadBlockComment()
else
error(string.format(
"Invalid comment: %s",
self:All()))
end
end
function JsonReader:ReadBlockComment()
local done = false
while not done do
local ch = self:Next()
if ch == '*' and self:Peek() == '/' then
done = true
end
if not done and
ch == '/' and
self:Peek() == "*" then
error(string.format(
"Invalid comment: %s, '/*' illegal.",
self:All()))
end
end
self:Next()
end
function JsonReader:ReadSingleLineComment()
local ch = self:Next()
while ch ~= '\r' and ch ~= '\n' do
ch = self:Next()
end
end
function JsonReader:ReadArray()
local result = {}
assert(self:Next() == '[')
local done = false
if self:Peek() == ']' then
done = true;
end
while not done do
local item = self:Read()
result[#result+1] = item
self:SkipWhiteSpace()
if self:Peek() == ']' then
done = true
end
if not done then
local ch = self:Next()
if ch ~= ',' then
error(string.format(
"Invalid array: '%s' due to: '%s'",
self:All(), ch))
end
end
end
assert(']' == self:Next())
return result
end
function JsonReader:ReadObject()
local result = {}
assert(self:Next() == '{')
local done = false
if self:Peek() == '}' then
done = true
end
while not done do
local key = self:Read()
if type(key) ~= "string" then
error(string.format(
"Invalid non-string object key: %s",
key))
end
self:SkipWhiteSpace()
local ch = self:Next()
if ch ~= ':' then
error(string.format(
"Invalid object: '%s' due to: '%s'",
self:All(),
ch))
end
self:SkipWhiteSpace()
local val = self:Read()
result[key] = val
self:SkipWhiteSpace()
if self:Peek() == '}' then
done = true
end
if not done then
ch = self:Next()
if ch ~= ',' then
error(string.format(
"Invalid array: '%s' near: '%s'",
self:All(),
ch))
end
end
end
assert(self:Next() == "}")
return result
end
function JsonReader:SkipWhiteSpace()
local p = self:Peek()
while p ~= nil and string.find(p, "[%s/]") do
if p == '/' then
self:ReadComment()
else
self:Next()
end
p = self:Peek()
end
end
function JsonReader:Peek()
return self.reader:Peek()
end
function JsonReader:Next()
return self.reader:Next()
end
function JsonReader:All()
return self.reader:All()
end
function json.encode(o)
local writer = JsonWriter:New()
writer:Write(o)
return writer:ToString()
end
function json.decode(s)
local reader = JsonReader:New(s)
return reader:Read()
end
end
local function sendObject(msg)
tcpSocket:settimeout(5)
tcpSocket:send(json.encode(msg)..'\n') ;
tcpSocket:settimeout(0)
end
--local json = require "json"
loadJson()
local jsonNull = "nil"
if(type(json.null)=="function") then
jsonNull = json.null()
elseif (json.Null) then
jsonNull = json.Null
end
local statusMessage
local previousLine, previousFile
local Root = {} --this is for variable dumps
local globalsBlacklist = {}
local breakpoints = {}
local breakpointLines = {}
local runToCursorKey = nil
local runToCursorKeyLine = nil
local snapshotCounter = 0
local lineBlacklist = {}
local workingEnterframe = false
local getinfo = debug.getinfo
local sethook = debug.sethook
local tostring = tostring
--override display methods so warnings are thrown
if(CiderRunMode==nil) then
CiderRunMode = {};
end
local function isAbsolute(path)
return path:match("^/.*$") or path:match("^%a:/.*$")
end
local function standardizePath( input, changecase )
if changecase then
input = string.lower( input )
end
input = string.gsub( input, "\\", "/" )
if(not isAbsolute(input)) then
input = GLIDER_MAIN_FOLDER..'/'..input
end
return input
end
------------------------------COROUTINE--------------------------------------
local coroutineCache = setmetatable({}, {__mode="k"})
local isMainThreadAdded = false;
local function shouldAddHook(co)
if(co==nil) then
if(isMainThreadAdded)then
return false
else
isMainThreadAdded = true
return true
end
end
if(coroutineCache[co]) then
return false;
else
coroutineCache[co] = true;
return true;
end
end
local function addHook(...)
local co = coroutine.running()
if(shouldAddHook(co)) then
sethook(...)
end
end
local function removeHook(...)
local co = coroutine.running()
if(co) then
coroutineCache[co]=nil;
else
isMainThreadAdded = false;
end
sethook(...)
end
local cocreate = coroutine.create
------------------------------------------------------------------------------
------------------------------FRAME TIMER-------------------------------------
local cpuFraction,memoryUsed,runningSumCPU,runningSumMemory=0,0,0,0;--used for averaging
local runningFrames = 1;
function enterFrame()
workingEnterframe = true
socketRecieveLoop()
local currentTime = socket.gettime();
if(preFrameTimer) then
frameTime = currentTime-preFrameTimer;
if(postFrameTimer) then
timeInFrame = currentTime-postFrameTimer
runningSumCPU = (frameTime-timeInFrame)/frameTime + runningSumCPU
runningSumMemory = collectgarbage("count")+runningSumMemory
runningFrames = runningFrames+1
end
end
preFrameTimer = currentTime
reporter()
end
------------------------------------------------------------------------------
if(CiderRunMode.assertImage) then
if(CiderRunMode.sdk=="CORONA") then
local ov = {"newImage", "newImageRect",}
local displayFunc = {}
for i,v in pairs(ov) do
local nativeF = display[v];
display[v] = function(...)
return assert(nativeF(...), "display."..v.." assertion failed, check filename")
end
end
end
end
for i,v in pairs(_G) do
globalsBlacklist[v] = true
end
local nativePrint = print
local nativeError = error
local function cat(...)
local n = select("#", ...)
local str = ""
for i=1,n do
str = str..tostring(select(i, ...)).."\t"
end
return str
end
local function sendConsoleMessage(...)
local message = {}
message.type = "pr"
local str = cat(...)
message.str = str
sendObject(message)
end
local function sendConsoleError(...)
local message = {}
message.type = "pe"
local str = cat(...)
message.str = str
sendObject(message)
end
local function debugPrint(...)
sendConsoleMessage(...)
nativePrint(...)
end
local function debugError(...)
sendConsoleError(...)
nativeError(...)
end
--error = debugError
--this will block the program initially and wait for netbeans connection
local varRefTable = {} --holds ref to all discovered vars, must remove or leak.
local function globalsDump()
local globalsVars = {}
for i,globalv in pairs(_G) do
if(globalsBlacklist[globalv]==nil) then
globalsVars[i] = globalv
end
end
--return serializeDump(globalsVars)
return globalsVars
end
local tostring = tostring
local serializeQueue = {}
local luaIDs
local queueIndex = 1;
local maxqueueIndex = 100;
local function serializeDump(tab, tables)--mirrors table and removes functions, userdata, and tries to identify type\
luaIDs = {}
if(tables == nil) then
tables = {}
end
luaIDs[tostring(tab)] = {[".CIDERPath"] = "root"}
while(tab) do
local tabKey = tostring(tab)
varRefTable[tabKey] = tab
if(tab == _G) then
--dealing with global so filter the blacklist and proxy this but leave refernces to global
tab = globalsDump()
end
--tab must be type table
if(tables[tabKey] == nil) then
local newTab = {}
newTab[".myRef"] = tabKey
if(tab._class and tab.x and tab.y and tab.rotation and tab.alpha) then
--in a displayGroup
newTab[".isDisplayObject"] = true
newTab.x, newTab.y, newTab.rotation, newTab.alpha, newTab.width, newTab.height, newTab.isVisible, newTab.xReference, newTab.yReference, newTab.xScale, newTab.yScale=
tab.x,tab.y,tab.rotation,tab.alpha,tab.width,tab.height,tab.isVisible, tab.xReference, tab.yReference, tab.xScale, tab.yScale
--also add the custom data
if(tab.numChildren) then
--in a display object
newTab.numChildren = tab.numChildren;
newTab[".isDisplayGroup"] = true
else
end
end
tables[tabKey] = newTab
local ciderPath = luaIDs[tabKey][".CIDERPath"] or "root";
--traverse through table and add values
for i,v in pairs(tab) do
local typev = type(v)
if(type(i)=="table") then
i = tostring(i)
end
if(type(i)=="userdata") then
i = tostring(i)
end
if(typev=="string" or type(v)=="boolean" or type(v)=="number" ) then
if(v == -math.huge or v == math.huge) then
newTab[i] = tostring(v)
else
newTab[i] = v
end
elseif(typev=="table" ) then
--local tabKey = tostring(v)
newTab[i] = {}
newTab[i][".isCiderRef"] = tostring(v);--save the reference of v
if(tables[tostring(v)]==nil) then --check if we have serialized this table or not
--check if this is a display object (see if there is a _class key)
--add it to the queue instead
if(maxqueueIndex ~= queueIndex) then
serializeQueue[queueIndex] = v;
queueIndex = queueIndex+1;
end
tabKey = tostring(v)
if(luaIDs[tabKey]==nil) then
luaIDs[tabKey]={}
end
local luaID = luaIDs[tabKey];
luaID[".CIDERPath"] = ciderPath..i
luaID[".luaID"] = i; --the table itself knows its ID
--serializeDump(v, tables)
end
elseif(v==nil) then
newTab[i] = jsonNull;
elseif(typev=="function") then
newTab[i] = {}
newTab[i].isCoronaBridgeFunction = true
newTab[i].id = i
elseif(typev=="userdata") then
newTab[i] = ".userdata"
end
end
end
queueIndex = queueIndex-1
tab = serializeQueue[queueIndex]
end
for i,v in pairs(luaIDs) do
if(tables[i]) then
tables[i][".CIDERPath"] = v[".CIDERPath"]
tables[i][".luaID"] = v[".luaID"]
end
end
return tables
end
local function localsDump(stackLevel, vars) --puts all locals into table
if(vars==nil) then
vars = {}
end
local db = debug.getinfo(stackLevel, "fS")
local func = db.func
local i = 1
while true do
local name, value = debug.getupvalue(func, i)
if not name then break end
if(value==nil) then
vars[name] = jsonNull
else
vars[name] = value
end
i = i + 1
end
i = 1
while true do
local name, value = debug.getlocal(stackLevel, i)
if not name then break end
if(name:sub(1,1)~="(") then
if(value==nil) then
vars[name] = jsonNull
else
vars[name] = value
end
end
i = i + 1
end
--setmetatable(vars, { __index = getfenv(func), __newindex = getfenv(func) })
-- local dump = serializeDump( vars )
return vars
end
local function searchLocals(localName,newValue,stackLevel) --puts all locals into table
local db = debug.getinfo(stackLevel, "fS")
local func = db.func
local i = 1
while true do
local name, value = debug.getlocal(stackLevel, i)
if not name then break end
if(name == localName) then debug.setlocal(stackLevel, i, newValue); return; end
i = i + 1
end
i = 1
while true do
local name, value = debug.getupvalue(func, i)
if not name then break end
if(name == localName) then debug.setupvalue(func, i, newValue); return; end
i = i + 1
end
end
function resolveName(nameToFind,stackLevel) --puts all locals into table
local db = debug.getinfo(stackLevel, "fS")
if(not db) then
return nil
end
local func = db.func
local i = 1
while true do
local name, value = debug.getlocal(stackLevel, i)
if not name then break end
if(name == nameToFind) then return value,"local" ; end
i = i + 1
end
i = 1
while true do
local name, value = debug.getupvalue(func, i)
if not name then break end
if(name == nameToFind) then return value,"upvalue"; end
i = i + 1
end
return _G[nameToFind],"global"
end
local function stackDump(stackLevel)
local stackDump = {};
local stackIndex = stackLevel or 5
local index = 0;
local info
local filename;
local info = debug.getinfo(stackIndex,"Sl")
while(info) do
filename = info.source
if( filename:find("CiderDebugger.lua") ) then
break;
end
if( filename:find( "@" ) ) then
filename = filename:sub( 2 )
end
filename = standardizePath(filename)
stackDump[index] = {filename,info.linedefined,info.currentline}
index = index+1
stackIndex = stackIndex+1
info = debug.getinfo(stackIndex,"Sl")
end
return stackDump
end
local function writeStackDump(stackLevel) --write the var dump to file
local stackString = json.encode(stackDump(stackLevel));
sendObject({["type"]="st",["data"]=stackString})
end
local tableIDCounter = 0;
idToTable = setmetatable({}, {__mode="v"})
tableToID = setmetatable({}, {__mode="k"})
local function getTableIDFor(var)
local id = tableToID[var]
if not id then
tableIDCounter = tableIDCounter+1;
id = tostring(tableIDCounter)
idToTable[id] = var
tableToID[var] = id
end
return id
end
local maxDepth = 0
local extraSymbols
local function convertVarToResult(var, name, depth)
depth = depth or 0
depth = depth+1
local result = {}
local tableID = getTableIDFor(var)
result["name"] = name
result["type"] = type(var)
result["tableID"] = tableID
local isTable = type(var)=="table"
local hasChildren = (isTable and next(var) ~=nil )
result["hasChildren"] = hasChildren
result["value"] = tostring(var)
local children = {}
if maxDepth >= depth and isTable and var then
for name,child in pairs(var) do
local result = convertVarToResult(child, name, depth)
table.insert(children, result)
end
if extraSymbols then
for _,name in pairs(extraSymbols) do
local resolved = var[name];
if not resolved then
break
end
local result = convertVarToResult(resolved, name, depth)
table.insert(children, result)
end
end
end
result["children"] = children
return result
end