stack_push.hpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_STACK_PUSH_HPP
  19. #define SOL_STACK_PUSH_HPP
  20. #include <sol/stack_core.hpp>
  21. #include <sol/raii.hpp>
  22. #include <sol/optional.hpp>
  23. #include <sol/usertype_traits.hpp>
  24. #include <sol/policies.hpp>
  25. #include <sol/unicode.hpp>
  26. #include <sol/assert.hpp>
  27. #include <memory>
  28. #include <type_traits>
  29. #include <cassert>
  30. #include <limits>
  31. #include <cmath>
  32. #include <string_view>
  33. #if SOL_IS_ON(SOL_STD_VARIANT)
  34. #include <variant>
  35. #endif // Can use variant
  36. #include <sol/debug.hpp>
  37. namespace sol { namespace stack {
  38. namespace stack_detail {
  39. template <typename T>
  40. inline bool integer_value_fits(const T& value) {
  41. // We check if we can rely on casts or a lack of padding bits to satisfy
  42. // the requirements here
  43. // If it lacks padding bits, we can jump back and forth between lua_Integer and whatever type without
  44. // loss of information
  45. constexpr bool is_same_signedness
  46. = (std::is_signed_v<T> && std::is_signed_v<lua_Integer>) || (std::is_unsigned_v<T> && std::is_unsigned_v<lua_Integer>);
  47. constexpr bool probaby_fits_within_lua_Integer = sizeof(T) == sizeof(lua_Integer)
  48. #if SOL_IS_ON(SOL_ALL_INTEGER_VALUES_FIT)
  49. && ((std::has_unique_object_representations_v<T> && std::has_unique_object_representations_v<lua_Integer>) ? true : is_same_signedness)
  50. #else
  51. && is_same_signedness
  52. #endif
  53. ;
  54. if constexpr (sizeof(T) < sizeof(lua_Integer) || probaby_fits_within_lua_Integer) {
  55. (void)value;
  56. return true;
  57. }
  58. else {
  59. auto u_min = static_cast<std::intmax_t>((std::numeric_limits<lua_Integer>::min)());
  60. auto u_max = static_cast<std::uintmax_t>((std::numeric_limits<lua_Integer>::max)());
  61. auto t_min = static_cast<std::intmax_t>((std::numeric_limits<T>::min)());
  62. auto t_max = static_cast<std::uintmax_t>((std::numeric_limits<T>::max)());
  63. return (u_min <= t_min || value >= static_cast<T>(u_min)) && (u_max >= t_max || value <= static_cast<T>(u_max));
  64. }
  65. }
  66. template <typename T>
  67. int msvc_is_ass_with_if_constexpr_push_enum(std::true_type, lua_State* L, const T& value) {
  68. if constexpr (meta::any_same_v<std::underlying_type_t<T>,
  69. char
  70. #if SOL_IS_ON(SOL_CHAR8_T)
  71. ,
  72. char8_t
  73. #endif
  74. ,
  75. char16_t,
  76. char32_t>) {
  77. if constexpr (std::is_signed_v<T>) {
  78. return stack::push(L, static_cast<std::int_least32_t>(value));
  79. }
  80. else {
  81. return stack::push(L, static_cast<std::uint_least32_t>(value));
  82. }
  83. }
  84. else {
  85. return stack::push(L, static_cast<std::underlying_type_t<T>>(value));
  86. }
  87. }
  88. template <typename T>
  89. int msvc_is_ass_with_if_constexpr_push_enum(std::false_type, lua_State*, const T&) {
  90. return 0;
  91. }
  92. } // namespace stack_detail
  93. inline int push_environment_of(lua_State* L, int target_index = -1) {
  94. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  95. luaL_checkstack(L, 1, detail::not_enough_stack_space_environment);
  96. #endif // make sure stack doesn't overflow
  97. #if SOL_LUA_VERSION_I_ < 502
  98. // Use lua_getfenv
  99. lua_getfenv(L, target_index);
  100. #else
  101. if (lua_iscfunction(L, target_index) != 0) {
  102. const char* maybe_upvalue_name = lua_getupvalue(L, target_index, 1);
  103. if (maybe_upvalue_name != nullptr) {
  104. // it worked, take this one
  105. return 1;
  106. }
  107. }
  108. // Nominally, we search for the `"_ENV"` value.
  109. // If we don't find it.... uh, well. We've got a problem?
  110. for (int upvalue_index = 1;; ++upvalue_index) {
  111. const char* maybe_upvalue_name = lua_getupvalue(L, target_index, upvalue_index);
  112. if (maybe_upvalue_name == nullptr) {
  113. push(L, lua_nil);
  114. break;
  115. }
  116. string_view upvalue_name(maybe_upvalue_name);
  117. if (upvalue_name == "_ENV") {
  118. // Keep this one!
  119. break;
  120. }
  121. // Discard what we received, loop back around
  122. lua_pop(L, 1);
  123. }
  124. #endif
  125. return 1;
  126. }
  127. template <typename T>
  128. int push_environment_of(const T& target) {
  129. lua_State* target_L = target.lua_state();
  130. int target_index = absolute_index(target_L, -target.push());
  131. int env_count = push_environment_of(target_L, target_index);
  132. SOL_ASSERT(env_count == 1);
  133. lua_rotate(target_L, target_index, 1);
  134. lua_pop(target_L, 1);
  135. return env_count;
  136. }
  137. template <typename T>
  138. struct unqualified_pusher<detail::as_value_tag<T>> {
  139. template <typename F, typename... Args>
  140. static int push_fx(lua_State* L, F&& f, Args&&... args) {
  141. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  142. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  143. #endif // make sure stack doesn't overflow
  144. // Basically, we store all user-data like this:
  145. // If it's a movable/copyable value (no std::ref(x)), then we store the pointer to the new
  146. // data in the first sizeof(T*) bytes, and then however many bytes it takes to
  147. // do the actual object. Things that are std::ref or plain T* are stored as
  148. // just the sizeof(T*), and nothing else.
  149. T* obj = detail::usertype_allocate<T>(L);
  150. f();
  151. std::allocator<T> alloc {};
  152. std::allocator_traits<std::allocator<T>>::construct(alloc, obj, std::forward<Args>(args)...);
  153. return 1;
  154. }
  155. template <typename K, typename... Args>
  156. static int push_keyed(lua_State* L, K&& k, Args&&... args) {
  157. stack_detail::undefined_metatable fx(L, &k[0], &stack::stack_detail::set_undefined_methods_on<T>);
  158. return push_fx(L, fx, std::forward<Args>(args)...);
  159. }
  160. template <typename Arg, typename... Args>
  161. static int push(lua_State* L, Arg&& arg, Args&&... args) {
  162. if constexpr (std::is_same_v<meta::unqualified_t<Arg>, detail::with_function_tag>) {
  163. (void)arg;
  164. return push_fx(L, std::forward<Args>(args)...);
  165. }
  166. else {
  167. return push_keyed(L, usertype_traits<T>::metatable(), std::forward<Arg>(arg), std::forward<Args>(args)...);
  168. }
  169. }
  170. static int push(lua_State* L) {
  171. return push_keyed(L, usertype_traits<T>::metatable());
  172. }
  173. };
  174. template <typename T>
  175. struct unqualified_pusher<detail::as_pointer_tag<T>> {
  176. typedef meta::unqualified_t<T> U;
  177. template <typename F>
  178. static int push_fx(lua_State* L, F&& f, T* obj) {
  179. if (obj == nullptr)
  180. return stack::push(L, lua_nil);
  181. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  182. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  183. #endif // make sure stack doesn't overflow
  184. T** pref = detail::usertype_allocate_pointer<T>(L);
  185. f();
  186. *pref = obj;
  187. return 1;
  188. }
  189. template <typename K>
  190. static int push_keyed(lua_State* L, K&& k, T* obj) {
  191. stack_detail::undefined_metatable fx(L, &k[0], &stack::stack_detail::set_undefined_methods_on<U*>);
  192. return push_fx(L, fx, obj);
  193. }
  194. template <typename Arg, typename... Args>
  195. static int push(lua_State* L, Arg&& arg, Args&&... args) {
  196. if constexpr (std::is_same_v<meta::unqualified_t<Arg>, detail::with_function_tag>) {
  197. (void)arg;
  198. return push_fx(L, std::forward<Args>(args)...);
  199. }
  200. else {
  201. return push_keyed(L, usertype_traits<U*>::metatable(), std::forward<Arg>(arg), std::forward<Args>(args)...);
  202. }
  203. }
  204. };
  205. template <>
  206. struct unqualified_pusher<detail::as_reference_tag> {
  207. template <typename T>
  208. static int push(lua_State* L, T&& obj) {
  209. return stack::push(L, detail::ptr(obj));
  210. }
  211. };
  212. namespace stack_detail {
  213. template <typename T>
  214. struct uu_pusher {
  215. using element = unique_usertype_element_t<T>;
  216. using actual = unique_usertype_actual_t<T>;
  217. template <typename Arg, typename... Args>
  218. static int push(lua_State* L, Arg&& arg, Args&&... args) {
  219. if constexpr (std::is_base_of_v<actual, meta::unqualified_t<Arg>>) {
  220. if (detail::unique_is_null(L, arg)) {
  221. return stack::push(L, lua_nil);
  222. }
  223. return push_deep(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  224. }
  225. else {
  226. return push_deep(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  227. }
  228. }
  229. template <typename... Args>
  230. static int push_deep(lua_State* L, Args&&... args) {
  231. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  232. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  233. #endif // make sure stack doesn't overflow
  234. element** pointer_to_memory = nullptr;
  235. detail::unique_destructor* fx = nullptr;
  236. detail::unique_tag* id = nullptr;
  237. actual* typed_memory = detail::usertype_unique_allocate<element, actual>(L, pointer_to_memory, fx, id);
  238. if (luaL_newmetatable(L, &usertype_traits<d::u<std::remove_cv_t<element>>>::metatable()[0]) == 1) {
  239. detail::lua_reg_table registration_table {};
  240. int index = 0;
  241. detail::indexed_insert insert_callable(registration_table, index);
  242. detail::insert_default_registrations<element>(insert_callable, detail::property_always_true);
  243. registration_table[index] = { to_string(meta_function::garbage_collect).c_str(), detail::make_destructor<T>() };
  244. luaL_setfuncs(L, registration_table, 0);
  245. }
  246. lua_setmetatable(L, -2);
  247. *fx = detail::usertype_unique_alloc_destroy<element, actual>;
  248. *id = &detail::inheritance<element>::template type_unique_cast<actual>;
  249. detail::default_construct::construct(typed_memory, std::forward<Args>(args)...);
  250. *pointer_to_memory = detail::unique_get<T>(L, *typed_memory);
  251. return 1;
  252. }
  253. };
  254. } // namespace stack_detail
  255. template <typename T>
  256. struct unqualified_pusher<detail::as_unique_tag<T>> {
  257. template <typename... Args>
  258. static int push(lua_State* L, Args&&... args) {
  259. stack_detail::uu_pusher<T> p;
  260. (void)p;
  261. return p.push(L, std::forward<Args>(args)...);
  262. }
  263. };
  264. template <typename T, typename>
  265. struct unqualified_pusher {
  266. template <typename... Args>
  267. static int push(lua_State* L, Args&&... args) {
  268. using Tu = meta::unqualified_t<T>;
  269. if constexpr (is_lua_reference_v<Tu>) {
  270. using int_arr = int[];
  271. int_arr p { (std::forward<Args>(args).push(L))... };
  272. return p[0];
  273. }
  274. else if constexpr (std::is_same_v<Tu, bool>) {
  275. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  276. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  277. #endif // make sure stack doesn't overflow
  278. lua_pushboolean(L, std::forward<Args>(args)...);
  279. return 1;
  280. }
  281. else if constexpr (std::is_integral_v<Tu> || std::is_same_v<Tu, lua_Integer>) {
  282. const Tu& value(std::forward<Args>(args)...);
  283. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  284. luaL_checkstack(L, 1, detail::not_enough_stack_space_integral);
  285. #endif // make sure stack doesn't overflow
  286. #if SOL_LUA_VERSION_I_ >= 503
  287. if (stack_detail::integer_value_fits<Tu>(value)) {
  288. lua_pushinteger(L, static_cast<lua_Integer>(value));
  289. return 1;
  290. }
  291. #endif // Lua 5.3 and above
  292. #if SOL_IS_ON(SOL_NUMBER_PRECISION_CHECKS)
  293. if (static_cast<T>(llround(static_cast<lua_Number>(value))) != value) {
  294. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  295. // Is this really worth it?
  296. SOL_ASSERT_MSG(false, "integer value will be misrepresented in lua");
  297. lua_pushinteger(L, static_cast<lua_Integer>(value));
  298. return 1;
  299. #else
  300. throw error(detail::direct_error, "integer value will be misrepresented in lua");
  301. #endif // No Exceptions
  302. }
  303. #endif // Safe Numerics and Number Precision Check
  304. lua_pushnumber(L, static_cast<lua_Number>(value));
  305. return 1;
  306. }
  307. else if constexpr (std::is_floating_point_v<Tu> || std::is_same_v<Tu, lua_Number>) {
  308. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  309. luaL_checkstack(L, 1, detail::not_enough_stack_space_floating);
  310. #endif // make sure stack doesn't overflow
  311. lua_pushnumber(L, std::forward<Args>(args)...);
  312. return 1;
  313. }
  314. else if constexpr (std::is_same_v<Tu, luaL_Stream*>) {
  315. luaL_Stream* source { std::forward<Args>(args)... };
  316. luaL_Stream* stream = static_cast<luaL_Stream*>(detail::alloc_newuserdata(L, sizeof(luaL_Stream)));
  317. stream->f = source->f;
  318. #if SOL_IS_ON(SOL_LUAL_STREAM_USE_CLOSE_FUNCTION)
  319. stream->closef = source->closef;
  320. #endif // LuaJIT and Lua 5.1 and below do not have
  321. return 1;
  322. }
  323. else if constexpr (std::is_same_v<Tu, luaL_Stream>) {
  324. luaL_Stream& source(std::forward<Args>(args)...);
  325. luaL_Stream* stream = static_cast<luaL_Stream*>(detail::alloc_newuserdata(L, sizeof(luaL_Stream)));
  326. stream->f = source.f;
  327. #if SOL_IS_ON(SOL_LUAL_STREAM_USE_CLOSE_FUNCTION)
  328. stream->closef = source.closef;
  329. #endif // LuaJIT and Lua 5.1 and below do not have
  330. return 1;
  331. }
  332. else if constexpr (std::is_enum_v<Tu>) {
  333. return stack_detail::msvc_is_ass_with_if_constexpr_push_enum(std::true_type(), L, std::forward<Args>(args)...);
  334. }
  335. else if constexpr (std::is_pointer_v<Tu>) {
  336. return stack::push<detail::as_pointer_tag<std::remove_pointer_t<T>>>(L, std::forward<Args>(args)...);
  337. }
  338. else if constexpr (is_unique_usertype_v<Tu>) {
  339. return stack::push<detail::as_unique_tag<T>>(L, std::forward<Args>(args)...);
  340. }
  341. else {
  342. return stack::push<detail::as_value_tag<T>>(L, std::forward<Args>(args)...);
  343. }
  344. }
  345. };
  346. template <typename T>
  347. struct unqualified_pusher<std::reference_wrapper<T>> {
  348. static int push(lua_State* L, const std::reference_wrapper<T>& t) {
  349. return stack::push(L, std::addressof(detail::deref(t.get())));
  350. }
  351. };
  352. template <typename T>
  353. struct unqualified_pusher<detail::as_table_tag<T>> {
  354. using has_kvp = meta::has_key_value_pair<meta::unqualified_t<std::remove_pointer_t<T>>>;
  355. static int push(lua_State* L, const T& tablecont) {
  356. return push(has_kvp(), std::false_type(), L, tablecont);
  357. }
  358. static int push(lua_State* L, const T& tablecont, nested_tag_t) {
  359. return push(has_kvp(), std::true_type(), L, tablecont);
  360. }
  361. static int push(std::true_type, lua_State* L, const T& tablecont) {
  362. return push(has_kvp(), std::true_type(), L, tablecont);
  363. }
  364. static int push(std::false_type, lua_State* L, const T& tablecont) {
  365. return push(has_kvp(), std::false_type(), L, tablecont);
  366. }
  367. template <bool is_nested>
  368. static int push(std::true_type, std::integral_constant<bool, is_nested>, lua_State* L, const T& tablecont) {
  369. auto& cont = detail::deref(detail::unwrap(tablecont));
  370. lua_createtable(L, static_cast<int>(cont.size()), 0);
  371. int tableindex = lua_gettop(L);
  372. for (const auto& pair : cont) {
  373. if (is_nested) {
  374. set_field(L, pair.first, as_nested_ref(pair.second), tableindex);
  375. }
  376. else {
  377. set_field(L, pair.first, pair.second, tableindex);
  378. }
  379. }
  380. return 1;
  381. }
  382. template <bool is_nested>
  383. static int push(std::false_type, std::integral_constant<bool, is_nested>, lua_State* L, const T& tablecont) {
  384. auto& cont = detail::deref(detail::unwrap(tablecont));
  385. lua_createtable(L, stack_detail::get_size_hint(cont), 0);
  386. int tableindex = lua_gettop(L);
  387. std::size_t index = 1;
  388. for (const auto& i : cont) {
  389. #if SOL_LUA_VERSION_I_ >= 503
  390. int p = is_nested ? stack::push(L, as_nested_ref(i)) : stack::push(L, i);
  391. for (int pi = 0; pi < p; ++pi) {
  392. lua_seti(L, tableindex, static_cast<lua_Integer>(index++));
  393. }
  394. #else
  395. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  396. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  397. #endif // make sure stack doesn't overflow
  398. lua_pushinteger(L, static_cast<lua_Integer>(index));
  399. int p = is_nested ? stack::push(L, as_nested_ref(i)) : stack::push(L, i);
  400. if (p == 1) {
  401. ++index;
  402. lua_settable(L, tableindex);
  403. }
  404. else {
  405. int firstindex = tableindex + 1 + 1;
  406. for (int pi = 0; pi < p; ++pi) {
  407. stack::push(L, index);
  408. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  409. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  410. #endif // make sure stack doesn't overflow
  411. lua_pushvalue(L, firstindex);
  412. lua_settable(L, tableindex);
  413. ++index;
  414. ++firstindex;
  415. }
  416. lua_pop(L, 1 + p);
  417. }
  418. #endif // Lua Version 5.3 and others
  419. }
  420. // TODO: figure out a better way to do this...?
  421. // set_field(L, -1, cont.size());
  422. return 1;
  423. }
  424. };
  425. template <typename T>
  426. struct unqualified_pusher<as_table_t<T>> {
  427. static int push(lua_State* L, const as_table_t<T>& value_) {
  428. using inner_t = std::remove_pointer_t<meta::unwrap_unqualified_t<T>>;
  429. if constexpr (is_container_v<inner_t>) {
  430. return stack::push<detail::as_table_tag<T>>(L, value_.value());
  431. }
  432. else {
  433. return stack::push(L, value_.value());
  434. }
  435. }
  436. static int push(lua_State* L, const T& value_) {
  437. using inner_t = std::remove_pointer_t<meta::unwrap_unqualified_t<T>>;
  438. if constexpr (is_container_v<inner_t>) {
  439. return stack::push<detail::as_table_tag<T>>(L, value_);
  440. }
  441. else {
  442. return stack::push(L, value_);
  443. }
  444. }
  445. };
  446. template <typename T>
  447. struct unqualified_pusher<nested<T>> {
  448. static int push(lua_State* L, const T& nested_value) noexcept {
  449. using Tu = meta::unwrap_unqualified_t<T>;
  450. using inner_t = std::remove_pointer_t<Tu>;
  451. if constexpr (is_container_v<inner_t>) {
  452. return stack::push<detail::as_table_tag<T>>(L, nested_value, nested_tag);
  453. }
  454. else {
  455. return stack::push<Tu>(L, nested_value);
  456. }
  457. }
  458. static int push(lua_State* L, const nested<T>& nested_wrapper_) noexcept {
  459. using Tu = meta::unwrap_unqualified_t<T>;
  460. using inner_t = std::remove_pointer_t<Tu>;
  461. if constexpr (is_container_v<inner_t>) {
  462. return stack::push<detail::as_table_tag<T>>(L, nested_wrapper_.value(), nested_tag);
  463. }
  464. else {
  465. return stack::push<Tu>(L, nested_wrapper_.value());
  466. }
  467. }
  468. };
  469. template <typename T>
  470. struct unqualified_pusher<std::initializer_list<T>> {
  471. static int push(lua_State* L, const std::initializer_list<T>& il) noexcept {
  472. unqualified_pusher<detail::as_table_tag<std::initializer_list<T>>> p {};
  473. return p.push(L, il);
  474. }
  475. };
  476. template <>
  477. struct unqualified_pusher<lua_nil_t> {
  478. static int push(lua_State* L, lua_nil_t) noexcept {
  479. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  480. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  481. #endif // make sure stack doesn't overflow
  482. lua_pushnil(L);
  483. return 1;
  484. }
  485. };
  486. template <>
  487. struct unqualified_pusher<stack_count> {
  488. static int push(lua_State*, stack_count st) noexcept {
  489. return st.count;
  490. }
  491. };
  492. template <>
  493. struct unqualified_pusher<metatable_key_t> {
  494. static int push(lua_State* L, metatable_key_t) {
  495. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  496. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  497. #endif // make sure stack doesn't overflow
  498. lua_pushlstring(L, to_string(meta_function::metatable).c_str(), 4);
  499. return 1;
  500. }
  501. };
  502. template <>
  503. struct unqualified_pusher<std::remove_pointer_t<lua_CFunction>> {
  504. static int push(lua_State* L, lua_CFunction func, int n = 0) noexcept {
  505. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  506. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  507. #endif // make sure stack doesn't overflow
  508. lua_pushcclosure(L, func, n);
  509. return 1;
  510. }
  511. };
  512. template <>
  513. struct unqualified_pusher<lua_CFunction> {
  514. static int push(lua_State* L, lua_CFunction func, int n = 0) {
  515. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  516. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  517. #endif // make sure stack doesn't overflow
  518. lua_pushcclosure(L, func, n);
  519. return 1;
  520. }
  521. };
  522. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE)
  523. template <>
  524. struct unqualified_pusher<std::remove_pointer_t<detail::lua_CFunction_noexcept>> {
  525. static int push(lua_State* L, detail::lua_CFunction_noexcept func, int n = 0) {
  526. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  527. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  528. #endif // make sure stack doesn't overflow
  529. lua_pushcclosure(L, func, n);
  530. return 1;
  531. }
  532. };
  533. template <>
  534. struct unqualified_pusher<detail::lua_CFunction_noexcept> {
  535. static int push(lua_State* L, detail::lua_CFunction_noexcept func, int n = 0) {
  536. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  537. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  538. #endif // make sure stack doesn't overflow
  539. lua_pushcclosure(L, func, n);
  540. return 1;
  541. }
  542. };
  543. #endif // noexcept function type
  544. template <>
  545. struct unqualified_pusher<c_closure> {
  546. static int push(lua_State* L, c_closure cc) {
  547. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  548. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  549. #endif // make sure stack doesn't overflow
  550. lua_pushcclosure(L, cc.c_function, cc.upvalues);
  551. return 1;
  552. }
  553. };
  554. template <typename Arg, typename... Args>
  555. struct unqualified_pusher<closure<Arg, Args...>> {
  556. template <std::size_t... I, typename T>
  557. static int push(std::index_sequence<I...>, lua_State* L, T&& c) {
  558. using f_tuple = decltype(std::forward<T>(c).upvalues);
  559. int pushcount = multi_push(L, std::get<I>(std::forward<f_tuple>(std::forward<T>(c).upvalues))...);
  560. return stack::push(L, c_closure(c.c_function, pushcount));
  561. }
  562. template <typename T>
  563. static int push(lua_State* L, T&& c) {
  564. return push(std::make_index_sequence<1 + sizeof...(Args)>(), L, std::forward<T>(c));
  565. }
  566. };
  567. template <>
  568. struct unqualified_pusher<void*> {
  569. static int push(lua_State* L, void* userdata) noexcept {
  570. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  571. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  572. #endif // make sure stack doesn't overflow
  573. lua_pushlightuserdata(L, userdata);
  574. return 1;
  575. }
  576. };
  577. template <>
  578. struct unqualified_pusher<const void*> {
  579. static int push(lua_State* L, const void* userdata) noexcept {
  580. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  581. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  582. #endif // make sure stack doesn't overflow
  583. lua_pushlightuserdata(L, const_cast<void*>(userdata));
  584. return 1;
  585. }
  586. };
  587. template <>
  588. struct unqualified_pusher<lightuserdata_value> {
  589. static int push(lua_State* L, lightuserdata_value userdata) noexcept {
  590. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  591. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  592. #endif // make sure stack doesn't overflow
  593. lua_pushlightuserdata(L, userdata);
  594. return 1;
  595. }
  596. };
  597. template <typename T>
  598. struct unqualified_pusher<light<T>> {
  599. static int push(lua_State* L, light<T> l) noexcept {
  600. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  601. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  602. #endif // make sure stack doesn't overflow
  603. lua_pushlightuserdata(L, static_cast<void*>(l.value()));
  604. return 1;
  605. }
  606. };
  607. template <typename T>
  608. struct unqualified_pusher<user<T>> {
  609. template <bool with_meta = true, typename Key, typename... Args>
  610. static int push_with(lua_State* L, Key&& name, Args&&... args) {
  611. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  612. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  613. #endif // make sure stack doesn't overflow
  614. // A dumb pusher
  615. T* data = detail::user_allocate<T>(L);
  616. if (with_meta) {
  617. // Make sure we have a plain GC set for this data
  618. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  619. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  620. #endif // make sure stack doesn't overflow
  621. if (luaL_newmetatable(L, name) != 0) {
  622. lua_CFunction cdel = detail::user_alloc_destroy<T>;
  623. lua_pushcclosure(L, cdel, 0);
  624. lua_setfield(L, -2, "__gc");
  625. }
  626. lua_setmetatable(L, -2);
  627. }
  628. std::allocator<T> alloc {};
  629. std::allocator_traits<std::allocator<T>>::construct(alloc, data, std::forward<Args>(args)...);
  630. return 1;
  631. }
  632. template <typename Arg, typename... Args>
  633. static int push(lua_State* L, Arg&& arg, Args&&... args) {
  634. if constexpr (std::is_same_v<meta::unqualified_t<Arg>, metatable_key_t>) {
  635. const auto name = &arg[0];
  636. return push_with<true>(L, name, std::forward<Args>(args)...);
  637. }
  638. else if constexpr (std::is_same_v<meta::unqualified_t<Arg>, no_metatable_t>) {
  639. (void)arg;
  640. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  641. return push_with<false>(L, name, std::forward<Args>(args)...);
  642. }
  643. else {
  644. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  645. return push_with(L, name, std::forward<Arg>(arg), std::forward<Args>(args)...);
  646. }
  647. }
  648. static int push(lua_State* L, const user<T>& u) {
  649. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  650. return push_with(L, name, u.value);
  651. }
  652. static int push(lua_State* L, user<T>&& u) {
  653. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  654. return push_with(L, name, std::move(u.value()));
  655. }
  656. static int push(lua_State* L, no_metatable_t, const user<T>& u) {
  657. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  658. return push_with<false>(L, name, u.value());
  659. }
  660. static int push(lua_State* L, no_metatable_t, user<T>&& u) {
  661. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  662. return push_with<false>(L, name, std::move(u.value()));
  663. }
  664. };
  665. template <>
  666. struct unqualified_pusher<userdata_value> {
  667. static int push(lua_State* L, userdata_value data) {
  668. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  669. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  670. #endif // make sure stack doesn't overflow
  671. void** ud = detail::usertype_allocate_pointer<void>(L);
  672. *ud = data.value();
  673. return 1;
  674. }
  675. };
  676. template <>
  677. struct unqualified_pusher<const char*> {
  678. static int push_sized(lua_State* L, const char* str, std::size_t len) {
  679. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  680. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  681. #endif // make sure stack doesn't overflow
  682. lua_pushlstring(L, str, len);
  683. return 1;
  684. }
  685. static int push(lua_State* L, const char* str) {
  686. if (str == nullptr)
  687. return stack::push(L, lua_nil);
  688. return push_sized(L, str, std::char_traits<char>::length(str));
  689. }
  690. static int push(lua_State* L, const char* strb, const char* stre) {
  691. return push_sized(L, strb, static_cast<std::size_t>(stre - strb));
  692. }
  693. static int push(lua_State* L, const char* str, std::size_t len) {
  694. return push_sized(L, str, len);
  695. }
  696. };
  697. template <>
  698. struct unqualified_pusher<char*> {
  699. static int push_sized(lua_State* L, const char* str, std::size_t len) {
  700. unqualified_pusher<const char*> p {};
  701. (void)p;
  702. return p.push_sized(L, str, len);
  703. }
  704. static int push(lua_State* L, const char* str) {
  705. unqualified_pusher<const char*> p {};
  706. (void)p;
  707. return p.push(L, str);
  708. }
  709. static int push(lua_State* L, const char* strb, const char* stre) {
  710. unqualified_pusher<const char*> p {};
  711. (void)p;
  712. return p.push(L, strb, stre);
  713. }
  714. static int push(lua_State* L, const char* str, std::size_t len) {
  715. unqualified_pusher<const char*> p {};
  716. (void)p;
  717. return p.push(L, str, len);
  718. }
  719. };
  720. template <size_t N>
  721. struct unqualified_pusher<char[N]> {
  722. static int push(lua_State* L, const char (&str)[N]) {
  723. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  724. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  725. #endif // make sure stack doesn't overflow
  726. lua_pushlstring(L, str, std::char_traits<char>::length(str));
  727. return 1;
  728. }
  729. static int push(lua_State* L, const char (&str)[N], std::size_t sz) {
  730. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  731. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  732. #endif // make sure stack doesn't overflow
  733. lua_pushlstring(L, str, sz);
  734. return 1;
  735. }
  736. };
  737. template <>
  738. struct unqualified_pusher<char> {
  739. static int push(lua_State* L, char c) {
  740. const char str[2] = { c, '\0' };
  741. return stack::push(L, static_cast<const char*>(str), 1u);
  742. }
  743. };
  744. #if SOL_IS_ON(SOL_CHAR8_T)
  745. template <>
  746. struct unqualified_pusher<const char8_t*> {
  747. static int push_sized(lua_State* L, const char8_t* str, std::size_t len) {
  748. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  749. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  750. #endif // make sure stack doesn't overflow
  751. lua_pushlstring(L, reinterpret_cast<const char*>(str), len);
  752. return 1;
  753. }
  754. static int push(lua_State* L, const char8_t* str) {
  755. if (str == nullptr)
  756. return stack::push(L, lua_nil);
  757. return push_sized(L, str, std::char_traits<char>::length(reinterpret_cast<const char*>(str)));
  758. }
  759. static int push(lua_State* L, const char8_t* strb, const char8_t* stre) {
  760. return push_sized(L, strb, static_cast<std::size_t>(stre - strb));
  761. }
  762. static int push(lua_State* L, const char8_t* str, std::size_t len) {
  763. return push_sized(L, str, len);
  764. }
  765. };
  766. template <>
  767. struct unqualified_pusher<char8_t*> {
  768. static int push_sized(lua_State* L, const char8_t* str, std::size_t len) {
  769. unqualified_pusher<const char8_t*> p {};
  770. (void)p;
  771. return p.push_sized(L, str, len);
  772. }
  773. static int push(lua_State* L, const char8_t* str) {
  774. unqualified_pusher<const char8_t*> p {};
  775. (void)p;
  776. return p.push(L, str);
  777. }
  778. static int push(lua_State* L, const char8_t* strb, const char8_t* stre) {
  779. unqualified_pusher<const char8_t*> p {};
  780. (void)p;
  781. return p.push(L, strb, stre);
  782. }
  783. static int push(lua_State* L, const char8_t* str, std::size_t len) {
  784. unqualified_pusher<const char8_t*> p {};
  785. (void)p;
  786. return p.push(L, str, len);
  787. }
  788. };
  789. template <size_t N>
  790. struct unqualified_pusher<char8_t[N]> {
  791. static int push(lua_State* L, const char8_t (&str)[N]) {
  792. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  793. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  794. #endif // make sure stack doesn't overflow
  795. const char* str_as_char = reinterpret_cast<const char*>(static_cast<const char8_t*>(str));
  796. lua_pushlstring(L, str_as_char, std::char_traits<char>::length(str_as_char));
  797. return 1;
  798. }
  799. static int push(lua_State* L, const char8_t (&str)[N], std::size_t sz) {
  800. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  801. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  802. #endif // make sure stack doesn't overflow
  803. lua_pushlstring(L, str, sz);
  804. return 1;
  805. }
  806. };
  807. template <>
  808. struct unqualified_pusher<char8_t> {
  809. static int push(lua_State* L, char8_t c) {
  810. const char8_t str[2] = { c, '\0' };
  811. return stack::push(L, static_cast<const char8_t*>(str), 1u);
  812. }
  813. };
  814. #endif // char8_t
  815. template <typename Ch, typename Traits, typename Al>
  816. struct unqualified_pusher<std::basic_string<Ch, Traits, Al>> {
  817. static int push(lua_State* L, const std::basic_string<Ch, Traits, Al>& str) {
  818. if constexpr (!std::is_same_v<Ch, char>) {
  819. return stack::push(L, str.data(), str.size());
  820. }
  821. else {
  822. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  823. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  824. #endif // make sure stack doesn't overflow
  825. lua_pushlstring(L, str.c_str(), str.size());
  826. return 1;
  827. }
  828. }
  829. static int push(lua_State* L, const std::basic_string<Ch, Traits, Al>& str, std::size_t sz) {
  830. if constexpr (!std::is_same_v<Ch, char>) {
  831. return stack::push(L, str.data(), sz);
  832. }
  833. else {
  834. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  835. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  836. #endif // make sure stack doesn't overflow
  837. lua_pushlstring(L, str.c_str(), sz);
  838. return 1;
  839. }
  840. }
  841. };
  842. template <typename Ch, typename Traits>
  843. struct unqualified_pusher<basic_string_view<Ch, Traits>> {
  844. static int push(lua_State* L, const basic_string_view<Ch, Traits>& sv) {
  845. return stack::push(L, sv.data(), sv.length());
  846. }
  847. static int push(lua_State* L, const basic_string_view<Ch, Traits>& sv, std::size_t n) {
  848. return stack::push(L, sv.data(), n);
  849. }
  850. };
  851. template <>
  852. struct unqualified_pusher<meta_function> {
  853. static int push(lua_State* L, meta_function m) {
  854. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  855. luaL_checkstack(L, 1, detail::not_enough_stack_space_meta_function_name);
  856. #endif // make sure stack doesn't overflow
  857. const std::string& str = to_string(m);
  858. lua_pushlstring(L, str.c_str(), str.size());
  859. return 1;
  860. }
  861. };
  862. template <>
  863. struct unqualified_pusher<absolute_index> {
  864. static int push(lua_State* L, absolute_index ai) {
  865. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  866. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  867. #endif // make sure stack doesn't overflow
  868. lua_pushvalue(L, ai);
  869. return 1;
  870. }
  871. };
  872. template <>
  873. struct unqualified_pusher<raw_index> {
  874. static int push(lua_State* L, raw_index ri) {
  875. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  876. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  877. #endif // make sure stack doesn't overflow
  878. lua_pushvalue(L, ri);
  879. return 1;
  880. }
  881. };
  882. template <>
  883. struct unqualified_pusher<ref_index> {
  884. static int push(lua_State* L, ref_index ri) {
  885. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  886. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  887. #endif // make sure stack doesn't overflow
  888. lua_rawgeti(L, LUA_REGISTRYINDEX, ri);
  889. return 1;
  890. }
  891. };
  892. template <>
  893. struct unqualified_pusher<const wchar_t*> {
  894. static int push(lua_State* L, const wchar_t* wstr) {
  895. return push(L, wstr, std::char_traits<wchar_t>::length(wstr));
  896. }
  897. static int push(lua_State* L, const wchar_t* wstr, std::size_t sz) {
  898. return push(L, wstr, wstr + sz);
  899. }
  900. static int push(lua_State* L, const wchar_t* strb, const wchar_t* stre) {
  901. if constexpr (sizeof(wchar_t) == 2) {
  902. const char16_t* sb = reinterpret_cast<const char16_t*>(strb);
  903. const char16_t* se = reinterpret_cast<const char16_t*>(stre);
  904. return stack::push(L, sb, se);
  905. }
  906. else {
  907. const char32_t* sb = reinterpret_cast<const char32_t*>(strb);
  908. const char32_t* se = reinterpret_cast<const char32_t*>(stre);
  909. return stack::push(L, sb, se);
  910. }
  911. }
  912. };
  913. template <>
  914. struct unqualified_pusher<wchar_t*> {
  915. static int push(lua_State* L, const wchar_t* str) {
  916. unqualified_pusher<const wchar_t*> p {};
  917. (void)p;
  918. return p.push(L, str);
  919. }
  920. static int push(lua_State* L, const wchar_t* strb, const wchar_t* stre) {
  921. unqualified_pusher<const wchar_t*> p {};
  922. (void)p;
  923. return p.push(L, strb, stre);
  924. }
  925. static int push(lua_State* L, const wchar_t* str, std::size_t len) {
  926. unqualified_pusher<const wchar_t*> p {};
  927. (void)p;
  928. return p.push(L, str, len);
  929. }
  930. };
  931. template <>
  932. struct unqualified_pusher<const char16_t*> {
  933. static int convert_into(lua_State* L, char* start, std::size_t, const char16_t* strb, const char16_t* stre) {
  934. char* target = start;
  935. char32_t cp = 0;
  936. for (const char16_t* strtarget = strb; strtarget < stre;) {
  937. auto dr = unicode::utf16_to_code_point(strtarget, stre);
  938. if (dr.error != unicode::error_code::ok) {
  939. cp = unicode::unicode_detail::replacement;
  940. }
  941. else {
  942. cp = dr.codepoint;
  943. }
  944. auto er = unicode::code_point_to_utf8(cp);
  945. const char* utf8data = er.code_units.data();
  946. std::memcpy(target, utf8data, er.code_units_size);
  947. target += er.code_units_size;
  948. strtarget = dr.next;
  949. }
  950. return stack::push(L, start, target);
  951. }
  952. static int push(lua_State* L, const char16_t* u16str) {
  953. return push(L, u16str, std::char_traits<char16_t>::length(u16str));
  954. }
  955. static int push(lua_State* L, const char16_t* u16str, std::size_t sz) {
  956. return push(L, u16str, u16str + sz);
  957. }
  958. static int push(lua_State* L, const char16_t* strb, const char16_t* stre) {
  959. char sbo[SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_];
  960. // if our max string space is small enough, use SBO
  961. // right off the bat
  962. std::size_t max_possible_code_units = static_cast<std::size_t>(static_cast<std::size_t>(stre - strb) * static_cast<std::size_t>(4));
  963. if (max_possible_code_units <= SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_) {
  964. return convert_into(L, sbo, max_possible_code_units, strb, stre);
  965. }
  966. // otherwise, we must manually count/check size
  967. std::size_t needed_size = 0;
  968. for (const char16_t* strtarget = strb; strtarget < stre;) {
  969. auto dr = unicode::utf16_to_code_point(strtarget, stre);
  970. auto er = unicode::code_point_to_utf8(dr.codepoint);
  971. needed_size += er.code_units_size;
  972. strtarget = dr.next;
  973. }
  974. if (needed_size < SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_) {
  975. return convert_into(L, sbo, needed_size, strb, stre);
  976. }
  977. std::string u8str("", 0);
  978. u8str.resize(needed_size);
  979. char* target = const_cast<char*>(u8str.data());
  980. return convert_into(L, target, needed_size, strb, stre);
  981. }
  982. };
  983. template <>
  984. struct unqualified_pusher<char16_t*> {
  985. static int push(lua_State* L, const char16_t* str) {
  986. unqualified_pusher<const char16_t*> p {};
  987. (void)p;
  988. return p.push(L, str);
  989. }
  990. static int push(lua_State* L, const char16_t* strb, const char16_t* stre) {
  991. unqualified_pusher<const char16_t*> p {};
  992. (void)p;
  993. return p.push(L, strb, stre);
  994. }
  995. static int push(lua_State* L, const char16_t* str, std::size_t len) {
  996. unqualified_pusher<const char16_t*> p {};
  997. (void)p;
  998. return p.push(L, str, len);
  999. }
  1000. };
  1001. template <>
  1002. struct unqualified_pusher<const char32_t*> {
  1003. static int convert_into(lua_State* L, char* start, std::size_t, const char32_t* strb, const char32_t* stre) {
  1004. char* target = start;
  1005. char32_t cp = 0;
  1006. for (const char32_t* strtarget = strb; strtarget < stre;) {
  1007. auto dr = unicode::utf32_to_code_point(strtarget, stre);
  1008. if (dr.error != unicode::error_code::ok) {
  1009. cp = unicode::unicode_detail::replacement;
  1010. }
  1011. else {
  1012. cp = dr.codepoint;
  1013. }
  1014. auto er = unicode::code_point_to_utf8(cp);
  1015. const char* data = er.code_units.data();
  1016. std::memcpy(target, data, er.code_units_size);
  1017. target += er.code_units_size;
  1018. strtarget = dr.next;
  1019. }
  1020. return stack::push(L, start, target);
  1021. }
  1022. static int push(lua_State* L, const char32_t* u32str) {
  1023. return push(L, u32str, u32str + std::char_traits<char32_t>::length(u32str));
  1024. }
  1025. static int push(lua_State* L, const char32_t* u32str, std::size_t sz) {
  1026. return push(L, u32str, u32str + sz);
  1027. }
  1028. static int push(lua_State* L, const char32_t* strb, const char32_t* stre) {
  1029. char sbo[SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_];
  1030. // if our max string space is small enough, use SBO
  1031. // right off the bat
  1032. std::size_t max_possible_code_units = static_cast<std::size_t>(static_cast<std::size_t>(stre - strb) * static_cast<std::size_t>(4));
  1033. if (max_possible_code_units <= SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_) {
  1034. return convert_into(L, sbo, max_possible_code_units, strb, stre);
  1035. }
  1036. // otherwise, we must manually count/check size
  1037. std::size_t needed_size = 0;
  1038. for (const char32_t* strtarget = strb; strtarget < stre;) {
  1039. auto dr = unicode::utf32_to_code_point(strtarget, stre);
  1040. auto er = unicode::code_point_to_utf8(dr.codepoint);
  1041. needed_size += er.code_units_size;
  1042. strtarget = dr.next;
  1043. }
  1044. if (needed_size < SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_) {
  1045. return convert_into(L, sbo, needed_size, strb, stre);
  1046. }
  1047. std::string u8str("", 0);
  1048. u8str.resize(needed_size);
  1049. char* target = const_cast<char*>(u8str.data());
  1050. return convert_into(L, target, needed_size, strb, stre);
  1051. }
  1052. };
  1053. template <>
  1054. struct unqualified_pusher<char32_t*> {
  1055. static int push(lua_State* L, const char32_t* str) {
  1056. unqualified_pusher<const char32_t*> p {};
  1057. (void)p;
  1058. return p.push(L, str);
  1059. }
  1060. static int push(lua_State* L, const char32_t* strb, const char32_t* stre) {
  1061. unqualified_pusher<const char32_t*> p {};
  1062. (void)p;
  1063. return p.push(L, strb, stre);
  1064. }
  1065. static int push(lua_State* L, const char32_t* str, std::size_t len) {
  1066. unqualified_pusher<const char32_t*> p {};
  1067. (void)p;
  1068. return p.push(L, str, len);
  1069. }
  1070. };
  1071. template <size_t N>
  1072. struct unqualified_pusher<wchar_t[N]> {
  1073. static int push(lua_State* L, const wchar_t (&str)[N]) {
  1074. return push(L, str, std::char_traits<wchar_t>::length(str));
  1075. }
  1076. static int push(lua_State* L, const wchar_t (&str)[N], std::size_t sz) {
  1077. const wchar_t* str_ptr = static_cast<const wchar_t*>(str);
  1078. return stack::push<const wchar_t*>(L, str_ptr, str_ptr + sz);
  1079. }
  1080. };
  1081. template <size_t N>
  1082. struct unqualified_pusher<char16_t[N]> {
  1083. static int push(lua_State* L, const char16_t (&str)[N]) {
  1084. return push(L, str, std::char_traits<char16_t>::length(str));
  1085. }
  1086. static int push(lua_State* L, const char16_t (&str)[N], std::size_t sz) {
  1087. const char16_t* str_ptr = static_cast<const char16_t*>(str);
  1088. return stack::push<const char16_t*>(L, str_ptr, str_ptr + sz);
  1089. }
  1090. };
  1091. template <size_t N>
  1092. struct unqualified_pusher<char32_t[N]> {
  1093. static int push(lua_State* L, const char32_t (&str)[N]) {
  1094. return push(L, str, std::char_traits<char32_t>::length(str));
  1095. }
  1096. static int push(lua_State* L, const char32_t (&str)[N], std::size_t sz) {
  1097. const char32_t* str_ptr = static_cast<const char32_t*>(str);
  1098. return stack::push<const char32_t*>(L, str_ptr, str_ptr + sz);
  1099. }
  1100. };
  1101. template <>
  1102. struct unqualified_pusher<wchar_t> {
  1103. static int push(lua_State* L, wchar_t c) {
  1104. const wchar_t str[2] = { c, '\0' };
  1105. return stack::push(L, static_cast<const wchar_t*>(str), 1u);
  1106. }
  1107. };
  1108. template <>
  1109. struct unqualified_pusher<char16_t> {
  1110. static int push(lua_State* L, char16_t c) {
  1111. const char16_t str[2] = { c, '\0' };
  1112. return stack::push(L, static_cast<const char16_t*>(str), 1u);
  1113. }
  1114. };
  1115. template <>
  1116. struct unqualified_pusher<char32_t> {
  1117. static int push(lua_State* L, char32_t c) {
  1118. const char32_t str[2] = { c, '\0' };
  1119. return stack::push(L, static_cast<const char32_t*>(str), 1u);
  1120. }
  1121. };
  1122. template <typename... Args>
  1123. struct unqualified_pusher<std::tuple<Args...>> {
  1124. template <std::size_t... I, typename T>
  1125. static int push(std::index_sequence<I...>, lua_State* L, T&& t) {
  1126. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  1127. luaL_checkstack(L, static_cast<int>(sizeof...(I)), detail::not_enough_stack_space_generic);
  1128. #endif // make sure stack doesn't overflow
  1129. int pushcount = 0;
  1130. (void)detail::swallow { 0, (pushcount += stack::push(L, std::get<I>(std::forward<T>(t))), 0)... };
  1131. return pushcount;
  1132. }
  1133. template <typename T>
  1134. static int push(lua_State* L, T&& t) {
  1135. return push(std::index_sequence_for<Args...>(), L, std::forward<T>(t));
  1136. }
  1137. };
  1138. template <typename A, typename B>
  1139. struct unqualified_pusher<std::pair<A, B>> {
  1140. template <typename T>
  1141. static int push(lua_State* L, T&& t) {
  1142. int pushcount = stack::push(L, std::get<0>(std::forward<T>(t)));
  1143. pushcount += stack::push(L, std::get<1>(std::forward<T>(t)));
  1144. return pushcount;
  1145. }
  1146. };
  1147. template <typename T>
  1148. struct unqualified_pusher<T, std::enable_if_t<meta::is_optional_v<T>>> {
  1149. using ValueType = typename meta::unqualified_t<T>::value_type;
  1150. template <typename Optional>
  1151. static int push(lua_State* L, Optional&& op) {
  1152. using QualifiedValueType = meta::conditional_t<std::is_lvalue_reference_v<Optional>, ValueType&, ValueType&&>;
  1153. if (!op) {
  1154. return stack::push(L, nullopt);
  1155. }
  1156. return stack::push(L, static_cast<QualifiedValueType>(op.value()));
  1157. }
  1158. };
  1159. template <typename T>
  1160. struct unqualified_pusher<forward_as_value_t<T>> {
  1161. static int push(lua_State* L, const forward_as_value_t<T>& value_) {
  1162. return stack::push<T>(L, value_.value());
  1163. }
  1164. static int push(lua_State* L, forward_as_value_t<T>&& value_) {
  1165. return stack::push<T>(L, std::move(value_).value());
  1166. }
  1167. };
  1168. template <>
  1169. struct unqualified_pusher<nullopt_t> {
  1170. static int push(lua_State* L, nullopt_t) noexcept {
  1171. return stack::push(L, lua_nil);
  1172. }
  1173. };
  1174. template <>
  1175. struct unqualified_pusher<std::nullptr_t> {
  1176. static int push(lua_State* L, std::nullptr_t) noexcept {
  1177. return stack::push(L, lua_nil);
  1178. }
  1179. };
  1180. template <>
  1181. struct unqualified_pusher<this_state> {
  1182. static int push(lua_State*, const this_state&) noexcept {
  1183. return 0;
  1184. }
  1185. };
  1186. template <>
  1187. struct unqualified_pusher<this_main_state> {
  1188. static int push(lua_State*, const this_main_state&) noexcept {
  1189. return 0;
  1190. }
  1191. };
  1192. template <>
  1193. struct unqualified_pusher<new_table> {
  1194. static int push(lua_State* L, const new_table& nt) {
  1195. lua_createtable(L, nt.sequence_hint, nt.map_hint);
  1196. return 1;
  1197. }
  1198. };
  1199. template <typename Allocator>
  1200. struct unqualified_pusher<basic_bytecode<Allocator>> {
  1201. template <typename T>
  1202. static int push(lua_State* L, T&& bc, const char* bytecode_name) {
  1203. const auto first = bc.data();
  1204. const auto bcsize = bc.size();
  1205. // pushes either the function, or an error
  1206. // if it errors, shit goes south, and people can test that upstream
  1207. (void)luaL_loadbuffer(
  1208. L, reinterpret_cast<const char*>(first), static_cast<std::size_t>(bcsize * (sizeof(*first) / sizeof(const char))), bytecode_name);
  1209. return 1;
  1210. }
  1211. template <typename T>
  1212. static int push(lua_State* L, T&& bc) {
  1213. return push(L, std::forward<bc>(bc), "bytecode");
  1214. }
  1215. };
  1216. #if SOL_IS_ON(SOL_STD_VARIANT)
  1217. namespace stack_detail {
  1218. struct push_function {
  1219. lua_State* L;
  1220. push_function(lua_State* L_) noexcept : L(L_) {
  1221. }
  1222. template <typename T>
  1223. int operator()(T&& value) const {
  1224. return stack::push<T>(L, std::forward<T>(value));
  1225. }
  1226. };
  1227. } // namespace stack_detail
  1228. template <typename... Tn>
  1229. struct unqualified_pusher<std::variant<Tn...>> {
  1230. static int push(lua_State* L, const std::variant<Tn...>& v) {
  1231. return std::visit(stack_detail::push_function(L), v);
  1232. }
  1233. static int push(lua_State* L, std::variant<Tn...>&& v) {
  1234. return std::visit(stack_detail::push_function(L), std::move(v));
  1235. }
  1236. };
  1237. #endif // Variant because Clang is terrible
  1238. }} // namespace sol::stack
  1239. #endif // SOL_STACK_PUSH_HPP