-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextmodule_lua.cpp
More file actions
1869 lines (1519 loc) · 46.1 KB
/
textmodule_lua.cpp
File metadata and controls
1869 lines (1519 loc) · 46.1 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 "textmodule_lua.hpp"
#include <lua.hpp>
#include <iostream>
#include <unicode/datefmt.h>
#include <unicode/dtfmtsym.h>
#include <unicode/gregocal.h>
#include <unicode/timezone.h>
#include <unicode/unistr.h>
#include <unicode/ustring.h>
#include <unicode/dtptngen.h>
#include <unicode/dtitvfmt.h>
#include <fmt/core.h>
#include <fmt/chrono.h>
#include <fmt/format.h>
#include <fmt/args.h>
#include <chrono>
#include <complex>
#include <Eigen/Geometry>
#include <Eigen/Dense>
#include <Windows.h>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include "textmodule_string.hpp"
#include "textmodule_math.hpp"
#include "textmodule_time.hpp"
#include "textmodule_exception.hpp"
#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1)
// Wstring
lua_Wstring lua_towstring(lua_State* L, int idx) {
return StrToWstr(lua_tostring(L, idx));
}
lua_Wstring tm_towstring(lua_State* L, int idx) {
return StrToWstr(luaL_checkstring(L, idx));
}
lua_Wstring tm_towstring_s(lua_State* L, int idx, lua_Wstring def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TSTRING, idx, "string/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_towstring(L, idx);
}
}
lua_Wstring tm_towstring_s(lua_State* L, int idx) {
return tm_towstring_s(L, idx, L"");
}
lua_Wstring lua_tolwstring(lua_State* L, int idx, size_t* len) {
return StrToWstr(lua_tolstring(L, idx, len));
}
lua_Wstring tm_tolwstring(lua_State* L, int idx, size_t* len) {
return StrToWstr(luaL_checklstring(L, idx, len));
}
lua_Wstring tm_tolwstring_s(lua_State* L, int idx, size_t* len, lua_Wstring def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TSTRING, idx, "string/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_tolwstring(L, idx, len);
}
}
lua_Wstring tm_tolwstring_s(lua_State* L, int idx, size_t* len) {
return tm_tolwstring_s(L, idx, len, L"");
}
void lua_pushwstring(lua_State* L, lua_Wstring s) {
lua_pushstring(L, WstrToStr(s).c_str());
}
void lua_pushlwstring(lua_State* L, lua_Wstring s, size_t l) {
lua_pushlstring(L, WstrToStr(s).c_str(), l);
}
// Sstring
lua_Sstring lua_tosstring(lua_State* L, int idx) {
return lua_tostring(L, idx);
}
lua_Sstring tm_tosstring(lua_State* L, int idx) {
return luaL_checkstring(L, idx);
}
lua_Sstring tm_tosstring_s(lua_State* L, int idx, lua_Sstring def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TSTRING, idx, "string/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_tostring(L, idx);
}
}
lua_Sstring tm_tosstring_s(lua_State* L, int idx) {
return tm_tosstring_s(L, idx, "");
}
lua_Sstring lua_tolsstring(lua_State* L, int idx, size_t* len) {
return lua_tolstring(L, idx, len);
}
lua_Sstring tm_tolsstring(lua_State* L, int idx, size_t* len) {
return luaL_checklstring(L, idx, len);
}
lua_Sstring tm_tolsstring_s(lua_State* L, int idx, size_t* len, lua_Sstring def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TSTRING, idx, "string/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_tolstring(L, idx, len);
}
}
lua_Sstring tm_tolsstring_s(lua_State* L, int idx, size_t* len) {
return tm_tolsstring_s(L, idx, len, "");
}
void lua_pushsstring(lua_State* L, lua_Sstring s) {
lua_pushstring(L, s.c_str());
}
void lua_pushlsstring(lua_State* L, lua_Sstring s, size_t l) {
lua_pushlstring(L, s.c_str(), l);
}
// Ustring
lua_Ustring lua_toustring(lua_State* L, int idx) {
return StrToUstr(lua_tostring(L, idx));
}
lua_Ustring tm_toustring(lua_State* L, int idx) {
return StrToUstr(luaL_checkstring(L, idx));
}
lua_Ustring tm_toustring_s(lua_State* L, int idx, lua_Ustring def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TSTRING, idx, "string/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_toustring(L, idx);
}
}
lua_Ustring tm_toustring_s(lua_State* L, int idx) {
return tm_toustring_s(L, idx, lua_Ustring(""));
}
void lua_pushustring(lua_State* L, lua_Ustring s) {
lua_pushstring(L, UstrToStr(s).c_str());
}
// String
lua_String tm_tostring(lua_State* L, int idx) {
return luaL_checkstring(L, idx);
}
lua_String tm_tostring_s(lua_State* L, int idx, lua_String def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TSTRING, idx, "string/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_tostring(L, idx);
}
}
lua_String tm_tostring_s(lua_State* L, int idx) {
return tm_tostring_s(L, idx, "");
}
lua_String tm_tolstring(lua_State* L, int idx, size_t* len) {
return luaL_checklstring(L, idx, len);
}
lua_String tm_tolstring_s(lua_State* L, int idx, size_t* len, lua_String def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TSTRING, idx, "string/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_tolstring(L, idx, len);
}
}
lua_String tm_tolstring_s(lua_State* L, int idx, size_t* len) {
return tm_tolstring_s(L, idx, len, "");
}
// String table
lua_SJIS lua_tosjis(lua_State* L, int idx) {
lua_SJIS ret;
int cnt = 1;
while(1) {
lua_rawgeti(L, idx, cnt);
if (!lua_isnumber(L, -1)) {
lua_pop(L, 1);
break;
}
unsigned char c = tm_tonumber(L, -1);
ret += (char)c;
lua_pop(L, 1);
cnt++;
}
return ret;
}
lua_SJIS tm_tosjis(lua_State* L, int idx) {
luaL_argcheck(L, lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_SJIS), idx, "string (shift-jis) expected");
return lua_tosjis(L, idx);
}
lua_SJIS tm_tosjis_s(lua_State* L, int idx) {
if (lua_type(L, idx) == LUA_TSTRING)
return lua_tostring(L, idx);
else if (lua_type(L, idx) == LUA_TTABLE && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_SJIS))
return tm_tosjis(L, idx);
else {
luaL_argerror(L, idx, "string/string (shift-jis) expected");
return "";
}
}
void lua_pushsjis(lua_State* L, lua_SJIS str) {
int len = str.length();
lua_newtable(L);
for (int i = 0; i < len; i++) {
lua_settablevalue(L, i+1, (unsigned char)str[i]);
}
luaL_getmetatable(L, TEXTMODULE_STRING_SJIS);
lua_setmetatable(L, -2);
}
bool lua_issjis(lua_State* L, int idx) {
return (lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_SJIS));
}
lua_UTF8 lua_toutf8(lua_State* L, int idx) {
lua_UTF8 ret;
int cnt = 1;
while (1) {
lua_rawgeti(L, idx, cnt);
if (!lua_isnumber(L, -1)) {
lua_pop(L, 1);
break;
}
unsigned char c = tm_tonumber(L, -1);
ret += (char8_t)c;
lua_pop(L, 1);
cnt++;
}
return ret;
}
lua_UTF8 tm_toutf8(lua_State* L, int idx) {
luaL_argcheck(L, lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_UTF8), idx, "string (utf-8) expected");
return lua_toutf8(L, idx);
}
lua_UTF8 tm_toutf8_s(lua_State* L, int idx) {
if (lua_type(L, idx) == LUA_TSTRING)
return StrToU8str(lua_tostring(L, idx));
else if (lua_type(L, idx) == LUA_TTABLE && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_UTF8))
return tm_toutf8(L, idx);
else {
luaL_argerror(L, idx, "string/string (utf-8) expected");
return u8"";
}
}
void lua_pushutf8(lua_State* L, lua_UTF8 str) {
int len = str.length();
lua_newtable(L);
for (int i = 0; i < len; i++) {
lua_settablevalue(L, i + 1, (lua_Number)str[i]);
}
luaL_getmetatable(L, TEXTMODULE_STRING_UTF8);
lua_setmetatable(L, -2);
}
bool lua_isutf8(lua_State* L, int idx) {
return (lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_UTF8));
}
lua_UTF16 lua_toutf16(lua_State* L, int idx) {
lua_UTF16 ret;
int cnt = 1;
while (1) {
lua_rawgeti(L, idx, cnt);
if (!lua_isnumber(L, -1)) {
lua_pop(L, 1);
break;
}
wchar_t c = tm_tonumber(L, -1);
ret += c;
lua_pop(L, 1);
cnt++;
}
return ret;
}
lua_UTF16 tm_toutf16(lua_State* L, int idx) {
luaL_argcheck(L, lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_UTF16), idx, "string (utf-16) expected");
return lua_toutf16(L, idx);
}
lua_UTF16 tm_toutf16_s(lua_State* L, int idx) {
if (lua_type(L, idx) == LUA_TSTRING)
return lua_towstring(L, 1);
else if (lua_type(L, idx) == LUA_TTABLE && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_UTF16))
return tm_toutf16(L, idx);
else {
luaL_argerror(L, idx, "string/string (utf-16) expected");
return L"";
}
}
void lua_pushutf16(lua_State* L, lua_UTF16 str) {
int len = str.length();
lua_newtable(L);
for (int i = 0; i < len; i++) {
lua_settablevalue(L, i + 1, (lua_Number)str[i]);
}
luaL_getmetatable(L, TEXTMODULE_STRING_UTF16);
lua_setmetatable(L, -2);
}
bool lua_isutf16(lua_State* L, int idx) {
return (lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_UTF16));
}
lua_UTF32 lua_toutf32(lua_State* L, int idx) {
lua_UTF32 ret;
int cnt = 1;
while (1) {
lua_rawgeti(L, idx, cnt);
if (!lua_isnumber(L, -1)) {
lua_pop(L, 1);
break;
}
unsigned int c = tm_tonumber(L, -1);
ret += (char32_t)c;
lua_pop(L, 1);
cnt++;
}
return ret;
}
lua_UTF32 tm_toutf32(lua_State* L, int idx) {
luaL_argcheck(L, lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_UTF32), idx, "string (utf-32) expected");
return lua_toutf32(L, idx);
}
lua_UTF32 tm_toutf32_s(lua_State* L, int idx) {
if (lua_type(L, idx) == LUA_TSTRING)
return StrToU32str(lua_tostring(L, 1));
else if (lua_type(L, idx) == LUA_TTABLE && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_UTF32))
return tm_toutf32(L, idx);
else {
luaL_argerror(L, idx, "string/string (utf-32) expected");
return U"";
}
}
void lua_pushutf32(lua_State* L, lua_UTF32 str) {
int len = str.length();
lua_newtable(L);
for (int i = 0; i < len; i++) {
lua_settablevalue(L, i + 1, (lua_Number)str[i]);
}
luaL_getmetatable(L, TEXTMODULE_STRING_UTF32);
lua_setmetatable(L, -2);
}
bool lua_isutf32(lua_State* L, int idx) {
return (lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_UTF32));
}
lua_EUCJP lua_toeucjp(lua_State* L, int idx) {
lua_EUCJP ret;
int cnt = 1;
while (1) {
lua_rawgeti(L, idx, cnt);
if (!lua_isnumber(L, -1)) {
lua_pop(L, 1);
break;
}
unsigned char c = tm_tonumber(L, -1);
ret += (char)c;
lua_pop(L, 1);
cnt++;
}
return ret;
}
lua_EUCJP tm_toeucjp(lua_State* L, int idx) {
luaL_argcheck(L, lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_EUCJP), idx, "string (euc-jp) expected");
return lua_toeucjp(L, idx);
}
lua_EUCJP tm_toeucjp_s(lua_State* L, int idx) {
if (lua_type(L, idx) == LUA_TSTRING)
return StrToEUCstr(lua_tostring(L, 1));
else if (lua_type(L, idx) == LUA_TTABLE && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_EUCJP))
return tm_toeucjp(L, idx);
else {
luaL_argerror(L, idx, "string/string (euc-jp) expected");
return "";
}
}
void lua_pusheucjp(lua_State* L, lua_EUCJP str) {
int len = str.length();
lua_newtable(L);
for (int i = 0; i < len; i++) {
lua_settablevalue(L, i + 1, (unsigned char)str[i]);
}
luaL_getmetatable(L, TEXTMODULE_STRING_EUCJP);
lua_setmetatable(L, -2);
}
bool lua_iseucjp(lua_State* L, int idx) {
return (lua_istable(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_STRING_EUCJP));
}
//Number
lua_Integer tm_tointeger(lua_State* L, int idx) {
return luaL_checkinteger(L, idx);
}
lua_Integer tm_tointeger_s(lua_State* L, int idx, lua_Integer def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TNUMBER, idx, "number/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_tointeger(L, idx);
}
}
lua_Integer tm_tointeger_s(lua_State* L, int idx) {
return tm_tointeger_s(L, idx, 0);
}
lua_Number tm_todouble(lua_State* L, int idx) {
return static_cast<double>(luaL_checknumber(L, idx));
}
lua_Number tm_tonumber(lua_State* L, int idx) {
return tm_todouble(L, idx);
}
lua_Number tm_tonumber_s(lua_State* L, int idx, lua_Number def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TNUMBER, idx, "number/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_tonumber(L, idx);
}
}
lua_Number tm_tonumber_s(lua_State* L, int idx) {
return tm_tonumber_s(L, idx, 0);
}
lua_Float tm_tofloat(lua_State* L, int idx) {
return static_cast<float>(luaL_checknumber(L, idx));
}
lua_Unsigned tm_tounsigned(lua_State* L, int idx) {
return static_cast<unsigned long>(luaL_checkinteger(L, idx));
}
// Bignumber
lua_Bignumber lua_tobignumber(lua_State* L, int idx) {
return *(reinterpret_cast<lua_Bignumber*>(luaL_checkudata(L, idx, TEXTMODULE_BIGNUMBER)));
}
lua_Bignumber tm_tobignumber(lua_State* L, int idx) {
int tp = lua_type(L, idx);
luaL_argcheck(L, (tp == LUA_TUSERDATA && luaL_checkmetatable(L, idx, TEXTMODULE_BIGNUMBER)) || tp==LUA_TSTRING || tp==LUA_TNUMBER, idx, "bignumber/number expected");
if (tp == LUA_TNUMBER) {
lua_Bignumber c = lua_tointeger(L, idx);
return c;
}
else if (tp == LUA_TSTRING) {
lua_Bignumber s(lua_tostring(L, idx));
return s;
}
else {
return lua_tobignumber(L, idx);
}
}
lua_Bignumber tm_tobignumber_s(lua_State* L, int idx, lua_Bignumber def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TNUMBER || tp==LUA_TSTRING || (lua_type(L, idx) == LUA_TUSERDATA && luaL_checkmetatable(L, idx, TEXTMODULE_BIGNUMBER)), idx, "number/bignumber/none expected");
if (tp == LUA_TNONE) {
return def;
}
else if(tp==LUA_TNUMBER) {
lua_Bignumber c = lua_tonumber(L, idx);
return c;
}
else if (tp == LUA_TSTRING) {
lua_Bignumber s(lua_tostring(L, idx));
return s;
}
else {
return lua_tobignumber(L, idx);
}
}
lua_Bignumber tm_tobignumber_s(lua_State* L, int idx) {
return tm_tobignumber_s(L, idx, 0);
}
lua_Bignumber lua_pushbignumber(lua_State* L, lua_Bignumber value) {
void* p = lua_newuserdata(L, sizeof(lua_Bignumber));
lua_Bignumber* ret = new(p) lua_Bignumber(value);
luaL_getmetatable(L, TEXTMODULE_BIGNUMBER);
lua_setmetatable(L, -2);
return *ret;
}
lua_Bignumber lua_pushbignumber(lua_State* L) {
lua_pushbignumber(L, 0);
}
bool lua_isbignumber(lua_State* L, int idx) {
return lua_isuserdata(L, idx) && luaL_checkmetatable(L, idx, TEXTMODULE_BIGNUMBER);
}
// Boolean
lua_Boolean tm_toboolean(lua_State* L, int idx) {
if(lua_type(L, idx)!=LUA_TBOOLEAN)
luaL_typerror(L, idx, lua_typename(L, LUA_TBOOLEAN));
return lua_toboolean(L, idx);
}
lua_Boolean tm_toboolean_s(lua_State* L, int idx, lua_Boolean def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TNONE || tp == LUA_TBOOLEAN, idx, "boolean/none expected");
if (tp == LUA_TNONE) {
return def;
}
else {
return lua_toboolean(L, idx);
}
}
lua_Boolean tm_toboolean_s(lua_State* L, int idx) {
return tm_toboolean_s(L, idx, false);
}
// Table
void lua_settablevalue(lua_State* L, int key, lua_Number value) {
lua_pushinteger(L, key);
lua_pushnumber(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_Integer value) {
lua_pushinteger(L, key);
lua_pushinteger(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_Boolean value) {
lua_pushinteger(L, key);
lua_pushboolean(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_String value) {
lua_pushinteger(L, key);
lua_pushstring(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_Sstring value) {
lua_pushinteger(L, key);
lua_pushsstring(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_Wstring value) {
lua_pushinteger(L, key);
lua_pushwstring(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_Bignumber value) {
lua_pushinteger(L, key);
lua_pushbignumber(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_Complex value) {
lua_pushinteger(L, key);
lua_pushcomplex(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_Vector2 value) {
lua_pushinteger(L, key);
lua_pushvector2(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_Vector3 value) {
lua_pushinteger(L, key);
lua_pushvector3(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, int key, lua_Vector4 value) {
lua_pushinteger(L, key);
lua_pushvector4(L, value);
lua_settable(L, -3);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Number value) {
lua_pushnumber(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Integer value) {
lua_pushinteger(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Boolean value) {
lua_pushboolean(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_String value) {
lua_pushstring(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Sstring value) {
lua_pushsstring(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Wstring value) {
lua_pushwstring(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Bignumber value) {
lua_pushbignumber(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Complex value) {
lua_pushcomplex(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Vector2 value) {
lua_pushvector2(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Vector3 value) {
lua_pushvector3(L, value);
lua_setfield(L, -2, key);
}
void lua_settablevalue(lua_State* L, const char* key, lua_Vector4 value) {
lua_pushvector4(L, value);
lua_setfield(L, -2, key);
}
void lua_setfield(lua_State* L, int idx, lua_Sstring k) {
lua_setfield(L, idx, k.c_str());
}
void lua_setfield(lua_State* L, int idx, lua_Wstring k) {
lua_setfield(L, idx, WstrToStr(k));
}
bool lua_isarray(lua_State* L, int idx) {
int h = lua_gettop(L);
bool ret = true;
double i = 1;
lua_pushnil(L);
while (lua_next(L, idx) != 0) {
if (lua_type(L, -2) == LUA_TNUMBER) {
double j = lua_tonumber(L, -2);
if (j - i != 0) ret = false;
}
else ret = false;
i++;
lua_pop(L, 1);
}
lua_settop(L, h);
return ret;
}
std::vector<lua_Number> lua_tosequence(lua_State* L, int idx, int start) {
int i = start;
std::vector<lua_Number> vec;
if (!lua_istable(L, idx)) {
luaL_argerror(L, idx, "table expected");
return vec;
}
while (1) {
lua_rawgeti(L, idx, i);
if (lua_isnumber(L, -1))
vec.push_back(lua_tonumber(L, -1));
else
break;
i++;
}
return vec;
}
std::vector<lua_Number> lua_tosequence(lua_State* L, int idx) {
return lua_tosequence(L, idx, 1);
}
std::vector<lua_Complex> lua_tocsequence(lua_State* L, int idx, int start) {
int i = start;
std::vector<lua_Complex> vec;
if (!lua_istable(L, idx)) {
luaL_argerror(L, idx, "table expected");
return vec;
}
while (1) {
lua_rawgeti(L, idx, i);
if (lua_isnumber(L, -1) || lua_iscomplex(L, -1))
vec.push_back(*lua_tocomplex(L, -1));
else
break;
i++;
}
return vec;
}
std::vector<lua_Complex> lua_tocsequence(lua_State* L, int idx) {
return lua_tocsequence(L, idx, 1);
}
// Clock
lua_Clock* lua_toclock(lua_State* L, int idx) {
return reinterpret_cast<std::chrono::utc_clock::time_point*>(luaL_checkudata(L, idx, TEXTMODULE_CLOCK));
}
lua_Clock* tm_toclock(lua_State* L, int idx) {
luaL_argcheck(L, lua_type(L, idx) == LUA_TUSERDATA && luaL_checkmetatable(L, idx, TEXTMODULE_CLOCK), idx, "clock expected");
return lua_toclock(L, idx);
}
lua_Clock* tm_toclock_s(lua_State* L, int idx, lua_Clock def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, (tp == LUA_TUSERDATA && luaL_checkmetatable(L, idx, TEXTMODULE_CLOCK)) || tp == LUA_TNONE, idx, "clock/none expected");
if (tp == LUA_TUSERDATA)
return lua_toclock(L, idx);
else if (tp == LUA_TNONE)
return &def;
}
lua_Clock* tm_toclock_s(lua_State* L, int idx) {
return tm_toclock_s(L, idx, std::chrono::utc_clock::now());
}
lua_Clock* lua_pushclock(lua_State* L, lua_Clock clock) {
void* p = lua_newuserdata(L, sizeof(lua_Clock));
lua_Clock* ret = new(p) lua_Clock(clock);
luaL_getmetatable(L, TEXTMODULE_CLOCK);
lua_setmetatable(L, -2);
return ret;
}
lua_Clock* lua_pushclock(lua_State* L) {
return lua_pushclock(L, std::chrono::utc_clock::now());
}
bool lua_isclock(lua_State* L, int idx) {
return lua_type(L, idx) == LUA_TUSERDATA && luaL_checkmetatable(L, idx, TEXTMODULE_CLOCK);
}
// Complex
lua_Complex* lua_tocomplex(lua_State* L, int idx) {
return reinterpret_cast<std::complex<double>*>(luaL_checkudata(L, idx, TEXTMODULE_COMPLEX));
}
lua_Complex* tm_tocomplex(lua_State* L, int idx) {
int tp = lua_type(L, idx);
luaL_argcheck(L, (tp == LUA_TUSERDATA && luaL_checkmetatable(L, idx, TEXTMODULE_COMPLEX)) || tp == LUA_TNUMBER, idx, "number/complex expected");
std::complex<double>* val = new std::complex<double>;
if (tp == LUA_TUSERDATA) {
val = lua_tocomplex(L, idx);
}
else if (tp == LUA_TNUMBER) {
val->real(lua_tonumber(L, idx));
val->imag(0);
}
return val;
}
lua_Complex* tm_tocomplex_s(lua_State* L, int idx, lua_Complex def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TUSERDATA || tp == LUA_TNUMBER || tp == LUA_TNONE, idx, "number/complex/none expected");
std::complex<double>* val = new std::complex<double>;
if (tp == LUA_TUSERDATA) {
val = lua_tocomplex(L, idx);
}
else if (tp == LUA_TNUMBER) {
val->real(lua_tonumber(L, idx));
val->imag(0);
}
else if (tp == LUA_TNONE) {
val->real(def.real());
val->imag(def.imag());
}
return val;
}
lua_Complex* tm_tocomplex_s(lua_State* L, int idx) {
return tm_tocomplex_s(L, idx, lua_Complex(0, 0));
}
lua_Complex* lua_pushcomplex(lua_State* L, double real, double imag) {
void* p = lua_newuserdata(L, sizeof(lua_Complex));
lua_Complex* ret = new(p) lua_Complex(real, imag);
luaL_getmetatable(L, TEXTMODULE_COMPLEX);
lua_setmetatable(L, -2);
return ret;
}
lua_Complex* lua_pushcomplex(lua_State* L, lua_Complex complex) {
return lua_pushcomplex(L, complex.real(), complex.imag());
}
lua_Complex* lua_pushcomplex(lua_State* L) {
return lua_pushcomplex(L, 0, 0);
}
bool lua_iscomplex(lua_State* L, int idx) {
return lua_type(L, idx) == LUA_TUSERDATA && luaL_checkmetatable(L, idx, TEXTMODULE_COMPLEX);
}
// Quaternion
lua_Quaternion* lua_toquaternion(lua_State* L, int idx) {
return reinterpret_cast<Quat*>(luaL_checkudata(L, idx, TEXTMODULE_QUATERNION));
}
lua_Quaternion* tm_toquaternion(lua_State* L, int idx) {
int tp = lua_type(L, idx);
luaL_argcheck(L, (tp == LUA_TUSERDATA && luaL_checkmetatable(L, idx, TEXTMODULE_QUATERNION)) || tp == LUA_TNUMBER, idx, "number/quaternion expected");
Quat* val = new Quat();
if (tp == LUA_TUSERDATA) {
val = lua_toquaternion(L, idx);
}
else if (tp == LUA_TNUMBER) {
val->w() = lua_tonumber(L, idx);
}
return val;
}
lua_Quaternion* tm_toquaternion_s(lua_State* L, int idx, lua_Quaternion def) {
int tp = lua_type(L, idx);
luaL_argcheck(L, tp == LUA_TUSERDATA || tp == LUA_TNUMBER || LUA_TNONE, idx, "number/quaternion/none expected");
Quat* val = new Quat();
if (tp == LUA_TUSERDATA) {
val = lua_toquaternion(L, idx);
}
else if (tp == LUA_TNUMBER) {
val->w() = lua_tonumber(L, idx);
}
else if (tp == LUA_TNONE) {
val->w() = def.w();
val->x() = def.x();
val->y() = def.y();
val->z() = def.z();
}
return val;
}
lua_Quaternion* tm_toquaternion_s(lua_State* L, int idx) {
return tm_toquaternion_s(L, idx, lua_Quaternion(0, 0, 0, 0));
}
lua_Quaternion* lua_pushquaternion(lua_State* L, double w, double x, double y, double z) {
Quat* ret = reinterpret_cast<Quat*>(lua_newuserdata(L, sizeof(Quat)));
luaL_getmetatable(L, TEXTMODULE_QUATERNION);
lua_setmetatable(L, -2);
ret->w() = w;
ret->x() = x;
ret->y() = y;
ret->z() = z;
return ret;
}
lua_Quaternion* lua_pushquaternion(lua_State* L, lua_Quaternion quaternion) {
return lua_pushquaternion(L, quaternion.w(), quaternion.x(), quaternion.y(), quaternion.z());
}