Repository navigation
Expand file tree
/
Copy pathbLua.h
More file actions
1142 lines (1130 loc) · 56.8 KB
/
Copy pathbLua.h
File metadata and controls
1142 lines (1130 loc) · 56.8 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
#pragma once
#if defined(__has_include)
#if __has_include(<lua.hpp>)
#include <lua.hpp>
#elif __has_include(<lua.h>)
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#else
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#endif
#else
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#endif
#include <typeinfo>
#include <type_traits>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
#include <map>
#include <optional>
#include <stdexcept>
#include <memory>
#include <functional>
#include <initializer_list>
#include <cstdint>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <new>
#include <limits>
#include <cmath>
#if defined(__GNUG__) && !defined(_MSC_VER)
#include <cxxabi.h>
#endif
namespace bLua {
enum class ownership { owning, non_owning };
namespace internal {
inline void ensure_stack(lua_State *L, int n) {
if (!lua_checkstack(L, n)) throw std::runtime_error("Lua stack capacity exhausted");
}
inline int absindex(lua_State *L, int i) {
return i > 0 || i <= LUA_REGISTRYINDEX ? i : lua_gettop(L) + i + 1;
}
inline size_t rawlen(lua_State *L, int i) {
#if LUA_VERSION_NUM < 502
return lua_objlen(L, i);
#else
return lua_rawlen(L, i);
#endif
}
inline void rawgetp(lua_State *L, int i, const void *p) {
ensure_stack(L, 2);
i = absindex(L, i);
lua_pushlightuserdata(L, const_cast<void *>(p));
lua_rawget(L, i);
}
inline void rawsetp(lua_State *L, int i, const void *p) {
ensure_stack(L, 2);
i = absindex(L, i);
lua_pushlightuserdata(L, const_cast<void *>(p));
lua_insert(L, -2);
lua_rawset(L, i);
}
inline void rawfield(lua_State *L, int i, const char *name) {
ensure_stack(L, 2);
i = absindex(L, i);
lua_pushstring(L, name);
lua_rawget(L, i);
}
inline int table_hint(size_t n) {
constexpr size_t cap = static_cast<size_t>(std::numeric_limits<int>::max());
return n > cap ? std::numeric_limits<int>::max() : static_cast<int>(n);
}
inline void rawgeti_idx(lua_State *L, int i, lua_Integer n) {
ensure_stack(L, 2);
i = absindex(L, i);
#if LUA_VERSION_NUM >= 503
lua_rawgeti(L, i, n);
#else
if (n >= static_cast<lua_Integer>(std::numeric_limits<int>::min()) &&
n <= static_cast<lua_Integer>(std::numeric_limits<int>::max())) {
lua_rawgeti(L, i, static_cast<int>(n));
} else {
lua_pushinteger(L, n);
lua_rawget(L, i);
}
#endif
}
inline void rawseti_idx(lua_State *L, int i, lua_Integer n) {
ensure_stack(L, 2);
i = absindex(L, i);
#if LUA_VERSION_NUM >= 503
lua_rawseti(L, i, n);
#else
if (n >= static_cast<lua_Integer>(std::numeric_limits<int>::min()) &&
n <= static_cast<lua_Integer>(std::numeric_limits<int>::max())) {
lua_rawseti(L, i, static_cast<int>(n));
} else {
lua_pushinteger(L, n);
lua_insert(L, -2);
lua_rawset(L, i);
}
#endif
}
inline void globals(lua_State *L) {
#if LUA_VERSION_NUM < 502
lua_pushvalue(L, LUA_GLOBALSINDEX);
#else
lua_pushglobaltable(L);
#endif
}
struct stack_guard {
lua_State *L;
int top;
explicit stack_guard(lua_State *s) : L(s), top(lua_gettop(s)) {}
~stack_guard() { lua_settop(L, top); }
stack_guard(const stack_guard &) = delete;
stack_guard &operator=(const stack_guard &) = delete;
};
// Conversion context lives in the Lua registry, not thread_local: LuaJIT's
// shared-library foreign DWARF unwinder mis-handles TLS access around the
// C++ exception -> luaL_error boundary (errors become "unknown C++ exception").
inline char conv_key;
struct conv_scope;
inline conv_scope *load_scope(lua_State *L) {
if (!L) return nullptr;
const int top = lua_gettop(L);
lua_pushlightuserdata(L, &conv_key);
lua_rawget(L, LUA_REGISTRYINDEX);
auto *p = lua_type(L, -1) == LUA_TLIGHTUSERDATA
? static_cast<conv_scope *>(lua_touserdata(L, -1)) : nullptr;
lua_settop(L, top);
return p;
}
inline void store_scope(lua_State *L, conv_scope *s) {
if (!L) return;
lua_pushlightuserdata(L, &conv_key);
if (s) lua_pushlightuserdata(L, s);
else lua_pushnil(L);
lua_rawset(L, LUA_REGISTRYINDEX);
}
struct conv_scope {
lua_State *L;
const char *kind;
int index;
conv_scope *prev;
conv_scope(lua_State *state, const char *k, int i)
: L(state), kind(k), index(i), prev(load_scope(state)) {
store_scope(L, this);
}
~conv_scope() { store_scope(L, prev); }
conv_scope(const conv_scope &) = delete;
conv_scope &operator=(const conv_scope &) = delete;
};
inline std::string conv_prefix(lua_State *L) {
std::vector<const conv_scope *> stack;
for (auto *c = load_scope(L); c; c = c->prev) stack.push_back(c);
std::string pre;
for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
if (!pre.empty()) pre += ": ";
pre += (*it)->kind;
if ((*it)->index > 0) {
pre += ' ';
pre += std::to_string((*it)->index);
}
}
return pre;
}
[[noreturn]] inline void fail(lua_State *L, const std::string &s) {
auto pre = conv_prefix(L);
throw std::runtime_error(pre.empty() ? s : pre + ": " + s);
}
[[noreturn]] inline void fail(lua_State *L, const char *s) { fail(L, std::string(s ? s : "")); }
// Never luaL_error while an exception / argument tuple is alive. All validation
// below throws C++; only this outermost callback converts it to a Lua error.
template<int (*Fn)(lua_State *)>
int boundary(lua_State *L) {
char error[1024];
try { return Fn(L); }
catch (const std::exception &e) { std::snprintf(error, sizeof(error), "%s", e.what()); }
catch (...) {
#if defined(__GNUG__) && !defined(_MSC_VER)
// LuaJIT uses foreign DWARF exceptions for Lua errors on ELF targets.
// Do not consume them (or terminate at a noexcept boundary): let Lua's
// protected call catch its own unwinder. Native C++ exceptions have a type.
if (abi::__cxa_current_exception_type() == nullptr) throw;
#endif
std::snprintf(error, sizeof(error), "unknown C++ exception");
}
return luaL_error(L, "%s", error);
}
template<typename T> inline char type_key;
template<typename T> inline char meta_key;
template<typename T> inline char table_key;
template<typename T> inline char class_table_key;
template<typename T> inline char getters_key;
template<typename T> inline char setters_key;
inline char parents_key;
inline char cache_key;
enum class hold : unsigned char { none, shared };
template<typename T> const std::string &fallback_name() {
static const std::string name = [] {
#if defined(__GNUG__) && !defined(_MSC_VER)
int status = 0;
std::unique_ptr<char, decltype(&std::free)> demangled(
abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status), &std::free);
if (status == 0 && demangled) return std::string(demangled.get());
#endif
return std::string(typeid(T).name());
}();
return name;
}
template<typename T> void delete_object(T *p) { delete p; }
template<typename T> struct class_state {
std::string name = fallback_name<T>();
ownership own = ownership::owning;
void (*deleter)(T *) = &delete_object<T>;
bool registered = false;
struct upcast {
const void *derived_table;
const void *derived_type;
void *(*cast)(void *);
};
struct base_alias {
void *(*to_base)(void *);
};
std::vector<upcast> upcasts;
std::vector<base_alias> bases;
};
template<typename T> int free_state(lua_State *L) {
// Private metatable, not exposed through any object.
static_cast<class_state<T> *>(lua_touserdata(L, 1))->~class_state<T>();
return 0;
}
template<typename T> class_state<T> &state(lua_State *L) {
stack_guard guard(L);
ensure_stack(L, 6);
rawgetp(L, LUA_REGISTRYINDEX, &meta_key<T>);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
auto p = static_cast<class_state<T> *>(lua_newuserdata(L, sizeof(class_state<T>)));
new (p) class_state<T>();
lua_newtable(L);
lua_pushcfunction(L, free_state<T>);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2);
lua_pushvalue(L, -1);
rawsetp(L, LUA_REGISTRYINDEX, &meta_key<T>);
}
return *static_cast<class_state<T> *>(lua_touserdata(L, -1));
}
template<typename T> void push_metatable(lua_State *L) {
rawgetp(L, LUA_REGISTRYINDEX, &table_key<T>);
if (!lua_istable(L, -1)) { lua_pop(L, 1); fail(L, "class is not registered"); }
}
struct userdata_base {
const void *type;
void *ptr;
bool alive;
bool owning;
hold extra_kind;
void *extra;
void (*drop_extra)(void *);
};
template<typename T> struct userdata {
userdata_base base;
void (*const deleter)(T *); // fixed when this wrapper is created
};
template<typename H> void drop_holder(void *e) { delete static_cast<H *>(e); }
inline void clear_extra(userdata_base *ud) {
if (ud->drop_extra && ud->extra) ud->drop_extra(ud->extra);
ud->extra = nullptr;
ud->drop_extra = nullptr;
ud->extra_kind = hold::none;
}
template<typename T> userdata<T> *test_object(lua_State *L, int i) {
ensure_stack(L, 3);
i = absindex(L, i);
if (lua_type(L, i) != LUA_TUSERDATA || rawlen(L, i) != sizeof(userdata<T>)) return nullptr;
stack_guard guard(L);
if (!lua_getmetatable(L, i)) return nullptr;
rawgetp(L, LUA_REGISTRYINDEX, &table_key<T>);
if (!lua_rawequal(L, -1, -2)) return nullptr;
// Do not interpret foreign userdata's bytes before validating its metatable.
auto ud = static_cast<userdata<T> *>(lua_touserdata(L, i));
return ud->base.type == &type_key<T> ? ud : nullptr;
}
// Validate the metatable against a known derived type before reading bytes.
template<typename T> T *try_upcast_ptr(lua_State *L, int i) {
ensure_stack(L, 4);
i = absindex(L, i);
if (lua_type(L, i) != LUA_TUSERDATA) return nullptr;
if (!lua_getmetatable(L, i)) return nullptr;
const auto &meta = state<T>(L);
for (const auto &u : meta.upcasts) {
rawgetp(L, LUA_REGISTRYINDEX, u.derived_table);
const bool match = lua_rawequal(L, -1, -2);
lua_pop(L, 1);
if (!match) continue;
lua_pop(L, 1);
auto ud = static_cast<userdata_base *>(lua_touserdata(L, i));
if (!ud || ud->type != u.derived_type) return nullptr;
if (!ud->alive || !ud->ptr) fail(L, "object has been destroyed");
return static_cast<T *>(u.cast(ud->ptr));
}
lua_pop(L, 1);
return nullptr;
}
template<typename T> T *check_t(lua_State *L, int i) {
if (auto ud = test_object<T>(L, i)) {
if (!ud->base.alive || !ud->base.ptr) fail(L, "object has been destroyed");
return static_cast<T *>(ud->base.ptr);
}
if (auto *p = try_upcast_ptr<T>(L, i)) return p;
auto &meta = state<T>(L);
if (!meta.registered) fail(L, "wrong userdata type (or class is not registered)");
fail(L, std::string("expected ") + meta.name + " (or derived)");
}
template<typename T> int free_class(lua_State *L) {
auto ud = test_object<T>(L, 1);
if (!ud) return 0;
auto p = static_cast<T *>(ud->base.ptr);
const bool alive = ud->base.alive;
ud->base.ptr = nullptr;
ud->base.alive = false; // invalidate before any possibly re-entrant deleter
if (alive && ud->base.owning && p && ud->deleter) ud->deleter(p);
clear_extra(&ud->base);
return 0;
}
template<typename T> void alias_bases(lua_State *L, T *p, int cache_idx, int ud_idx) {
cache_idx = absindex(L, cache_idx);
ud_idx = absindex(L, ud_idx);
for (const auto &b : state<T>(L).bases) {
void *addr = b.to_base(static_cast<void *>(p));
lua_pushvalue(L, ud_idx);
rawsetp(L, cache_idx, addr);
}
}
template<typename T> bool reuse_cached(lua_State *L, T *p, ownership own, void (*del)(T *), bool reuse_policy) {
if (auto ud = test_object<T>(L, -1)) {
if (ud->base.alive) {
if (!reuse_policy && (ud->base.owning != (own == ownership::owning) || ud->deleter != del))
fail(L, "pointer ownership/deleter conflict");
lua_remove(L, -2);
return true;
}
return false;
}
if (try_upcast_ptr<T>(L, -1) == p) {
auto hdr = static_cast<userdata_base *>(lua_touserdata(L, -1));
if (!hdr || !hdr->alive) fail(L, "object has been destroyed");
const bool cached_owning = hdr->owning;
if (!reuse_policy && cached_owning != (own == ownership::owning))
fail(L, "pointer ownership/deleter conflict");
lua_remove(L, -2);
return true;
}
if (!lua_isnil(L, -1)) fail(L, "pointer is already exposed with a different type");
return false;
}
template<typename T> void push_ptr(lua_State *L, T *p, ownership own, bool reuse_policy = false) {
using U = std::remove_const_t<T>;
if constexpr (std::is_const_v<T>) {
if (own == ownership::owning) fail(L, "const pointers cannot be Lua-owned");
push_ptr<U>(L, const_cast<U *>(p), ownership::non_owning, reuse_policy);
return;
} else {
if (!p) { lua_pushnil(L); return; }
auto &meta = state<T>(L);
if (!meta.registered) fail(L, "register class before pushing its objects");
auto del = own == ownership::owning ? meta.deleter : nullptr;
const int top = lua_gettop(L);
ensure_stack(L, 8);
try {
rawgetp(L, LUA_REGISTRYINDEX, &cache_key);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
lua_newtable(L);
lua_pushliteral(L, "v"); lua_setfield(L, -2, "__mode");
lua_setmetatable(L, -2);
lua_pushvalue(L, -1); rawsetp(L, LUA_REGISTRYINDEX, &cache_key);
}
rawgetp(L, -1, p);
if (reuse_cached<T>(L, p, own, del, reuse_policy)) return;
lua_pop(L, 1);
auto ud = static_cast<userdata<T> *>(lua_newuserdata(L, sizeof(userdata<T>)));
new (ud) userdata<T>{{&type_key<T>, p, true, own == ownership::owning, hold::none, nullptr, nullptr}, del};
push_metatable<T>(L); lua_setmetatable(L, -2);
lua_pushvalue(L, -1); rawsetp(L, -3, p);
alias_bases<T>(L, p, -2, -1);
lua_remove(L, -2);
} catch (...) { lua_settop(L, top); throw; }
}
}
template<typename T> void push_shared(lua_State *L, const std::shared_ptr<T> &holder) {
if (!holder) { lua_pushnil(L); return; }
T *p = holder.get();
auto &meta = state<T>(L);
if (!meta.registered) fail(L, "register class before pushing its objects");
const int top = lua_gettop(L);
ensure_stack(L, 8);
try {
rawgetp(L, LUA_REGISTRYINDEX, &cache_key);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
lua_newtable(L);
lua_pushliteral(L, "v"); lua_setfield(L, -2, "__mode");
lua_setmetatable(L, -2);
lua_pushvalue(L, -1); rawsetp(L, LUA_REGISTRYINDEX, &cache_key);
}
rawgetp(L, -1, p);
if (auto ud = test_object<T>(L, -1)) {
if (!ud->base.alive) fail(L, "object has been destroyed");
if (ud->base.extra_kind != hold::shared || !ud->base.extra)
fail(L, "pointer ownership/deleter conflict");
*static_cast<std::shared_ptr<T> *>(ud->base.extra) = holder;
lua_remove(L, -2);
return;
}
if (!lua_isnil(L, -1)) fail(L, "pointer is already exposed with a different type");
lua_pop(L, 1);
auto ud = static_cast<userdata<T> *>(lua_newuserdata(L, sizeof(userdata<T>)));
new (ud) userdata<T>{{&type_key<T>, p, true, true, hold::shared,
new std::shared_ptr<T>(holder), &drop_holder<std::shared_ptr<T>>}, nullptr};
push_metatable<T>(L); lua_setmetatable(L, -2);
lua_pushvalue(L, -1); rawsetp(L, -3, p);
alias_bases<T>(L, p, -2, -1);
lua_remove(L, -2);
} catch (...) { lua_settop(L, top); throw; }
}
template<typename T> void push_unique(lua_State *L, std::unique_ptr<T> holder) {
if (!holder) { lua_pushnil(L); return; }
T *p = holder.release();
try {
push_ptr(L, p, ownership::owning, false);
} catch (...) {
holder.reset(p);
throw;
}
}
template<typename T, typename Enable = void> struct converter;
template<typename T> T lua_to_native(lua_State *L, int i) { return converter<T>::get(L, i); }
template<typename T> void native_to_lua(lua_State *L, const T &v) { converter<std::decay_t<T>>::push(L, v); }
template<typename T> void native_to_lua(lua_State *L, T *v, ownership own) { push_ptr(L, v, own); }
template<typename T> struct converter<T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>> {
static T get(lua_State *L, int i) {
if (lua_type(L, i) != LUA_TNUMBER) fail(L, "number expected");
#if LUA_VERSION_NUM >= 503
if (lua_isinteger(L, i)) {
const lua_Integer n = lua_tointeger(L, i);
if constexpr (std::is_unsigned_v<T>) {
if (n < 0 || static_cast<lua_Unsigned>(n) > std::numeric_limits<T>::max()) fail(L, "integer out of range");
} else {
if (n < std::numeric_limits<T>::lowest() || n > std::numeric_limits<T>::max()) fail(L, "integer out of range");
}
return static_cast<T>(n);
}
#endif
const lua_Number n = lua_tonumber(L, i);
// Avoid undefined out-of-range floating-to-integer conversions, including NaN.
const long double value = static_cast<long double>(n);
const long double limit = std::ldexp(1.0L, std::numeric_limits<T>::digits);
const long double lower = std::is_signed_v<T> ? -limit : 0.0L;
if (!(value >= lower && value < limit)) fail(L, "integer out of range");
if (std::trunc(n) != n) fail(L, "integer expected");
return static_cast<T>(n);
}
static void push(lua_State *L, T v) {
if constexpr (std::is_unsigned_v<T>) {
if (v > static_cast<std::make_unsigned_t<lua_Integer>>(std::numeric_limits<lua_Integer>::max()))
fail(L, "integer does not fit lua_Integer");
} else if constexpr (sizeof(T) > sizeof(lua_Integer)) {
if (v < std::numeric_limits<lua_Integer>::lowest() || v > std::numeric_limits<lua_Integer>::max())
fail(L, "integer does not fit lua_Integer");
}
lua_pushinteger(L, static_cast<lua_Integer>(v));
}
};
template<typename T> struct converter<T, std::enable_if_t<std::is_floating_point_v<T>>> {
static T get(lua_State *L, int i) {
if (lua_type(L, i) != LUA_TNUMBER) fail(L, "number expected");
return static_cast<T>(lua_tonumber(L, i));
}
static void push(lua_State *L, T v) { lua_pushnumber(L, static_cast<lua_Number>(v)); }
};
template<> struct converter<bool> {
static bool get(lua_State *L, int i) {
if (lua_type(L, i) != LUA_TBOOLEAN) fail(L, "boolean expected");
return lua_toboolean(L, i) != 0;
}
static void push(lua_State *L, bool v) { lua_pushboolean(L, v); }
};
template<> struct converter<std::string> {
static std::string get(lua_State *L, int i) {
if (lua_type(L, i) != LUA_TSTRING) fail(L, "string expected");
size_t n = 0; const char *s = lua_tolstring(L, i, &n);
return std::string(s, n);
}
static void push(lua_State *L, const std::string &s) { lua_pushlstring(L, s.data(), s.size()); }
};
template<> struct converter<std::string_view> {
static std::string_view get(lua_State *L, int i) {
if (lua_type(L, i) != LUA_TSTRING) fail(L, "string expected");
size_t n = 0; const char *s = lua_tolstring(L, i, &n);
return std::string_view(s, n);
}
static void push(lua_State *L, std::string_view s) { lua_pushlstring(L, s.empty() ? "" : s.data(), s.size()); }
};
template<> struct converter<const char *> {
static const char *get(lua_State *L, int i) {
if (lua_isnil(L, i)) return nullptr;
if (lua_type(L, i) != LUA_TSTRING) fail(L, "string or nil expected");
return lua_tostring(L, i);
}
static void push(lua_State *L, const char *s) { if (s) lua_pushstring(L, s); else lua_pushnil(L); }
};
template<> struct converter<char *> {
static void push(lua_State *L, const char *s) { converter<const char *>::push(L, s); }
};
template<> struct converter<std::nullptr_t> {
static void push(lua_State *L, std::nullptr_t) { lua_pushnil(L); }
};
template<typename T> struct converter<T *, std::enable_if_t<!std::is_same_v<std::remove_cv_t<T>, char>>> {
static T *get(lua_State *L, int i) {
using U = std::remove_const_t<T>;
if (lua_isnil(L, i)) return nullptr;
return check_t<U>(L, i);
}
static void push(lua_State *L, T *p) {
using U = std::remove_const_t<T>;
if constexpr (std::is_const_v<T>)
push_ptr(L, const_cast<U *>(p), ownership::non_owning, true);
else
push_ptr(L, p, state<U>(L).own, true);
}
};
template<typename T> struct is_unique_ptr : std::false_type {};
template<typename T, typename D> struct is_unique_ptr<std::unique_ptr<T, D>> : std::true_type {};
template<typename T> struct is_shared_ptr : std::false_type {};
template<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
template<typename T> struct is_std_vector : std::false_type {};
template<typename T, typename A> struct is_std_vector<std::vector<T, A>> : std::true_type {};
template<typename T> struct is_std_map : std::false_type {};
template<typename K, typename V, typename C, typename A> struct is_std_map<std::map<K, V, C, A>> : std::true_type {};
template<typename T> struct is_tuple_like : std::false_type {};
template<typename... Ts> struct is_tuple_like<std::tuple<Ts...>> : std::true_type {};
template<typename A, typename B> struct is_tuple_like<std::pair<A, B>> : std::true_type {};
template<typename T> struct is_optional : std::false_type {};
template<typename T> struct is_optional<std::optional<T>> : std::true_type {};
template<typename T> constexpr bool is_user_class_v =
std::is_class_v<T> && std::is_copy_constructible_v<T> &&
!std::is_same_v<T, std::string> && !std::is_same_v<T, std::string_view> &&
!is_unique_ptr<T>::value && !is_shared_ptr<T>::value &&
!is_std_vector<T>::value && !is_std_map<T>::value && !is_tuple_like<T>::value &&
!is_optional<T>::value;
template<typename T> struct converter<std::unique_ptr<T>> {
static std::unique_ptr<T> get(lua_State *L, int) {
fail(L, "unique_ptr can only be moved into Lua, not recovered");
return {};
}
static void push(lua_State *L, const std::unique_ptr<T> &) {
fail(L, "unique_ptr must be moved into Lua");
}
};
template<typename T> void native_to_lua(lua_State *L, std::unique_ptr<T> &&v) { push_unique(L, std::move(v)); }
template<typename T> struct converter<std::shared_ptr<T>> {
static std::shared_ptr<T> get(lua_State *L, int i) {
if (lua_isnil(L, i)) return {};
auto ud = test_object<T>(L, i);
if (!ud) fail(L, std::string("expected shared_ptr-backed ") + state<T>(L).name);
if (!ud->base.alive || !ud->base.ptr) fail(L, "object has been destroyed");
if (ud->base.extra_kind != hold::shared || !ud->base.extra)
fail(L, "userdata is not shared_ptr-backed");
return *static_cast<std::shared_ptr<T> *>(ud->base.extra);
}
static void push(lua_State *L, const std::shared_ptr<T> &p) { push_shared(L, p); }
};
template<typename T> struct converter<T, std::enable_if_t<is_user_class_v<T>>> {
static T get(lua_State *L, int i) { return *check_t<T>(L, i); }
static void push(lua_State *L, const T &v) {
auto p = std::make_unique<T>(v);
push_unique(L, std::move(p));
}
};
template<typename E> struct converter<E, std::enable_if_t<std::is_enum_v<E>>> {
using U = std::underlying_type_t<E>;
static E get(lua_State *L, int i) { return static_cast<E>(converter<U>::get(L, i)); }
static void push(lua_State *L, E v) { converter<U>::push(L, static_cast<U>(v)); }
};
template<typename T> struct converter<std::vector<T>> {
static std::vector<T> get(lua_State *L, int i) {
stack_guard guard(L); ensure_stack(L, 4); i = absindex(L, i);
if (!lua_istable(L, i)) fail(L, "table expected for vector");
std::vector<T> result; const size_t n = rawlen(L, i); result.reserve(n);
for (size_t k = 1; k <= n; ++k) {
const lua_Integer idx = static_cast<lua_Integer>(k);
conv_scope scope(L, "element", idx > 0 && idx <= std::numeric_limits<int>::max()
? static_cast<int>(idx) : 0);
rawgeti_idx(L, i, idx);
result.push_back(lua_to_native<T>(L, -1)); lua_pop(L, 1);
}
return result;
}
static void push(lua_State *L, const std::vector<T> &v) {
const int top = lua_gettop(L);
ensure_stack(L, 4);
try {
lua_createtable(L, table_hint(v.size()), 0);
for (size_t k = 0; k < v.size(); ++k) {
if constexpr (std::is_same_v<T, bool>) native_to_lua(L, static_cast<bool>(v[k]));
else native_to_lua(L, v[k]);
rawseti_idx(L, -2, static_cast<lua_Integer>(k + 1));
}
} catch (...) { lua_settop(L, top); throw; }
}
};
template<typename K, typename V> struct converter<std::map<K, V>> {
static std::map<K, V> get(lua_State *L, int i) {
stack_guard guard(L); ensure_stack(L, 4); i = absindex(L, i);
if (!lua_istable(L, i)) fail(L, "table expected for map");
std::map<K, V> result;
lua_pushnil(L);
while (lua_next(L, i)) {
// Preserve lua_next's original key even if a converter manipulates its slot.
lua_pushvalue(L, -2);
K key; V value;
{ conv_scope scope(L, "map key", 0); key = lua_to_native<K>(L, -1); }
lua_pop(L, 1);
{ conv_scope scope(L, "map value", 0); value = lua_to_native<V>(L, -1); }
if (!result.emplace(std::move(key), std::move(value)).second) fail(L, "duplicate converted map key");
lua_pop(L, 1);
}
return result;
}
static void push(lua_State *L, const std::map<K, V> &m) {
const int top = lua_gettop(L);
ensure_stack(L, 4);
try {
lua_createtable(L, 0, table_hint(m.size()));
for (const auto &kv : m) {
native_to_lua(L, kv.first);
if (lua_isnil(L, -1)) fail(L, "nil map key");
if (lua_type(L, -1) == LUA_TNUMBER && std::isnan(lua_tonumber(L, -1))) fail(L, "NaN map key");
native_to_lua(L, kv.second); lua_rawset(L, -3);
}
} catch (...) { lua_settop(L, top); throw; }
}
};
template<typename F> struct function_traits;
template<typename R, typename... A> struct function_traits<R (*)(A...)> {
using result = R; using args = std::tuple<std::decay_t<A>...>;
static constexpr int offset = 1;
};
template<typename T, typename R, typename... A> struct function_traits<R (T::*)(A...)> {
using result = R; using args = std::tuple<std::decay_t<A>...>; using object = T;
static constexpr int offset = 2;
};
template<typename T, typename R, typename... A> struct function_traits<R (T::*)(A...) const>
: function_traits<R (T::*)(A...)> {};
template<typename Tuple, size_t... I> Tuple read_args([[maybe_unused]] lua_State *L, [[maybe_unused]] int offset, std::index_sequence<I...>) {
return Tuple{[&] {
conv_scope scope(L, "argument", offset + static_cast<int>(I));
return lua_to_native<std::tuple_element_t<I, Tuple>>(L, offset + static_cast<int>(I));
}()...};
}
template<typename Tuple> int push_result(lua_State *L, Tuple &&value) {
if constexpr (is_tuple_like<std::decay_t<Tuple>>::value) {
return std::apply([&](auto &&...xs) {
(native_to_lua(L, std::forward<decltype(xs)>(xs)), ...);
return static_cast<int>(sizeof...(xs));
}, std::forward<Tuple>(value));
} else {
native_to_lua(L, std::forward<Tuple>(value));
return 1;
}
}
template<typename F> int call_function(lua_State *L) {
using traits = function_traits<F>; using args_type = typename traits::args;
auto func = *static_cast<F *>(lua_touserdata(L, lua_upvalueindex(1)));
auto args = read_args<args_type>(L, traits::offset, std::make_index_sequence<std::tuple_size_v<args_type>>{});
auto invoke = [&](auto &...a) -> decltype(auto) {
if constexpr (std::is_member_function_pointer_v<F>) return std::invoke(func, check_t<typename traits::object>(L, 1), a...);
else return func(a...);
};
if constexpr (std::is_void_v<typename traits::result>) { std::apply(invoke, args); return 0; }
else { return push_result(L, std::apply(invoke, args)); }
}
template<typename F> void push_function(lua_State *L, F func) {
if (func == nullptr) fail(L, "null native function");
ensure_stack(L, 2);
auto mem = static_cast<F *>(lua_newuserdata(L, sizeof(F))); new (mem) F(func);
lua_pushcclosure(L, boundary<call_function<F>>, 1);
}
template<typename T, typename... A> int construct(lua_State *L) {
int offset = 1;
rawgetp(L, LUA_REGISTRYINDEX, &class_table_key<T>);
if (lua_istable(L, -1) && lua_rawequal(L, 1, -1)) offset = 2;
lua_pop(L, 1);
using tuple = std::tuple<std::decay_t<A>...>;
auto args = read_args<tuple>(L, offset, std::index_sequence_for<A...>{});
auto obj = std::apply([](auto &...a) { return std::make_unique<T>(a...); }, args);
push_ptr(L, obj.get(), ownership::owning); obj.release(); return 1;
}
// parents table layout: { base_mt, base_getters, base_setters, ... } most-derived first.
// Lua is built as C, so luaL_error longjmps. Never lua_call from inside a
// boundary try-block: catch the error with lua_pcall and rethrow as C++ so
// the boundary can convert it after unwinding (MSVC 0xC0000409 otherwise).
inline void raise_lua_error(lua_State *L) {
if (lua_type(L, -1) == LUA_TSTRING) {
std::string msg = lua_tostring(L, -1) ? lua_tostring(L, -1) : "Lua error";
lua_pop(L, 1);
fail(L, msg);
}
lua_pop(L, 1);
fail(L, "Lua error (non-string error object)");
}
inline void pcall_checked(lua_State *L, int nargs, int nresults) {
// Lua 5.1 has no LUA_OK; success is 0 on every supported version.
if (lua_pcall(L, nargs, nresults, 0)) raise_lua_error(L);
}
inline bool index_method(lua_State *L, int table) {
lua_pushvalue(L, 2);
lua_rawget(L, table);
if (!lua_isnil(L, -1)) return true;
lua_pop(L, 1);
return false;
}
inline bool index_getter(lua_State *L, int table) {
lua_pushvalue(L, 2);
lua_rawget(L, table);
if (!lua_isfunction(L, -1)) { lua_pop(L, 1); return false; }
lua_pushvalue(L, 1);
pcall_checked(L, 1, 1);
return true;
}
inline int class_index(lua_State *L) {
if (index_method(L, lua_upvalueindex(1))) return 1;
if (index_getter(L, lua_upvalueindex(2))) return 1;
rawgetp(L, lua_upvalueindex(1), &parents_key);
if (lua_istable(L, -1)) {
const size_t n = rawlen(L, -1);
for (size_t i = 1; i + 2 <= n; i += 3) {
rawgeti_idx(L, -1, static_cast<lua_Integer>(i));
const bool have = index_method(L, lua_gettop(L));
lua_remove(L, have ? -2 : -1);
if (have) { lua_remove(L, -2); return 1; }
rawgeti_idx(L, -1, static_cast<lua_Integer>(i + 1));
const bool got = index_getter(L, lua_gettop(L));
lua_remove(L, got ? -2 : -1);
if (got) { lua_remove(L, -2); return 1; }
}
}
lua_pop(L, 1);
lua_pushnil(L);
return 1;
}
inline bool newindex_setter(lua_State *L, int table) {
lua_pushvalue(L, 2);
lua_rawget(L, table);
if (!lua_isfunction(L, -1)) { lua_pop(L, 1); return false; }
lua_pushvalue(L, 1);
lua_pushvalue(L, 3);
pcall_checked(L, 2, 0);
return true;
}
inline int class_newindex(lua_State *L) {
if (newindex_setter(L, lua_upvalueindex(1))) return 0;
rawgetp(L, lua_upvalueindex(2), &parents_key);
if (lua_istable(L, -1)) {
const size_t n = rawlen(L, -1);
for (size_t i = 3; i <= n; i += 3) {
rawgeti_idx(L, -1, static_cast<lua_Integer>(i));
const bool got = newindex_setter(L, lua_gettop(L));
lua_pop(L, 1);
if (got) { lua_pop(L, 1); return 0; }
}
}
lua_pop(L, 1);
const char *key = lua_type(L, 2) == LUA_TSTRING ? lua_tostring(L, 2) : "?";
fail(L, std::string("no writable property '") + (key ? key : "?") + "'");
}
template<typename T, typename M> int get_property(lua_State *L) {
auto member = *static_cast<M T::**>(lua_touserdata(L, lua_upvalueindex(1)));
native_to_lua(L, check_t<T>(L, 1)->*member); return 1;
}
template<typename T, typename M> int set_property(lua_State *L) {
auto member = *static_cast<M T::**>(lua_touserdata(L, lua_upvalueindex(1)));
auto obj = check_t<T>(L, 1);
obj->*member = lua_to_native<std::decay_t<M>>(L, 2); return 0;
}
inline int dispatch(lua_State *L) {
const int n = lua_gettop(L);
lua_rawgeti(L, lua_upvalueindex(1), n);
if (!lua_isfunction(L, -1)) fail(L, std::string("no overload matches argument count ") + std::to_string(n));
lua_insert(L, 1); pcall_checked(L, n, LUA_MULTRET); return lua_gettop(L);
}
inline void push_traceback(lua_State *L) {
globals(L); rawfield(L, -1, "debug"); lua_remove(L, -2);
if (lua_istable(L, -1)) { rawfield(L, -1, "traceback"); lua_remove(L, -2); }
if (!lua_isfunction(L, -1)) { lua_pop(L, 1); lua_pushnil(L); }
}
template<typename Rets, typename Args> struct call_context {
const std::vector<std::string> *tables;
const char *name;
Rets &rets;
const Args &args;
};
template<typename Tuple, size_t... I> auto read_results([[maybe_unused]] lua_State *L, std::index_sequence<I...>) {
return std::tuple<std::decay_t<std::tuple_element_t<I, Tuple>>...>{
[&] {
conv_scope scope(L, "return", static_cast<int>(I) + 1);
return lua_to_native<std::decay_t<std::tuple_element_t<I, Tuple>>>(
L, -static_cast<int>(sizeof...(I)) + static_cast<int>(I));
}()...};
}
template<typename Rets, typename Args> int perform_call(lua_State *L) {
auto &ctx = *static_cast<call_context<Rets, Args> *>(lua_touserdata(L, 1));
lua_settop(L, 0);
ensure_stack(L, static_cast<int>(std::tuple_size_v<Args> + std::tuple_size_v<Rets> + 8));
push_traceback(L); const int handler = lua_isfunction(L, 1) ? 1 : 0;
globals(L);
if (ctx.tables) {
if (ctx.tables->empty()) fail(L, "no tables");
std::string path;
for (const auto &name : *ctx.tables) {
rawfield(L, -1, name.c_str()); lua_remove(L, -2);
if (!path.empty()) path += '.';
path += name;
if (!lua_istable(L, -1)) fail(L, "no table '" + name + "' in path '" + path + "'");
}
}
rawfield(L, -1, ctx.name); lua_remove(L, -2);
if (!lua_isfunction(L, -1)) {
std::string who = ctx.name ? ctx.name : "";
if (ctx.tables && !ctx.tables->empty()) {
std::string path;
for (const auto &t : *ctx.tables) {
if (!path.empty()) path += '.';
path += t;
}
fail(L, "no function '" + path + "." + who + "'");
}
fail(L, "no function '" + who + "'");
}
std::apply([&](const auto &...a) { (native_to_lua(L, a), ...); }, ctx.args);
// Only trivial locals cross Lua's error longjmp. Argument / result validation
// uses C++ exceptions, not luaL_check*; user function errors stay in lua_pcall.
if (lua_pcall(L, static_cast<int>(std::tuple_size_v<Args>), static_cast<int>(std::tuple_size_v<Rets>), handler))
raise_lua_error(L);
auto values = read_results<Rets>(L, std::make_index_sequence<std::tuple_size_v<Rets>>{});
ctx.rets = std::move(values);
return 0;
}
template<typename... R, typename... A>
std::optional<std::string> call(lua_State *L, const std::vector<std::string> *tables, const char *name,
std::tuple<R &...> &rets, const A &...args) {
stack_guard guard(L);
auto tuple = std::tie(args...);
call_context<std::tuple<R &...>, decltype(tuple)> ctx{tables, name, rets, tuple};
lua_pushcfunction(L, (boundary<perform_call<std::tuple<R &...>, decltype(tuple)>>));
lua_pushlightuserdata(L, &ctx);
if (lua_pcall(L, 1, 0, 0)) {
if (lua_type(L, -1) == LUA_TSTRING) return std::string(lua_tostring(L, -1));
return std::string("Lua error (non-string error object)");
}
return std::nullopt;
}
}
template<typename T> void reg_class(lua_State *L, const char *name = nullptr,
ownership own = ownership::owning, void (*deleter)(T *) = nullptr) {
internal::stack_guard guard(L);
auto &meta = internal::state<T>(L);
std::string wanted = name ? name : meta.name;
if (wanted.empty()) internal::fail(L, "empty type name");
auto del = deleter ? deleter : &internal::delete_object<T>;
if (meta.registered) {
if (meta.name != wanted || meta.own != own || meta.deleter != del)
internal::fail(L, "conflicting class registration");
return;
}
lua_pushstring(L, wanted.c_str()); lua_rawget(L, LUA_REGISTRYINDEX);
if (!lua_isnil(L, -1)) internal::fail(L, "type name already registered");
lua_pop(L, 1);
lua_newtable(L);
lua_pushcfunction(L, (internal::boundary<internal::free_class<T>>)); lua_setfield(L, -2, "__gc");
lua_pushlstring(L, wanted.data(), wanted.size()); lua_setfield(L, -2, "__name");
lua_newtable(L); internal::rawsetp(L, LUA_REGISTRYINDEX, &internal::getters_key<T>);
lua_newtable(L); internal::rawsetp(L, LUA_REGISTRYINDEX, &internal::setters_key<T>);
lua_newtable(L); internal::rawsetp(L, -2, &internal::parents_key);
lua_pushvalue(L, -1);
internal::rawgetp(L, LUA_REGISTRYINDEX, &internal::getters_key<T>);
lua_pushcclosure(L, internal::boundary<internal::class_index>, 2);
lua_setfield(L, -2, "__index");
internal::rawgetp(L, LUA_REGISTRYINDEX, &internal::setters_key<T>);
lua_pushvalue(L, -2);
lua_pushcclosure(L, internal::boundary<internal::class_newindex>, 2);
lua_setfield(L, -2, "__newindex");
lua_pushvalue(L, -1); internal::rawsetp(L, LUA_REGISTRYINDEX, &internal::table_key<T>);
lua_pushstring(L, wanted.c_str()); lua_pushvalue(L, -2); lua_rawset(L, LUA_REGISTRYINDEX);
meta.name = std::move(wanted); meta.own = own; meta.deleter = del; meta.registered = true;
}
template<typename T> void reg_class_non_owning(lua_State *L, const char *name = nullptr) {
reg_class<T>(L, name, ownership::non_owning);
}
template<typename T> void reg_class_with_deleter(lua_State *L, void (*deleter)(T *), const char *name = nullptr) {
reg_class<T>(L, name, ownership::owning, deleter);
}
// Names are state-local and copied. Changing a live registration is forbidden.
template<typename T> void set_type_name(lua_State *L, const char *name) {
if (!name || !*name) internal::fail(L, "empty type name");
auto &meta = internal::state<T>(L);
if (meta.registered && meta.name != name) internal::fail(L, "cannot rename registered class");
meta.name = name;
}
template<typename T> const char *get_type_name(lua_State *L) { return internal::state<T>(L).name.c_str(); }
template<typename Derived, typename Base> void reg_base(lua_State *L) {
static_assert(std::is_base_of_v<Base, Derived> && !std::is_same_v<Base, Derived>,
"reg_base requires a unique public base");
if (!internal::state<Derived>(L).registered || !internal::state<Base>(L).registered)
internal::fail(L, "register both classes before reg_base");
auto to_base = +[](void *p) -> void * {
return static_cast<Base *>(static_cast<Derived *>(p));
};
auto &base = internal::state<Base>(L);
for (const auto &u : base.upcasts) {
if (u.derived_table == &internal::table_key<Derived>) return;
}
base.upcasts.push_back({&internal::table_key<Derived>, &internal::type_key<Derived>, to_base});
internal::state<Derived>(L).bases.push_back({to_base});
internal::stack_guard guard(L);
internal::push_metatable<Derived>(L);
internal::rawgetp(L, -1, &internal::parents_key);
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
internal::rawsetp(L, -3, &internal::parents_key);
}
auto already = [&]() {
const size_t n = internal::rawlen(L, -1);
for (size_t i = 1; i <= n; i += 3) {
internal::rawgeti_idx(L, -1, static_cast<lua_Integer>(i));
internal::rawgetp(L, LUA_REGISTRYINDEX, &internal::table_key<Base>);
const bool match = lua_rawequal(L, -1, -2);
lua_pop(L, 2);
if (match) return true;
}
return false;
};
if (!already()) {
auto append = [&]() {
const int parents = lua_gettop(L);
auto push_entry = [&](auto /*tag*/, auto table, auto getters, auto setters) {
(void)table;
internal::rawgetp(L, LUA_REGISTRYINDEX, table);
lua_rawseti(L, parents, static_cast<int>(internal::rawlen(L, parents) + 1));
internal::rawgetp(L, LUA_REGISTRYINDEX, getters);
lua_rawseti(L, parents, static_cast<int>(internal::rawlen(L, parents) + 1));
internal::rawgetp(L, LUA_REGISTRYINDEX, setters);
lua_rawseti(L, parents, static_cast<int>(internal::rawlen(L, parents) + 1));
};
push_entry(0, &internal::table_key<Base>, &internal::getters_key<Base>, &internal::setters_key<Base>);
internal::rawgetp(L, LUA_REGISTRYINDEX, &internal::table_key<Base>);
internal::rawgetp(L, -1, &internal::parents_key);
if (lua_istable(L, -1)) {
const size_t n = internal::rawlen(L, -1);
for (size_t i = 1; i <= n; ++i) {
internal::rawgeti_idx(L, -1, static_cast<lua_Integer>(i));
lua_rawseti(L, parents, static_cast<int>(internal::rawlen(L, parents) + 1));
}
}
lua_pop(L, 2);
};