forked from terralang/terrac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
1149 lines (945 loc) · 23.8 KB
/
main.cc
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 <cassert>
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <list>
#include <terra/terra.h>
#include "xopt.h"
#include "filesystem.hpp"
#ifdef _WIN32
# define TERRA_PATHSEP ';'
#else
# define TERRA_PATHSEP ':'
#endif
using namespace std;
const char * const DEFAULT_MODPATH =
"/usr/share/terra/modules"
":/usr/local/share/terra/modules";
// https://github.com/stevedonovan/Penlight/blob/d90956418cdf9e315719a7e4ce314db6b512ae00/lua/pl/compat.lua#L145-L155
// MIT License
// TODO convert to C
const char * const SEARCHPATH_LUA =
"if not package.searchpath then\n"
" local sep = package.config:sub(1,1)\n"
" function package.searchpath (mod,path)\n"
" mod = mod:gsub('%.',sep)\n"
" for m in path:gmatch('[^;]+') do\n"
" local nm = m:gsub('?',mod)\n"
" local f = io.open(nm,'r')\n"
" if f then f:close(); return nm end\n"
" end\n"
" end\n"
"end\n";
struct config {
int verbosity;
bool help;
bool debug;
const char *filename;
const char *output;
const char *depfile;
const char *depfile_target;
unique_ptr<list<string>> depfiles;
unique_ptr<list<filesystem::path>> include_dirs;
unique_ptr<list<filesystem::path>> lib_dirs;
unique_ptr<list<string>> libs;
unique_ptr<list<filesystem::path>> modulepaths;
bool nosysmods;
};
static void on_verbose(const char *v, void *data, const struct xoptOption *option, bool longArg, const char **err) {
(void) v;
(void) option;
(void) err;
(void) longArg;
config *conf = (config *) data;
assert(!longArg);
if (conf->verbosity < 3) {
++conf->verbosity;
}
}
static void on_multi_path(const char *v, void *data, const struct xoptOption *option, bool longArg, const char **err) {
(void) option;
(void) longArg;
(void) err;
(*((unique_ptr<list<filesystem::path>> *)((char *) data + option->offset)))->push_back(filesystem::path(v));
}
static void on_multi_string(const char *v, void *data, const struct xoptOption *option, bool longArg, const char **err) {
(void) option;
(void) longArg;
(void) err;
(*((unique_ptr<list<string>> *)((char *) data + option->offset)))->push_back(v);
}
static xoptOption options[] = {
{
"output",
'o',
offsetof(config, output),
0,
XOPT_TYPE_STRING,
0,
"If specified, outputs the terra code to the given filename"
},
{
"include-dir",
'I',
offsetof(config, include_dirs),
&on_multi_path,
XOPT_TYPE_STRING,
"dir",
"Adds a search path for C header files (can be passed multiple times)"
},
{
"lib-dir",
'L',
offsetof(config, lib_dirs),
&on_multi_path,
XOPT_TYPE_STRING,
"dir",
"Adds a search path for libraries (can be passed multiple times)"
},
{
"lib",
'l',
offsetof(config, libs),
&on_multi_string,
XOPT_TYPE_STRING,
"name",
"Specifies a library to be linked against the resulting binary"
},
{
"mod-dir",
'm',
offsetof(config, modulepaths),
&on_multi_path,
XOPT_TYPE_STRING,
"dir",
"Adds a module search path"
},
{
"nostdmod",
0,
offsetof(config, nosysmods),
0,
XOPT_TYPE_BOOL,
0,
"If specified, default system module paths are omitted from the modpath"
},
{
"depfile",
'D',
offsetof(config, depfile),
0,
XOPT_TYPE_STRING,
0,
"If specified, emits a Ninja-compatible depfile with all included files during the build"
},
{
"depfile-path",
'P',
offsetof(config, depfile_target),
0,
XOPT_TYPE_STRING,
0,
"If specified, all depfile paths are relativized to this path"
},
{
0, // only allow -v[v[v]]
'v',
offsetof(config, verbosity),
&on_verbose,
XOPT_TYPE_BOOL,
0,
"Increase verbosity (default level 0, max level 3)"
},
{
"debug",
'g',
offsetof(config, debug),
0,
XOPT_TYPE_BOOL,
0,
"Enable debugging information"
},
{
"help",
'h',
offsetof(config, help),
0,
XOPT_TYPE_BOOL,
0,
"Shows this help message"
},
XOPT_NULLOPTION
};
#ifdef NDEBUG
# define topcheck(...) ((void)0)
# define disarmtopcheck() ((void)0)
#else
struct _topcheck {
_topcheck(lua_State *_L, int ret = 0) : top(lua_gettop(_L)) {
this->armed = true;
this->L = _L;
this->ret = ret;
}
~_topcheck() {
if (armed) {
assert(lua_gettop(this->L) == (this->top + this->ret));
}
}
bool armed;
const int top;
int ret;
lua_State *L;
};
# define topcheck(...) _topcheck __tc(__VA_ARGS__)
# define disarmtopcheck() __tc.armed = false
#endif
static int errfn_ref = -1;
static int errfn(lua_State *L) {
topcheck(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
lua_getfield(L, -1, "traceback");
lua_remove(L, -2);
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
return 1;
}
static bool is_terrafn(lua_State *L) {
topcheck(L);
assert(!lua_isnil(L, -2));
assert(!lua_isnil(L, -1));
if (!lua_isstring(L, -2)) {
return false;
}
lua_getglobal(L, "terralib");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "type");
assert(!lua_isnil(L, -1));
lua_pushvalue(L, -3);
string where = " (at global '" + string(lua_tostring(L, -5)) + "')";
if (lua_pcall(L, 1, 1, 0) != 0) {
cerr << "terrac: call to terralib.type() failed: " << lua_tostring(L, -1) << where << endl;
lua_pop(L, 2);
return false;
}
string type = lua_tostring(L, -1);
lua_pop(L, 2);
return type == "terrafunction" || type == "terraglobalvariable";
}
static void resolve_path(lua_State *L, const char *pathname) {
topcheck(L, 1);
lua_getglobal(L, "package");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "searchpath");
assert(lua_isfunction(L, -1));
lua_pushvalue(L, -3);
lua_getfield(L, -3, pathname);
assert(!lua_isnil(L, -1));
lua_remove(L, -4);
if (lua_pcall(L, 2, 1, 0) != 0) {
cerr << "terrac: call to package.searchpath() failed: " << lua_tostring(L, -1) << endl;
lua_pop(L, 1);
lua_pushnil(L);
return;
}
if (lua_isnil(L, -1)) {
// not found
return;
}
// get realpath
char *rpth = realpath(lua_tostring(L, -1), NULL);
if (rpth == NULL) {
string where = "call to realpath() failed (module path: " + string(lua_tostring(L, -1)) + ")";
perror(where.c_str());
}
lua_pop(L, 1);
if (rpth == NULL) {
lua_pushnil(L);
} else {
lua_pushstring(L, rpth);
free(rpth);
}
}
static int on_loaded_newindex(lua_State *L) {
assert(lua_gettop(L) == 3); // we expect 3 args
lua_pushvalue(L, 2);
lua_pushvalue(L, 3);
lua_rawset(L, 1);
config *conf = (config *) lua_touserdata(L, lua_upvalueindex(1));
if (conf->verbosity > 0) {
cerr << "terrac: detected module: " << lua_tostring(L, 2) << endl;
}
lua_pushvalue(L, 2);
resolve_path(L, "path");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
resolve_path(L, "cpath");
}
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
resolve_path(L, "terrapath");
}
lua_remove(L, -2);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
cerr << "terrac: could not resolve module (skipping): " << lua_tostring(L, 2) << endl;
return 0;
}
if (conf->verbosity > 0) {
cerr << "terrac: resolved " << lua_tostring(L, 2) << " -> " << lua_tostring(L, -1) << endl;
}
if (conf->depfile) {
conf->depfiles->push_back(lua_tostring(L, -1));
}
lua_pop(L, 1);
return 0;
}
static bool get_terrac(lua_State *L) {
topcheck(L, 1);
lua_getglobal(L, "terrac");
if (lua_isnil(L, -1)) {
cerr << "terrac: ERROR: global `terrac` not found" << endl;
return false;
}
return true;
}
static bool get_link_flags(lua_State *L) {
topcheck(L, 1);
if (!get_terrac(L)) {
return false;
}
lua_getfield(L, -1, "link_flags");
lua_remove(L, -2);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
} else if (!lua_istable(L, -1)) {
cerr << "terrac: WARNING: terrac.link_flags is not nil and is not a table - replacing with empty table (must be table or nil)" << endl;
lua_pop(L, 1);
lua_newtable(L);
}
return true;
}
static bool get_cflags(lua_State *L) {
topcheck(L, 1);
if (!get_terrac(L)) {
return false;
}
lua_getfield(L, -1, "c_flags");
lua_remove(L, -2);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
} else if (!lua_istable(L, -1)) {
cerr << "terrac: WARNING: terrac.c_flags is not nil and is not a table - replacing with empty table (must be table or nil)" << endl;
lua_pop(L, 1);
lua_newtable(L);
}
return true;
}
static bool inject_link_flags(lua_State *L, config &conf) {
topcheck(L);
if (!get_link_flags(L)) {
lua_pop(L, 1);
return false;
}
size_t n = lua_objlen(L, -1);
for (const filesystem::path &ip : *conf.lib_dirs) {
lua_pushinteger(L, ++n);
lua_pushstring(L, "-L");
lua_settable(L, -3);
lua_pushinteger(L, ++n);
lua_pushstring(L, ip.str().c_str());
lua_settable(L, -3);
}
for (const string &l : *conf.libs) {
string fullarg = "-l" + l;
lua_pushinteger(L, ++n);
lua_pushstring(L, fullarg.c_str());
lua_settable(L, -3);
}
lua_pop(L, 1);
return true;
}
static filesystem::path mod_to_path(string mod) {
istringstream iss(mod);
filesystem::path result;
string leaf;
while (getline(iss, leaf, '.')) {
result.push_back(leaf);
}
return result;
}
static filesystem::path mod_to_relpath(string mod) {
assert(mod[0] == '.');
filesystem::path result;
size_t i = 0;
while (mod[i] == '.') {
result.push_back("..");
++i;
}
return (result / mod_to_path(mod.substr(i)));
}
static void pathenv_to_paths(string pathenv, vector<filesystem::path> &paths) {
istringstream iss(pathenv);
string path;
while (getline(iss, path, TERRA_PATHSEP)) {
paths.emplace_back(path);
}
}
static int terra_loadmodule(lua_State *L) {
/*
The only parameter that is required is
the first one - the module name. It must
also not be empty.
The second parameter - the origin file - is
only required if the module is a relative
path.
The third parameter - the original require()
function - is only used if passed and provides
a fallback mechanism for requiring files.
*/
config &conf = *((config *)lua_touserdata(L, lua_upvalueindex(1)));
string mod = luaL_checkstring(L, 1);
string origin = lua_isstring(L, 2) ? lua_tostring(L, 2) : "";
stringstream reason;
string terramodpath;
vector<filesystem::path> terrapaths;
filesystem::path modpath;
filesystem::path trypath;
if (mod.empty()) {
luaL_error(L, "module path cannot be empty");
return 0; /* not hit */
}
// Fail fast: valid module paths are only [.a-z0-9_-]i
for (int i = 0, len = mod.length(); i < len; i++) {
char c = mod[i];
if (!(
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '.' || c == '_' || c == '-'))
{
reason << "\n\tnot a valid module string (contains invalid characters)";
goto fallback_loader;
}
}
if (mod[0] == '.') {
if (origin.empty()) {
reason << "\n\tmodule looked like a relative path but no origin was provided";
goto fallback_loader;
}
modpath = mod_to_relpath(mod);
trypath = (filesystem::path(origin) / modpath.with_extension(".t")).resolve(false);
if (trypath.exists()) {
modpath = trypath;
goto resolved;
}
reason << "\n\tno terra module '" << trypath << "'";
trypath = (filesystem::path(origin) / modpath / "init.t").resolve(false);
if (trypath.exists()) {
modpath = trypath;
goto resolved;
}
reason << "\n\tno terra module '" << trypath << "'";
} else {
// get the path
lua_getglobal(L, "terralib");
lua_getfield(L, -1, "modpath");
terramodpath = lua_isnil(L, -1) ? "" : lua_tostring(L, -1);
lua_pop(L, 2);
if (terramodpath.empty()) {
reason << "\n\tterralib.modpath is empty";
goto fallback_loader;
}
modpath = mod_to_path(mod);
pathenv_to_paths(terramodpath, terrapaths);
for (const auto &path : terrapaths) {
trypath = (path / modpath).with_extension(".t");
if (trypath.exists()) {
modpath = trypath;
goto resolved;
}
reason << "\n\tno terra module '" << trypath << "'";
trypath = path / modpath / "init.t";
if (trypath.exists()) {
modpath = trypath;
goto resolved;
}
reason << "\n\tno terra module '" << trypath << "'";
}
}
fallback_loader:
if (lua_isfunction(L, 3)) {
// try to load it
lua_pushvalue(L, 3);
lua_pushvalue(L, 1);
if (lua_pcall(L, 1, 1, 0) != 0) {
// better error message opportunity here
luaL_error(L, "terra module '%s' not found:%s\ndefault loader also failed: %s",
lua_tostring(L, 1),
reason.str().c_str(),
lua_tostring(L, -1));
return 0; /* not hit */
}
// success!
conf.depfiles->push_back(lua_tostring(L, 1));
return 1;
}
luaL_error(L, "could not find module '%s'");
return 0; /* not hit */
resolved:
if (terra_loadfile(L, modpath.str().c_str())) {
lua_error(L);
return 0; /* not hit */
}
lua_call(L, 0, 1);
// success!
conf.depfiles->push_back(modpath.str());
return 1;
}
static int try_module_load(lua_State *L) {
topcheck(L, 1);
lua_getglobal(L, "terralib");
lua_getfield(L, -1, "loadmodule");
lua_remove(L, -2);
if (!lua_isfunction(L, -1)) {
lua_pop(L, 1);
disarmtopcheck();
luaL_error(L, "terralib.loadmodule is not a function");
return 0; /* not hit */
}
lua_pushvalue(L, 1); // the module being require()'d
lua_pushvalue(L, lua_upvalueindex(1)); // the origin filename
lua_pushvalue(L, lua_upvalueindex(2)); // the original (lua-provided) require()
lua_call(L, 3, 1);
return 1;
}
static int make_module_loader(lua_State *L) {
topcheck(L, 1);
if (string(lua_tostring(L, 2)) != "require") {
// fallback to regular index
lua_pushvalue(L, lua_upvalueindex(1));
lua_pushvalue(L, 1);
lua_pushvalue(L, 2);
lua_call(L, 2, 1);
return 1;
}
lua_Debug dbg;
assert(lua_getstack(L, 1, &dbg) == 1);
assert(lua_getinfo(L, "S", &dbg) != 0);
const char *source = dbg.source;
if (*source == '@') {
++source;
} else {
// @ signifies a module-loadable chunk.
// this chunk doesn't start with it,
// so give it the old require.
lua_pushvalue(L, lua_upvalueindex(2));
return 1;
}
lua_pushstring(L, source);
lua_pushvalue(L, lua_upvalueindex(2));
lua_pushcclosure(L, &try_module_load, 2);
return 1;
}
static void inject_mod_loader(lua_State *L, config &conf) {
topcheck(L);
// let's tango.
//
// - push original _G.__index and _G.require as upvalues to make_module_loader
// - set make_module_loader as _G.__index
// - nil-out _G.require in order to start triggering new module loader
lua_getglobal(L, "_G");
lua_getmetatable(L, -1);
lua_getfield(L, -1, "__index");
lua_getfield(L, -3, "require");
assert(!lua_isnil(L, -1));
lua_pushcclosure(L, &make_module_loader, 2);
lua_setfield(L, -2, "__index");
lua_pushnil(L);
lua_setfield(L, -3, "require");
lua_pop(L, 2);
// install the default module loader
lua_getglobal(L, "terralib");
lua_pushlightuserdata(L, &conf);
lua_pushcclosure(L, &terra_loadmodule, 1);
lua_setfield(L, -2, "loadmodule");
lua_pop(L, 1);
}
/*
static void print_table(lua_State *L, int t) {
topcheck(L);
size_t len = lua_objlen(L, t);
cerr << "\x1b[35mBEGIN TABLE: " << len << " elements" << endl;
for (size_t i = 1; i <= len; i++) {
lua_pushinteger(L, i);
lua_gettable(L, t - 1);
cerr << "\t" << i << ": " << lua_tostring(L, -1) << endl;
lua_pop(L, 1);
}
cerr << "END TABLE.\x1b[m" << endl;
}
*/
static bool inject_cflags(lua_State *L, config &conf) {
topcheck(L);
if (!get_cflags(L)) {
lua_pop(L, 1);
return false;
}
size_t n = lua_objlen(L, -1);
for (const filesystem::path &ip : *conf.include_dirs) {
lua_pushinteger(L, ++n);
lua_pushstring(L, "-I");
lua_settable(L, -3);
lua_pushinteger(L, ++n);
lua_pushstring(L, ip.str().c_str());
lua_settable(L, -3);
}
lua_pop(L, 1);
return true;
}
static int table_assign(lua_State *L) {
int nargs = lua_gettop(L);
luaL_checktype(L, 1, LUA_TTABLE);
for (int i = 2; i <= nargs; i++) {
luaL_checktype(L, i, LUA_TTABLE);
}
topcheck(L, 1);
for (int i = 2; i <= nargs; i++) {
lua_pushnil(L);
while (lua_next(L, i)) {
lua_pushvalue(L, -2);
lua_pushvalue(L, -2);
lua_settable(L, 1);
lua_pop(L, 1);
}
}
lua_pushvalue(L, 1);
return 1;
}
/*
can be used for both includec and includecstring as it's
just a simple proxy.
if terra changes the prototype between the two, this will
have to be updated.
*/
static int terrac_includec(lua_State *L) {
luaL_checkstring(L, 1);
size_t nargs = lua_gettop(L);
/*
"artificial" means that we're artifically
injecting the second parameter where the Lua
function was caled without one.
*/
int artificial = 0;
if (nargs < 2) {
artificial = 1;
nargs = 2;
}
// upvalue 1 has the original includec[string] function
lua_pushvalue(L, lua_upvalueindex(1));
assert(!lua_isnil(L, -1));
for (size_t i = 1; i <= nargs; i++) {
if (i == 2) {
topcheck(L, 1);
size_t n = 0;
lua_newtable(L);
// cflags object
get_cflags(L);
assert(!lua_isnil(L, -1));
size_t cn = lua_objlen(L, -1);
for (size_t j = 1; j <= cn; j++) {
topcheck(L);
lua_pushinteger(L, ++n); // args_k = ++n
lua_pushinteger(L, j); // cflag_k = j
lua_gettable(L, -3); // push(cflags[cflag_k])
lua_settable(L, -4); // args[args_k] = pop()
}
lua_pop(L, 1);
// args object
if (!artificial && !lua_isnil(L, i)) {
luaL_checktype(L, i, LUA_TTABLE);
cn = lua_objlen(L, i);
for (size_t j = 1; j <= cn; j++) {
topcheck(L);
lua_pushinteger(L, ++n); // args_k = ++n
lua_pushinteger(L, j); // cflag_k = j
lua_gettable(L, i); // push(cflags[cflag_k])
lua_settable(L, -3); // args[args_k] = pop()
}
}
continue;
}
lua_pushvalue(L, i);
}
if (lua_pcall(L, nargs, LUA_MULTRET, 0) != 0) {
lua_error(L);
return 0;
}
return lua_gettop(L) - (nargs - artificial);
}
static bool inject_includec(lua_State *L, config& conf) {
topcheck(L);
lua_getglobal(L, "terralib");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "includec");
assert(!lua_isnil(L, -1));
lua_pushlightuserdata(L, (void *) &conf);
lua_pushcclosure(L, &terrac_includec, 2);
lua_setfield(L, -2, "includec");
lua_getfield(L, -1, "includecstring");
assert(!lua_isnil(L, -1));
lua_pushlightuserdata(L, (void *) &conf);
lua_pushcclosure(L, &terrac_includec, 2);
lua_setfield(L, -2, "includecstring");
lua_pop(L, 1);
return true;
}
int pmain(config &conf) {
int status = 0;
assert(conf.filename != nullptr);
// init environment
lua_State *L = luaL_newstate();
if (L == NULL) {
cerr << "terrac: memory allocation for LuaJIT state failed" << endl;
return 42;
}
// load libraries
luaL_openlibs(L);
// patch package.searchpath
if (luaL_dostring(L, SEARCHPATH_LUA) != 0) {
cerr << "terrac: could not patch package.searchpath(): " << lua_tostring(L, -1) << endl;
return 1;
}
// configure Terra
terra_Options topts;
topts.verbose = conf.verbosity == 0 ? 0 : conf.verbosity - 1;
topts.debug = (int) conf.debug;
topts.usemcjit = 0;
terra_initwithoptions(L, &topts);
// set error handler ref
lua_pushcfunction(L, &errfn);
errfn_ref = lua_gettop(L);
// override loaded table to detect dependencies
{
topcheck(L);
lua_getglobal(L, "package");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "loaded");
lua_newtable(L);
lua_pushlightuserdata(L, (void *) &conf);
lua_pushcclosure(L, &on_loaded_newindex, 1);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
lua_pop(L, 2);
}
// inject module path
{
topcheck(L);
stringstream modpath;
char *terramodpath = getenv("TERRA_MODPATH");
if (terramodpath != NULL) {
modpath << TERRA_PATHSEP << terramodpath;
}
for (const auto &mpath : *conf.modulepaths) {
modpath << TERRA_PATHSEP << mpath;
}
if (!conf.nosysmods) {
modpath << TERRA_PATHSEP << DEFAULT_MODPATH;
}
string modpaths = modpath.str().substr(1);
lua_getglobal(L, "terralib");
lua_pushstring(L, modpaths.c_str());
lua_setfield(L, -2, "modpath");
lua_pop(L, 1);
}
// inject module loader
{
topcheck(L);
inject_mod_loader(L, conf);
}
// create terrac object
{
topcheck(L);
lua_newtable(L);
lua_newtable(L);
lua_setfield(L, -2, "c_flags");
lua_newtable(L);
lua_setfield(L, -2, "link_flags");
lua_setglobal(L, "terrac");
}
// inject configuration
{
topcheck(L);
if (!inject_cflags(L, conf)) return 1;
if (!inject_link_flags(L, conf)) return 1;
}
// inject includec replacements
{
topcheck(L);
if (!inject_includec(L, conf)) return 1;
}
// inject table.assign
{
topcheck(L);
lua_getglobal(L, "table");
assert(!lua_isnil(L, -1));
lua_pushcfunction(L, &table_assign);
lua_setfield(L, -2, "assign");
lua_pop(L, 1);
}
// run the file
if ((terra_loadfile(L, conf.filename) || lua_pcall(L, 0, LUA_MULTRET, errfn_ref))) {
cerr << "terrac: terra error: " << lua_tostring(L, -1) << endl;
return 1;
}
if (conf.output) {
topcheck(L);
// enumerate public globals
lua_newtable(L);
lua_pushnil(L);
while (lua_next(L, LUA_GLOBALSINDEX) != 0) {
if (is_terrafn(L)) {
if (conf.verbosity > 0) {
cerr << "terrac: export: " << lua_tostring(L, -2) << endl;
}
lua_pushvalue(L, -2);
lua_pushvalue(L, -2);
lua_settable(L, -5);
}
lua_pop(L, 1);
}
// compile it
lua_getglobal(L, "terralib");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "saveobj");
assert(!lua_isnil(L, -1));
lua_pushstring(L, conf.output);
lua_pushvalue(L, -4);