-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterstellar.cpp
More file actions
4063 lines (3396 loc) · 135 KB
/
interstellar.cpp
File metadata and controls
4063 lines (3396 loc) · 135 KB
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
#include <string.h>
#include "interstellar.hpp"
#include "interstellar_signal.hpp"
#include <vector>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <algorithm>
#include <cmath>
#include <string_view>
#include <array>
#include <sstream>
#include <iomanip>
namespace INTERSTELLAR_NAMESPACE {
namespace Engine {
/* Get C data pointer. */
void* cdata_getptr(void* p, CTSize sz)
{
if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */
return ((void*)(uintptr_t) * (uint32_t*)p);
}
else if (sz == CTSIZE_PTR) {
return *(void**)p;
}
}
/* Set C data pointer. */
void cdata_setptr(void* p, CTSize sz, const void* v)
{
if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */
*(uint32_t*)p = (uint32_t)(uintptr_t)v;
}
else if (sz == CTSIZE_PTR) {
*(void**)p = (void*)v;
}
}
/* Get C type state. */
CTState* ctype_cts(lua_State* L)
{
CTState* cts = ctype_ctsG(mref(L->glref, global_State));
cts->L = L; /* Save L for errors and allocations. */
return cts;
}
/* Check C type ID for validity when assertions are enabled. */
CTypeID ctype_check(CTState *cts, CTypeID id)
{
if (id > 0 && id < cts->top) {
return id;
}
}
/* Get C type for C type ID. */
CType *ctype_get(CTState *cts, CTypeID id)
{
return &cts->tab[ctype_check(cts, id)];
}
/* Get child C type. */
CType *ctype_child(CTState *cts, CType *ct)
{
if (!(ctype_isvoid(ct->info) || ctype_isstruct(ct->info) ||
ctype_isbitfield(ct->info))) {
return ctype_get(cts, ctype_cid(ct->info));
}
}
/* Get raw type for a C type ID. */
CType *ctype_raw(CTState *cts, CTypeID id)
{
CType *ct = ctype_get(cts, id);
while (ctype_isattrib(ct->info)) ct = ctype_child(cts, ct);
return ct;
}
/* Get raw type of the child of a C type. */
CType *ctype_rawchild(CTState *cts, CType *ct)
{
do { ct = ctype_child(cts, ct); } while (ctype_isattrib(ct->info));
return ct;
}
}
namespace API
{
// Giant garble of GetProcAddress assignments
// Oh mah gawd
#define grab n2p
#define cast reinterpret_cast
// luaL functions
namespace luaL
{
type::addlstring addlstring;
type::addstring addstring;
type::addvalue addvalue;
type::argerror argerror;
type::buffinit buffinit;
type::callmeta callmeta;
type::checkany checkany;
type::checkinteger checkinteger;
type::checklstring checklstring;
type::checknumber checknumber;
type::checkoption checkoption;
type::checkstack checkstack;
type::checktype checktype;
type::checkudata checkudata;
type::error error;
type::execresult execresult;
type::fileresult fileresult;
type::findtable findtable;
type::getmetafield getmetafield;
type::gsub gsub;
type::loadbuffer loadbuffer;
type::loadbufferx loadbufferx;
type::loadfile loadfile;
type::loadfilex loadfilex;
type::loadstring loadstring;
type::newmetatable newmetatable;
type::newstate newstate;
type::openlib openlib;
type::openlibs openlibs;
type::optinteger optinteger;
type::optlstring optlstring;
type::optnumber optnumber;
type::prepbuffer prepbuffer;
type::pushmodule pushmodule;
type::pushresult pushresult;
type::ref ref;
type::makelib makelib;
type::setfuncs setfuncs;
type::setmetatable setmetatable;
type::testudata testudata;
type::traceback traceback;
type::typerror typerror;
type::unref unref;
type::where where;
#ifdef INTERSTELLAR_EXTERNAL
#define include(hndle, name, offset) \
{ void* f = grab(hndle, "luaL_" #name); if (f == NULL) { printf("[Interstellar] couldn't locate luaL_" #name "\n"); return offset; } name = cast<type::name>(f); offset++; }
#define includeA(hndle, real, name, offset) \
{ void* f = grab(hndle, "luaL_" #real); if (f == NULL) { printf("[Interstellar] couldn't locate luaL_" #real "\n"); return offset; } name = cast<type::name>(f); offset++; }
#else
#define include(hndle, name, offset) \
name = cast<type::name>(Engine::luaL_##name);
#define includeA(hndle, real, name, offset) \
name = cast<type::name>(Engine::luaL_##real);
#endif
std::string trace(lua_State* L, int index)
{
traceback(L, L, "", index);
std::string trace = lua::tocstring(L, -1);
lua::pop(L, 1);
size_t pos = trace.find("stack traceback:");
if (pos != std::string::npos) {
trace.erase(pos, 16);
}
std::string cleaned_trace;
cleaned_trace.reserve(trace.size());
bool first_char = true;
for (size_t i = 0; i < trace.size(); ++i) {
if (trace[i] == '\t' && (first_char || (i > 0 && trace[i - 1] == '\n'))) {
continue;
}
first_char = false;
cleaned_trace += trace[i];
}
while (!cleaned_trace.empty() && cleaned_trace.front() == '\n') {
cleaned_trace.erase(0, 1);
}
return cleaned_trace.empty() ? "[C]" : cleaned_trace;
}
std::string checkcstring(lua_State* L, int index) {
size_t size = 0;
const char* str = checklstring(L, index, &size);
return std::string(str, size);
}
bool checkboolean(lua_State* L, int index) {
luaL::checktype(L, index, datatype::boolean);
return lua::toboolean(L, index);
}
void checkfunction(lua_State* L, int index) {
luaL::checktype(L, index, datatype::function);
}
void* checklightuserdata(lua_State* L, int index) {
luaL::checktype(L, index, datatype::lightuserdata);
return lua::touserdata(L, index);
}
void* checkuserdata(lua_State* L, int index) {
luaL::checktype(L, index, datatype::userdata);
return lua::touserdata(L, index);
}
void* checkuserdatatype(lua_State* L, int index, int type) {
luaL::checktype(L, index, datatype::userdata);
lua_udata data =(lua_udata)lua::touserdata(L, index);
if (data.type != type) {
luaL::error(L, "userdata is not of type %d", type);
return nullptr;
}
return data.data;
}
void checkcfunction(lua_State* L, int index) {
luaL::checktype(L, index, datatype::function);
if (!lua::iscfunction(L, index)) {
luaL::error(L, "provided function is not a cfunction");
}
}
void checklfunction(lua_State* L, int index) {
luaL::checktype(L, index, datatype::function);
if (lua::iscfunction(L, index)) {
luaL::error(L, "provided function is not a lfunction");
}
}
void checkcdata(lua_State* L, int index) {
luaL::checktype(L, index, datatype::cdata);
}
void checkproto(lua_State* L, int index) {
luaL::checktype(L, index, datatype::proto);
}
void checktable(lua_State* L, int index) {
luaL::checktype(L, index, datatype::table);
}
int newref(lua_State* L, int index) {
lua::pushvalue(L, index);
if (index < 0) lua::remove(L, index);
return luaL::ref(L, indexer::registry);
}
void rmref(lua_State* L, int reference) {
luaL::unref(L, indexer::registry, reference);
}
static int writer_dump(lua_State* L, const void* p, size_t size, void* sb)
{
std::string& buffer = *(std::string*)sb;
buffer.append((const char*)p, size);
return 0;
}
std::string dump(lua_State* L, int index)
{
if (!lua::islfunction(L, index)) {
return "";
}
std::string buffer;
lua::pushvalue(L, index);
lua::dump(L, writer_dump, (void*)&buffer);
lua::pop(L);
return buffer;
}
int fetch(UMODULE hndle)
{
int start = 1;
include(hndle, addlstring, start);
include(hndle, addstring, start);
include(hndle, addvalue, start);
include(hndle, argerror, start);
include(hndle, buffinit, start);
include(hndle, callmeta, start);
include(hndle, checkany, start);
include(hndle, checkinteger, start);
include(hndle, checklstring, start);
include(hndle, checknumber, start);
include(hndle, checkoption, start);
include(hndle, checkstack, start);
include(hndle, checktype, start);
include(hndle, checkudata, start);
include(hndle, error, start);
include(hndle, execresult, start);
include(hndle, fileresult, start);
include(hndle, findtable, start);
include(hndle, getmetafield, start);
include(hndle, gsub, start);
include(hndle, loadbuffer, start);
include(hndle, loadbufferx, start);
include(hndle, loadfile, start);
include(hndle, loadfilex, start);
include(hndle, loadstring, start);
include(hndle, newmetatable, start);
include(hndle, newstate, start);
include(hndle, openlib, start);
include(hndle, openlibs, start);
include(hndle, optinteger, start);
include(hndle, optlstring, start);
include(hndle, optnumber, start);
include(hndle, prepbuffer, start);
/*
#ifndef __linux
include(hndle, pushmodule, start);
#endif
*/
include(hndle, pushresult, start);
include(hndle, ref, start);
includeA(hndle, register, makelib, start);
/*
#ifndef __linux
include(hndle, setfuncs, start);
include(hndle, setmetatable, start);
include(hndle, testudata, start);
#endif
*/
include(hndle, traceback, start);
include(hndle, typerror, start);
include(hndle, unref, start);
include(hndle, where, start);
return 0;
}
}
// lua functions
namespace lua
{
type::atpanic atpanic;
type::call call;
type::checkstack checkstack;
type::close close;
type::concat concat;
type::copy copy;
type::cpcall cpcall;
type::createtable createtable;
type::dump dump;
type::equal equal;
type::error error;
type::gc gc;
type::getallocf getallocf;
type::getfenv getfenv;
type::getfield getfield;
type::gethook gethook;
type::gethookcount gethookcount;
type::gethookmask gethookmask;
type::getinfo getinfo;
type::getlocal getlocal;
type::getmetatable getmetatable;
type::getstack getstack;
type::gettable gettable;
type::gettop gettop;
type::getupvalue getupvalue;
type::insert insert;
type::iscfunction iscfunction;
type::isnumber isnumber;
type::isstring isstring;
type::isuserdata isuserdata;
type::isyieldable isyieldable;
type::lessthan lessthan;
type::load load;
type::loadx loadx;
type::newstate newstate;
type::newthread newthread;
type::newuserdata newuserdata;
type::next next;
type::objlen objlen;
type::pcall pcall;
type::pushboolean pushboolean;
type::pushcclosure pushcclosure;
type::pushfstring pushfstring;
type::pushinteger pushinteger;
type::pushlightuserdata pushlightuserdata;
type::pushlstring pushlstring;
type::pushnil pushnil;
type::pushnumber pushnumber;
type::pushstring pushstring;
type::pushthread pushthread;
type::pushvalue pushvalue;
type::pushvfstring pushvfstring;
type::rawequal rawequal;
type::rawget rawget;
type::rawgeti rawgeti;
type::rawset rawset;
type::rawseti rawseti;
type::remove remove;
type::replace replace;
type::setallocf setallocf;
type::setfenv setfenv;
type::setfield setfield;
type::sethook sethook;
type::setlocal setlocal;
type::setmetatable setmetatable;
type::settable settable;
type::settop settop;
type::setupvalue setupvalue;
type::status status;
type::toboolean toboolean;
type::tocfunction tocfunction;
type::tointeger tointeger;
type::tointegerx tointegerx;
type::tolstring tolstring;
type::tonumber tonumber;
type::tonumberx tonumberx;
type::topointer topointer;
type::tothread tothread;
type::touserdata touserdata;
type::gettype gettype;
type::gettypename gettypename;
type::upvalueid upvalueid;
type::upvaluejoin upvaluejoin;
type::version version;
type::xmove xmove;
type::yield yield;
#ifdef INTERSTELLAR_EXTERNAL
#define include(hndle, name, offset) \
{ void* f = grab(hndle, "lua_" #name); if (f == NULL) { printf("[Interstellar] couldn't locate lua_" #name "\n"); return offset; } name = cast<type::name>(f); offset++; }
#define includeA(hndle, real, name, offset) \
{ void* f = grab(hndle, "lua_" #real); if (f == NULL) { printf("[Interstellar] couldn't locate lua_" #real "\n"); return offset; } name = cast<type::name>(f); offset++; }
#else
#define include(hndle, name, offset) \
name = cast<type::name>(Engine::lua_##name);
#define includeA(hndle, real, name, offset) \
name = cast<type::name>(Engine::lua_##real);
#endif
int fetch(UMODULE hndle)
{
int start = 1;
include(hndle, atpanic, start);
include(hndle, call, start);
include(hndle, checkstack, start);
include(hndle, close, start);
include(hndle, concat, start);
/*#ifndef __linux
include(hndle, copy, start);
#endif*/
include(hndle, cpcall, start);
include(hndle, createtable, start);
include(hndle, dump, start);
include(hndle, equal, start);
include(hndle, error, start);
include(hndle, gc, start);
include(hndle, getallocf, start);
include(hndle, getfenv, start);
include(hndle, getfield, start);
include(hndle, gethook, start);
include(hndle, gethookcount, start);
include(hndle, gethookmask, start);
include(hndle, getinfo, start);
include(hndle, getlocal, start);
include(hndle, getmetatable, start);
include(hndle, getstack, start);
include(hndle, gettable, start);
include(hndle, gettop, start);
include(hndle, getupvalue, start);
include(hndle, insert, start);
include(hndle, iscfunction, start);
include(hndle, isnumber, start);
include(hndle, isstring, start);
include(hndle, isuserdata, start);
/*#ifndef __linux
include(hndle, isyieldable, start);
#endif*/
include(hndle, lessthan, start);
include(hndle, load, start);
include(hndle, loadx, start);
include(hndle, newstate, start);
include(hndle, newthread, start);
include(hndle, newuserdata, start);
include(hndle, next, start);
include(hndle, objlen, start);
include(hndle, pcall, start);
include(hndle, pushboolean, start);
include(hndle, pushcclosure, start);
include(hndle, pushfstring, start);
include(hndle, pushinteger, start);
include(hndle, pushlightuserdata, start);
include(hndle, pushlstring, start);
include(hndle, pushnil, start);
include(hndle, pushnumber, start);
include(hndle, pushstring, start);
include(hndle, pushthread, start);
include(hndle, pushvalue, start);
include(hndle, pushvfstring, start);
include(hndle, rawequal, start);
include(hndle, rawget, start);
include(hndle, rawgeti, start);
include(hndle, rawset, start);
include(hndle, rawseti, start);
include(hndle, remove, start);
include(hndle, replace, start);
include(hndle, setallocf, start);
include(hndle, setfenv, start);
include(hndle, setfield, start);
include(hndle, sethook, start);
include(hndle, setlocal, start);
include(hndle, setmetatable, start);
include(hndle, settable, start);
include(hndle, settop, start);
include(hndle, setupvalue, start);
include(hndle, status, start);
include(hndle, toboolean, start);
include(hndle, tocfunction, start);
include(hndle, tointeger, start);
/*#ifndef __linux
include(hndle, tointegerx, start);
#endif*/
include(hndle, tolstring, start);
include(hndle, tonumber, start);
/*#ifndef __linux
include(hndle, tonumberx, start);
#endif*/
include(hndle, topointer, start);
include(hndle, tothread, start);
include(hndle, touserdata, start);
includeA(hndle, type, gettype, start);
includeA(hndle, typename, gettypename, start);
include(hndle, upvalueid, start);
include(hndle, upvaluejoin, start);
/*#ifndef __linux
include(hndle, version, start);
#endif*/
include(hndle, xmove, start);
include(hndle, yield, start);
return 0;
}
std::string toastring(lua_State* L, int index)
{
lua::pushvalue(L, index);
lua::pushvalue(L, indexer::global);
lua::getfield(L, -1, "tostring");
lua::remove(L, -2);
lua::insert(L, -2);
if (lua::isfunction(L, -2)) {
if (lua::pcall(L, 1, 1, 0)) {
std::string err = lua::tocstring(L, -1);
lua::pop(L);
return err;
}
if (lua::isstring(L, -1)) {
std::string res = lua::tocstring(L, -1);
lua::pop(L);
return res;
}
lua::pop(L);
return "";
}
lua::pop(L, 2);
return "";
}
bool isinteger(lua_State* L, int index)
{
if (!lua::isnumber(L, index)) return false;
lua_Number num = lua::tonumber(L, index);
// 2^53 - 1 (for doubles)
constexpr lua_Number maximum = 9007199254740991.0;
constexpr lua_Number minimum = -9007199254740991.0;
if (num < minimum || num > maximum)
return false;
lua_Number intpart;
return std::modf(num, &intpart) == 0.0;
}
int trace(lua_State* L)
{
std::string msg = lua::tocstring(L, 1);
luaL::traceback(L, L, msg.c_str(), 1);
return 1;
}
int tcall(lua_State* L, int nargs, int nret) {
int hpos = gettop(L) - nargs;
int ret = 0;
pushcfunction(L, trace);
insert(L, hpos);
ret = pcall(L, nargs, nret, hpos);
remove(L, hpos);
return ret;
}
std::string tocstring(lua_State* L, int index)
{
size_t size = 0;
const char* str = tolstring(L, index, &size);
return std::string(str, size);
}
void pushcstring(lua_State* L, std::string str)
{
pushlstring(L, str.c_str(), str.size());
}
void pushraw(lua_State* L, Engine::GCobj* data, uint32_t type)
{
using namespace Engine;
lua::pushnil(L);
TValue* value = lua::toraw(L, -1);
if (type == 0) type = ~data->gch.gct;
#if LJ_GC64
setgcreft(value->gcr, data, type);
#else
setgcref(value->gcr, data); setitype(value, type);
#endif
}
Engine::TValue* toraw(lua_State* L, int index)
{
using namespace Engine;
TValue* base = L->base;
if (index > 0) {
return (TValue*)(base + (index - 1));
}
else {
return (TValue*)(L->top + index);
}
}
void setraw(lua_State* L, int index, Engine::GCobj* data, uint32_t type)
{
using namespace Engine;
TValue* base = L->base;
TValue* value;
if (index > 0) {
value = (TValue*)(base + (index - 1));
}
else {
value = (TValue*)(L->top + index);
}
if (type == 0) type = ~data->gch.gct;
#if LJ_GC64
setgcreft(value->gcr, data, type);
#else
setgcref(value->gcr, data); setitype(value, type);
#endif
}
void pushref(lua_State* L, int reference) {
lua::rawgeti(L, indexer::registry, reference);
}
void getcfield(lua_State* L, int index, std::string str)
{
pushlstring(L, str.c_str(), str.size());
gettable(L, index > 0 ? index : index - 1);
}
void getrfield(lua_State* L, int index, const char* str)
{
pushstring(L, str);
rawget(L, index > 0 ? index : index - 1);
}
void getcrfield(lua_State* L, int index, std::string str)
{
pushlstring(L, str.c_str(), str.size());
rawget(L, index > 0 ? index : index - 1);
}
void setcfield(lua_State* L, int index, std::string str)
{
pushlstring(L, str.c_str(), str.size());
insert(L, -2);
settable(L, index > 0 ? index : index - 1);
}
void setrfield(lua_State* L, int index, const char* str)
{
pushstring(L, str);
insert(L, -2);
rawset(L, index > 0 ? index : index - 1);
}
void setcrfield(lua_State* L, int index, std::string str)
{
pushlstring(L, str.c_str(), str.size());
insert(L, -2);
rawset(L, index > 0 ? index : index - 1);
}
bool istype(lua_State* L, int index, int type)
{
return gettype(L, index) == type;
}
bool isnil(lua_State* L, int index)
{
int t = gettype(L, index);
return t == datatype::nil || t == datatype::none;
}
bool isboolean(lua_State* L, int index)
{
return gettype(L, index) == datatype::boolean;
}
bool islightuserdata(lua_State* L, int index)
{
return gettype(L, index) == datatype::lightuserdata;
}
bool istable(lua_State* L, int index)
{
return gettype(L, index) == datatype::table;
}
bool isfunction(lua_State* L, int index)
{
return gettype(L, index) == datatype::function;
}
bool islfunction(lua_State* L, int index)
{
return gettype(L, index) == datatype::function && !lua::iscfunction(L, index);
}
bool iscdata(lua_State* L, int index)
{
return gettype(L, index) == datatype::cdata;
}
bool isuserdatatype(lua_State* L, int index, int type) {
if (gettype(L, index) != datatype::userdata) {
return false;
}
lua_udata data = (lua_udata)lua::touserdata(L, index);
if (data.type != type) {
return false;
}
return true;
}
bool isproto(lua_State* L, int index)
{
return gettype(L, index) == datatype::proto;
}
bool isthread(lua_State* L, int index)
{
return gettype(L, index) == datatype::thread;
}
void newtable(lua_State* L)
{
createtable(L, 0, 0);
}
void pop(lua_State* L, int count)
{
for (int a = 0; a < count; a++) remove(L, -1);
}
void newuserdatatype(lua_State* L, void* data, size_t size, unsigned char type) {
lua_udata* userdata = (lua_udata*)lua::newuserdata(L, sizeof(lua_udata));
*userdata = {
data,
type
};
}
void* touserdatatype(lua_State* L, int index) {
lua_udata* userdata = (lua_udata*)lua::touserdata(L, index);
return userdata->data;
}
void* tocdata(lua_State* L, int index)
{
using namespace Engine;
TValue* tv = toraw(L, index);
GCcdata* cd = cdataV(tv);
CTState* cts = ctype_cts(L);
CType* ct = ctype_raw(cts, cd->ctypeid);
CTSize sz = ct->size;
return cdata_getptr(cdataptr(cd), sz);
}
void* tocdataptr(lua_State* L, int index)
{
using namespace Engine;
TValue* tv = toraw(L, index);
GCcdata* cd = cdataV(tv);
return cdataptr(cd);
}
void setcdata(lua_State* L, int index, const void *value)
{
using namespace Engine;
TValue* tv = toraw(L, index);
GCcdata* cd = cdataV(tv);
CTState* cts = ctype_cts(L);
CType* ct = ctype_raw(cts, cd->ctypeid);
cdata_setptr(cdataptr(cd), ct->size, value);
}
void setcdataptr(lua_State* L, int index, const void* value)
{
using namespace Engine;
TValue* tv = toraw(L, index);
GCcdata* cd = cdataV(tv);
CTState* cts = ctype_cts(L);
CType* ct = ctype_raw(cts, cd->ctypeid);
CTSize sz = ct->size;
memcpy(cdataptr(cd), value, sz);
}
void* tocdatafunc(lua_State* L, int index)
{
using namespace Engine;
TValue* tv = toraw(L, index);
GCcdata* cd = cdataV(tv);
CTState* cts = ctype_cts(L);
CType* ct = ctype_raw(cts, cd->ctypeid);
CTSize sz = ct->size;
if (ctype_isfunc(ct->info)) {
return cdata_getptr(cdataptr(cd), sz);
}
return nullptr;
}
bool setcdatafunc(lua_State* L, int index, const void* value)
{
using namespace Engine;
TValue* tv = toraw(L, index);
GCcdata* cd = cdataV(tv);
CTState* cts = ctype_cts(L);
CType* ct = ctype_raw(cts, cd->ctypeid);
CTSize sz = CTSIZE_PTR;
if (ctype_isptr(ct->info)) {
sz = ct->size;
ct = ctype_rawchild(cts, ct);
}
if (ctype_isfunc(ct->info)) {
cdata_setptr(cdataptr(cd), sz, value);
return true;
}
return false;
}
void pushcfunction(lua_State* L, lua_CFunction f)
{
pushcclosure(L, f, 0);
}
char unique_tochar(int index) {
if (index < 26) return 'a' + index; // 0-25: a-z
else return 'A' + (index - 26); // 26-51: A-Z
}
std::string unique_char(unsigned int num) {
std::string result;
num--;
while (true) {
int index = num % 52;
result = unique_tochar(index) + result;
if (num < 52) break;
num = (num / 52) - 1;
}
return result;
}
int blank(lua_State* L) { return 0; }
void pushlfunction(lua_State* L, Engine::GCproto* proto, Engine::GCupval* upvptr, int upvalues)
{
using namespace Engine;
if (upvptr == nullptr) {
upvalues = proto->sizeuv;
}
std::string nfc = "return function() ";
for (uint8_t i = 0; i < upvalues; i++) {
std::string var = unique_char(i + 1);
nfc = "local " + var + " " + nfc + "local " + var + "=" + var + " ";
}
nfc = nfc + "end";
luaL::loadbufferx(L, nfc.c_str(), nfc.size(), "", 0);
lua::call(L, 0, 1);
TValue* copy = lua::toraw(L, -1);
GCfunc* gccopy = funcV(copy);
setmref(gccopy->l.pc, proto_bc(proto));
if (upvalues != 0 && upvptr != nullptr) {
memcpy(gccopy->l.uvptr, upvptr, sizeof(GCupval) * upvalues);
}
}
void pushnfunction(lua_State* L, std::string name)
{
std::string nfc = " ";
luaL::loadbufferx(L, nfc.c_str(), nfc.size(), name.c_str(), 0);
}
void getfunc(lua_State* L, int index)
{
int type = gettype(L, index);
if (type != datatype::function && type != datatype::number) {
luaL::argerror(L, 1, "invalid function or level");
}
if (type == datatype::number)
{
int level = tonumber(L, index);
if (index < 0) lua::remove(L, index);
lua_Debug ar;
if (!getstack(L, level, &ar))
luaL::argerror(L, 1, "invalid level");
getinfo(L, "f", &ar);
if (istype(L, -1, datatype::nil))
luaL::error(L, "no function environment for tail call at level %d", level);
}
else if (type == datatype::function)
{
if (index < 0) lua::remove(L, index);
pushvalue(L, index);
}
}
void getfunci(lua_State* L, int level)
{
lua_Debug ar;
if (!getstack(L, level, &ar))
luaL::argerror(L, 1, "invalid level");
getinfo(L, "f", &ar);
if (istype(L, -1, datatype::nil))
luaL::error(L, "no function environment for tail call at level %d", level);
}
int upvalues(lua_State* L, int index) {
luaL::checkfunction(L, index);
lua_Debug ar;
lua::pushvalue(L, index);
if (lua::getinfo(L, ">u", &ar)) {
return 0;
}
return ar.nups;
}
std::string typestack(lua_State* L, int count, int offset)
{
std::string stack;
for (int i = 0; i < count; i++)
{
int id = -(i + 1 + offset);
if (i > 0) stack += "\n";
stack += std::to_string(id) + ": " + std::string(lua::gettypename(L, lua::gettype(L, id)));
}
return stack;
}
}
// luaopen functions
namespace luaopen
{
type::base base;
type::bit bit;
type::debug debug;
type::jit jit;
type::math math;
type::os os;