-
Notifications
You must be signed in to change notification settings - Fork 7
/
mylib52.lua
2472 lines (2244 loc) · 58.3 KB
/
mylib52.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
if false then -- debug mode functions. (significant performance overhead when enabled.)
ipairs_old=ipairs
function ipairs(t,f)
if t==nil then dbg.console('ipairs error') end
return ipairs_old(t,f)
end
end
dbg={ _count=1}
util=util or {}
function assert(bVal)
if not bVal then
-- when a log function is defined
if fineLog~=nil then
debug.sethook() -- stop all kinds of debugger
fineLog("assert failed")
fineLog(dbg.callstackString(3))
fineLog(util.tostring(dbg.locals()))
end
print("assert failed: type cs or dbg.traceBack() or help for more information.")
-- dbg.callstack()
-- debug.debug()
dbg.console() -- my debugger.
end
return bVal
end
--[[
print2=print
function print(...)
local a={...}
if a[1]==700000 then
dbg.console()
end
print2(...)
end
]]--
function dbg.lunaType(c)
if type(c)~='userdata' then
return nil
end
local mt= getmetatable(c)
if mt==nil then return nil end
return mt.luna_class
end
function dbg.listLunaClasses(line)
local usrCnam= string.sub(line, 7)
local out2=''
local out=''
local outp=''
for k,v in pairs(__luna)do
if type(v)=='table' then
local cname=v.luna_class
if cname then
local _, className=string.rightTokenize(cname,'%.')
local nn=string.sub(k, 1, -string.len(className)-1)
local namspac=string.gsub(nn,'_', '.')
if namspac=='.' then namspac='' end
if usrCnam=='' then
out=out..namspac.. className..', '
else
if namspac..className==usrCnam then
local map={__add='+', __mul='*',__div='/', __unm='-', __sub='-'}
local lastFn='funcName'
for kk,vv in pairs(v) do
if type(vv)=='function' then
if string.sub(kk,1,13)=='_property_get' then
outp=outp .. string.sub(kk,15)..', '
elseif string.sub(kk,1,13)=='_property_set' then
--outp=outp .. string.sub(kk,15)..','
else
if map[kk] then
out2=out2..map[kk]..', '
else
out2=out2..kk..', '
end
lastFn=kk
end
end
end
out2=out2..'\n\n You can see a function signature by typing for example "'..usrCnam..'.'..lastFn..'()"!'
out2=out2..'\n Known bug: property names can be incorrectly displayed. "'
end
end
end
end
end
if outp~='' then print('Properties:\n', outp) end
print(out)
if out2~='' then print('Member functions:\n', out2) end
end
function dbg.readLine(cursor)
io.write(cursor)
return io.read('*line')
end
function dbg.traceBack(level)
if level==nil then
level=1
end
while true do
local info=debug.getinfo(level)
local k=info.name
if k==nil then
break
else
print('----------------------------------------------------------')
print('Level: ', level)
print(info.short_src..":"..info.currentline..":"..k)
print('Local variables:')
dbg.locals(level)
level=level+1
end
end
end
function os.findVIM()
if os.isWindows() then
if os.isCygwin() then
local search={
"/cygdrive/c/Program Files/Vim/vim73/vim.exe",
"/cygdrive/c/Program Files (x86)/Vim/vim73/vim.exe",
"/cygdrive/c/Program Files (x86)/Vim/vim73/vimrun.exe",
}
for i,v in ipairs(search) do
if os.isFileExist(v) then
local vimpath= '"'..v..'"'
local gvimpath= '"'..string.gsub(v, 'vim.exe', 'gvim.exe')..'"'
gvimpath= string.gsub(gvimpath, 'vimrun.exe', 'gvim.exe')
return 'vim', gvimpath
end
end
else
local search={
"c:\\Program Files\\Vim\\vim73\\vim.exe",
"c:\\Program Files (x86)\\Vim\\vim73\\vim.exe",
"c:\\Program Files\\Git\\share\\vim\\vim73\\vim.exe",
"c:\\Program Files (x86)\\Git\\share\\vim\\vim73\\vim.exe",
'c:\\msysgit\\msysgit\\share\\vim\\vim73\\vim',
}
for i,v in ipairs(search) do
if os.isFileExist(v) then
local vimpath= '"'..v..'"'
local gvimpath= '"'..string.gsub(v, 'vim.exe', 'gvim.exe')..'"'
return vimpath, gvimpath
end
end
end
end
return 'vim', 'gvim'
end
function os.findGIT()
if os.isWindows() and not os.isCygwin() then
local search={
"c:\\Program Files\\Git\\bin\\git.exe",
"c:\\Program Files (x86)\\Git\\bin\\git.exe",
}
for i,v in ipairs(search) do
if os.isFileExist(v) then
return '"'..v..'"'
end
end
end
return 'git'
end
function os.VI_path()
if os.isUnix() then
-- return "vim" -- use vim in a gnome-terminal
return "gvim"
else
return "gvim"
end
end
function os.toWindowsFileName(file)
local fn=string.gsub(file, "/","\\")
if string.sub(fn,1,1)=="\\" then
fn=string.sub(fn,2,2)..':'..string.sub(fn,3)
end
return fn
end
function os.fromWindowsFileName(file)
local fn=string.gsub(file, "\\","/")
if string.sub(fn,2,2)==':' then
fn='/'..string.upper(string.sub(fn,1,1))..string.sub(fn,3)
end
return fn
end
function os.open(file)
if os.isUnix() then
os.execute('gnome-open "'..file..'"')
else
os.execute('start /b cmd /c "'..os.toWindowsFileName(file)..'"')
end
end
function os.openFolder(folder)
if os.isUnix() then
os.execute('nautilus "'..folder..'"')
else
os.execute('explorer "'..os.toWindowsFileName(folder)..'"')
end
end
function os.openTerminal(folder)
if os.isUnix() then
os.execute('gnome-terminal --working-directory "'..os.relativeToAbsolutePath(folder)..'"')
else
os.execute('cmd /k cd "'..os.toWindowsFileName(folder)..'"')
end
end
function os.vi_check(fn)
local otherVim='vim'
local servers=string.tokenize(os.capture(otherVim..' --serverlist 2>&1',true), "\n")
local out=array.filter(function (x) return string.upper(fn)==x end,servers)
local out_error=array.filter(function (x) return string.find(x,"Unknown option argument")~=nil end,servers)
if #out_error>=1 then return nil end
return #out>=1
end
function os.fileinfo(fn)
return os.capture('ls -l "'..fn..'"')
end
function os.vi_console_close_all()
local servers=string.tokenize(os.capture('vim --serverlist',true), "\n")
local out=array.filter(function (x) return fn~="GVIM" end,servers)
for i,v in ipairs(out) do
os.execute('vim --servername "'..v..'" --remote-send ":q<CR>"')
end
end
function os.vi_console_cmd(fn, line)
local cc
if line then
cc=' +'..line..' "'..fn..'"'
else
cc=' "'..fn..'"'
end
if not os.isUnix() and os.isFileExist("C:/msysgit/msysgit/share/vim/vim73/vim.exe") then
return '"C:/msysgit/msysgit/share/vim/vim73/vim.exe" '..cc
end
return 'vim '..cc
end
function os.gedit_cmd(fn, line)
local cc
if line then
cc=' +'..line..' "'..fn..'"'
else
cc=' "'..fn..'"'
end
return 'gedit '..cc
end
function os.vi_readonly_console_cmd(fn, line)
local cc
if line then
cc=' +'..line..' "'..fn..'"'
else
cc=' "'..fn..'"'
end
return 'vim -R -M -c ":set nomodifiable" '..cc
end
function os.vi_line(fn, line)
if os.vi_check(fn) then
os.execute2(os.vi_console_cmd(fn,line))
return
end
if not os.launch_vi_server() then
print('Please launch gvim first!')
return
end
local VI=os.VI_path()..' --remote-silent'
local cmd=VI..' +'..line..' "'..fn..'"'
--print(cmd)
os.execute2(cmd)
end
function os.launch_vi_server()
local lenvipath=string.len(os.VI_path())
if os.vi_check(string.upper(os.VI_path())) then
print("VI server GVIM open")
return true
end
if false then -- recent ubuntu gvim doesn't start up from a terminal.
print("launching GVIM server...")
if os.isUnix() then
if os.VI_path()=="vim" then
os.execute2('cd ../..', 'gnome-terminal -e "vim --servername vim"&') -- this line is unused by default. (assumed gnome dependency)
else
os.execute2('cd ../..', os.VI_path())
end
else
if os.isFileExist(os.capture('echo %WINDIR%').."\\vim.bat") then
os.execute2('cd ..\\..', os.VI_path())
else
os.execute2('cd ..\\..', "start "..os.VI_path())
end
end
for i=1,10 do
if os.vi_check(string.upper(os.VI_path())) then
print("VI server GVIM open")
break
else
print('.')
--os.sleep(1)
end
end
return true
end
return false
end
function os.vi(...)
os._vi(os.VI_path(), ...)
end
function os._vi(servername, ...)
local VI=os.VI_path() ..' --servername '..servername..' --remote-silent'
local VI2=os.VI_path() ..' --servername '..servername..' --remote-send ":n '
local VI3='<CR>"'
local targets={...}
local otherVim='vim'
local vicwd=os.capture(otherVim..' --servername '..servername..' --remote-expr "getcwd()"')
if vicwd=="" then
if not os.launch_vi_server() then
print('Please launch gvim first!')
return
end
-- try one more time
vicwd=os.capture(otherVim ..' --servername '..servername..' --remote-expr "getcwd()"')
end
print('vicwd=',vicwd)
local itgt, target
for itgt,target2 in ipairs(targets) do
-- local target=string.sub(target2,4)
local target=target2
if string.find(target, '*') ~=nil or string.find(target, '?')~=nil then
if false then
-- open each file. too slow
local subtgts=os.glob(target)
local istgt,subtgt
for istgt,subtgt in ipairs(subtgts) do
local cmd=VI..' "'..subtgt..'"'
if string.find(cmd,'~')==nil then
os.execute(cmd)
end
end
elseif string.sub(target, 1,6)=="../../" and string.sub(vicwd, -10)=="taesoo_cmu" then -- fastest method
local cmd=VI2..string.sub(target,7)..VI3
print(cmd)
if os.isUnix() then
os.execute(cmd.."&")
else
os.execute("start "..cmd)
end
else
local lastSep
local newSep=0
local count=0
repeat lastSep=newSep
newSep=string.find(target, "/", lastSep+1)
count=count+1
until newSep==nil
local path=string.sub(target, 0, lastSep-1)
local filename
if lastSep==0 then filename=string.sub(target,lastSep) else filename=string.sub(target, lastSep+1) end
print(filename, path, count)
print("cd "..path, VI.." "..filename)
if os.isUnix() then
os.execute2("cd "..path, "rm -f *.lua~", VI.." "..filename.."&")
else
os.execute2("cd "..path, "rm -f *.lua~", "rm -f #*#", VI.." "..filename)
end
-- end
end
else
local cmd=VI..' "'..target..'"'
print(cmd)
if os.isUnix() then
os.execute(cmd.."&")
else
os.execute(cmd)
end
end
end
end
function dbg.showCode(fn,ln)
util.iterateFile(fn,
{
iterate=function (self, lineno, c)
if lineno>ln-5 and lineno<ln+5 then
c=string.gsub(c, "\t", " ")
if #c > 70 then
c=string.sub(c,1,65).."..."
end
if lineno==ln then
print(lineno.."* "..c)
else
print(lineno.." "..c)
end
end
end
}
)
end
-- e.g. dbg.setFunctionHook(RE, 'createVRMLskin')
function dbg.setFunctionHook(table, functionName)
dbg[functionName..'_old']=table[functionName]
table[functionName]=function (...)
print('Function :'..functionName)
dbg.console()
return dbg[functionName..'_old'](...)
end
end
function dbg.console(msg, stackoffset)
stackoffset=stackoffset or 0
if(msg) then print (msg) end
if dbg._consoleLevel==nil then
dbg._consoleLevel=0
else
dbg._consoleLevel=dbg._consoleLevel+1
end
if coarseLog~=nil and rank~=nil then
debug.sethook() -- stop all kinds of debugger
coarseLog("dbg.console called")
coarseLog(dbg.callstackString(1))
coarseLog(util.tostring(dbg.locals()))
dbg.callstack0()
return
end
local function at(line, index)
return string.sub(line, index, index)
end
local function handleStatement(statement)
local output
if string.find(statement, "=") and not string.find(statement, "==") then -- assignment statement
output={pcall(loadstring(statement))}
else -- function calls or print variables: get results
output={pcall(loadstring("return ("..statement..")"))}
if output[1]==false and output[2]=="attempt to call a nil value" then
-- statement
output={pcall(loadstring(statement))}
end
end
if output[1]==false then
print("Error! ", output[2])
else
if type(output[1])~='boolean' then
output[2]=output[1] -- sometimes error code is not returned for unknown reasons.
end
if type(output[2])=='table' then
if getmetatable(output[2]) and getmetatable(output[2]).__tostring then
print(output[2])
else
printTable(output[2])
end
elseif output[2] then
dbg.print(unpack(table.isubset(output, 2)))
elseif type(output[2])=='boolean' then
print('false')
end
end
end
local event
while true do
local cursor="[DEBUG"..dbg._consoleLevel.."] > "
line=dbg.readLine(cursor)
local cmd=at(line,1)
local cmd_arg=tonumber(string.sub(line,2))
if not (string.sub(line,2)=="" or cmd_arg) then
if not ( cmd=="r" and at(line,2)==" ") then
if not string.isOneOf(cmd, ":", ";") then
cmd=nil
end
end
end
if cmd=="h" or string.sub(line,1,4)=="help" then --help
if string.sub(line,1,5)=='help ' then
dbg.listLunaClasses(' '..line)
else
print('bt[level=3] : backtrace. Prints callstack')
print(';(lua statement) : eval lua statements. Usually, ";" can be omitted. e.g.) print(a) ')
print(' print or printTable can be omitted too e.g.) a ')
print(':(lua statement) : eval lua statements and exit debug console. e.g.) :dbg.startCount(10)')
print('s[number=1] : proceed n steps')
print('fi : finish the current function')
print('r filename [lineno] : run until execution of a line. filename can be a postfix substring. e.g.) r syn.lua 32')
print('c[level=2] : print source code at a stack level')
print('e[level=2] : show current line (at callstack level 2) in gedit editor')
print('v[level=2] : show current line (at callstack level 2) in vi editor')
print('c[level=2] : show nearby lines (at callstack level 2) here')
print('l[level=2] : print local variables. Results are saved into \'l variable.')
print(" e.g) DEBUG]>print('l.self.mVec)")
print('clist : list luna classes')
print('clist className : list functions in the class')
print('cont : exit debug mode')
print('global variables : Simply type "a" to print the content of a global variable "a".')
print('local variables : Simply type "`a" to print the content of a local variable "a".')
print('lua statement : run it')
end
elseif line=="cont" then break
elseif string.sub(line,1,2)=="bt" then dbg.callstack(tonumber(string.sub(line,3)) or 3)
elseif line=="clist" or string.sub(line,1,6)=='clist ' then
dbg.listLunaClasses(line)
elseif cmd=="c" or cmd=="v" then
if cmd_arg==nil then
local level=stackoffset
while true do
local info=debug.getinfo(level)
if info then
local a=string.sub(info.source, 1,1)
if a=='=' or a=='[' then
level=level+1
elseif select(1,string.find(info.source, 'mylib.lua')) then
level=level+1
else
break
end
else
level=level+1
if level>40 then break end
end
end
cmd_arg=level-stackoffset+1
print('c'..cmd_arg..':')
end
local level=(cmd_arg or 1)+stackoffset-1 -- -1 means 'excluding dbg.showCode'
local info=debug.getinfo(level)
if info then
local a=string.sub(info.source, 1,1)
if a=='=' or a=='[' then
print(info.source)
else
local ln=info.currentline
print(string.sub(info.source,2))
if cmd=="v" then
local fn=string.sub(info.source,2)
fn=os.relativeToAbsolutePath(fn)
os.vi_line(fn,info.currentline)
else
dbg.showCode(string.sub(info.source,2),ln)
dbg._saveLocals=dbg.locals(level+1,true)
end
end
else
print('no such level')
end
elseif cmd=="e" then
local info=debug.getinfo((cmd_arg or 1)+stackoffset-1)
if info then
os.execute(os.gedit_cmd(os.relativeToAbsolutePath(string.sub(info.source,2)),info.currentline)..'&')
end
elseif cmd==";" then
handleStatement(string.sub(line,2))
elseif cmd==":" then
handleStatement(string.sub(line,2))
break
elseif cmd=="s" or cmd=="'" then
local count=cmd_arg or 1
event={"s", count}
break
elseif cmd=="r" then
event={"r", string.sub(line, 3)}
break
elseif line=="f" or line=="fi" or line=="fin" or line=="fini" or line=="finish" then
event={"fi"}
break
elseif cmd=="l" then
local level=(cmd_arg or 1)
dbg._saveLocals=dbg.locals(level)
else
statement=string.gsub(line, '``', 'dbg._saveLocals')
statement=string.gsub(line, '`', 'dbg._saveLocals.')
handleStatement(statement)
end
end
dbg._consoleLevel=dbg._consoleLevel-1
if event then
if event[1]=="s" then
return dbg.step(event[2])
elseif event[1]=="r" then
return dbg.run(event[2])
elseif event[1]=="fi" then
return dbg.finish(event[2])
end
end
end
function dbg._stepFunc (event, line)
dbg._step=dbg._step+1
if dbg._step==dbg._nstep then
debug.sethook()
local level=2
local info=debug.getinfo(level)
if info then
if select(1,string.find (info.source, 'mylib.lua')) then
return dbg.step(1)
end
print(info.source, info.currentline)
dbg.showCode(string.sub(info.source,2), info.currentline)
dbg._saveLocals=dbg.locals(level+1,true)
end
return dbg.console()
end
end
function dbg.step(n)
dbg._step=0
dbg._nstep=n
debug.sethook(dbg._stepFunc, "l")
end
function dbg.callstack(level)
if level==nil then
level=1
end
while true do
local info=debug.getinfo(level)
local k=info.name
if k==nil then
break
else
print(info.short_src..":"..info.currentline..":"..k)
level=level+1
end
end
end
function dbg._finishFunc(event, line)
for i=1,16 do
-- search the current function from stack 1 to 16
local info=debug.getinfo(i)
if info then
if info.func==dbg._finishFunc_until then
return
end
else
break
end
end
debug.sethook()
local level=2
local info=debug.getinfo(level)
if info then
if select(1,string.find (info.source, 'mylib.lua')) then
return dbg.step(1)
end
print(info.source, info.currentline)
dbg.showCode(string.sub(info.source,2), info.currentline)
dbg._saveLocals=dbg.locals(level+1,true)
end
return dbg.console()
end
function dbg.finish(n)
local info=debug.getinfo(3)
if info then
if info.source=="=(tail call)" then
info=debug.getinfo(4)
end
print('run until ', info.name, 'finishes :', info.source, info.func)
dbg._finishFunc_until=info.func
debug.sethook(dbg._finishFunc, "l")
else
print('cannot find the current function')
end
end
function dbg.callstack0(level)
if level==nil then
level=1
end
while true do
local info=debug.getinfo(level)
if info==nil then break end
local k=info.name
if k==nil then
printTable(info)
level=level+1
else
print(info.short_src..":"..info.currentline..":"..k)
level=level+1
end
end
end
function dbg.locals(level, noprint)
local output={}
if level==nil then level=1 end
cur=1
while true do
if debug.getinfo(level, 'n')==nil then return output end
k,v=debug.getlocal(level, cur)
if k~=nil then
output[k]=v or "(nil)"
cur=cur+1
else
break
end
end
if not noprint then
os.print(output)
end
return output
end
function dbg.run(run_str) -- run_str example: a.lua 374
local tbl=string.tokenize(run_str, " ")
local filename=tbl[1]
local lineno=tonumber(tbl[2])
--print(filename..","..tostring(lineno))
if tonumber(filename)~=nil then
lineno=tonumber(filename)
filename=''
end
if filename=='' then
local info=debug.getinfo(3)
filename=info.source
if string.sub(info.source,1,1)=="=" then
info=debug.getinfo(4)
filename=info.source
end
end
print("stop at "..filename.." +",lineno)
local strlen=string.len(filename)*-1
dbg._runFuncParam={filename, strlen, lineno}
debug.sethook(dbg._runFunc, "l")
end
function dbg._runFunc (event, line)
local src=debug.getinfo(2).source
local param=dbg._runFuncParam
if string.sub(src, param[2])==param[1] and (param[3]==nil or line==param[3]) then
debug.sethook()
print(debug.getinfo(2).source, line)
return dbg.console()
end
end
-- outputs counts to trace.txt
function dbg.startCount(dbgtime)
if dbg.filePtr==nil then
if dbgtime then
print('Start re-counting until '..dbgtime)
else
print('Start counting.. ')
print('Output will go to trace.txt')
print('You can debug a crashing program by re-running the program using dbg.startCount(lastCount)')
dbg.filePtr, msg=io.open("trace.txt", "w")
if dbg.filePtr==nil then
print(msg)
return
end
end
dbg._dbgtime=dbgtime
dbg._count=0
--debug.sethook(dbg.countHookF, "l")
debug.sethook(dbg.countHookF, "c") -- much faster though less accurate
else
end
end
function dbg.countHookF(event)
local _count=dbg._count
local _dbgtime=dbg._dbgtime
if _dbgtime then
if _count>_dbgtime-100 then
local info=debug.getinfo(2)
print('coundown', _dbgtime-_count, info.name, info.short_src, info.currentline)
if _count==_dbgtime then
debug.sethook()
dbg.console()
end
end
else
local filePtr=dbg.filePtr
filePtr:seek("set", 0)
filePtr:write(_count)
filePtr:flush()
end
dbg._count=_count+1
end
function dbg.startTrace() -- minimal trace to trace.txt (much faster)
dbg.filePtr, msg=io.open("trace.txt", "w")
if dbg.filePtr==nil then
print(msg)
return
end
debug.sethook(dbg._mtraceHook, "c")
end
function dbg._mtraceHook (event, line)
local info=debug.getinfo(2)
line =line or info.currentline or ''
if line~=-1 then
local s = info.short_src
dbg.filePtr:write(s..":"..line..'\n')
end
dbg.filePtr:flush()
end
-- collection of utility functions that depends only on standard LUA. (no dependency on baseLib or mainLib)
-- all functions are platform independent
function string.trimSpaces(s)
s = string.gsub(s, '^%s+', '') --trim left spaces
s = string.gsub(s, '%s+$', '') --trim right spaces
return s
end
function string.startsWith(a,b)
return string.sub(a,1,string.len(b))==b
end
function os.capture(cmd, raw)
local s
if os.isApple() then
-- macport's lua51 package does not support io.popen -.-;
os.execute(cmd ..">/tmp/capture.txt")
local f = assert(io.open('/tmp/capture.txt', 'r'))
s = assert(f:read('*a'))
f:close()
else
local f = assert(io.popen(cmd, 'r'))
s = assert(f:read('*a'))
f:close()
end
if raw then return s end
s = string.gsub(string.trimSpaces(s), '[\n\r]+', ' ')
return s
end
function os.rightTokenize(str, sep, includeSep)
--deprecated
return string.rightTokenize(str, sep, includeSep)
end
function string.rightTokenize(str, sep, includeSep)
local len=string.len(str)
for i=len,1,-1 do
local s=string.find(string.sub(str, i,i),sep)
if s then
if includeSep then
return string.sub(str, 1, i-1)..sep, string.sub(str, i+1)
end
return string.sub(str, 1, i-1), string.sub(str, i+1)
end
end
return "", str
end
function os.sleep(aa)
local a=os.clock()
while os.difftime(os.clock(),a)<aa do -- actually busy waits rather then sleeps
end
end
function string.isLongestMatched(str, patterns, prefix, postfix)
local matched=nil
local ll=0
for k,ip in ipairs(patterns) do
if str==ip then return k end -- exact match has the highest priority
if prefix then ip=prefix..ip end
if postfix then ip=ip..postfix end
local idx,idx2=string.find(str, ip)
if idx~=nil then
if idx2-idx>=ll then
matched=k
ll=idx2-idx
end
end
end
return matched
end
function string.isMatched(str, patterns)
local matched=nil
for k,ip in ipairs(patterns) do
local idx=string.find(str, ip)
if idx~=nil or str==ip then
matched=k
end
end
return matched
end
function string.findLastOf(str, pattern)
local lastS=nil
local idx=0
while idx+1<#str do
idx=string.find(str, pattern, idx+1)
if idx then
lastS=idx
else
break
end
end
return lastS
end
function os.isUnix() -- has posix commands
local isWin=os.isWindows()
if isWin then
if os.isCygwin() then
return true
end
return false
end
return true
end
function os.isMsysgit()
local isMsysgit=string.find(string.lower(os.getenv('PATH') or 'nil'), 'msysgit')~=nil
return isMsysgit
end
function os.isCygwin()
local isCygwin=string.find(string.lower(os.getenv('PATH') or 'nil'), 'cygdrive')~=nil
return isCygwin
end
function os.isWindows()
local isWin=string.find(string.lower(os.getenv('OS') or 'nil'),'windows')~=nil
return isWin
end
function os.isApple()
return false -- deprecated
end
-- LUAclass method is for avoiding so many bugs in luabind's "class" method (especially garbage collection).
-- usage: MotionLoader=LUAclass()
-- ... MotionLoader:__init(a,b,c)
-- VRMLloader=LUAclass(MotionLoader)
-- ... VRMLloader:__init(a,b,c)
-- MotionLoader.__init(self,a,b,c)
-- end
-- loader=VRMLloader(a,b,c)
function LUAclass(baseClass)
local classobj={}
classobj.__index=classobj
classobj.new=function (classobj, ...)
local new_inst={}
setmetatable(new_inst, classobj)
new_inst:__init(...)
return new_inst
end
if baseClass~=nil then
if baseClass.luna_class then
local derivedName=baseClass.luna_class..'_derived'
while __luna['_'..derivedName] do
derivedName=derivedName..'_'
end
__luna['_'..derivedName]=classobj
classobj.luna_class=derivedName
if baseClass.new_modified_T==nil then
print("Error!".. baseClass.luna_class.." doesn't have new_modified_T member!")
print("Did you forget to set isLuaInheritable=true?")
end
for k,v in pairs(baseClass) do
classobj[k]=v
end
classobj.new=function (classobj, ...)
local new_inst=classobj.new_modified_T(classobj, '_'..derivedName) -- has to have default constructor.
new_inst:__init(...)
return new_inst
end
end
setmetatable(classobj, {__index=baseClass,__call=classobj.new})
else
setmetatable(classobj, {__call=classobj.new})
end