/
Interpreter.cpp
4305 lines (3860 loc) · 123 KB
/
Interpreter.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 (c) Martin Schweiger
// Licensed under the MIT License
#define INTERPRETER_IMPLEMENTATION
#include "Interpreter.h"
#include "VesselAPI.h"
#include "MFDAPI.h"
#include "DrawAPI.h"
#include <list>
using std::min;
using std::max;
/***
Module oapi: General Orbiter API interface functions
@module oapi
*/
std::list<NOTEHANDLE *> g_notehandles;
int OpenHelp (void *context);
// ============================================================================
// nonmember functions
/***
A table representing a 3D cartesian vector.
@field x x-component
@field y y-component
@field z z-component
@table vector
*/
VECTOR3 lua_tovector (lua_State *L, int idx)
{
VECTOR3 vec;
lua_getfield (L, idx, "x");
vec.x = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "y");
vec.y = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "z");
vec.z = lua_tonumber (L, -1); lua_pop (L,1);
return vec;
}
// ============================================================================
// class Interpreter
Interpreter::Interpreter ()
{
L = luaL_newstate(); // create new Lua context
is_busy = false; // waiting for input
is_term = false; // no attached terminal by default
bExecLocal = false; // flag for locally created mutexes
bWaitLocal = false;
jobs = 0; // background jobs
status = 0; // normal
term_verbose = 0; // verbosity level
postfunc = 0;
postcontext = 0;
// store interpreter context in the registry
lua_pushlightuserdata (L, this);
lua_setfield (L, LUA_REGISTRYINDEX, "interp");
hExecMutex = CreateMutex (NULL, TRUE, NULL);
hWaitMutex = CreateMutex (NULL, FALSE, NULL);
}
Interpreter::~Interpreter ()
{
lua_close (L);
if (hExecMutex) CloseHandle (hExecMutex);
if (hWaitMutex) CloseHandle (hWaitMutex);
}
void Interpreter::Initialise ()
{
luaL_openlibs (L); // load the default libraries
LoadAPI (); // load default set of API interface functions
LoadVesselAPI (); // load vessel-specific part of API
LoadLightEmitterMethods (); // load light source methods
LoadMFDAPI (); // load MFD methods
LoadSketchpadAPI (); // load Sketchpad methods
LoadAnnotationAPI (); // load screen annotation methods
LoadStartupScript (); // load default initialisation script
}
int Interpreter::Status () const
{
return status;
}
bool Interpreter::IsBusy () const
{
return is_busy;
}
void Interpreter::Terminate ()
{
status = 1;
}
void Interpreter::PostStep (double simt, double simdt, double mjd)
{
if (postfunc) {
postfunc (postcontext);
postfunc = 0;
postcontext = 0;
}
}
int Interpreter::lua_tointeger_safe (lua_State *L, int idx, int prmno, const char *funcname)
{
AssertPrmType(L, idx, prmno, PRMTP_NUMBER, funcname);
return lua_tointeger(L, idx);
}
int Interpreter::lua_tointeger_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tointeger_safe (L, idx, idx, funcname);
}
int Interpreter::luamtd_tointeger_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tointeger_safe (L, idx, idx-1, funcname);
}
double Interpreter::lua_tonumber_safe (lua_State *L, int idx, int prmno, const char *funcname)
{
AssertPrmType(L, idx, prmno, PRMTP_NUMBER, funcname);
return lua_tonumber(L, idx);
}
double Interpreter::lua_tonumber_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tonumber_safe (L, idx, idx, funcname);
}
double Interpreter::luamtd_tonumber_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tonumber_safe (L, idx, idx-1, funcname);
}
bool Interpreter::lua_toboolean_safe (lua_State *L, int idx, int prmno, const char *funcname)
{
AssertPrmType(L, idx, prmno, PRMTP_BOOLEAN, funcname);
return lua_toboolean(L, idx) != 0;
}
bool Interpreter::lua_toboolean_safe (lua_State *L, int idx, const char *funcname)
{
return lua_toboolean_safe (L, idx, idx, funcname);
}
bool Interpreter::luamtd_toboolean_safe (lua_State *L, int idx, const char *funcname)
{
return lua_toboolean_safe (L, idx, idx-1, funcname);
}
const char *Interpreter::lua_tostring_safe (lua_State *L, int idx, int prmno, const char *funcname)
{
AssertPrmType(L, idx, prmno, PRMTP_STRING, funcname);
return lua_tostring(L, idx);
}
const char *Interpreter::lua_tostring_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tostring_safe (L, idx, idx, funcname);
}
const char *Interpreter::luamtd_tostring_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tostring_safe (L, idx, idx-1, funcname);
}
void *Interpreter::lua_tolightuserdata_safe (lua_State *L, int idx, int prmno, const char *funcname)
{
AssertPrmType(L, idx, prmno, PRMTP_LIGHTUSERDATA, funcname);
return lua_touserdata(L, idx);
}
void *Interpreter::lua_tolightuserdata_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tolightuserdata_safe (L, idx, idx, funcname);
}
void *Interpreter::luamtd_tolightuserdata_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tolightuserdata_safe (L, idx, idx-1, funcname);
}
VECTOR3 Interpreter::lua_tovector_safe (lua_State *L, int idx, int prmno, const char *funcname)
{
AssertPrmType(L, idx, prmno, PRMTP_VECTOR, funcname);
return lua_tovector(L, idx);
}
VECTOR3 Interpreter::lua_tovector_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tovector_safe (L, idx, idx, funcname);
}
VECTOR3 Interpreter::luamtd_tovector_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tovector_safe (L, idx, idx-1, funcname);
}
MATRIX3 Interpreter::lua_tomatrix_safe (lua_State *L, int idx, int prmno, const char *funcname)
{
AssertPrmType(L, idx, prmno, PRMTP_MATRIX, funcname);
return lua_tomatrix(L, idx);
}
MATRIX3 Interpreter::lua_tomatrix_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tomatrix_safe (L, idx, idx - 1, funcname);
}
MATRIX3 Interpreter::luamtd_tomatrix_safe (lua_State *L, int idx, const char *funcname)
{
return lua_tomatrix_safe (L, idx, idx - 1, funcname);
}
double Interpreter::lua_field_tonumber_safe (lua_State *L, int idx, int prmno, const char *fieldname, const char *funcname)
{
lua_getfield(L, idx, fieldname);
AssertPrmType(L, -1, prmno, PRMTP_NUMBER, funcname, fieldname);
double v = lua_tonumber(L, -1);
lua_pop(L, 1);
return v;
}
double Interpreter::lua_field_tonumber_safe (lua_State *L, int idx, const char *fieldname, const char *funcname)
{
return lua_field_tonumber_safe (L, idx, idx, fieldname, funcname);
}
double Interpreter::luamtd_field_tonumber_safe (lua_State *L, int idx, const char *fieldname, const char *funcname)
{
return lua_field_tonumber_safe (L, idx, idx-1, fieldname, funcname);
}
void *Interpreter::lua_field_tolightuserdata_safe (lua_State *L, int idx, int prmno, const char *fieldname, const char *funcname)
{
lua_getfield(L, idx, fieldname);
AssertPrmType(L, -1, prmno, PRMTP_LIGHTUSERDATA, funcname, fieldname);
void *v = lua_touserdata(L, -1);
lua_pop(L, 1);
return v;
}
void *Interpreter::lua_field_tolightuserdata_safe (lua_State *L, int idx, const char *fieldname, const char *funcname)
{
return lua_field_tolightuserdata_safe (L, idx, idx, fieldname, funcname);
}
void *Interpreter::luamtd_field_tolightuserdata_safe (lua_State *L, int idx, const char *fieldname, const char *funcname)
{
return lua_field_tolightuserdata_safe (L, idx, idx-1, fieldname, funcname);
}
VECTOR3 Interpreter::lua_field_tovector_safe (lua_State *L, int idx, int prmno, const char *fieldname, const char *funcname)
{
lua_getfield(L, idx, fieldname);
AssertPrmType(L, -1, prmno, PRMTP_VECTOR, funcname, fieldname);
VECTOR3 v = lua_tovector(L, -1);
lua_pop(L, 1);
return v;
}
VECTOR3 Interpreter::lua_field_tovector_safe (lua_State *L, int idx, const char *fieldname, const char *funcname)
{
return lua_field_tovector_safe (L, idx, idx, fieldname, funcname);
}
VECTOR3 Interpreter::luamtd_field_tovector_safe (lua_State *L, int idx, const char *fieldname, const char *funcname)
{
return lua_field_tovector_safe (L, idx, idx-1, fieldname, funcname);
}
const char *Interpreter::lua_tostringex (lua_State *L, int idx, char *cbuf)
{
static char cbuf_loc[256];
if (!cbuf) cbuf = cbuf_loc;
const char *str = lua_tostring (L,idx);
if (str) {
return str;
} else if (lua_isvector (L,idx)) {
VECTOR3 v = lua_tovector (L,idx);
sprintf (cbuf, "[%g %g %g]", v.x, v.y, v.z);
return cbuf;
} else if (lua_ismatrix (L,idx)) {
MATRIX3 m = lua_tomatrix(L,idx);
int i, len[9], lmax[3];
for (i = 0; i < 9; i++) {
sprintf (cbuf, "%g", m.data[i]);
len[i] = strlen(cbuf);
}
lmax[0] = max(len[0], max(len[3], len[6]));
lmax[1] = max(len[1], max(len[4], len[7]));
lmax[2] = max(len[2], max(len[5], len[8]));
sprintf (cbuf, "[%*g %*g %*g]\n[%*g %*g %*g]\n[%*g %*g %*g]",
lmax[0], m.m11, lmax[1], m.m12, lmax[2], m.m13,
lmax[0], m.m21, lmax[1], m.m22, lmax[2], m.m23,
lmax[0], m.m31, lmax[1], m.m32, lmax[2], m.m33);
return cbuf;
}
else if (lua_istouchdownvtx (L,idx)) {
TOUCHDOWNVTX tdvx = lua_totouchdownvtx (L,idx);
sprintf (cbuf, "{pos=[%g %g %g] stiffness=%g damping=%g mu=%g mu_lng=%g}",
tdvx.pos.x, tdvx.pos.y, tdvx.pos.z,
tdvx.stiffness, tdvx.damping, tdvx.mu, tdvx.mu_lng);
return cbuf;
} else if (lua_isnil (L,idx)) {
strcpy (cbuf, "nil");
return cbuf;
} else if (lua_isboolean (L,idx)) {
int res = lua_toboolean (L,idx);
strcpy (cbuf, res ? "true":"false");
return cbuf;
} else if (lua_islightuserdata (L,idx)) {
void *p = lua_touserdata(L,idx);
sprintf (cbuf, "0x%08p [data]", p);
return cbuf;
} else if (lua_isuserdata (L,idx)) {
void *p = lua_touserdata(L,idx);
sprintf (cbuf, "0x%08p [object]", p);
return cbuf;
} else if (lua_istable (L, idx)) {
if (idx < 0) idx--;
lua_pushnil(L); /* first key */
static char tbuf[1024]; tbuf[0] = '\0';
while (lua_next(L, idx) != 0) {
/* uses 'key' (at index -2) and 'value' (at index -1) */
char fieldstr[256] = "\0";
if (lua_isstring(L,-2)) sprintf (fieldstr, "%s=", lua_tostring(L,-2));
strcat (fieldstr, lua_tostringex (L,-1));
strcat (tbuf, fieldstr); strcat (tbuf, "\n");
lua_pop(L, 1);
}
return tbuf;
} else {
cbuf[0] = '\0';
return cbuf;
}
}
void Interpreter::lua_pushvector (lua_State *L, const VECTOR3 &vec)
{
lua_createtable (L, 0, 3);
lua_pushnumber (L, vec.x);
lua_setfield (L, -2, "x");
lua_pushnumber (L, vec.y);
lua_setfield (L, -2, "y");
lua_pushnumber (L, vec.z);
lua_setfield (L, -2, "z");
}
int Interpreter::lua_isvector (lua_State *L, int idx)
{
if (!lua_istable (L, idx)) return 0;
static char fieldname[3] = {'x','y','z'};
static char field[2] = "x";
int i, ii, n;
bool fail;
lua_pushnil(L);
ii = (idx >= 0 ? idx : idx-1);
n = 0;
while(lua_next(L,ii)) {
lua_pop(L,1);
n++;
}
if (n != 3) return 0;
for (i = 0; i < 3; i++) {
field[0] = fieldname[i];
lua_getfield (L, idx, field);
fail = (lua_isnil (L,-1));
lua_pop (L,1);
if (fail) return 0;
}
return 1;
}
void Interpreter::lua_pushmatrix (lua_State *L, const MATRIX3 &mat)
{
lua_createtable(L,0,9);
lua_pushnumber(L,mat.m11); lua_setfield(L,-2,"m11");
lua_pushnumber(L,mat.m12); lua_setfield(L,-2,"m12");
lua_pushnumber(L,mat.m13); lua_setfield(L,-2,"m13");
lua_pushnumber(L,mat.m21); lua_setfield(L,-2,"m21");
lua_pushnumber(L,mat.m22); lua_setfield(L,-2,"m22");
lua_pushnumber(L,mat.m23); lua_setfield(L,-2,"m23");
lua_pushnumber(L,mat.m31); lua_setfield(L,-2,"m31");
lua_pushnumber(L,mat.m32); lua_setfield(L,-2,"m32");
lua_pushnumber(L,mat.m33); lua_setfield(L,-2,"m33");
}
MATRIX3 Interpreter::lua_tomatrix (lua_State *L, int idx)
{
MATRIX3 mat;
lua_getfield (L, idx, "m11"); mat.m11 = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "m12"); mat.m12 = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "m13"); mat.m13 = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "m21"); mat.m21 = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "m22"); mat.m22 = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "m23"); mat.m23 = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "m31"); mat.m31 = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "m32"); mat.m32 = lua_tonumber (L, -1); lua_pop (L,1);
lua_getfield (L, idx, "m33"); mat.m33 = lua_tonumber (L, -1); lua_pop (L,1);
return mat;
}
int Interpreter::lua_ismatrix (lua_State *L, int idx)
{
if (!lua_istable (L, idx)) return 0;
static const char *fieldname[9] = {"m11","m12","m13","m21","m22","m23","m31","m32","m33"};
int i, ii, n;
bool fail;
lua_pushnil(L);
ii = (idx >= 0 ? idx : idx-1);
n = 0;
while(lua_next(L,ii)) {
lua_pop(L,1);
n++;
}
if (n != 9) return 0;
for (i = 0; i < 9; i++) {
lua_getfield (L, idx, fieldname[i]);
fail = (lua_isnil (L,-1));
lua_pop (L,1);
if (fail) return 0;
}
return 1;
}
COLOUR4 Interpreter::lua_torgba (lua_State *L, int idx)
{
COLOUR4 col = {0,0,0,0};
lua_getfield (L, idx, "r");
if (lua_isnumber(L,-1)) col.r = (float)lua_tonumber (L, -1);
lua_pop (L,1);
lua_getfield (L, idx, "g");
if (lua_isnumber(L,-1)) col.g = (float)lua_tonumber (L, -1);
lua_pop (L,1);
lua_getfield (L, idx, "b");
if (lua_isnumber(L,-1)) col.b = (float)lua_tonumber (L, -1);
lua_pop (L,1);
lua_getfield (L, idx, "a");
if (lua_isnumber(L,-1)) col.a = (float)lua_tonumber (L, -1);
lua_pop (L,1);
return col;
}
void Interpreter::lua_pushvessel (lua_State *L, VESSEL *v)
{
lua_pushlightuserdata(L,v); // use object pointer as key
lua_gettable(L,LUA_REGISTRYINDEX); // retrieve object from registry
if (lua_isnil(L,-1)) { // object not found
lua_pop(L,1); // pop nil
VESSEL **pv = (VESSEL**)lua_newuserdata(L,sizeof(VESSEL*));
*pv = v;
luaL_getmetatable (L, "VESSEL.vtable"); // retrieve metatable
lua_setmetatable (L,-2); // and attach to new object
LoadVesselExtensions(L,v); // vessel environment
lua_pushlightuserdata(L,v); // create key
lua_pushvalue(L,-2); // push object
lua_settable(L,LUA_REGISTRYINDEX); // and store in registry
// note that now the object is on top of the stack
}
}
void Interpreter::lua_pushmfd (lua_State *L, MFD2 *mfd)
{
lua_pushlightuserdata(L,mfd); // use object pointer as key
lua_gettable(L,LUA_REGISTRYINDEX); // retrieve object from registry
if (lua_isnil(L,-1)) { // object not found
lua_pop(L,1); // pop nil
MFD2 **pmfd = (MFD2**)lua_newuserdata(L,sizeof(MFD2*));
*pmfd = mfd;
luaL_getmetatable (L, "MFD.vtable"); // retrieve metatable
lua_setmetatable (L,-2); // and attach to new object
lua_pushlightuserdata(L, mfd); // create key
lua_pushvalue(L,-2); // push object
lua_settable(L, LUA_REGISTRYINDEX); // and store in registry
// note that now the object is on top of the stack
}
}
MFD2 *Interpreter::lua_tomfd (lua_State *L, int idx)
{
MFD2 **pmfd = (MFD2**)lua_touserdata(L,idx);
return *pmfd;
}
#ifdef UNDEF
void Interpreter::lua_pushmfd (lua_State *L, MFD2 *mfd)
{
lua_pushlightuserdata(L,mfd);
//MFD2 **pm = (MFD2**)lua_newuserdata (L, sizeof(MFD*));
//*pm = mfd;
luaL_getmetatable (L, "MFD.vtable");
lua_setmetatable (L, -2);
}
#endif
void Interpreter::lua_pushlightemitter (lua_State *L, const LightEmitter *le)
{
lua_pushlightuserdata (L, (void*)le); // use object pointer as key
lua_gettable (L, LUA_REGISTRYINDEX); // retrieve object from registry
if (lua_isnil (L,-1)) { // object not found
lua_pop (L,1); // pop nil
const LightEmitter **ple = (const LightEmitter**)lua_newuserdata(L,sizeof(const LightEmitter*));
*ple = le;
luaL_getmetatable (L, "LightEmitter.vtable"); // retrieve metatable
lua_setmetatable (L,-2); // and attach to new object
lua_pushlightuserdata (L, (void*)le); // create key
lua_pushvalue (L,-2); // push object
lua_settable (L,LUA_REGISTRYINDEX); // and store in registry
// note that now the object is on top of the stack
}
}
LightEmitter *Interpreter::lua_tolightemitter (lua_State *L, int idx)
{
LightEmitter **le = (LightEmitter**)lua_touserdata (L, idx);
return *le;
}
void Interpreter::lua_pushsketchpad (lua_State *L, oapi::Sketchpad *skp)
{
lua_pushlightuserdata(L,skp); // use object pointer as key
lua_gettable(L,LUA_REGISTRYINDEX); // retrieve object from registry
if (lua_isnil(L,-1)) { // object not found
lua_pop(L,1); // pop nil
oapi::Sketchpad **pskp = (oapi::Sketchpad**)lua_newuserdata(L,sizeof(oapi::Sketchpad*));
*pskp = skp;
luaL_getmetatable (L, "SKP.vtable"); // retrieve metatable
lua_setmetatable (L,-2); // and attach to new object
lua_pushlightuserdata(L,skp); // create key
lua_pushvalue(L,-2); // push object
lua_settable(L, LUA_REGISTRYINDEX); // and store in registry
// note that now the object is on top of the stack
}
#ifdef UNDEF
//lua_pushlightuserdata(L,skp);
oapi::Sketchpad **ps = (oapi::Sketchpad**)lua_newuserdata (L, sizeof(oapi::Sketchpad*));
*ps = skp;
luaL_getmetatable (L, "SKP.vtable");
lua_setmetatable (L, -2);
#endif
}
void Interpreter::WaitExec (DWORD timeout)
{
// Called by orbiter thread or interpreter thread to wait its turn
// Orbiter waits for the script for 1 second to return
WaitForSingleObject (hWaitMutex, timeout); // wait for synchronisation mutex
WaitForSingleObject (hExecMutex, timeout); // wait for execution mutex
ReleaseMutex (hWaitMutex); // release synchronisation mutex
}
void Interpreter::EndExec ()
{
// called by orbiter thread or interpreter thread to hand over control
ReleaseMutex (hExecMutex);
}
void Interpreter::frameskip (lua_State *L)
{
if (status == 1) { // termination request
lua_pushboolean(L, 1);
lua_setfield (L, LUA_GLOBALSINDEX, "wait_exit");
} else {
EndExec();
WaitExec();
}
}
int Interpreter::ProcessChunk (const char *chunk, int n)
{
WaitExec();
int res = RunChunk (chunk, n);
EndExec();
return res;
}
int Interpreter::RunChunk (const char *chunk, int n)
{
int res = 0;
if (chunk[0]) {
is_busy = true;
// run command
luaL_loadbuffer (L, chunk, n, "line");
res = lua_pcall (L, 0, 0, 0);
if (res) {
auto error = lua_tostring(L, -1);
if (error) { // can be nullptr
if (is_term) {
// term_strout ("Execution error.");
term_strout(error, true);
}
else {
oapiWriteLogError(error);
}
is_busy = false;
return res;
}
}
// check for leftover background jobs
lua_getfield (L, LUA_GLOBALSINDEX, "_nbranch");
lua_call (L, 0, 1);
jobs = lua_tointeger (L, -1);
lua_pop (L, 1);
is_busy = false;
} else {
// idle loop: execute background jobs
lua_getfield (L, LUA_GLOBALSINDEX, "_idle");
lua_call (L, 0, 1);
jobs = lua_tointeger (L, -1);
lua_pop (L, 1);
res = -1;
}
return res;
}
void Interpreter::term_out (lua_State *L, bool iserr)
{
const char *str = lua_tostringex (L,-1);
if (str) term_strout (str, iserr);
}
void Interpreter::LoadAPI ()
{
// Load global functions
static const struct luaL_reg glob[] = {
{"help", help},
//{"api", help_api},
{NULL, NULL}
};
for (int i = 0; i < ARRAYSIZE(glob) && glob[i].name; i++) {
lua_pushcfunction (L, glob[i].func);
lua_setglobal (L, glob[i].name);
}
// Load the vector library
static const struct luaL_reg vecLib[] = {
{"set", vec_set},
{"add", vec_add},
{"sub", vec_sub},
{"mul", vec_mul},
{"div", vec_div},
{"dotp", vec_dotp},
{"crossp", vec_crossp},
{"length", vec_length},
{"dist", vec_dist},
{"unit", vec_unit},
{NULL, NULL}
};
luaL_openlib (L, "vec", vecLib, 0);
static const struct luaL_reg matLib[] = {
{"identity", mat_identity},
{"mul", mat_mul},
{"tmul", mat_tmul},
{"mmul", mat_mmul},
{NULL, NULL}
};
luaL_openlib (L, "mat", matLib, 0);
// Load the process library
static const struct luaL_reg procLib[] = {
{"Frameskip", procFrameskip},
{NULL, NULL}
};
luaL_openlib (L, "proc", procLib, 0);
// Load the oapi library
static const struct luaL_reg oapiLib[] = {
{"get_orbiter_version", oapi_get_orbiter_version},
{"get_viewport_size", oapi_get_viewport_size},
{"get_objhandle", oapiGetObjectHandle},
{"get_objcount", oapiGetObjectCount},
{"get_objname", oapiGetObjectName},
{"create_annotation", oapiCreateAnnotation},
{"del_annotation", oapiDelAnnotation},
{"get_annotations", oapiGetAnnotations},
{"dbg_out", oapiDbgOut},
{"write_log", oapiWriteLog},
{"open_help", oapiOpenHelp},
{"exit", oapiExit},
{"open_inputbox", oapiOpenInputBox},
{"receive_input", oapiReceiveInput},
{"del_vessel", oapi_del_vessel},
// time functions
{"get_simtime", oapi_get_simtime},
{"get_simstep", oapi_get_simstep},
{"get_systime", oapi_get_systime},
{"get_sysstep", oapi_get_sysstep},
{"get_simmjd", oapi_get_simmjd},
{"set_simmjd", oapi_set_simmjd},
{"get_sysmjd", oapi_get_sysmjd},
{"time2mjd", oapi_time2mjd},
{"get_tacc", oapi_get_tacc},
{"set_tacc", oapi_set_tacc},
{"get_pause", oapi_get_pause},
{"set_pause", oapi_set_pause},
// menu functions
{"get_mainmenuvisibilitymode", oapi_get_mainmenuvisibilitymode},
{"set_mainmenuvisibilitymode", oapi_set_mainmenuvisibilitymode},
{"get_maininfovisibilitymode", oapi_get_maininfovisibilitymode},
{"set_maininfovisibilitymode", oapi_set_maininfovisibilitymode},
// coordinate transformations
{"global_to_equ", oapi_global_to_equ},
{"equ_to_global", oapi_equ_to_global},
{"orthodome", oapi_orthodome},
// body functions
{"get_size", oapi_get_size},
{"get_mass", oapi_get_mass},
{"get_globalpos", oapi_get_globalpos},
{"get_globalvel", oapi_get_globalvel},
{"get_relativepos", oapi_get_relativepos},
{"get_relativevel", oapi_get_relativevel},
// vessel functions
{"get_propellanthandle", oapi_get_propellanthandle},
{"get_propellantmass", oapi_get_propellantmass},
{"get_propellantmaxmass", oapi_get_propellantmaxmass},
{"get_fuelmass", oapi_get_fuelmass},
{"get_maxfuelmass", oapi_get_maxfuelmass},
{"get_emptymass", oapi_get_emptymass},
{"set_emptymass", oapi_set_emptymass},
{"get_altitude", oapi_get_altitude},
{"get_pitch", oapi_get_pitch},
{"get_bank", oapi_get_bank},
{"get_heading", oapi_get_heading},
{"get_groundspeed", oapi_get_groundspeed},
{"get_groundspeedvector", oapi_get_groundspeedvector},
{"get_airspeed", oapi_get_airspeed},
{"get_airspeedvector", oapi_get_airspeedvector},
{"get_shipairspeedvector", oapi_get_shipairspeedvector},
{"get_equpos", oapi_get_equpos},
{"get_atm", oapi_get_atm},
{"get_induceddrag", oapi_get_induceddrag},
{"get_wavedrag", oapi_get_wavedrag},
// Navigation radio transmitter functions
{"get_navpos", oapi_get_navpos},
{"get_navchannel", oapi_get_navchannel},
{"get_navrange", oapi_get_navrange},
{"get_navdata", oapi_get_navdata},
{"get_navsignal", oapi_get_navsignal},
{"get_navtype", oapi_get_navtype},
// Camera functions
{"set_cameramode", oapi_set_cameramode},
{"get_cameratarget", oapi_get_cameratarget},
{"set_cameratarget", oapi_set_cameratarget},
{"get_cameraaperture", oapi_get_cameraaperture},
{"set_cameraaperture", oapi_set_cameraaperture},
{"get_cameraglobalpos", oapi_get_cameraglobalpos},
{"get_cameraglobaldir", oapi_get_cameraglobaldir},
{"move_groundcamera", oapi_move_groundcamera},
// animation functions
{"create_animationcomponent", oapi_create_animationcomponent},
{"del_animationcomponent", oapi_del_animationcomponent},
// instrument panel functions
{"open_mfd", oapi_open_mfd},
{"set_hudmode", oapi_set_hudmode},
{"set_panelblink", oapi_set_panelblink},
// user i/o functions
{"keydown", oapi_keydown},
{"resetkey", oapi_resetkey},
{"simulatebufferedkey", oapi_simulatebufferedkey},
{"simulateimmediatekey", oapi_simulateimmediatekey},
// file i/o functions
{"openfile", oapi_openfile},
{"closefile", oapi_closefile},
{"savescenario", oapi_savescenario},
{"writeline", oapi_writeline},
// {"writelog", oapi_writelog}, // see "write_log" above!
// {"writelogv", oapi_writelogv}, // ???
{"writescenario_string", oapi_writescenario_string},
{"writescenario_int", oapi_writescenario_int},
{"writescenario_float", oapi_writescenario_float},
{"writescenario_vec", oapi_writescenario_vec},
{"readscenario_nextline", oapi_readscenario_nextline},
{"readitem_string", oapi_readitem_string},
{"readitem_float", oapi_readitem_float},
{"readitem_int", oapi_readitem_int},
{"readitem_bool", oapi_readitem_bool},
{"readitem_vec", oapi_readitem_vec},
{"writeitem_string", oapi_writeitem_string},
{"writeitem_float", oapi_writeitem_float},
{"writeitem_int", oapi_writeitem_int},
{"writeitem_bool", oapi_writeitem_bool},
{"writeitem_vec", oapi_writeitem_vec},
// utility functions
{"rand", oapi_rand},
{"deflate", oapi_deflate},
{"inflate", oapi_inflate},
{"get_color", oapi_get_color},
{"formatvalue", oapi_formatvalue},
{NULL, NULL}
};
luaL_openlib (L, "oapi", oapiLib, 0);
// Load the (dummy) term library
static const struct luaL_reg termLib[] = {
{"out", termOut},
{NULL, NULL}
};
luaL_openlib (L, "term", termLib, 0);
// Set up global tables of constants
// Key ID table
lua_createtable (L, 0, 100);
lua_pushnumber (L, OAPI_KEY_ESCAPE); lua_setfield (L, -2, "ESCAPE");
lua_pushnumber (L, OAPI_KEY_1); lua_setfield (L, -2, "1");
lua_pushnumber (L, OAPI_KEY_2); lua_setfield (L, -2, "2");
lua_pushnumber (L, OAPI_KEY_3); lua_setfield (L, -2, "3");
lua_pushnumber (L, OAPI_KEY_4); lua_setfield (L, -2, "4");
lua_pushnumber (L, OAPI_KEY_5); lua_setfield (L, -2, "5");
lua_pushnumber (L, OAPI_KEY_6); lua_setfield (L, -2, "6");
lua_pushnumber (L, OAPI_KEY_7); lua_setfield (L, -2, "7");
lua_pushnumber (L, OAPI_KEY_8); lua_setfield (L, -2, "8");
lua_pushnumber (L, OAPI_KEY_9); lua_setfield (L, -2, "9");
lua_pushnumber (L, OAPI_KEY_0); lua_setfield (L, -2, "0");
lua_pushnumber (L, OAPI_KEY_MINUS); lua_setfield (L, -2, "MINUS");
lua_pushnumber (L, OAPI_KEY_EQUALS); lua_setfield (L, -2, "EQUALS");
lua_pushnumber (L, OAPI_KEY_BACK); lua_setfield (L, -2, "BACK");
lua_pushnumber (L, OAPI_KEY_TAB); lua_setfield (L, -2, "TAB");
lua_pushnumber (L, OAPI_KEY_Q); lua_setfield (L, -2, "Q");
lua_pushnumber (L, OAPI_KEY_W); lua_setfield (L, -2, "W");
lua_pushnumber (L, OAPI_KEY_E); lua_setfield (L, -2, "E");
lua_pushnumber (L, OAPI_KEY_R); lua_setfield (L, -2, "R");
lua_pushnumber (L, OAPI_KEY_T); lua_setfield (L, -2, "T");
lua_pushnumber (L, OAPI_KEY_Y); lua_setfield (L, -2, "Y");
lua_pushnumber (L, OAPI_KEY_U); lua_setfield (L, -2, "U");
lua_pushnumber (L, OAPI_KEY_I); lua_setfield (L, -2, "I");
lua_pushnumber (L, OAPI_KEY_O); lua_setfield (L, -2, "O");
lua_pushnumber (L, OAPI_KEY_P); lua_setfield (L, -2, "P");
lua_pushnumber (L, OAPI_KEY_LBRACKET); lua_setfield (L, -2, "LBRACKET");
lua_pushnumber (L, OAPI_KEY_RBRACKET); lua_setfield (L, -2, "RBRACKET");
lua_pushnumber (L, OAPI_KEY_RETURN); lua_setfield (L, -2, "RETURN");
lua_pushnumber (L, OAPI_KEY_LCONTROL); lua_setfield (L, -2, "LCONTROL");
lua_pushnumber (L, OAPI_KEY_A); lua_setfield (L, -2, "A");
lua_pushnumber (L, OAPI_KEY_S); lua_setfield (L, -2, "S");
lua_pushnumber (L, OAPI_KEY_D); lua_setfield (L, -2, "D");
lua_pushnumber (L, OAPI_KEY_F); lua_setfield (L, -2, "F");
lua_pushnumber (L, OAPI_KEY_G); lua_setfield (L, -2, "G");
lua_pushnumber (L, OAPI_KEY_H); lua_setfield (L, -2, "H");
lua_pushnumber (L, OAPI_KEY_J); lua_setfield (L, -2, "J");
lua_pushnumber (L, OAPI_KEY_K); lua_setfield (L, -2, "K");
lua_pushnumber (L, OAPI_KEY_L); lua_setfield (L, -2, "L");
lua_pushnumber (L, OAPI_KEY_SEMICOLON); lua_setfield (L, -2, "SEMICOLON");
lua_pushnumber (L, OAPI_KEY_APOSTROPHE); lua_setfield (L, -2, "APOSTROPHE");
lua_pushnumber (L, OAPI_KEY_GRAVE); lua_setfield (L, -2, "GRAVE");
lua_pushnumber (L, OAPI_KEY_LSHIFT); lua_setfield (L, -2, "LSHIFT");
lua_pushnumber (L, OAPI_KEY_BACKSLASH); lua_setfield (L, -2, "BACKSLASH");
lua_pushnumber (L, OAPI_KEY_Z); lua_setfield (L, -2, "Z");
lua_pushnumber (L, OAPI_KEY_X); lua_setfield (L, -2, "X");
lua_pushnumber (L, OAPI_KEY_C); lua_setfield (L, -2, "C");
lua_pushnumber (L, OAPI_KEY_V); lua_setfield (L, -2, "V");
lua_pushnumber (L, OAPI_KEY_B); lua_setfield (L, -2, "B");
lua_pushnumber (L, OAPI_KEY_N); lua_setfield (L, -2, "N");
lua_pushnumber (L, OAPI_KEY_M); lua_setfield (L, -2, "M");
lua_pushnumber (L, OAPI_KEY_COMMA); lua_setfield (L, -2, "COMMA");
lua_pushnumber (L, OAPI_KEY_PERIOD); lua_setfield (L, -2, "PERIOD");
lua_pushnumber (L, OAPI_KEY_SLASH); lua_setfield (L, -2, "SLASH");
lua_pushnumber (L, OAPI_KEY_RSHIFT); lua_setfield (L, -2, "RSHIFT");
lua_pushnumber (L, OAPI_KEY_MULTIPLY); lua_setfield (L, -2, "MULTIPLY");
lua_pushnumber (L, OAPI_KEY_LALT); lua_setfield (L, -2, "LALT");
lua_pushnumber (L, OAPI_KEY_SPACE); lua_setfield (L, -2, "SPACE");
lua_pushnumber (L, OAPI_KEY_CAPITAL); lua_setfield (L, -2, "CAPITAL");
lua_pushnumber (L, OAPI_KEY_F1); lua_setfield (L, -2, "F1");
lua_pushnumber (L, OAPI_KEY_F2); lua_setfield (L, -2, "F2");
lua_pushnumber (L, OAPI_KEY_F3); lua_setfield (L, -2, "F3");
lua_pushnumber (L, OAPI_KEY_F4); lua_setfield (L, -2, "F4");
lua_pushnumber (L, OAPI_KEY_F5); lua_setfield (L, -2, "F5");
lua_pushnumber (L, OAPI_KEY_F6); lua_setfield (L, -2, "F6");
lua_pushnumber (L, OAPI_KEY_F7); lua_setfield (L, -2, "F7");
lua_pushnumber (L, OAPI_KEY_F8); lua_setfield (L, -2, "F8");
lua_pushnumber (L, OAPI_KEY_F9); lua_setfield (L, -2, "F9");
lua_pushnumber (L, OAPI_KEY_F10); lua_setfield (L, -2, "F10");
lua_pushnumber (L, OAPI_KEY_NUMLOCK); lua_setfield (L, -2, "NUMLOCK");
lua_pushnumber (L, OAPI_KEY_SCROLL); lua_setfield (L, -2, "SCROLL");
lua_pushnumber (L, OAPI_KEY_NUMPAD7); lua_setfield (L, -2, "NUMPAD7");
lua_pushnumber (L, OAPI_KEY_NUMPAD8); lua_setfield (L, -2, "NUMPAD8");
lua_pushnumber (L, OAPI_KEY_NUMPAD9); lua_setfield (L, -2, "NUMPAD9");
lua_pushnumber (L, OAPI_KEY_SUBTRACT); lua_setfield (L, -2, "SUBTRACT");
lua_pushnumber (L, OAPI_KEY_NUMPAD4); lua_setfield (L, -2, "NUMPAD4");
lua_pushnumber (L, OAPI_KEY_NUMPAD5); lua_setfield (L, -2, "NUMPAD5");
lua_pushnumber (L, OAPI_KEY_NUMPAD6); lua_setfield (L, -2, "NUMPAD6");
lua_pushnumber (L, OAPI_KEY_ADD); lua_setfield (L, -2, "ADD");
lua_pushnumber (L, OAPI_KEY_NUMPAD1); lua_setfield (L, -2, "NUMPAD1");
lua_pushnumber (L, OAPI_KEY_NUMPAD2); lua_setfield (L, -2, "NUMPAD2");
lua_pushnumber (L, OAPI_KEY_NUMPAD3); lua_setfield (L, -2, "NUMPAD3");
lua_pushnumber (L, OAPI_KEY_NUMPAD0); lua_setfield (L, -2, "NUMPAD0");
lua_pushnumber (L, OAPI_KEY_DECIMAL); lua_setfield (L, -2, "DECIMAL");
lua_pushnumber (L, OAPI_KEY_OEM_102); lua_setfield (L, -2, "OEM_102");
lua_pushnumber (L, OAPI_KEY_F11); lua_setfield (L, -2, "F11");
lua_pushnumber (L, OAPI_KEY_F12); lua_setfield (L, -2, "F12");
lua_pushnumber (L, OAPI_KEY_NUMPADENTER); lua_setfield (L, -2, "NUMPADENTER");
lua_pushnumber (L, OAPI_KEY_RCONTROL); lua_setfield (L, -2, "RCONTROL");
lua_pushnumber (L, OAPI_KEY_DIVIDE); lua_setfield (L, -2, "DIVIDE");
lua_pushnumber (L, OAPI_KEY_RALT); lua_setfield (L, -2, "RALT");
lua_pushnumber (L, OAPI_KEY_HOME); lua_setfield (L, -2, "HOME");
lua_pushnumber (L, OAPI_KEY_UP); lua_setfield (L, -2, "UP");
lua_pushnumber (L, OAPI_KEY_PRIOR); lua_setfield (L, -2, "PRIOR");
lua_pushnumber (L, OAPI_KEY_LEFT); lua_setfield (L, -2, "LEFT");
lua_pushnumber (L, OAPI_KEY_RIGHT); lua_setfield (L, -2, "RIGHT");
lua_pushnumber (L, OAPI_KEY_END); lua_setfield (L, -2, "END");
lua_pushnumber (L, OAPI_KEY_DOWN); lua_setfield (L, -2, "DOWN");
lua_pushnumber (L, OAPI_KEY_NEXT); lua_setfield (L, -2, "NEXT");
lua_pushnumber (L, OAPI_KEY_INSERT); lua_setfield (L, -2, "INSERT");
lua_pushnumber (L, OAPI_KEY_DELETE); lua_setfield (L, -2, "DELETE");
lua_setglobal (L, "OAPI_KEY");
// mouse event identifiers
lua_createtable (L, 0, 11);
lua_pushnumber (L, PANEL_MOUSE_IGNORE); lua_setfield (L, -2, "IGNORE");
lua_pushnumber (L, PANEL_MOUSE_LBDOWN); lua_setfield (L, -2, "LBDOWN");
lua_pushnumber (L, PANEL_MOUSE_RBDOWN); lua_setfield (L, -2, "RBDOWN");
lua_pushnumber (L, PANEL_MOUSE_LBUP); lua_setfield (L, -2, "LBUP");
lua_pushnumber (L, PANEL_MOUSE_RBUP); lua_setfield (L, -2, "RBUP");
lua_pushnumber (L, PANEL_MOUSE_LBPRESSED);lua_setfield (L, -2, "LBPRESSED");
lua_pushnumber (L, PANEL_MOUSE_RBPRESSED);lua_setfield (L, -2, "RBPRESSED");
lua_pushnumber (L, PANEL_MOUSE_DOWN); lua_setfield (L, -2, "DOWN");
lua_pushnumber (L, PANEL_MOUSE_UP); lua_setfield (L, -2, "UP");
lua_pushnumber (L, PANEL_MOUSE_PRESSED); lua_setfield (L, -2, "PRESSED");
lua_pushnumber (L, PANEL_MOUSE_ONREPLAY); lua_setfield (L, -2, "ONREPLAY");
lua_setglobal (L, "PANEL_MOUSE");
// frame of reference identifiers
lua_createtable (L, 0, 4);
lua_pushnumber (L, FRAME_GLOBAL); lua_setfield (L, -2, "GLOBAL");
lua_pushnumber (L, FRAME_LOCAL); lua_setfield (L, -2, "LOCAL");
lua_pushnumber (L, FRAME_REFLOCAL); lua_setfield (L, -2, "REFLOCAL");
lua_pushnumber (L, FRAME_HORIZON); lua_setfield (L, -2, "HORIZON");
lua_setglobal (L, "REFFRAME");
// altitude mode identifiers
lua_createtable (L, 0, 2);
lua_pushnumber (L, ALTMODE_MEANRAD); lua_setfield (L, -2, "MEANRAD");
lua_pushnumber (L, ALTMODE_GROUND); lua_setfield (L, -2, "GROUND");
lua_setglobal (L, "ALTMODE");
// file access mode identifiers
lua_createtable(L, 0, 4);
lua_pushnumber(L, FileAccessMode::FILE_IN); lua_setfield(L, -2, "FILE_IN");
lua_pushnumber(L, FileAccessMode::FILE_OUT); lua_setfield(L, -2, "FILE_OUT");
lua_pushnumber(L, FileAccessMode::FILE_APP); lua_setfield(L, -2, "FILE_APP");
lua_pushnumber(L, FileAccessMode::FILE_IN_ZEROONFAIL); lua_setfield(L, -2, "FILE_IN_ZEROONFAIL");
lua_setglobal(L, "FILE_ACCESS_MODE");
// path root identifiers
lua_createtable(L, 0, 4);
lua_pushnumber(L, PathRoot::ROOT); lua_setfield(L, -2, "ROOT");
lua_pushnumber(L, PathRoot::CONFIG); lua_setfield(L, -2, "CONFIG");
lua_pushnumber(L, PathRoot::SCENARIOS); lua_setfield(L, -2, "SCENARIOS");
lua_pushnumber(L, PathRoot::TEXTURES); lua_setfield(L, -2, "TEXTURES");
lua_pushnumber(L, PathRoot::TEXTURES2); lua_setfield(L, -2, "TEXTURES2");
lua_pushnumber(L, PathRoot::MESHES); lua_setfield(L, -2, "MESHES");
lua_pushnumber(L, PathRoot::MODULES); lua_setfield(L, -2, "MODULES");
lua_setglobal(L, "PATH_ROOT");
}
void Interpreter::LoadMFDAPI ()
{
static const struct luaL_reg mfdLib[] = {
{"get_size", mfd_get_size},
{"set_title", mfd_set_title},
{"get_defaultpen", mfd_get_defaultpen},
{"get_defaultfont", mfd_get_defaultfont},
{"invalidate_display", mfd_invalidate_display},
{"invalidate_buttons", mfd_invalidate_buttons},
{NULL, NULL}
};
luaL_newmetatable (L, "MFD.vtable");
lua_pushstring (L, "__index");
lua_pushvalue (L, -2); // push metatable
lua_settable (L, -3); // metatable.__index = metatable