-
Notifications
You must be signed in to change notification settings - Fork 15
/
Engine2.cpp
1625 lines (1483 loc) · 44.2 KB
/
Engine2.cpp
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
/*
* Copyright 2019, 2020 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the JuaJIT BC Viewer application.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to me@rochus-keller.ch.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include <lua.hpp>
#include "LuaJitComposer.h"
#include "Engine2.h"
#include <QCoreApplication>
#include <math.h>
#include <QtDebug>
#include <iostream>
#include <QTime>
#include <QFileInfo>
#include <QDir>
using namespace Lua;
static Engine2* s_this = 0;
static const char* s_path = "path";
static const char* s_cpath = "cpath";
static Engine2::Breaks s_dummy;
static const int s_aliveCount = 10000;
static QMap<QByteArray,QByteArray> preloads; // name -> buffer
int Engine2::_print (lua_State *L)
{
Engine2* e = Engine2::getInst();
Q_ASSERT( e != 0 );
QByteArray val1;
int n = lua_gettop(L); /* number of arguments */
int i;
lua_getglobal(L, "tostring");
for (i=1; i<=n; i++)
{
const char *s;
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, i); /* value to print */
lua_call(L, 1, 1);
s = lua_tostring(L, -1); /* get result */
if (s == NULL)
return luaL_error(L, "`tostring' must return a string to `print'");
if (i>1)
val1 += "\t";
val1 += s;
lua_pop(L, 1); /* pop result */
}
if( !val1.endsWith('\n') )
val1 += '\n';
try
{
e->notify( Print, val1 );
}catch( std::exception& e )
{
luaL_error( L, "Error calling host: %s", e.what() );
}catch( ... )
{
luaL_error( L, "Unknown exception while calling host" );
}
return 0;
}
extern "C" {
static int dbgout(lua_State* L)
{
const int top = lua_gettop(L);
QByteArray buf;
QTextStream ts(&buf,QIODevice::WriteOnly);
for( int i = 1; i <= top; i++ )
{
if( i != 1 )
ts << "\t";
switch( lua_type(L,i) )
{
case LUA_TNIL:
ts << "nil";
break;
case LUA_TSTRING:
{
const char* str = lua_tostring(L,i);
ts << str;
}
break;
case LUA_TBOOLEAN:
ts << ( lua_toboolean(L,i) ? true : false );
break;
case LUA_TNUMBER:
{
const lua_Number n = lua_tonumber(L,i);
const int nn = n;
if( n == lua_Number(nn) )
ts << nn;
else
ts << n;
}
break;
case LUA_TLIGHTUSERDATA:
ts << "LUA_TLIGHTUSERDATA";
break;
case LUA_TTABLE:
ts << "LUA_TTABLE";
break;
case LUA_TFUNCTION:
ts << "LUA_TFUNCTION";
break;
case LUA_TUSERDATA:
ts << "LUA_TUSERDATA";
break;
case LUA_TTHREAD:
ts << "LUA_TTHREAD";
break;
default:
ts << "<unknown>";
break;
}
}
ts.flush();
qDebug() << buf.constData();
return 0;
}
static int doPreload(lua_State* L)
{
const char* name = lua_tostring(L,-1);
if( preloads.contains(name) )
{
QByteArray source = preloads.value(name);
const int status = luaL_loadbuffer( L, source, source.size(), name );
if( status != 0 )
lua_error(L);
lua_call(L,0,1);
}else
lua_pushnil(L);
return 1;
}
static int _flush(lua_State* L)
{
return 0; // NOP
}
}
int Engine2::_writeStdout(lua_State* L)
{
return _writeImp(L,false);
}
int Engine2::_writeStderr(lua_State* L)
{
return _writeImp(L,true);
}
int Engine2::_writeImp(lua_State *L, bool err) {
QByteArray buf;
QTextStream out(&buf);
int arg = 2;
int nargs = lua_gettop(L) - 1;
for (; nargs--; arg++) {
if (lua_type(L, arg) == LUA_TNUMBER) {
/* optimization: could be done exactly as for strings */
out << lua_tonumber(L, arg);
}
else {
size_t l;
const char *s = luaL_checklstring(L, arg, &l);
out << s;
}
}
out.flush();
Engine2* e = Engine2::getInst();
Q_ASSERT( e != 0 );
try
{
e->notify( err ? Engine2::Cerr : Engine2::Cout, buf );
}catch( std::exception& e )
{
luaL_error( L, "Error calling host: %s", e.what() );
}catch( ... )
{
luaL_error( L, "Unknown exception while calling host" );
}
return 0;
}
int Engine2::_prettyTraceLoc(lua_State* L)
{
const QByteArray loc = lua_tostring( L, 1 );
const QByteArray source = lua_tostring( L, 2 );
//const int linedefined = lua_tointeger( L, 3 );
const int colon = loc.lastIndexOf(':');
if( colon == -1 )
lua_pushvalue(L,1);
else
{
const int line = loc.mid(colon+1).toInt();
QByteArray res = loc.left(colon+1);
if( res.startsWith("0x") && !source.isEmpty() )
res = QFileInfo(source).fileName().toUtf8() + ":"; // rhs is already linedefined
if( JitComposer::isRowCol() )
res += QByteArray::number( JitComposer::unpackRow(line) ) + ":"
+ QByteArray::number( JitComposer::unpackCol(line) );
else
res += QByteArray::number(line);
lua_pushstring( L, res.constData() );
}
return 1;
}
Engine2::Engine2(QObject *p):QObject(p),
d_ctx( 0 ), d_debugging( false ), d_running(false), d_waitForCommand(false),
d_dbgCmd(RunToBreakPoint), d_defaultDbgCmd(RunToBreakPoint), d_activeLevel(0), d_dbgShell(0),
d_printToStdout(false), d_aliveSignal(false), d_mode(LineMode), d_aliveCount(0), d_stepCallDepth(0),
d_stepOverSync(false)
{
if( !restart() )
throw Exception( "failed to create engine" );
}
Engine2::~Engine2()
{
if( d_ctx )
lua_close( d_ctx );
}
void Engine2::addStdLibs()
{
addLibrary( TABLE );
addLibrary( STRING );
addLibrary( MATH );
}
bool Engine2::restart()
{
// necessary because there seems to be no other way to get rid of ffi.cdef declarations
if( isExecuting() )
return false;
if( d_ctx )
{
lua_close( d_ctx );
d_ctx = 0;
}
lua_State* ctx = lua_open();
if( ctx == 0 )
{
qCritical() << "Not enough memory to create Lua context";
return false;
}
LUAJIT_VERSION_SYM();
d_ctx = ctx;
addLibrary( BASE ); // Das muss hier stehen, sonst wird ev. print wieder überschrieben
#ifndef LUA_ENGINE_USE_DEFAULT_PRINT
lua_pushcfunction( ctx, _print );
lua_setglobal( ctx, "print" );
#endif
lua_pushcfunction( ctx, dbgout );
lua_setglobal( ctx, "dbgout" );
lua_pushcfunction( ctx, _prettyTraceLoc );
lua_setglobal( ctx, "_prettyTraceLoc" );
if( d_debugging )
{
d_debugging = false;
setDebug(true);
}
if( d_aliveSignal )
{
d_aliveSignal = false;
setAliveSignal(true);
}
return true;
}
void Engine2::addLibrary(Lib what)
{
if( d_running )
return;
switch( what )
{
case BIT:
lua_pushcfunction( d_ctx, luaopen_bit );
lua_pushstring(d_ctx, LUA_BITLIBNAME );
lua_call(d_ctx, 1, 0);
break;
case JIT:
lua_pushcfunction( d_ctx, luaopen_jit );
lua_pushstring(d_ctx, LUA_JITLIBNAME );
lua_call(d_ctx, 1, 0);
break;
case FFI:
lua_pushcfunction( d_ctx, luaopen_ffi );
lua_pushstring(d_ctx, LUA_FFILIBNAME );
lua_call(d_ctx, 1, 0);
break;
#if LUA_VERSION_NUM >= 501
case PACKAGE:
lua_pushcfunction( d_ctx, luaopen_package );
lua_pushstring(d_ctx, LUA_LOADLIBNAME );
lua_call(d_ctx, 1, 0);
// am 28.12.11 Konzept geändert. Neu werden wieder Lua-Standard-Loader verwendet.
break;
#endif
case BASE:
lua_pushcfunction( d_ctx, luaopen_base );
lua_pushstring(d_ctx, "" );
lua_call(d_ctx, 1, 0);
break;
case REMOVE_LOADS:
lua_pushnil( d_ctx );
lua_setglobal( d_ctx, "dofile" );
lua_pushnil( d_ctx );
lua_setglobal( d_ctx, "loadfile" );
lua_pushnil( d_ctx );
lua_setglobal( d_ctx, "load" );
lua_pushnil( d_ctx );
lua_setglobal( d_ctx, "loadstring" );
break;
case TABLE:
lua_pushcfunction( d_ctx, luaopen_table );
lua_pushstring(d_ctx, LUA_TABLIBNAME );
lua_call(d_ctx, 1, 0);
break;
case STRING:
lua_pushcfunction( d_ctx, luaopen_string );
lua_pushstring(d_ctx, LUA_STRLIBNAME );
lua_call(d_ctx, 1, 0);
break;
case MATH:
lua_pushcfunction( d_ctx, luaopen_math );
lua_pushstring(d_ctx, LUA_MATHLIBNAME );
lua_call(d_ctx, 1, 0);
break;
case IO:
lua_pushcfunction( d_ctx, luaopen_io );
lua_pushstring(d_ctx, LUA_IOLIBNAME );
lua_call(d_ctx, 1, 0);
#ifndef LUA_ENGINE_USE_DEFAULT_PRINT
// redirect stdout
lua_createtable(d_ctx,0,1); // file
lua_pushcfunction( d_ctx, _writeStdout );
lua_setfield(d_ctx, -2, "write" );
lua_pushcfunction( d_ctx, _flush );
lua_setfield(d_ctx, -2, "flush" );
lua_getglobal(d_ctx, LUA_IOLIBNAME );
lua_pushvalue(d_ctx,-2);
lua_setfield(d_ctx, -2, "stdout" );
lua_pop(d_ctx,2); // file + io
// redirect stderr
lua_createtable(d_ctx,0,1); // file
lua_pushcfunction( d_ctx, _writeStderr );
lua_setfield(d_ctx, -2, "write" );
lua_pushcfunction( d_ctx, _flush );
lua_setfield(d_ctx, -2, "flush" );
lua_getglobal(d_ctx, LUA_IOLIBNAME );
lua_pushvalue(d_ctx,-2);
lua_setfield(d_ctx, -2, "stderr" );
lua_pop(d_ctx,2); // file + io
#endif
break;
#if LUA_VERSION_NUM >= 501
case OS:
lua_pushcfunction( d_ctx, luaopen_os );
lua_pushstring(d_ctx, LUA_OSLIBNAME );
lua_call(d_ctx, 1, 0);
break;
#endif
case DBG:
lua_pushcfunction( d_ctx, luaopen_debug );
lua_pushstring(d_ctx, LUA_DBLIBNAME );
lua_call(d_ctx, 1, 0);
break;
break;
default:
break;
}
}
bool Engine2::pushFunction(const QByteArray &source, const QByteArray& name )
{
d_lastError = "";
const int status = luaL_loadbuffer( d_ctx, source, source.size(), name );
switch( status )
{
case 0:
// Stack: function
break;
case LUA_ERRSYNTAX:
case LUA_ERRMEM:
d_lastError = lua_tostring( d_ctx, -1 );
lua_pop( d_ctx, 1 ); /* remove error message */
// Stack: -
return false;
}
return true;
}
const char* Engine2::getVersion() const
{
#if LUA_VERSION_NUM >= 501
return LUA_RELEASE;
#else
return lua_version();
#endif
}
static int file_writer (lua_State *L, const void* b, size_t size, void* f)
{
Q_UNUSED(L);
int res = ::fwrite( b, size, 1, (FILE*)f );
if( res != 1 )
return -1; // = error
return 0; // = no error
}
bool Engine2::saveBinary(const QByteArray& source, const QByteArray& name, const QByteArray& path)
{
d_lastError.clear();
FILE* f = ::fopen( path,"wb" );
if( f == 0 )
{
d_lastError = "Unable to open file for writing";
return false;
}
if( !pushFunction( source, name ) )
return false;
// Stack: Function
const int res = lua_dump( d_ctx, file_writer, f);
::fclose( f );
lua_pop( d_ctx, 1 ); // Function
#if LUA_VERSION_NUM >= 501
if( res != 0 )
#else
if( res != 1 )
#endif
{
d_lastError = "Unable to write compiled script";
return false;
}else
return true;
}
bool Engine2::executeCmd(const QByteArray &source, const QByteArray &name)
{
if( d_running )
{
d_lastError = "Cannot run commands while another script is running!";
error( d_lastError );
return false;
}
d_lastError.clear();
d_waitForCommand = false;
d_aliveCount = false;
if( !pushFunction( source, name ) )
{
error( d_lastError );
return false;
}
const bool res = runFunction( 0, LUA_MULTRET );
if( !res )
error( d_lastError );
return res;
}
bool Engine2::executeFile(const QByteArray &path)
{
if( d_running )
{
d_lastError = "Cannot run script while another script is running!";
error( d_lastError );
return false;
}
d_lastError.clear();
d_waitForCommand = false;
d_aliveCount = 0;
switch( luaL_loadfile( d_ctx, path ) )
{
case 0: // no Error
break;
case LUA_ERRFILE:
case LUA_ERRSYNTAX:
case LUA_ERRMEM:
d_lastError = lua_tostring( d_ctx, -1);
lua_pop( d_ctx, 1 ); /* remove error message */
error( d_lastError );
return false;
}
const bool res = runFunction( 0, LUA_MULTRET );
if( !res )
error( d_lastError );
return res;
}
bool Engine2::runFunction(int nargs, int nresults)
{
if( d_running )
{
lua_pop(d_ctx, 1 + nargs);
return false;
}
int preTop = lua_gettop( d_ctx );
if( d_waitForCommand )
{
d_lastError = "Cannot run another Lua function while script is waiting in debugger!";
lua_pop( d_ctx, nargs + 1 ); // funktion + args
if( nresults != LUA_MULTRET )
Q_ASSERT( lua_gettop( d_ctx ) == ( preTop - nargs - 1 ) );
return false;
}
lua_pushcfunction( d_ctx, ErrHandler );
const int errf = preTop-nargs;
lua_insert(d_ctx,errf);
preTop = lua_gettop( d_ctx );
d_lastError.clear();
d_dbgCmd = d_defaultDbgCmd;
d_returns.clear();
notifyStart();
// Lua: All arguments and the function value are popped from the stack when the function is called.
int err = lua_pcall( d_ctx, nargs, nresults, errf );
switch( err )
{
case LUA_ERRRUN:
if( d_dbgCmd != AbortSilently)
{
if( d_dbgCmd == Abort )
d_lastError = "Execution terminated by user";
else
d_lastError = lua_tostring( d_ctx, -1 );
lua_pop( d_ctx, 1 ); /* remove error message */
lua_pop( d_ctx, 1 ); // remove ErrHandler
nresults = 0;
notifyEnd();
return false;
}//else
err = 0;
break;
case LUA_ERRMEM:
d_lastError = "Lua memory exception";
notifyEnd();
return false;
case LUA_ERRERR:
// should not happen
d_lastError = "Lua unknown error";
notifyEnd();
return false;
}
// func + nargs were popped by pcall; nresults were pushed
preTop = preTop - 1 - nargs;
int postTop = lua_gettop( d_ctx );
for( int i = preTop + 1; i <= postTop; i++ )
d_returns << getValueString( i );
if( postTop - preTop )
lua_pop(d_ctx, postTop - preTop );
lua_pop( d_ctx, 1 ); // remove ErrHandler
notifyEnd();
return (err == 0);
}
bool Engine2::addSourceLib(const QByteArray& source, const QByteArray& libname)
{
if( d_waitForCommand )
{
d_lastError = "Cannot run another Lua function while script is waiting in debugger!";
return false;
}
const int prestack = lua_gettop(d_ctx);
lua_pushcfunction( d_ctx, ErrHandler );
const int errf = lua_gettop(d_ctx);
d_lastError.clear();
d_returns.clear();
if( !pushFunction( source, libname ) )
return false;
d_dbgCmd = d_defaultDbgCmd;
d_aliveCount = 0;
notifyStart();
const int err = lua_pcall( d_ctx, 0, 1, errf );
switch( err )
{
case LUA_ERRRUN:
d_lastError = lua_tostring( d_ctx, -1 );
lua_pop( d_ctx, 1 ); /* remove error message */
lua_pop( d_ctx, 1 ); // remove ErrHandler
Q_ASSERT( prestack == lua_gettop(d_ctx) );
notifyEnd();
return false;
case LUA_ERRMEM:
d_lastError = "Lua memory exception";
notifyEnd();
return false;
case LUA_ERRERR:
// should not happen
d_lastError = "Lua unknown error";
notifyEnd();
return false;
}
int stack = lua_gettop(d_ctx);
// stack: lib
// sets it as the value of the global variable libname, sets it as the value of package.loaded[libname]
lua_pushvalue(d_ctx, -1 ); // stack: lib lib
lua_setfield(d_ctx, LUA_GLOBALSINDEX, libname.constData() ); // stack: lib
lua_getfield(d_ctx, LUA_GLOBALSINDEX, "package"); // stack: lib package
lua_getfield(d_ctx, -1, "loaded"); // stack: lib package loaded
lua_pushvalue(d_ctx, -3 ); // stack: lib package loaded lib
lua_setfield(d_ctx, -2, libname.constData() ); // stack: lib package loaded
lua_pop(d_ctx,3); // stack: -
lua_pop( d_ctx, 1 ); // remove ErrHandler
stack = lua_gettop(d_ctx);
Q_ASSERT( prestack == stack );
notifyEnd();
return true;
}
bool Engine2::addPreloadLib(const QByteArray& source, const QByteArray& libname)
{
if( d_waitForCommand )
{
d_lastError = "Cannot run another Lua function while script is waiting in debugger!";
return false;
}
preloads[libname] = source;
lua_getfield(d_ctx, LUA_GLOBALSINDEX, "package"); // stack: package
lua_getfield(d_ctx, -1, "preload"); // stack: package preload
const int preload = lua_gettop(d_ctx);
lua_pushstring(d_ctx, libname.constData() );
lua_pushcfunction(d_ctx, doPreload);
lua_rawset(d_ctx,preload);
lua_pop(d_ctx,1); // remove preload
// remove a possibly remaining version of the last run from package.loaded
// so it is loaded (and the module function is run) again
lua_getfield(d_ctx, -1, "loaded"); // stack: package loaded
const int loaded = lua_gettop(d_ctx);
lua_pushnil(d_ctx); // stack: package loaded nil
lua_setfield(d_ctx, loaded, libname.constData() ); // stack: package loaded
lua_pop(d_ctx,2); // remove package loaded
return true;
}
void Engine2::collect()
{
if( d_ctx )
lua_gc( d_ctx, LUA_GCCOLLECT, 0 );
}
void Engine2::setActiveLevel(int level)
{
if( d_activeLevel == level )
return;
d_activeLevel = level;
notify( ActiveLevel, d_curScript, lineForNotify() );
}
int Engine2::ErrHandler( lua_State* L )
{
// This function leaves the error message just where it is
Engine2* e = Engine2::getInst();
//const char* msg = lua_tostring(L, 1 ); // TEST
if( e == 0 )
{
qWarning() << "Engine2::ErrHandler: getInst returns 0";
return 1;
}
if( e->d_dbgCmd == Abort || e->d_dbgCmd == AbortSilently )
return 1; // don't break if user wants to abort
StackLevel l = e->getStackLevel(0,false);
e->d_breakHit = false;
e->d_curScript = l.d_source;
e->d_curRowCol = l.d_line;
e->d_activeLevel = 0;
e->d_waitForCommand = true;
e->notify( ErrorHit, e->d_curScript, e->lineForNotify() );
if( e->d_dbgShell )
e->d_dbgShell->handleBreak( e, e->d_curScript, e->lineForBreak() );
e->d_waitForCommand = false;
if( e->d_dbgCmd == Abort || e->d_dbgCmd == AbortSilently )
e->notify( Aborted );
else
e->notify( Continued );
return 1;
}
void Engine2::debugHook(lua_State *L, lua_Debug *ar)
{
Engine2* e = Engine2::getInst();
Q_ASSERT( e != 0 );
const StackLevel l = e->getStackLevel(0,false,ar);
#if 0
{
QByteArray action;
int depth = e->d_stepCallDepth;
switch(ar->event)
{
case LUA_HOOKRET:
if( !l.d_inC )
depth--;
action = "RETURN";
break;
case LUA_HOOKCALL:
if( !l.d_inC )
depth++;
if( l.d_inC )
action = "NATIVE_CALL";
else
action = "CALL";
break;
case LUA_HOOKLINE:
action = "LINE";
break;
case LUA_HOOKCOUNT:
action = "COUNT";
break;
case LUA_HOOKTAILRET:
action = "TAILRET";
break;
}
qDebug() << "Engine2::debugHook" << "calldepth" << depth << action
<< l.d_source << JitComposer::unpackRow2(l.d_line) << JitComposer::unpackCol2(l.d_line);
}
#endif
if( ar->event == LUA_HOOKCALL )
{
// HOOKRET comes when already in the function to be called
if( e->d_dbgCmd == StepOut || e->d_dbgCmd == StepOver )
{
if( !l.d_inC )
e->d_stepCallDepth++;
}
return;
}
if( ar->event == LUA_HOOKRET ) // LUA_HOOKRET isn't fired in case of CALLT!!! Use _LJTOOLS_DONT_CREATE_TAIL_CALLS to avoid.
{
// HOOKRET comes when still in the returning function; even the pc is still the same as the previous HOOKLINE
if( e->d_dbgCmd == StepOut || e->d_dbgCmd == StepOver )
{
Q_ASSERT( !l.d_inC && !e->d_stepBreak.first.isEmpty() ); // see runToNextLine
e->d_stepCallDepth--;
if( e->d_stepCallDepth < 0 && e->d_stepBreak.first == l.d_source &&
e->d_stepBreak.second == l.d_lineDefined )
e->d_dbgCmd = Engine2::StepNext;
// Change StepOut to StepNext if we leave the function in which StepOut was called (i.e. callDepth<0)
// Change StepOver to StepNext if we're about to leave the function where StepOver was called (i.e. callDepth<0)
else if( e->d_dbgCmd == StepOver && e->d_stepCallDepth == 0 )
{
// if we step over a procedure call reset curLine/Script to the original value to avoid that
// we break twice on the line of the function call (by avoiding lineChanged to become true)
e->d_curScript = e->d_stepBreak.first;
e->d_curRowCol = e->d_stepCurRowCol;
e->d_dbgCmd = Engine2::StepNext;
e->d_stepOverSync = true;
}
}
return;
}
Q_ASSERT( ar->event == LUA_HOOKLINE || ar->event == LUA_HOOKCOUNT );
e->d_aliveCount++;
bool lineChanged = false;
switch( e->d_mode )
{
case LineMode:
if( e->d_stepOverSync && e->d_stepCallDepth == 0 && JitComposer::isRowCol() )
e->d_dbgCmd = StepOver; // happens if arg or a call is yet another call on the same line
else
lineChanged = ( JitComposer::unpackRow(e->d_curRowCol) != JitComposer::unpackRow(l.d_line) ||
e->d_curScript != l.d_source );
break;
case RowColMode:
case PcMode:
lineChanged = ( unpackDeflinePc(e->d_curRowCol).second != l.d_line || e->d_curScript != l.d_source );
break;
}
e->d_curScript = l.d_source;
if( e->d_mode == PcMode )
e->d_curRowCol = packDeflinePc( JitComposer::unpackRow(l.d_lineDefined),l.d_line);
else
e->d_curRowCol = l.d_line;
if( e->d_mode == PcMode || lineChanged )
{
e->d_breakHit = false;
e->d_activeLevel = 0;
const quint32 line = e->lineForBreak();
if( e->d_dbgCmd == Engine2::StepNext ||
( e->d_dbgCmd == Engine2::StepOver && e->d_stepCallDepth == 0 &&
e->d_stepBreak.first == l.d_source &&
e->d_stepBreak.second == l.d_lineDefined ) )
// the Engine2::StepOver case is still relevant here because we can step over non-calls too.
{
e->d_waitForCommand = true;
e->d_breakHit = true;
e->notify( LineHit, e->d_curScript, e->lineForNotify() );
if( e->d_dbgShell )
e->d_dbgShell->handleBreak( e, e->d_curScript, line );
e->d_waitForCommand = false;
}else if( e->d_breaks.value( e->d_curScript ).contains( line ) )
{
e->d_waitForCommand = true;
e->d_breakHit = true;
e->notify( BreakHit, e->d_curScript,e->lineForNotify() );
if( e->d_dbgShell )
e->d_dbgShell->handleBreak( e, e->d_curScript, line );
e->d_waitForCommand = false;
}
if( e->d_dbgCmd == Abort || e->d_dbgCmd == AbortSilently )
{
lua_pushnil(L);
lua_error(L);
}
e->notify( Continued );
}else if( /* e->d_aliveSignal && */ e->d_aliveCount > s_aliveCount / 2 && e->d_dbgShell )
{
// even if aliveSignal is not enabled it is necessary to give calculation time to the shell
// from time to time during long runs without breaks
e->d_dbgShell->handleAliveSignal( e );
e->d_aliveCount = 0;
#if 0
// why should this be useful?
if( e->isStepping() )
{
e->d_waitForCommand = true;
e->d_breakHit = true;
e->notify( LineHit, e->d_curScript, e->d_curLine );
if( e->d_dbgShell )
e->d_dbgShell->handleBreak( e, e->d_curScript, e->d_curLine );
e->d_waitForCommand = false;
}
#endif
}
e->d_stepOverSync = false;
//else // this doesn't seem to be necessary, even counterproductive
// lua_pop(L, 1); // function
}
void Engine2::aliveSignal(lua_State* L, lua_Debug* ar)
{
Engine2* e = Engine2::getInst();
Q_ASSERT( e != 0 );
if( e->d_dbgShell )
{
e->d_dbgShell->handleAliveSignal(e);
if( e->isStepping() )
{
e->d_waitForCommand = true;
e->notify( LineHit, e->d_curScript, e->lineForNotify() );
e->d_dbgShell->handleBreak( e, e->d_curScript, e->lineForBreak() );
e->d_waitForCommand = false; // TODO
}
}
}
static int array_writer (lua_State *L, const void* b, size_t size, void* f)
{
Q_UNUSED(L);
QByteArray* ba = (QByteArray*)f;
ba->append( QByteArray( (const char *) b, size ) );
return 0; // = no error
}
QByteArray Engine2::getBinaryFromFunc(lua_State *L)
{
if( lua_type(L, -1) != LUA_TFUNCTION )
return QByteArray();
QByteArray ba;
const int res = lua_dump( L, array_writer, &ba );
if( res != 0 )
ba.clear();
return ba;
}
void Engine2::notifyStart()
{
d_running = true;
notify( Started );
}
void Engine2::notifyEnd()
{
d_running = false;
notify( (d_dbgCmd == Abort || d_dbgCmd == AbortSilently)? Aborted : Finished );
}
void Engine2::setDebug(bool on)
{
if( d_debugging == on )
return;
if( on )
{
if( d_mode == PcMode )
lua_sethook( d_ctx, debugHook, LUA_MASKCOUNT | LUA_MASKRET | LUA_MASKCALL, 1);
else
lua_sethook( d_ctx, debugHook, LUA_MASKLINE | LUA_MASKRET | LUA_MASKCALL, 1);
}else if( d_aliveSignal )
lua_sethook( d_ctx, aliveSignal, LUA_MASKCOUNT, s_aliveCount); // get's a hook call with each bytecode op when 1
else
lua_sethook( d_ctx, 0, 0, 0);
d_debugging = on;
}
void Engine2::setJit(bool on)
{
int flags = LUAJIT_MODE_ENGINE;
if( on )
flags |= LUAJIT_MODE_ON;
else
flags |= LUAJIT_MODE_OFF;
luaJIT_setmode( d_ctx, 0, flags );
}
void Engine2::setAliveSignal(bool on)
{
if( d_aliveSignal == on )
return;
d_aliveSignal = on;
if( d_debugging )
return;
d_aliveCount = 0;
if( on )
lua_sethook( d_ctx, aliveSignal, LUA_MASKCOUNT, s_aliveCount);
else
lua_sethook( d_ctx, aliveSignal, 0, 0);
}
void Engine2::setDebugMode(Engine2::Mode m)
{
d_mode = m;
if( d_debugging )
{
setDebug(false);
setDebug(true);
}
}
void Engine2::setBytecodeMode(bool on)
{
d_mode = on ? PcMode : LineMode;
if( d_debugging )
{
setDebug(false);
setDebug(true);