-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathLuaBridge.h
More file actions
10948 lines (8552 loc) · 274 KB
/
LuaBridge.h
File metadata and controls
10948 lines (8552 loc) · 274 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
// https://github.com/kunitoki/LuaBridge3
// Copyright 2026, kunitoki
// SPDX-License-Identifier: MIT
// clang-format off
#pragma once
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
// Begin File: Source/LuaBridge/detail/Config.h
#if !(__cplusplus >= 201703L || (defined(_MSC_VER) && _HAS_CXX17))
#error LuaBridge 3 requires a compliant C++17 compiler, or C++17 has not been enabled !
#endif
#if !defined(LUABRIDGE_HAS_EXCEPTIONS)
#if defined(_MSC_VER)
#if _CPPUNWIND || _HAS_EXCEPTIONS
#define LUABRIDGE_HAS_EXCEPTIONS 1
#else
#define LUABRIDGE_HAS_EXCEPTIONS 0
#endif
#elif defined(__clang__)
#if __EXCEPTIONS && __has_feature(cxx_exceptions)
#define LUABRIDGE_HAS_EXCEPTIONS 1
#else
#define LUABRIDGE_HAS_EXCEPTIONS 0
#endif
#elif defined(__GNUC__)
#if defined(__cpp_exceptions) || defined(__EXCEPTIONS)
#define LUABRIDGE_HAS_EXCEPTIONS 1
#else
#define LUABRIDGE_HAS_EXCEPTIONS 0
#endif
#endif
#endif
#if LUABRIDGE_HAS_EXCEPTIONS
#define LUABRIDGE_IF_EXCEPTIONS(...) __VA_ARGS__
#define LUABRIDGE_IF_NO_EXCEPTIONS(...)
#else
#define LUABRIDGE_IF_EXCEPTIONS(...)
#define LUABRIDGE_IF_NO_EXCEPTIONS(...) __VA_ARGS__
#endif
#if defined(LUAU_FASTMATH_BEGIN)
#define LUABRIDGE_ON_LUAU 1
#elif defined(LUAJIT_VERSION)
#define LUABRIDGE_ON_LUAJIT 1
#elif defined(RAVI_OPTION_STRING2)
#define LUABRIDGE_ON_RAVI 1
#elif defined(LUA_VERSION_NUM)
#define LUABRIDGE_ON_LUA 1
#else
#error "Lua headers must be included prior to LuaBridge ones"
#endif
#if defined(__OBJC__)
#define LUABRIDGE_ON_OBJECTIVE_C 1
#endif
#if !defined(LUABRIDGE_SAFE_STACK_CHECKS)
#define LUABRIDGE_SAFE_STACK_CHECKS 1
#endif
#if !defined(LUABRIDGE_SAFE_LUA_C_EXCEPTION_HANDLING)
#define LUABRIDGE_SAFE_LUA_C_EXCEPTION_HANDLING 0
#endif
#if !defined(LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE)
#if LUABRIDGE_HAS_EXCEPTIONS
#define LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE 1
#else
#define LUABRIDGE_RAISE_UNREGISTERED_CLASS_USAGE 0
#endif
#endif
#if !defined(LUABRIDGE_ASSERT)
#if defined(NDEBUG) && !defined(LUABRIDGE_FORCE_ASSERT_RELEASE)
#define LUABRIDGE_ASSERT(expr) ((void)(expr))
#else
#define LUABRIDGE_ASSERT(expr) assert(expr)
#endif
#endif
// End File: Source/LuaBridge/detail/Config.h
// Begin File: Source/LuaBridge/detail/LuaHelpers.h
namespace luabridge {
template <class... Args>
constexpr void unused(Args&&...)
{
}
#if LUA_VERSION_NUM < 502
using lua_Unsigned = std::make_unsigned_t<lua_Integer>;
#if ! LUABRIDGE_ON_LUAU
inline int lua_absindex(lua_State* L, int idx)
{
if (idx > LUA_REGISTRYINDEX && idx < 0)
return lua_gettop(L) + idx + 1;
else
return idx;
}
#endif
#define LUA_OPEQ 1
#define LUA_OPLT 2
#define LUA_OPLE 3
inline int lua_compare(lua_State* L, int idx1, int idx2, int op)
{
switch (op)
{
case LUA_OPEQ:
return lua_equal(L, idx1, idx2);
case LUA_OPLT:
return lua_lessthan(L, idx1, idx2);
case LUA_OPLE:
return lua_equal(L, idx1, idx2) || lua_lessthan(L, idx1, idx2);
default:
return 0;
}
}
#if ! LUABRIDGE_ON_LUAJIT
inline void* luaL_testudata(lua_State* L, int ud, const char* tname)
{
void* p = lua_touserdata(L, ud);
if (p == nullptr)
return nullptr;
if (! lua_getmetatable(L, ud))
return nullptr;
luaL_getmetatable(L, tname);
if (! lua_rawequal(L, -1, -2))
p = nullptr;
lua_pop(L, 2);
return p;
}
#endif
inline int get_length(lua_State* L, int idx)
{
return static_cast<int>(lua_objlen(L, idx));
}
#else
inline int get_length(lua_State* L, int idx)
{
lua_len(L, idx);
const int len = static_cast<int>(luaL_checknumber(L, -1));
lua_pop(L, 1);
return len;
}
#endif
#if LUABRIDGE_ON_LUAU
inline int luaL_ref(lua_State* L, int idx)
{
LUABRIDGE_ASSERT(idx == LUA_REGISTRYINDEX);
const int ref = lua_ref(L, -1);
lua_pop(L, 1);
return ref;
}
inline void luaL_unref(lua_State* L, int idx, int ref)
{
unused(idx);
lua_unref(L, ref);
}
template <class T>
inline void* lua_newuserdata_x(lua_State* L, size_t sz)
{
return lua_newuserdatadtor(L, sz, [](void* x)
{
T* object = static_cast<T*>(x);
object->~T();
});
}
inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn, const char* debugname)
{
lua_pushcfunction(L, fn, debugname);
}
inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, const char* debugname, int n)
{
lua_pushcclosure(L, fn, debugname, n);
}
inline int lua_error_x(lua_State* L)
{
lua_error(L);
return 0;
}
inline int lua_getstack_x(lua_State* L, int level, lua_Debug* ar)
{
return lua_getinfo(L, level, "nlS", ar);
}
inline int lua_getstack_info_x(lua_State* L, int level, const char* what, lua_Debug* ar)
{
return lua_getinfo(L, level, what, ar);
}
inline int lua_rawgetp_x(lua_State* L, int idx, void* p)
{
return lua_rawgetp(L, idx, p);
}
inline void lua_rawsetp_x(lua_State* L, int idx, void* p)
{
lua_rawsetp(L, idx, p);
}
#else
using ::luaL_ref;
using ::luaL_unref;
template <class T>
inline void* lua_newuserdata_x(lua_State* L, size_t sz)
{
return lua_newuserdata(L, sz);
}
inline void lua_pushcfunction_x(lua_State *L, lua_CFunction fn, const char* debugname)
{
unused(debugname);
lua_pushcfunction(L, fn);
}
inline void lua_pushcclosure_x(lua_State* L, lua_CFunction fn, const char* debugname, int n)
{
unused(debugname);
lua_pushcclosure(L, fn, n);
}
inline int lua_error_x(lua_State* L)
{
return lua_error(L);
}
inline int lua_getstack_x(lua_State* L, int level, lua_Debug* ar)
{
return lua_getstack(L, level, ar);
}
inline int lua_getstack_info_x(lua_State* L, int level, const char* what, lua_Debug* ar)
{
lua_getstack(L, level, ar);
return lua_getinfo(L, what, ar);
}
inline int lua_rawgetp_x(lua_State* L, int idx, void* p)
{
#if LUA_VERSION_NUM < 503
idx = lua_absindex(L, idx);
luaL_checkstack(L, 1, "not enough stack slots");
lua_pushlightuserdata(L, p);
lua_rawget(L, idx);
return lua_type(L, -1);
#else
return lua_rawgetp(L, idx, p);
#endif
}
inline void lua_rawsetp_x(lua_State* L, int idx, void* p)
{
#if LUA_VERSION_NUM < 503
idx = lua_absindex(L, idx);
luaL_checkstack(L, 1, "not enough stack slots");
lua_pushlightuserdata(L, p);
lua_insert(L, -2);
lua_rawset(L, idx);
#else
lua_rawsetp(L, idx, p);
#endif
}
#endif
#if LUA_VERSION_NUM < 505
inline lua_State* lua_newstate_x(lua_Alloc f, void* ud, [[maybe_unused]] unsigned seed)
{
return lua_newstate(f, ud);
}
#else
inline lua_State* lua_newstate_x(lua_Alloc f, void* ud, unsigned seed)
{
return lua_newstate(f, ud, seed);
}
#endif
#if LUA_VERSION_NUM < 503
inline lua_Number to_numberx(lua_State* L, int idx, int* isnum)
{
lua_Number n = lua_tonumber(L, idx);
if (isnum)
*isnum = (n != 0 || lua_isnumber(L, idx));
return n;
}
inline lua_Integer to_integerx(lua_State* L, int idx, int* isnum)
{
int ok = 0;
lua_Number n = to_numberx(L, idx, &ok);
if (ok)
{
if (n < static_cast<lua_Number>(std::numeric_limits<lua_Integer>::min()) ||
n > static_cast<lua_Number>(std::numeric_limits<lua_Integer>::max()))
{
if (isnum)
*isnum = 0;
return 0;
}
const auto int_n = static_cast<lua_Integer>(n);
if (n == static_cast<lua_Number>(int_n))
{
if (isnum)
*isnum = 1;
return int_n;
}
}
if (isnum)
*isnum = 0;
return 0;
}
#endif
inline int lua_rawgetp_x(lua_State* L, int idx, const void* p)
{
return lua_rawgetp_x(L, idx, const_cast<void*>(p));
}
inline void lua_rawsetp_x(lua_State* L, int idx, const void* p)
{
lua_rawsetp_x(L, idx, const_cast<void*>(p));
}
#ifndef LUA_OK
#define LUABRIDGE_LUA_OK 0
#else
#define LUABRIDGE_LUA_OK LUA_OK
#endif
template <class T, class ErrorType>
std::error_code throw_or_error_code(ErrorType error)
{
#if LUABRIDGE_HAS_EXCEPTIONS
throw T(makeErrorCode(error).message().c_str());
#else
return makeErrorCode(error);
#endif
}
template <class T, class ErrorType>
std::error_code throw_or_error_code(lua_State* L, ErrorType error)
{
#if LUABRIDGE_HAS_EXCEPTIONS
throw T(L, makeErrorCode(error));
#else
return unused(L), makeErrorCode(error);
#endif
}
template <class T, class... Args>
void throw_or_assert(Args&&... args)
{
#if LUABRIDGE_HAS_EXCEPTIONS
throw T(std::forward<Args>(args)...);
#else
unused(std::forward<Args>(args)...);
LUABRIDGE_ASSERT(false);
#endif
}
template <class T>
void pushunsigned(lua_State* L, T value)
{
static_assert(std::is_unsigned_v<T>);
lua_pushinteger(L, static_cast<lua_Integer>(value));
}
inline lua_Number tonumber(lua_State* L, int idx, int* isnum)
{
#if ! LUABRIDGE_ON_LUAU && LUA_VERSION_NUM > 502
return lua_tonumberx(L, idx, isnum);
#else
return to_numberx(L, idx, isnum);
#endif
}
inline lua_Integer tointeger(lua_State* L, int idx, int* isnum)
{
#if ! LUABRIDGE_ON_LUAU && LUA_VERSION_NUM > 502
return lua_tointegerx(L, idx, isnum);
#else
return to_integerx(L, idx, isnum);
#endif
}
inline constexpr char main_thread_name[] = "__luabridge_main_thread";
inline void register_main_thread(lua_State* threadL)
{
#if LUA_VERSION_NUM < 502
if (threadL == nullptr)
lua_pushnil(threadL);
else
lua_pushthread(threadL);
lua_setglobal(threadL, main_thread_name);
#else
unused(threadL);
#endif
}
inline lua_State* main_thread(lua_State* threadL)
{
#if LUA_VERSION_NUM < 502
lua_getglobal(threadL, main_thread_name);
if (lua_isthread(threadL, -1))
{
auto L = lua_tothread(threadL, -1);
lua_pop(threadL, 1);
return L;
}
LUABRIDGE_ASSERT(false);
lua_pop(threadL, 1);
return threadL;
#else
lua_rawgeti(threadL, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
lua_State* L = lua_tothread(threadL, -1);
lua_pop(threadL, 1);
return L;
#endif
}
inline int rawgetfield(lua_State* L, int index, const char* key)
{
LUABRIDGE_ASSERT(lua_istable(L, index));
index = lua_absindex(L, index);
lua_pushstring(L, key);
#if LUA_VERSION_NUM <= 502
lua_rawget(L, index);
return lua_type(L, -1);
#else
return lua_rawget(L, index);
#endif
}
inline void rawsetfield(lua_State* L, int index, const char* key)
{
LUABRIDGE_ASSERT(lua_istable(L, index));
index = lua_absindex(L, index);
lua_pushstring(L, key);
lua_insert(L, -2);
lua_rawset(L, index);
}
[[nodiscard]] inline bool isfulluserdata(lua_State* L, int index)
{
return lua_isuserdata(L, index) && !lua_islightuserdata(L, index);
}
[[nodiscard]] inline bool equalstates(lua_State* L1, lua_State* L2)
{
return lua_topointer(L1, LUA_REGISTRYINDEX) == lua_topointer(L2, LUA_REGISTRYINDEX);
}
[[nodiscard]] inline int table_length(lua_State* L, int index)
{
LUABRIDGE_ASSERT(lua_istable(L, index));
int items_count = 0;
lua_pushnil(L);
while (lua_next(L, index) != 0)
{
++items_count;
lua_pop(L, 1);
}
return items_count;
}
template <class T>
[[nodiscard]] T* align(void* ptr) noexcept
{
const auto address = reinterpret_cast<size_t>(ptr);
const auto offset = address % alignof(T);
const auto aligned_address = (offset == 0) ? address : (address + alignof(T) - offset);
return reinterpret_cast<T*>(aligned_address);
}
template <std::size_t Alignment, class T, std::enable_if_t<std::is_pointer_v<T>, int> = 0>
[[nodiscard]] bool is_aligned(T address) noexcept
{
static_assert(Alignment > 0u);
return (reinterpret_cast<std::uintptr_t>(address) & (Alignment - 1u)) == 0u;
}
template <class T>
[[nodiscard]] constexpr size_t maximum_space_needed_to_align() noexcept
{
return sizeof(T) + alignof(T) - 1;
}
template <class T>
int lua_deleteuserdata_aligned(lua_State* L)
{
LUABRIDGE_ASSERT(isfulluserdata(L, 1));
T* aligned = align<T>(lua_touserdata(L, 1));
aligned->~T();
return 0;
}
template <class T, class... Args>
void* lua_newuserdata_aligned(lua_State* L, Args&&... args)
{
using U = std::remove_reference_t<T>;
#if LUABRIDGE_ON_LUAU
void* pointer = lua_newuserdatadtor(L, maximum_space_needed_to_align<U>(), [](void* x)
{
U* aligned = align<U>(x);
aligned->~U();
});
#else
void* pointer = lua_newuserdata_x<U>(L, maximum_space_needed_to_align<U>());
lua_newtable(L);
lua_pushcfunction_x(L, &lua_deleteuserdata_aligned<U>, "");
rawsetfield(L, -2, "__gc");
lua_setmetatable(L, -2);
#endif
U* aligned = align<U>(pointer);
new (aligned) U(std::forward<Args>(args)...);
return pointer;
}
inline int raise_lua_error(lua_State* L, const char* fmt, ...)
{
va_list argp;
va_start(argp, fmt);
lua_pushvfstring(L, fmt, argp);
va_end(argp);
const char* message = lua_tostring(L, -1);
if (message != nullptr)
{
if (auto str = std::string_view(message); !str.empty() && str[0] == '[')
return lua_error_x(L);
}
bool pushed_error = false;
for (int level = 1; level <= 2; ++level)
{
lua_Debug ar;
#if LUABRIDGE_ON_LUAU
if (lua_getinfo(L, level, "sl", &ar) == 0)
continue;
#else
if (lua_getstack(L, level, &ar) == 0 || lua_getinfo(L, "Sl", &ar) == 0)
continue;
#endif
if (ar.currentline <= 0)
continue;
lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline);
pushed_error = true;
break;
}
if (! pushed_error)
lua_pushliteral(L, "");
lua_pushvalue(L, -2);
lua_remove(L, -3);
lua_concat(L, 2);
return lua_error_x(L);
}
template <class U = lua_Integer, class T>
constexpr bool is_integral_representable_by(T value)
{
constexpr bool same_signedness = (std::is_unsigned_v<T> && std::is_unsigned_v<U>)
|| (!std::is_unsigned_v<T> && !std::is_unsigned_v<U>);
if constexpr (sizeof(T) == sizeof(U))
{
if constexpr (same_signedness)
{
return true;
}
else if constexpr (std::is_unsigned_v<T>)
{
return value <= static_cast<T>((std::numeric_limits<U>::max)());
}
else
{
return value >= static_cast<T>((std::numeric_limits<U>::min)())
&& static_cast<U>(value) <= (std::numeric_limits<U>::max)();
}
}
else if constexpr (sizeof(T) < sizeof(U))
{
return static_cast<U>(value) >= (std::numeric_limits<U>::min)()
&& static_cast<U>(value) <= (std::numeric_limits<U>::max)();
}
else if constexpr (std::is_unsigned_v<T>)
{
return value <= static_cast<T>((std::numeric_limits<U>::max)());
}
else
{
return value >= static_cast<T>((std::numeric_limits<U>::min)())
&& value <= static_cast<T>((std::numeric_limits<U>::max)());
}
}
template <class U = lua_Integer>
bool is_integral_representable_by(lua_State* L, int index)
{
int isValid = 0;
const auto value = tointeger(L, index, &isValid);
return isValid ? is_integral_representable_by<U>(value) : false;
}
template <class U = lua_Number, class T>
bool is_floating_point_representable_by(T value)
{
if constexpr (sizeof(T) == sizeof(U))
{
return true;
}
else if constexpr (sizeof(T) < sizeof(U))
{
if (std::isnan(value) || std::isinf(value))
return true;
return static_cast<U>(value) >= -(std::numeric_limits<U>::max)()
&& static_cast<U>(value) <= (std::numeric_limits<U>::max)();
}
else
{
if (std::isnan(value) || std::isinf(value))
return true;
return value >= static_cast<T>(-(std::numeric_limits<U>::max)())
&& value <= static_cast<T>((std::numeric_limits<U>::max)());
}
}
template <class U = lua_Number>
bool is_floating_point_representable_by(lua_State* L, int index)
{
int isValid = 0;
const auto value = tonumber(L, index, &isValid);
return isValid ? is_floating_point_representable_by<U>(value) : false;
}
}
// End File: Source/LuaBridge/detail/LuaHelpers.h
// Begin File: Source/LuaBridge/detail/Errors.h
namespace luabridge {
namespace detail {
static inline constexpr char error_lua_stack_overflow[] = "stack overflow";
}
enum class ErrorCode
{
ClassNotRegistered = 1,
LuaStackOverflow,
LuaFunctionCallFailed,
IntegerDoesntFitIntoLuaInteger,
FloatingPointDoesntFitIntoLuaNumber,
InvalidTypeCast,
InvalidTableSizeInCast
};
namespace detail {
struct ErrorCategory : std::error_category
{
const char* name() const noexcept override
{
return "luabridge";
}
std::string message(int ev) const override
{
return errorString(ev);
}
static const char* errorString(int ev) noexcept
{
switch (static_cast<ErrorCode>(ev))
{
case ErrorCode::ClassNotRegistered:
return "The class is not registered in LuaBridge";
case ErrorCode::LuaStackOverflow:
return "The lua stack has overflow";
case ErrorCode::LuaFunctionCallFailed:
return "The lua function invocation raised an error";
case ErrorCode::IntegerDoesntFitIntoLuaInteger:
return "The native integer can't fit inside a lua integer";
case ErrorCode::FloatingPointDoesntFitIntoLuaNumber:
return "The native floating point can't fit inside a lua number";
case ErrorCode::InvalidTypeCast:
return "The lua object can't be cast to desired type";
case ErrorCode::InvalidTableSizeInCast:
return "The lua table has different size than expected";
default:
return "Unknown error";
}
}
static const ErrorCategory& getInstance() noexcept
{
static ErrorCategory category;
return category;
}
};
}
inline std::error_code makeErrorCode(ErrorCode e)
{
return { static_cast<int>(e), detail::ErrorCategory::getInstance() };
}
inline std::error_code make_error_code(ErrorCode e)
{
return { static_cast<int>(e), detail::ErrorCategory::getInstance() };
}
}
namespace std {
template <> struct is_error_code_enum<luabridge::ErrorCode> : true_type {};
}
// End File: Source/LuaBridge/detail/Errors.h
// Begin File: Source/LuaBridge/detail/Expected.h
#if LUABRIDGE_HAS_EXCEPTIONS
#endif
namespace luabridge {
namespace detail {
using std::swap;
template <class T, class... Args>
T* construct_at(T* ptr, Args&&... args) noexcept(std::is_nothrow_constructible<T, Args...>::value)
{
return static_cast<T*>(::new (const_cast<void*>(static_cast<const void*>(ptr))) T(std::forward<Args>(args)...));
}
template <class T, class U, class = void>
struct is_swappable_with_impl : std::false_type
{
};
template <class T, class U>
struct is_swappable_with_impl<T, U, std::void_t<decltype(swap(std::declval<T>(), std::declval<U>()))>>
: std::true_type
{
};
template <class T, class U>
struct is_nothrow_swappable_with_impl
{
static constexpr bool value = noexcept(swap(std::declval<T>(), std::declval<U>())) && noexcept(swap(std::declval<U>(), std::declval<T>()));
using type = std::bool_constant<value>;
};
template <class T, class U>
struct is_swappable_with
: std::conjunction<
is_swappable_with_impl<std::add_lvalue_reference_t<T>, std::add_lvalue_reference_t<U>>,
is_swappable_with_impl<std::add_lvalue_reference_t<U>, std::add_lvalue_reference_t<T>>>::type
{
};
template <class T, class U>
struct is_nothrow_swappable_with
: std::conjunction<is_swappable_with<T, U>, is_nothrow_swappable_with_impl<T, U>>::type
{
};
template <class T>
struct is_nothrow_swappable
: std::is_nothrow_swappable_with<std::add_lvalue_reference_t<T>, std::add_lvalue_reference_t<T>>
{
};
template <class T, class = void>
struct has_member_message : std::false_type
{
};
template <class T>
struct has_member_message<T, std::void_t<decltype(std::declval<T>().message())>> : std::true_type
{
};
template <class T>
inline static constexpr bool has_member_message_v = has_member_message<T>::value;
}
template <class T, class E>
class Expected;
struct UnexpectType
{
constexpr UnexpectType() = default;
};
static constexpr auto unexpect = UnexpectType();
namespace detail {
template <class T, class E, bool = std::is_default_constructible_v<T>, bool = (std::is_void_v<T> || std::is_trivial_v<T>) && std::is_trivial_v<E>>
union expected_storage
{
public:
template <class U = T, class = std::enable_if_t<std::is_default_constructible_v<U>>>
constexpr expected_storage() noexcept
: value_()
{
}
template <class... Args>
constexpr explicit expected_storage(std::in_place_t, Args&&... args) noexcept
: value_(std::forward<Args>(args)...)
{
}
template <class... Args>
constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept
: error_(std::forward<Args>(args)...)
{
}
~expected_storage() = default;
constexpr const T& value() const noexcept
{
return value_;
}
constexpr T& value() noexcept
{
return value_;
}
constexpr const E& error() const noexcept
{
return error_;
}
constexpr E& error() noexcept
{
return error_;
}
private:
T value_;
E error_;
};
template <class E>
union expected_storage<void, E, true, true>
{
public:
constexpr expected_storage() noexcept
: dummy_(0)
{
}
template <class... Args>
constexpr explicit expected_storage(UnexpectType, Args&&... args) noexcept
: error_(std::forward<Args>(args)...)
{
}
~expected_storage() = default;
constexpr const E& error() const noexcept
{
return error_;
}
constexpr E& error() noexcept
{
return error_;
}
private:
char dummy_;
E error_;
};
template <class T, class E>
union expected_storage<T, E, true, false>
{
public:
constexpr expected_storage() noexcept(std::is_nothrow_default_constructible_v<T>)
: value_()