2021-03-06 23:14:48 +08:00
|
|
|
// sol2
|
2017-12-20 17:58:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2016-03-25 03:45:44 +08:00
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
// Copyright (c) 2013-2021 Rapptz, ThePhD and contributors
|
2016-03-25 03:45:44 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_STACK_CORE_HPP
|
|
|
|
#define SOL_STACK_CORE_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/types.hpp>
|
|
|
|
#include <sol/inheritance.hpp>
|
|
|
|
#include <sol/error_handler.hpp>
|
|
|
|
#include <sol/reference.hpp>
|
|
|
|
#include <sol/stack_reference.hpp>
|
|
|
|
#include <sol/tuple.hpp>
|
|
|
|
#include <sol/traits.hpp>
|
|
|
|
#include <sol/tie.hpp>
|
|
|
|
#include <sol/stack_guard.hpp>
|
|
|
|
#include <sol/demangle.hpp>
|
|
|
|
#include <sol/forward_detail.hpp>
|
2017-08-22 03:25:43 +08:00
|
|
|
|
2016-08-20 09:08:03 +08:00
|
|
|
#include <vector>
|
2018-11-19 02:06:01 +08:00
|
|
|
#include <bitset>
|
2017-08-06 07:20:28 +08:00
|
|
|
#include <forward_list>
|
2016-08-20 09:08:03 +08:00
|
|
|
#include <string>
|
2020-10-15 13:18:12 +08:00
|
|
|
#include <limits>
|
2017-08-22 03:25:43 +08:00
|
|
|
#include <algorithm>
|
2018-12-20 12:17:15 +08:00
|
|
|
#include <sstream>
|
2018-11-21 20:34:32 +08:00
|
|
|
#include <optional>
|
2020-10-15 13:18:12 +08:00
|
|
|
#include <type_traits>
|
2016-03-25 03:45:44 +08:00
|
|
|
|
|
|
|
namespace sol {
|
2016-06-17 08:15:56 +08:00
|
|
|
namespace detail {
|
2020-09-06 05:25:15 +08:00
|
|
|
struct with_function_tag { };
|
|
|
|
struct as_reference_tag { };
|
2016-08-24 09:42:27 +08:00
|
|
|
template <typename T>
|
2020-09-06 05:25:15 +08:00
|
|
|
struct as_pointer_tag { };
|
2016-08-24 09:42:27 +08:00
|
|
|
template <typename T>
|
2020-09-06 05:25:15 +08:00
|
|
|
struct as_value_tag { };
|
2017-09-23 06:54:33 +08:00
|
|
|
template <typename T>
|
2020-09-06 05:25:15 +08:00
|
|
|
struct as_unique_tag { };
|
2020-01-25 08:58:12 +08:00
|
|
|
template <typename T>
|
2020-09-06 05:25:15 +08:00
|
|
|
struct as_table_tag { };
|
2016-06-17 08:15:56 +08:00
|
|
|
|
2021-02-14 13:28:10 +08:00
|
|
|
template <typename Tag>
|
|
|
|
inline constexpr bool is_tagged_v
|
|
|
|
= meta::is_specialization_of_v<Tag,
|
|
|
|
detail::
|
|
|
|
as_pointer_tag> || meta::is_specialization_of_v<Tag, as_value_tag> || meta::is_specialization_of_v<Tag, as_unique_tag> || meta::is_specialization_of_v<Tag, as_table_tag> || std::is_same_v<Tag, as_reference_tag> || std::is_same_v<Tag, with_function_tag>;
|
|
|
|
|
2018-11-19 02:06:01 +08:00
|
|
|
using lua_reg_table = luaL_Reg[64];
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
using unique_destructor = void (*)(void*);
|
2018-06-16 01:19:09 +08:00
|
|
|
using unique_tag = detail::inheritance_unique_cast_function;
|
2016-06-17 08:15:56 +08:00
|
|
|
|
2020-10-24 06:59:07 +08:00
|
|
|
inline void* alloc_newuserdata(lua_State* L, std::size_t bytesize) {
|
2021-05-07 06:44:25 +08:00
|
|
|
#if SOL_LUA_VERSION_I_ >= 504
|
2020-11-02 18:53:57 +08:00
|
|
|
return lua_newuserdatauv(L, bytesize, 1);
|
2020-10-24 06:59:07 +08:00
|
|
|
#else
|
|
|
|
return lua_newuserdata(L, bytesize);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-03 05:32:58 +08:00
|
|
|
inline void* align(std::size_t alignment, std::size_t size, void*& ptr, std::size_t& space, std::size_t& required_space) {
|
|
|
|
// this handels arbitrary alignments...
|
|
|
|
// make this into a power-of-2-only?
|
|
|
|
// actually can't: this is a C++14-compatible framework,
|
|
|
|
// power of 2 alignment is C++17
|
|
|
|
std::uintptr_t initial = reinterpret_cast<std::uintptr_t>(ptr);
|
|
|
|
std::uintptr_t offby = static_cast<std::uintptr_t>(initial % alignment);
|
|
|
|
std::uintptr_t padding = (alignment - offby) % alignment;
|
|
|
|
required_space += size + padding;
|
|
|
|
if (space < required_space) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ptr = static_cast<void*>(static_cast<char*>(ptr) + padding);
|
|
|
|
space -= padding;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* align(std::size_t alignment, std::size_t size, void*& ptr, std::size_t& space) {
|
|
|
|
std::size_t required_space = 0;
|
|
|
|
return align(alignment, size, ptr, space, required_space);
|
|
|
|
}
|
|
|
|
|
2019-01-14 10:46:53 +08:00
|
|
|
inline void align_one(std::size_t a, std::size_t s, void*& target_alignment) {
|
|
|
|
std::size_t space = (std::numeric_limits<std::size_t>::max)();
|
|
|
|
target_alignment = align(a, s, target_alignment, space);
|
|
|
|
target_alignment = static_cast<void*>(static_cast<char*>(target_alignment) + s);
|
|
|
|
}
|
|
|
|
|
2017-10-03 05:32:58 +08:00
|
|
|
template <typename... Args>
|
2019-05-21 14:13:56 +08:00
|
|
|
std::size_t aligned_space_for(void* alignment = nullptr) {
|
2020-09-06 05:25:15 +08:00
|
|
|
// use temporary storage to prevent strict UB shenanigans
|
|
|
|
char alignment_shim[(std::max)({ sizeof(Args)... }) + (std::max)({ alignof(Args)... })] {};
|
2020-11-21 06:07:24 +08:00
|
|
|
char* start = alignment != nullptr ? static_cast<char*>(alignment) : alignment_shim;
|
2020-12-17 04:25:35 +08:00
|
|
|
alignment = start;
|
2020-03-31 12:22:46 +08:00
|
|
|
(void)detail::swallow { int {}, (align_one(std::alignment_of_v<Args>, sizeof(Args), alignment), int {})... };
|
2020-12-17 04:25:35 +08:00
|
|
|
return static_cast<std::size_t>(static_cast<char*>(alignment) - start);
|
2017-10-03 05:32:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void* align_usertype_pointer(void* ptr) {
|
2019-02-14 15:40:57 +08:00
|
|
|
using use_align = std::integral_constant<bool,
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
false
|
2017-10-03 05:32:58 +08:00
|
|
|
#else
|
2018-12-20 12:17:15 +08:00
|
|
|
(std::alignment_of<void*>::value > 1)
|
2017-10-03 05:32:58 +08:00
|
|
|
#endif
|
2019-02-14 15:40:57 +08:00
|
|
|
>;
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!use_align::value) {
|
|
|
|
return ptr;
|
|
|
|
}
|
2017-11-17 20:20:17 +08:00
|
|
|
std::size_t space = (std::numeric_limits<std::size_t>::max)();
|
2017-10-03 05:32:58 +08:00
|
|
|
return align(std::alignment_of<void*>::value, sizeof(void*), ptr, space);
|
|
|
|
}
|
|
|
|
|
2018-06-18 03:42:24 +08:00
|
|
|
template <bool pre_aligned = false, bool pre_shifted = false>
|
2019-05-21 14:13:56 +08:00
|
|
|
void* align_usertype_unique_destructor(void* ptr) {
|
2019-02-14 15:40:57 +08:00
|
|
|
using use_align = std::integral_constant<bool,
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
false
|
2017-10-03 05:32:58 +08:00
|
|
|
#else
|
2018-12-20 12:17:15 +08:00
|
|
|
(std::alignment_of<unique_destructor>::value > 1)
|
2017-10-03 05:32:58 +08:00
|
|
|
#endif
|
2019-02-14 15:40:57 +08:00
|
|
|
>;
|
2018-06-16 01:19:09 +08:00
|
|
|
if (!pre_aligned) {
|
|
|
|
ptr = align_usertype_pointer(ptr);
|
2018-06-18 03:42:24 +08:00
|
|
|
}
|
|
|
|
if (!pre_shifted) {
|
2018-06-16 01:19:09 +08:00
|
|
|
ptr = static_cast<void*>(static_cast<char*>(ptr) + sizeof(void*));
|
|
|
|
}
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!use_align::value) {
|
|
|
|
return static_cast<void*>(static_cast<void**>(ptr) + 1);
|
|
|
|
}
|
2017-11-17 20:20:17 +08:00
|
|
|
std::size_t space = (std::numeric_limits<std::size_t>::max)();
|
2017-10-03 05:32:58 +08:00
|
|
|
return align(std::alignment_of<unique_destructor>::value, sizeof(unique_destructor), ptr, space);
|
|
|
|
}
|
|
|
|
|
2018-06-18 03:42:24 +08:00
|
|
|
template <bool pre_aligned = false, bool pre_shifted = false>
|
2019-05-21 14:13:56 +08:00
|
|
|
void* align_usertype_unique_tag(void* ptr) {
|
2019-02-14 15:40:57 +08:00
|
|
|
using use_align = std::integral_constant<bool,
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
false
|
2018-06-16 01:19:09 +08:00
|
|
|
#else
|
2018-12-20 12:17:15 +08:00
|
|
|
(std::alignment_of<unique_tag>::value > 1)
|
2018-06-16 01:19:09 +08:00
|
|
|
#endif
|
2019-02-14 15:40:57 +08:00
|
|
|
>;
|
2018-06-16 01:19:09 +08:00
|
|
|
if (!pre_aligned) {
|
|
|
|
ptr = align_usertype_unique_destructor(ptr);
|
2018-06-18 03:42:24 +08:00
|
|
|
}
|
|
|
|
if (!pre_shifted) {
|
2018-06-16 01:19:09 +08:00
|
|
|
ptr = static_cast<void*>(static_cast<char*>(ptr) + sizeof(unique_destructor));
|
|
|
|
}
|
|
|
|
if (!use_align::value) {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
std::size_t space = (std::numeric_limits<std::size_t>::max)();
|
|
|
|
return align(std::alignment_of<unique_tag>::value, sizeof(unique_tag), ptr, space);
|
|
|
|
}
|
2018-06-18 03:42:24 +08:00
|
|
|
|
|
|
|
template <typename T, bool pre_aligned = false, bool pre_shifted = false>
|
2019-05-21 14:13:56 +08:00
|
|
|
void* align_usertype_unique(void* ptr) {
|
2017-10-03 05:32:58 +08:00
|
|
|
typedef std::integral_constant<bool,
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
false
|
2017-10-03 05:32:58 +08:00
|
|
|
#else
|
2020-09-26 21:46:11 +08:00
|
|
|
(std::alignment_of_v<T> > 1)
|
2017-10-03 05:32:58 +08:00
|
|
|
#endif
|
2018-12-20 12:17:15 +08:00
|
|
|
>
|
|
|
|
use_align;
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!pre_aligned) {
|
2018-06-16 01:19:09 +08:00
|
|
|
ptr = align_usertype_unique_tag(ptr);
|
2018-06-18 03:42:24 +08:00
|
|
|
}
|
|
|
|
if (!pre_shifted) {
|
2018-06-16 01:19:09 +08:00
|
|
|
ptr = static_cast<void*>(static_cast<char*>(ptr) + sizeof(unique_tag));
|
2017-10-03 05:32:58 +08:00
|
|
|
}
|
|
|
|
if (!use_align::value) {
|
|
|
|
return ptr;
|
|
|
|
}
|
2017-11-17 20:20:17 +08:00
|
|
|
std::size_t space = (std::numeric_limits<std::size_t>::max)();
|
2020-09-26 21:46:11 +08:00
|
|
|
return align(std::alignment_of_v<T>, sizeof(T), ptr, space);
|
2017-10-03 05:32:58 +08:00
|
|
|
}
|
|
|
|
|
2016-06-17 08:15:56 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
void* align_user(void* ptr) {
|
2017-10-03 05:32:58 +08:00
|
|
|
typedef std::integral_constant<bool,
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
false
|
2017-10-03 05:32:58 +08:00
|
|
|
#else
|
2020-09-26 21:46:11 +08:00
|
|
|
(std::alignment_of_v<T> > 1)
|
2017-10-03 05:32:58 +08:00
|
|
|
#endif
|
2018-12-20 12:17:15 +08:00
|
|
|
>
|
|
|
|
use_align;
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!use_align::value) {
|
|
|
|
return ptr;
|
|
|
|
}
|
2017-11-17 20:20:17 +08:00
|
|
|
std::size_t space = (std::numeric_limits<std::size_t>::max)();
|
2020-09-26 21:46:11 +08:00
|
|
|
return align(std::alignment_of_v<T>, sizeof(T), ptr, space);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
2016-08-20 09:08:03 +08:00
|
|
|
|
2016-08-24 09:42:27 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
T** usertype_allocate_pointer(lua_State* L) {
|
2017-10-03 05:32:58 +08:00
|
|
|
typedef std::integral_constant<bool,
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
false
|
2017-10-03 05:32:58 +08:00
|
|
|
#else
|
2018-12-20 12:17:15 +08:00
|
|
|
(std::alignment_of<T*>::value > 1)
|
2017-10-03 05:32:58 +08:00
|
|
|
#endif
|
2018-12-20 12:17:15 +08:00
|
|
|
>
|
|
|
|
use_align;
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!use_align::value) {
|
2020-10-24 06:59:07 +08:00
|
|
|
T** pointerpointer = static_cast<T**>(alloc_newuserdata(L, sizeof(T*)));
|
2017-10-03 05:32:58 +08:00
|
|
|
return pointerpointer;
|
|
|
|
}
|
|
|
|
static const std::size_t initial_size = aligned_space_for<T*>(nullptr);
|
|
|
|
static const std::size_t misaligned_size = aligned_space_for<T*>(reinterpret_cast<void*>(0x1));
|
|
|
|
|
|
|
|
std::size_t allocated_size = initial_size;
|
2020-10-24 06:59:07 +08:00
|
|
|
void* unadjusted = alloc_newuserdata(L, initial_size);
|
2017-10-03 05:32:58 +08:00
|
|
|
void* adjusted = align(std::alignment_of<T*>::value, sizeof(T*), unadjusted, allocated_size);
|
|
|
|
if (adjusted == nullptr) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
// what kind of absolute garbage trash allocator are we dealing with?
|
|
|
|
// whatever, add some padding in the case of MAXIMAL alignment waste...
|
|
|
|
allocated_size = misaligned_size;
|
2020-10-24 06:59:07 +08:00
|
|
|
unadjusted = alloc_newuserdata(L, allocated_size);
|
2017-10-03 05:32:58 +08:00
|
|
|
adjusted = align(std::alignment_of<T*>::value, sizeof(T*), unadjusted, allocated_size);
|
|
|
|
if (adjusted == nullptr) {
|
|
|
|
// trash allocator can burn in hell
|
|
|
|
lua_pop(L, 1);
|
2018-12-20 12:17:15 +08:00
|
|
|
// luaL_error(L, "if you are the one that wrote this allocator you should feel bad for doing a
|
|
|
|
// worse job than malloc/realloc and should go read some books, yeah?");
|
2017-10-03 05:32:58 +08:00
|
|
|
luaL_error(L, "cannot properly align memory for '%s'", detail::demangle<T*>().data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return static_cast<T**>(adjusted);
|
|
|
|
}
|
|
|
|
|
2019-01-14 10:46:53 +08:00
|
|
|
inline bool attempt_alloc(lua_State* L, std::size_t ptr_align, std::size_t ptr_size, std::size_t value_align, std::size_t value_size,
|
|
|
|
std::size_t allocated_size, void*& pointer_adjusted, void*& data_adjusted) {
|
2020-10-24 06:59:07 +08:00
|
|
|
void* adjusted = alloc_newuserdata(L, allocated_size);
|
2019-01-14 10:46:53 +08:00
|
|
|
pointer_adjusted = align(ptr_align, ptr_size, adjusted, allocated_size);
|
|
|
|
if (pointer_adjusted == nullptr) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// subtract size of what we're going to allocate there
|
|
|
|
allocated_size -= ptr_size;
|
|
|
|
adjusted = static_cast<void*>(static_cast<char*>(pointer_adjusted) + ptr_size);
|
|
|
|
data_adjusted = align(value_align, value_size, adjusted, allocated_size);
|
|
|
|
if (data_adjusted == nullptr) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool attempt_alloc_unique(lua_State* L, std::size_t ptr_align, std::size_t ptr_size, std::size_t real_align, std::size_t real_size,
|
|
|
|
std::size_t allocated_size, void*& pointer_adjusted, void*& dx_adjusted, void*& id_adjusted, void*& data_adjusted) {
|
2020-10-24 06:59:07 +08:00
|
|
|
void* adjusted = alloc_newuserdata(L, allocated_size);
|
2019-01-14 10:46:53 +08:00
|
|
|
pointer_adjusted = align(ptr_align, ptr_size, adjusted, allocated_size);
|
|
|
|
if (pointer_adjusted == nullptr) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
allocated_size -= ptr_size;
|
|
|
|
|
|
|
|
adjusted = static_cast<void*>(static_cast<char*>(pointer_adjusted) + ptr_size);
|
|
|
|
dx_adjusted = align(std::alignment_of_v<unique_destructor>, sizeof(unique_destructor), adjusted, allocated_size);
|
|
|
|
if (dx_adjusted == nullptr) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
allocated_size -= sizeof(unique_destructor);
|
|
|
|
|
|
|
|
adjusted = static_cast<void*>(static_cast<char*>(dx_adjusted) + sizeof(unique_destructor));
|
|
|
|
|
|
|
|
id_adjusted = align(std::alignment_of_v<unique_tag>, sizeof(unique_tag), adjusted, allocated_size);
|
|
|
|
if (id_adjusted == nullptr) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
allocated_size -= sizeof(unique_tag);
|
|
|
|
|
|
|
|
adjusted = static_cast<void*>(static_cast<char*>(id_adjusted) + sizeof(unique_tag));
|
|
|
|
data_adjusted = align(real_align, real_size, adjusted, allocated_size);
|
|
|
|
if (data_adjusted == nullptr) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-03 05:32:58 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
T* usertype_allocate(lua_State* L) {
|
2017-10-03 05:32:58 +08:00
|
|
|
typedef std::integral_constant<bool,
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
false
|
2017-10-03 05:32:58 +08:00
|
|
|
#else
|
2020-09-26 21:46:11 +08:00
|
|
|
(std::alignment_of<T*>::value > 1 || std::alignment_of_v<T> > 1)
|
2017-10-03 05:32:58 +08:00
|
|
|
#endif
|
2018-12-20 12:17:15 +08:00
|
|
|
>
|
|
|
|
use_align;
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!use_align::value) {
|
2020-10-24 06:59:07 +08:00
|
|
|
T** pointerpointer = static_cast<T**>(alloc_newuserdata(L, sizeof(T*) + sizeof(T)));
|
2017-10-03 05:32:58 +08:00
|
|
|
T*& pointerreference = *pointerpointer;
|
|
|
|
T* allocationtarget = reinterpret_cast<T*>(pointerpointer + 1);
|
|
|
|
pointerreference = allocationtarget;
|
|
|
|
return allocationtarget;
|
|
|
|
}
|
|
|
|
|
2020-10-24 06:59:07 +08:00
|
|
|
/* the assumption is that `alloc_newuserdata` -- unless someone
|
2017-10-03 05:32:58 +08:00
|
|
|
passes a specific lua_Alloc that gives us bogus, un-aligned pointers
|
|
|
|
-- uses malloc, which tends to hand out more or less aligned pointers to memory
|
|
|
|
(most of the time, anyhow)
|
|
|
|
|
|
|
|
but it's not guaranteed, so we have to do a post-adjustment check and increase padding
|
|
|
|
|
|
|
|
we do this preliminarily with compile-time stuff, to see
|
|
|
|
if we strike lucky with the allocator and alignment values
|
|
|
|
|
|
|
|
otherwise, we have to re-allocate the userdata and
|
|
|
|
over-allocate some space for additional padding because
|
|
|
|
compilers are optimized for aligned reads/writes
|
|
|
|
(and clang will barf UBsan errors on us for not being aligned)
|
|
|
|
*/
|
|
|
|
static const std::size_t initial_size = aligned_space_for<T*, T>(nullptr);
|
|
|
|
static const std::size_t misaligned_size = aligned_space_for<T*, T>(reinterpret_cast<void*>(0x1));
|
|
|
|
|
|
|
|
void* pointer_adjusted;
|
|
|
|
void* data_adjusted;
|
2019-01-29 01:33:21 +08:00
|
|
|
bool result
|
|
|
|
= attempt_alloc(L, std::alignment_of_v<T*>, sizeof(T*), std::alignment_of_v<T>, sizeof(T), initial_size, pointer_adjusted, data_adjusted);
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!result) {
|
|
|
|
// we're likely to get something that fails to perform the proper allocation a second time,
|
|
|
|
// so we use the suggested_new_size bump to help us out here
|
|
|
|
pointer_adjusted = nullptr;
|
|
|
|
data_adjusted = nullptr;
|
2019-01-29 01:33:21 +08:00
|
|
|
result = attempt_alloc(
|
|
|
|
L, std::alignment_of_v<T*>, sizeof(T*), std::alignment_of_v<T>, sizeof(T), misaligned_size, pointer_adjusted, data_adjusted);
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!result) {
|
|
|
|
if (pointer_adjusted == nullptr) {
|
|
|
|
luaL_error(L, "aligned allocation of userdata block (pointer section) for '%s' failed", detail::demangle<T>().c_str());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
luaL_error(L, "aligned allocation of userdata block (data section) for '%s' failed", detail::demangle<T>().c_str());
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T** pointerpointer = reinterpret_cast<T**>(pointer_adjusted);
|
|
|
|
T*& pointerreference = *pointerpointer;
|
|
|
|
T* allocationtarget = reinterpret_cast<T*>(data_adjusted);
|
|
|
|
pointerreference = allocationtarget;
|
|
|
|
return allocationtarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Real>
|
2019-05-21 14:13:56 +08:00
|
|
|
Real* usertype_unique_allocate(lua_State* L, T**& pref, unique_destructor*& dx, unique_tag*& id) {
|
2017-10-03 05:32:58 +08:00
|
|
|
typedef std::integral_constant<bool,
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
false
|
2017-10-03 05:32:58 +08:00
|
|
|
#else
|
2018-12-20 12:17:15 +08:00
|
|
|
(std::alignment_of<T*>::value > 1 || std::alignment_of<unique_tag>::value > 1 || std::alignment_of<unique_destructor>::value > 1
|
|
|
|
|| std::alignment_of<Real>::value > 1)
|
2017-10-03 05:32:58 +08:00
|
|
|
#endif
|
2018-12-20 12:17:15 +08:00
|
|
|
>
|
|
|
|
use_align;
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!use_align::value) {
|
2020-10-24 06:59:07 +08:00
|
|
|
pref = static_cast<T**>(alloc_newuserdata(L, sizeof(T*) + sizeof(detail::unique_destructor) + sizeof(unique_tag) + sizeof(Real)));
|
2017-10-03 05:32:58 +08:00
|
|
|
dx = static_cast<detail::unique_destructor*>(static_cast<void*>(pref + 1));
|
2018-06-16 01:19:09 +08:00
|
|
|
id = static_cast<unique_tag*>(static_cast<void*>(dx + 1));
|
|
|
|
Real* mem = static_cast<Real*>(static_cast<void*>(id + 1));
|
2017-10-03 05:32:58 +08:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2018-06-16 01:19:09 +08:00
|
|
|
static const std::size_t initial_size = aligned_space_for<T*, unique_destructor, unique_tag, Real>(nullptr);
|
|
|
|
static const std::size_t misaligned_size = aligned_space_for<T*, unique_destructor, unique_tag, Real>(reinterpret_cast<void*>(0x1));
|
2017-10-03 05:32:58 +08:00
|
|
|
|
|
|
|
void* pointer_adjusted;
|
|
|
|
void* dx_adjusted;
|
2018-06-16 01:19:09 +08:00
|
|
|
void* id_adjusted;
|
2017-10-03 05:32:58 +08:00
|
|
|
void* data_adjusted;
|
2019-01-14 10:46:53 +08:00
|
|
|
bool result = attempt_alloc_unique(L,
|
|
|
|
std::alignment_of_v<T*>,
|
|
|
|
sizeof(T*),
|
|
|
|
std::alignment_of_v<Real>,
|
|
|
|
sizeof(Real),
|
|
|
|
initial_size,
|
|
|
|
pointer_adjusted,
|
|
|
|
dx_adjusted,
|
|
|
|
id_adjusted,
|
|
|
|
data_adjusted);
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!result) {
|
|
|
|
// we're likely to get something that fails to perform the proper allocation a second time,
|
|
|
|
// so we use the suggested_new_size bump to help us out here
|
|
|
|
pointer_adjusted = nullptr;
|
|
|
|
dx_adjusted = nullptr;
|
2018-06-16 01:19:09 +08:00
|
|
|
id_adjusted = nullptr;
|
2017-10-03 05:32:58 +08:00
|
|
|
data_adjusted = nullptr;
|
2019-01-14 10:46:53 +08:00
|
|
|
result = attempt_alloc_unique(L,
|
|
|
|
std::alignment_of_v<T*>,
|
|
|
|
sizeof(T*),
|
|
|
|
std::alignment_of_v<Real>,
|
|
|
|
sizeof(Real),
|
|
|
|
misaligned_size,
|
|
|
|
pointer_adjusted,
|
|
|
|
dx_adjusted,
|
|
|
|
id_adjusted,
|
|
|
|
data_adjusted);
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!result) {
|
|
|
|
if (pointer_adjusted == nullptr) {
|
|
|
|
luaL_error(L, "aligned allocation of userdata block (pointer section) for '%s' failed", detail::demangle<T>().c_str());
|
|
|
|
}
|
|
|
|
else if (dx_adjusted == nullptr) {
|
2018-06-16 01:19:09 +08:00
|
|
|
luaL_error(L, "aligned allocation of userdata block (deleter section) for '%s' failed", detail::demangle<T>().c_str());
|
2017-10-03 05:32:58 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-06-16 01:19:09 +08:00
|
|
|
luaL_error(L, "aligned allocation of userdata block (data section) for '%s' failed", detail::demangle<T>().c_str());
|
2017-10-03 05:32:58 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pref = static_cast<T**>(pointer_adjusted);
|
|
|
|
dx = static_cast<detail::unique_destructor*>(dx_adjusted);
|
2018-06-16 01:19:09 +08:00
|
|
|
id = static_cast<unique_tag*>(id_adjusted);
|
2017-10-03 05:32:58 +08:00
|
|
|
Real* mem = static_cast<Real*>(data_adjusted);
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
T* user_allocate(lua_State* L) {
|
2017-10-03 05:32:58 +08:00
|
|
|
typedef std::integral_constant<bool,
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
|
2018-12-20 12:17:15 +08:00
|
|
|
false
|
2017-10-03 05:32:58 +08:00
|
|
|
#else
|
2020-09-26 21:46:11 +08:00
|
|
|
(std::alignment_of_v<T> > 1)
|
2017-10-03 05:32:58 +08:00
|
|
|
#endif
|
2018-12-20 12:17:15 +08:00
|
|
|
>
|
|
|
|
use_align;
|
2017-10-03 05:32:58 +08:00
|
|
|
if (!use_align::value) {
|
2020-10-24 06:59:07 +08:00
|
|
|
T* pointer = static_cast<T*>(alloc_newuserdata(L, sizeof(T)));
|
2017-10-03 05:32:58 +08:00
|
|
|
return pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const std::size_t initial_size = aligned_space_for<T>(nullptr);
|
|
|
|
static const std::size_t misaligned_size = aligned_space_for<T>(reinterpret_cast<void*>(0x1));
|
|
|
|
|
|
|
|
std::size_t allocated_size = initial_size;
|
2020-10-24 06:59:07 +08:00
|
|
|
void* unadjusted = alloc_newuserdata(L, allocated_size);
|
2020-09-26 21:46:11 +08:00
|
|
|
void* adjusted = align(std::alignment_of_v<T>, sizeof(T), unadjusted, allocated_size);
|
2017-10-03 05:32:58 +08:00
|
|
|
if (adjusted == nullptr) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
// try again, add extra space for alignment padding
|
|
|
|
allocated_size = misaligned_size;
|
2020-10-24 06:59:07 +08:00
|
|
|
unadjusted = alloc_newuserdata(L, allocated_size);
|
2020-09-26 21:46:11 +08:00
|
|
|
adjusted = align(std::alignment_of_v<T>, sizeof(T), unadjusted, allocated_size);
|
2017-10-03 05:32:58 +08:00
|
|
|
if (adjusted == nullptr) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
luaL_error(L, "cannot properly align memory for '%s'", detail::demangle<T>().data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return static_cast<T*>(adjusted);
|
2016-08-24 09:42:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2021-01-21 02:55:44 +08:00
|
|
|
int usertype_alloc_destroy(lua_State* L) noexcept {
|
2017-10-03 05:32:58 +08:00
|
|
|
void* memory = lua_touserdata(L, 1);
|
|
|
|
memory = align_usertype_pointer(memory);
|
|
|
|
T** pdata = static_cast<T**>(memory);
|
2016-08-24 09:42:27 +08:00
|
|
|
T* data = *pdata;
|
2020-03-31 12:22:46 +08:00
|
|
|
std::allocator<T> alloc {};
|
2017-12-07 21:24:50 +08:00
|
|
|
std::allocator_traits<std::allocator<T>>::destroy(alloc, data);
|
2016-08-24 09:42:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-22 03:25:43 +08:00
|
|
|
template <typename T>
|
2021-01-21 02:55:44 +08:00
|
|
|
int unique_destroy(lua_State* L) noexcept {
|
2017-10-03 05:32:58 +08:00
|
|
|
void* memory = lua_touserdata(L, 1);
|
|
|
|
memory = align_usertype_unique_destructor(memory);
|
|
|
|
unique_destructor& dx = *static_cast<unique_destructor*>(memory);
|
2018-06-16 01:19:09 +08:00
|
|
|
memory = align_usertype_unique_tag<true>(memory);
|
2017-10-03 05:32:58 +08:00
|
|
|
(dx)(memory);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2021-01-21 02:55:44 +08:00
|
|
|
int user_alloc_destroy(lua_State* L) noexcept {
|
2017-10-03 05:32:58 +08:00
|
|
|
void* memory = lua_touserdata(L, 1);
|
2021-01-04 18:52:30 +08:00
|
|
|
void* aligned_memory = align_user<T>(memory);
|
|
|
|
T* typed_memory = static_cast<T*>(aligned_memory);
|
2017-10-03 05:32:58 +08:00
|
|
|
std::allocator<T> alloc;
|
2021-01-04 18:52:30 +08:00
|
|
|
std::allocator_traits<std::allocator<T>>::destroy(alloc, typed_memory);
|
2017-10-03 05:32:58 +08:00
|
|
|
return 0;
|
2017-08-22 03:25:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Real>
|
2019-05-21 14:13:56 +08:00
|
|
|
void usertype_unique_alloc_destroy(void* memory) {
|
2021-01-04 18:52:30 +08:00
|
|
|
void* aligned_memory = align_usertype_unique<Real, true>(memory);
|
|
|
|
Real* typed_memory = static_cast<Real*>(aligned_memory);
|
2017-08-22 03:25:43 +08:00
|
|
|
std::allocator<Real> alloc;
|
2021-01-04 18:52:30 +08:00
|
|
|
std::allocator_traits<std::allocator<Real>>::destroy(alloc, typed_memory);
|
2017-08-22 03:25:43 +08:00
|
|
|
}
|
|
|
|
|
2017-10-03 05:32:58 +08:00
|
|
|
template <typename T>
|
2021-01-21 02:55:44 +08:00
|
|
|
int cannot_destroy(lua_State* L) {
|
2018-12-20 12:17:15 +08:00
|
|
|
return luaL_error(L,
|
|
|
|
"cannot call the destructor for '%s': it is either hidden (protected/private) or removed with '= "
|
2021-01-21 02:55:44 +08:00
|
|
|
"delete' and thusly this type is being destroyed without properly destroying, invoking undefined "
|
2018-12-20 12:17:15 +08:00
|
|
|
"behavior: please bind a usertype and specify a custom destructor to define the behavior properly",
|
|
|
|
detail::demangle<T>().data());
|
2017-10-03 05:32:58 +08:00
|
|
|
}
|
|
|
|
|
2016-08-20 09:08:03 +08:00
|
|
|
template <typename T>
|
2017-09-13 14:46:56 +08:00
|
|
|
void reserve(T&, std::size_t) {
|
|
|
|
}
|
2016-08-20 09:08:03 +08:00
|
|
|
|
|
|
|
template <typename T, typename Al>
|
2019-03-23 04:28:17 +08:00
|
|
|
void reserve(std::vector<T, Al>& vec, std::size_t hint) {
|
|
|
|
vec.reserve(hint);
|
2016-08-20 09:08:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Tr, typename Al>
|
2019-03-23 04:28:17 +08:00
|
|
|
void reserve(std::basic_string<T, Tr, Al>& str, std::size_t hint) {
|
|
|
|
str.reserve(hint);
|
2016-08-20 09:08:03 +08:00
|
|
|
}
|
2018-11-19 02:06:01 +08:00
|
|
|
|
|
|
|
inline bool property_always_true(meta_function) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct properties_enrollment_allowed {
|
2019-05-30 05:14:35 +08:00
|
|
|
int& times_through;
|
2018-11-19 02:06:01 +08:00
|
|
|
std::bitset<64>& properties;
|
|
|
|
automagic_enrollments& enrollments;
|
|
|
|
|
2021-01-21 16:26:42 +08:00
|
|
|
properties_enrollment_allowed(int& times_through_, std::bitset<64>& properties_, automagic_enrollments& enrollments_)
|
|
|
|
: times_through(times_through_), properties(properties_), enrollments(enrollments_) {
|
2018-11-19 02:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(meta_function mf) const {
|
2020-12-17 04:25:35 +08:00
|
|
|
bool p = properties[static_cast<std::size_t>(mf)];
|
2019-05-30 05:14:35 +08:00
|
|
|
if (times_through > 0) {
|
|
|
|
return p;
|
|
|
|
}
|
2018-11-19 02:06:01 +08:00
|
|
|
switch (mf) {
|
|
|
|
case meta_function::length:
|
|
|
|
return enrollments.length_operator && !p;
|
|
|
|
case meta_function::pairs:
|
|
|
|
return enrollments.pairs_operator && !p;
|
|
|
|
case meta_function::call:
|
|
|
|
return enrollments.call_operator && !p;
|
|
|
|
case meta_function::less_than:
|
|
|
|
return enrollments.less_than_operator && !p;
|
|
|
|
case meta_function::less_than_or_equal_to:
|
|
|
|
return enrollments.less_than_or_equal_to_operator && !p;
|
|
|
|
case meta_function::equal_to:
|
|
|
|
return enrollments.equal_to_operator && !p;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return !p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct indexed_insert {
|
2021-01-04 18:52:30 +08:00
|
|
|
lua_reg_table& registration_table;
|
2018-11-19 02:06:01 +08:00
|
|
|
int& index;
|
|
|
|
|
2021-01-04 18:52:30 +08:00
|
|
|
indexed_insert(lua_reg_table& registration_table_, int& index_ref_) : registration_table(registration_table_), index(index_ref_) {
|
2018-11-19 02:06:01 +08:00
|
|
|
}
|
2021-01-04 18:52:30 +08:00
|
|
|
void operator()(meta_function meta_function_name_, lua_CFunction c_function_) {
|
|
|
|
registration_table[index] = luaL_Reg { to_string(meta_function_name_).c_str(), c_function_ };
|
2018-11-19 02:06:01 +08:00
|
|
|
++index;
|
|
|
|
}
|
|
|
|
};
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace detail
|
2016-06-17 08:15:56 +08:00
|
|
|
|
|
|
|
namespace stack {
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, bool global = false, bool raw = false, typename = void>
|
2016-06-17 08:15:56 +08:00
|
|
|
struct field_getter;
|
2018-05-10 19:30:51 +08:00
|
|
|
template <typename T, typename P, bool global = false, bool raw = false, typename = void>
|
2016-06-17 08:15:56 +08:00
|
|
|
struct probe_field_getter;
|
2019-03-10 23:09:52 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, bool global = false, bool raw = false, typename = void>
|
2016-06-17 08:15:56 +08:00
|
|
|
struct field_setter;
|
2019-03-10 23:09:52 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename = void>
|
2018-12-20 12:17:15 +08:00
|
|
|
struct unqualified_getter;
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename = void>
|
2018-06-16 01:19:09 +08:00
|
|
|
struct qualified_getter;
|
2019-03-10 23:09:52 +08:00
|
|
|
|
2018-06-16 01:19:09 +08:00
|
|
|
template <typename T, typename = void>
|
2019-02-14 15:40:57 +08:00
|
|
|
struct qualified_interop_getter;
|
|
|
|
template <typename T, typename = void>
|
|
|
|
struct unqualified_interop_getter;
|
2019-03-10 23:09:52 +08:00
|
|
|
|
2017-09-24 08:26:03 +08:00
|
|
|
template <typename T, typename = void>
|
2016-06-17 08:15:56 +08:00
|
|
|
struct popper;
|
2019-03-10 23:09:52 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename = void>
|
2018-12-23 04:36:42 +08:00
|
|
|
struct unqualified_pusher;
|
2019-03-10 23:09:52 +08:00
|
|
|
|
2019-03-18 19:41:51 +08:00
|
|
|
template <typename T, type t, typename = void>
|
2018-12-21 14:07:05 +08:00
|
|
|
struct unqualified_checker;
|
2019-03-18 19:41:51 +08:00
|
|
|
template <typename T, type t, typename = void>
|
2018-06-16 01:19:09 +08:00
|
|
|
struct qualified_checker;
|
2019-03-10 23:09:52 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename = void>
|
2018-12-23 04:36:42 +08:00
|
|
|
struct unqualified_check_getter;
|
2018-06-16 01:19:09 +08:00
|
|
|
template <typename T, typename = void>
|
|
|
|
struct qualified_check_getter;
|
2016-06-17 08:15:56 +08:00
|
|
|
|
|
|
|
struct probe {
|
|
|
|
bool success;
|
|
|
|
int levels;
|
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
probe(bool s, int l) : success(s), levels(l) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2016-06-17 08:15:56 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
operator bool() const {
|
|
|
|
return success;
|
|
|
|
};
|
2016-06-17 08:15:56 +08:00
|
|
|
};
|
|
|
|
|
2016-07-29 12:57:47 +08:00
|
|
|
struct record {
|
|
|
|
int last;
|
|
|
|
int used;
|
|
|
|
|
2020-09-26 21:46:11 +08:00
|
|
|
record() noexcept : last(), used() {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2020-09-26 21:46:11 +08:00
|
|
|
void use(int count) noexcept {
|
2016-07-29 12:57:47 +08:00
|
|
|
last = count;
|
|
|
|
used += count;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-26 21:46:11 +08:00
|
|
|
namespace stack_detail {
|
|
|
|
template <typename Function>
|
|
|
|
Function* get_function_pointer(lua_State*, int, record&) noexcept;
|
|
|
|
template <typename Function, typename Handler>
|
|
|
|
bool check_function_pointer(lua_State* L, int index, Handler&& handler, record& tracking) noexcept;
|
|
|
|
} // namespace stack_detail
|
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
} // namespace stack
|
|
|
|
|
|
|
|
namespace meta { namespace meta_detail {
|
2019-02-14 15:40:57 +08:00
|
|
|
template <typename T>
|
2019-03-10 09:57:49 +08:00
|
|
|
using adl_sol_lua_get_test_t = decltype(sol_lua_get(types<T>(), static_cast<lua_State*>(nullptr), -1, std::declval<stack::record&>()));
|
2019-02-14 15:40:57 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename T>
|
2019-03-10 09:57:49 +08:00
|
|
|
using adl_sol_lua_interop_get_test_t
|
|
|
|
= decltype(sol_lua_interop_get(types<T>(), static_cast<lua_State*>(nullptr), -1, static_cast<void*>(nullptr), std::declval<stack::record&>()));
|
2018-12-20 12:17:15 +08:00
|
|
|
|
2019-02-14 15:40:57 +08:00
|
|
|
template <typename T>
|
2021-01-04 18:52:30 +08:00
|
|
|
using adl_sol_lua_check_test_t = decltype(sol_lua_check(types<T>(), static_cast<lua_State*>(nullptr), -1, &no_panic, std::declval<stack::record&>()));
|
2019-02-14 15:40:57 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename T>
|
2019-03-10 09:57:49 +08:00
|
|
|
using adl_sol_lua_interop_check_test_t
|
2021-01-04 18:52:30 +08:00
|
|
|
= decltype(sol_lua_interop_check(types<T>(), static_cast<lua_State*>(nullptr), -1, type::none, &no_panic, std::declval<stack::record&>()));
|
2018-12-20 12:17:15 +08:00
|
|
|
|
2019-03-10 09:57:49 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
using adl_sol_lua_check_get_test_t
|
2021-01-04 18:52:30 +08:00
|
|
|
= decltype(sol_lua_check_get(types<T>(), static_cast<lua_State*>(nullptr), -1, &no_panic, std::declval<stack::record&>()));
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
template <typename... Args>
|
2019-03-10 09:57:49 +08:00
|
|
|
using adl_sol_lua_push_test_t = decltype(sol_lua_push(static_cast<lua_State*>(nullptr), std::declval<Args>()...));
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
2019-03-10 09:57:49 +08:00
|
|
|
using adl_sol_lua_push_exact_test_t = decltype(sol_lua_push(types<T>(), static_cast<lua_State*>(nullptr), std::declval<Args>()...));
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2019-03-10 09:57:49 +08:00
|
|
|
inline constexpr bool is_adl_sol_lua_get_v = meta::is_detected_v<adl_sol_lua_get_test_t, T>;
|
2018-12-20 12:17:15 +08:00
|
|
|
|
2019-02-14 15:40:57 +08:00
|
|
|
template <typename T>
|
2019-03-10 09:57:49 +08:00
|
|
|
inline constexpr bool is_adl_sol_lua_interop_get_v = meta::is_detected_v<adl_sol_lua_interop_get_test_t, T>;
|
2019-02-14 15:40:57 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename T>
|
2019-03-10 09:57:49 +08:00
|
|
|
inline constexpr bool is_adl_sol_lua_check_v = meta::is_detected_v<adl_sol_lua_check_test_t, T>;
|
2018-12-20 12:17:15 +08:00
|
|
|
|
2019-02-14 15:40:57 +08:00
|
|
|
template <typename T>
|
2019-03-10 09:57:49 +08:00
|
|
|
inline constexpr bool is_adl_sol_lua_interop_check_v = meta::is_detected_v<adl_sol_lua_interop_check_test_t, T>;
|
2019-02-14 15:40:57 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename T>
|
2019-03-10 09:57:49 +08:00
|
|
|
inline constexpr bool is_adl_sol_lua_check_get_v = meta::is_detected_v<adl_sol_lua_check_get_test_t, T>;
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
template <typename... Args>
|
2019-03-10 09:57:49 +08:00
|
|
|
inline constexpr bool is_adl_sol_lua_push_v = meta::is_detected_v<adl_sol_lua_push_test_t, Args...>;
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
2019-03-10 09:57:49 +08:00
|
|
|
inline constexpr bool is_adl_sol_lua_push_exact_v = meta::is_detected_v<adl_sol_lua_push_exact_test_t, T, Args...>;
|
2018-12-20 12:17:15 +08:00
|
|
|
}} // namespace meta::meta_detail
|
|
|
|
|
|
|
|
|
|
|
|
namespace stack {
|
2016-06-17 08:15:56 +08:00
|
|
|
namespace stack_detail {
|
2018-12-20 12:17:15 +08:00
|
|
|
constexpr const char* not_enough_stack_space = "not enough space left on Lua stack";
|
|
|
|
constexpr const char* not_enough_stack_space_floating = "not enough space left on Lua stack for a floating point number";
|
|
|
|
constexpr const char* not_enough_stack_space_integral = "not enough space left on Lua stack for an integral number";
|
|
|
|
constexpr const char* not_enough_stack_space_string = "not enough space left on Lua stack for a string";
|
|
|
|
constexpr const char* not_enough_stack_space_meta_function_name = "not enough space left on Lua stack for the name of a meta_function";
|
2021-03-06 23:14:48 +08:00
|
|
|
constexpr const char* not_enough_stack_space_userdata = "not enough space left on Lua stack to create a sol2 userdata";
|
2018-12-20 12:17:15 +08:00
|
|
|
constexpr const char* not_enough_stack_space_generic = "not enough space left on Lua stack to push valuees";
|
|
|
|
constexpr const char* not_enough_stack_space_environment = "not enough space left on Lua stack to retrieve environment";
|
|
|
|
|
2016-06-17 08:15:56 +08:00
|
|
|
template <typename T>
|
|
|
|
struct strip {
|
|
|
|
typedef T type;
|
|
|
|
};
|
|
|
|
template <typename T>
|
|
|
|
struct strip<std::reference_wrapper<T>> {
|
|
|
|
typedef T& type;
|
|
|
|
};
|
|
|
|
template <typename T>
|
2016-07-08 04:52:39 +08:00
|
|
|
struct strip<user<T>> {
|
|
|
|
typedef T& type;
|
|
|
|
};
|
|
|
|
template <typename T>
|
2016-06-17 08:15:56 +08:00
|
|
|
struct strip<non_null<T>> {
|
|
|
|
typedef T type;
|
|
|
|
};
|
|
|
|
template <typename T>
|
|
|
|
using strip_t = typename strip<T>::type;
|
2017-05-29 22:32:37 +08:00
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
template <typename C>
|
2019-03-10 09:57:49 +08:00
|
|
|
static int get_size_hint(C& c) {
|
2017-08-06 07:20:28 +08:00
|
|
|
return static_cast<int>(c.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename V, typename Al>
|
2017-08-07 14:07:21 +08:00
|
|
|
static int get_size_hint(const std::forward_list<V, Al>&) {
|
2017-08-06 07:20:28 +08:00
|
|
|
// forward_list makes me sad
|
|
|
|
return static_cast<int>(32);
|
|
|
|
}
|
|
|
|
|
2018-06-16 01:19:09 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) unchecked_unqualified_get(lua_State* L, int index, record& tracking) {
|
2018-12-20 12:17:15 +08:00
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_get_v<Tu>) {
|
|
|
|
return sol_lua_get(types<Tu>(), L, index, tracking);
|
|
|
|
}
|
|
|
|
else {
|
2020-03-31 12:22:46 +08:00
|
|
|
unqualified_getter<Tu> g {};
|
2018-12-20 12:17:15 +08:00
|
|
|
(void)g;
|
|
|
|
return g.get(L, index, tracking);
|
|
|
|
}
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) unchecked_get(lua_State* L, int index, record& tracking) {
|
2018-12-20 12:17:15 +08:00
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_get_v<T>) {
|
|
|
|
return sol_lua_get(types<T>(), L, index, tracking);
|
|
|
|
}
|
|
|
|
else {
|
2020-03-31 12:22:46 +08:00
|
|
|
qualified_getter<T> g {};
|
2018-12-20 12:17:15 +08:00
|
|
|
(void)g;
|
|
|
|
return g.get(L, index, tracking);
|
|
|
|
}
|
2017-12-11 04:56:49 +08:00
|
|
|
}
|
2019-02-14 15:40:57 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) unqualified_interop_get(lua_State* L, int index, void* unadjusted_pointer, record& tracking) {
|
2019-02-14 15:40:57 +08:00
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_interop_get_v<Tu>) {
|
|
|
|
return sol_lua_interop_get(types<Tu>(), L, index, unadjusted_pointer, tracking);
|
|
|
|
}
|
|
|
|
else {
|
2019-03-10 09:57:49 +08:00
|
|
|
(void)L;
|
|
|
|
(void)index;
|
|
|
|
(void)unadjusted_pointer;
|
|
|
|
(void)tracking;
|
2019-03-10 23:09:52 +08:00
|
|
|
using Ti = stack_detail::strip_t<Tu>;
|
2020-03-31 12:22:46 +08:00
|
|
|
return std::pair<bool, Ti*> { false, nullptr };
|
2019-02-14 15:40:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) interop_get(lua_State* L, int index, void* unadjusted_pointer, record& tracking) {
|
2019-02-14 15:40:57 +08:00
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_interop_get_v<T>) {
|
|
|
|
return sol_lua_interop_get(types<T>(), L, index, unadjusted_pointer, tracking);
|
|
|
|
}
|
|
|
|
else {
|
2019-03-10 09:57:49 +08:00
|
|
|
return unqualified_interop_get<T>(L, index, unadjusted_pointer, tracking);
|
2019-02-14 15:40:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Handler>
|
|
|
|
bool unqualified_interop_check(lua_State* L, int index, type index_type, Handler&& handler, record& tracking) {
|
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_interop_check_v<Tu>) {
|
|
|
|
return sol_lua_interop_check(types<Tu>(), L, index, index_type, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
|
|
|
else {
|
2019-03-10 09:57:49 +08:00
|
|
|
(void)L;
|
|
|
|
(void)index;
|
|
|
|
(void)index_type;
|
|
|
|
(void)handler;
|
|
|
|
(void)tracking;
|
|
|
|
return false;
|
2019-02-14 15:40:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Handler>
|
|
|
|
bool interop_check(lua_State* L, int index, type index_type, Handler&& handler, record& tracking) {
|
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_interop_check_v<T>) {
|
|
|
|
return sol_lua_interop_check(types<T>(), L, index, index_type, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
|
|
|
else {
|
2019-03-10 09:57:49 +08:00
|
|
|
return unqualified_interop_check<T>(L, index, index_type, std::forward<Handler>(handler), tracking);
|
2019-02-14 15:40:57 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-21 14:13:56 +08:00
|
|
|
|
2019-03-23 04:28:17 +08:00
|
|
|
using undefined_method_func = void (*)(stack_reference);
|
|
|
|
|
|
|
|
struct undefined_metatable {
|
|
|
|
lua_State* L;
|
|
|
|
const char* key;
|
|
|
|
undefined_method_func on_new_table;
|
|
|
|
|
|
|
|
undefined_metatable(lua_State* l, const char* k, undefined_method_func umf) : L(l), key(k), on_new_table(umf) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()() const {
|
|
|
|
if (luaL_newmetatable(L, key) == 1) {
|
|
|
|
on_new_table(stack_reference(L, -1));
|
|
|
|
}
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}
|
|
|
|
};
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace stack_detail
|
2016-06-17 08:15:56 +08:00
|
|
|
|
|
|
|
inline bool maybe_indexable(lua_State* L, int index = -1) {
|
|
|
|
type t = type_of(L, index);
|
|
|
|
return t == type::userdata || t == type::table;
|
|
|
|
}
|
|
|
|
|
2017-08-06 07:20:28 +08:00
|
|
|
inline int top(lua_State* L) {
|
|
|
|
return lua_gettop(L);
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:33:27 +08:00
|
|
|
inline bool is_main_thread(lua_State* L) {
|
|
|
|
int ismainthread = lua_pushthread(L);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return ismainthread == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void coroutine_create_guard(lua_State* L) {
|
|
|
|
if (is_main_thread(L)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int stacksize = lua_gettop(L);
|
|
|
|
if (stacksize < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type_of(L, 1) != type::function) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// well now we're screwed...
|
|
|
|
// we can clean the stack and pray it doesn't destroy anything?
|
|
|
|
lua_pop(L, stacksize);
|
|
|
|
}
|
|
|
|
|
2019-03-23 04:28:17 +08:00
|
|
|
inline void clear(lua_State* L, int table_index) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, table_index) != 0) {
|
|
|
|
// remove value
|
|
|
|
lua_pop(L, 1);
|
|
|
|
// duplicate key to protect form rawset
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
// push new value
|
|
|
|
lua_pushnil(L);
|
|
|
|
// table_index%[key] = nil
|
|
|
|
lua_rawset(L, table_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void clear(reference& r) {
|
|
|
|
auto pp = push_pop<false>(r);
|
|
|
|
int stack_index = pp.index_of(r);
|
|
|
|
clear(r.lua_state(), stack_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void clear(stack_reference& r) {
|
|
|
|
clear(r.lua_state(), r.stack_index());
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
inline void clear(lua_State* L_, stateless_reference& r) {
|
|
|
|
r.push(L_);
|
|
|
|
int stack_index = absolute_index(L_, -1);
|
|
|
|
clear(L_, stack_index);
|
|
|
|
r.pop(L_);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void clear(lua_State* L_, stateless_stack_reference& r) {
|
|
|
|
clear(L_, r.stack_index());
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename... Args>
|
2019-05-21 14:13:56 +08:00
|
|
|
int push(lua_State* L, T&& t, Args&&... args) {
|
2018-12-20 12:17:15 +08:00
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_push_exact_v<T, T, Args...>) {
|
|
|
|
return sol_lua_push(types<T>(), L, std::forward<T>(t), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
else if constexpr (meta::meta_detail::is_adl_sol_lua_push_exact_v<Tu, T, Args...>) {
|
|
|
|
return sol_lua_push(types<Tu>(), L, std::forward<T>(t), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
else if constexpr (meta::meta_detail::is_adl_sol_lua_push_v<T, Args...>) {
|
|
|
|
return sol_lua_push(L, std::forward<T>(t), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
else {
|
2020-03-31 12:22:46 +08:00
|
|
|
unqualified_pusher<Tu> p {};
|
2018-12-20 12:17:15 +08:00
|
|
|
(void)p;
|
|
|
|
return p.push(L, std::forward<T>(t), std::forward<Args>(args)...);
|
|
|
|
}
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// overload allows to use a pusher of a specific type, but pass in any kind of args
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename Arg, typename... Args, typename = std::enable_if_t<!std::is_same<T, Arg>::value>>
|
2019-05-21 14:13:56 +08:00
|
|
|
int push(lua_State* L, Arg&& arg, Args&&... args) {
|
2018-12-20 12:17:15 +08:00
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_push_exact_v<T, Arg, Args...>) {
|
|
|
|
return sol_lua_push(types<T>(), L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
else if constexpr (meta::meta_detail::is_adl_sol_lua_push_exact_v<Tu, Arg, Args...>) {
|
|
|
|
return sol_lua_push(types<Tu>(), L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
|
|
|
}
|
2021-02-14 13:28:10 +08:00
|
|
|
else if constexpr (meta::meta_detail::is_adl_sol_lua_push_v<Arg, Args...> && !detail::is_tagged_v<Tu>) {
|
2018-12-20 12:17:15 +08:00
|
|
|
return sol_lua_push(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
else {
|
2020-03-31 12:22:46 +08:00
|
|
|
unqualified_pusher<Tu> p {};
|
2018-12-20 12:17:15 +08:00
|
|
|
(void)p;
|
|
|
|
return p.push(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
|
|
|
}
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:22:52 +08:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
int push_userdata(lua_State* L, T&& t, Args&&... args) {
|
|
|
|
using U = meta::unqualified_t<T>;
|
2020-01-25 08:58:12 +08:00
|
|
|
using Tr = meta::conditional_t<std::is_pointer_v<U>,
|
2020-03-31 12:22:46 +08:00
|
|
|
detail::as_pointer_tag<std::remove_pointer_t<U>>,
|
|
|
|
meta::conditional_t<is_unique_usertype_v<U>, detail::as_unique_tag<U>, detail::as_value_tag<U>>>;
|
2020-01-17 18:22:52 +08:00
|
|
|
return stack::push<Tr>(L, std::forward<T>(t), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Arg, typename... Args>
|
|
|
|
int push_userdata(lua_State* L, Arg&& arg, Args&&... args) {
|
|
|
|
using U = meta::unqualified_t<T>;
|
2020-01-25 08:58:12 +08:00
|
|
|
using Tr = meta::conditional_t<std::is_pointer_v<U>,
|
2020-03-31 12:22:46 +08:00
|
|
|
detail::as_pointer_tag<std::remove_pointer_t<U>>,
|
|
|
|
meta::conditional_t<is_unique_usertype_v<U>, detail::as_unique_tag<U>, detail::as_value_tag<U>>>;
|
2020-01-17 18:22:52 +08:00
|
|
|
return stack::push<Tr>(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
namespace stack_detail {
|
|
|
|
|
|
|
|
template <typename T, typename Arg, typename... Args>
|
2019-05-21 14:13:56 +08:00
|
|
|
int push_reference(lua_State* L, Arg&& arg, Args&&... args) {
|
2020-12-17 04:25:35 +08:00
|
|
|
// clang-format off
|
|
|
|
using use_reference_tag =
|
|
|
|
meta::all<
|
|
|
|
meta::neg<is_value_semantic_for_function<T>>
|
|
|
|
#if SOL_IS_OFF(SOL_FUNCTION_CALL_VALUE_SEMANTICS_I_)
|
|
|
|
, std::is_lvalue_reference<T>,
|
|
|
|
meta::neg<std::is_const<std::remove_reference_t<T>>>,
|
|
|
|
meta::neg<is_lua_primitive<meta::unqualified_t<T>>>,
|
|
|
|
meta::neg<is_unique_usertype<meta::unqualified_t<T>>>
|
|
|
|
#endif
|
|
|
|
>;
|
|
|
|
// clang-format on
|
2018-12-20 12:17:15 +08:00
|
|
|
using Tr = meta::conditional_t<use_reference_tag::value, detail::as_reference_tag, meta::unqualified_t<T>>;
|
|
|
|
return stack::push<Tr>(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace stack_detail
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename... Args>
|
2019-05-21 14:13:56 +08:00
|
|
|
int push_reference(lua_State* L, T&& t, Args&&... args) {
|
2017-05-29 22:32:37 +08:00
|
|
|
return stack_detail::push_reference<T>(L, std::forward<T>(t), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename Arg, typename... Args>
|
2019-05-21 14:13:56 +08:00
|
|
|
int push_reference(lua_State* L, Arg&& arg, Args&&... args) {
|
2017-05-29 22:32:37 +08:00
|
|
|
return stack_detail::push_reference<T>(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int multi_push(lua_State*) {
|
|
|
|
// do nothing
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename... Args>
|
2019-05-21 14:13:56 +08:00
|
|
|
int multi_push(lua_State* L, T&& t, Args&&... args) {
|
2016-06-17 08:15:56 +08:00
|
|
|
int pushcount = push(L, std::forward<T>(t));
|
2020-03-31 12:22:46 +08:00
|
|
|
void(detail::swallow { (pushcount += stack::push(L, std::forward<Args>(args)), 0)... });
|
2016-06-17 08:15:56 +08:00
|
|
|
return pushcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int multi_push_reference(lua_State*) {
|
|
|
|
// do nothing
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename... Args>
|
2019-05-21 14:13:56 +08:00
|
|
|
int multi_push_reference(lua_State* L, T&& t, Args&&... args) {
|
2020-12-17 04:25:35 +08:00
|
|
|
int pushcount = stack::push_reference(L, std::forward<T>(t));
|
2020-03-31 12:22:46 +08:00
|
|
|
void(detail::swallow { (pushcount += stack::push_reference(L, std::forward<Args>(args)), 0)... });
|
2016-06-17 08:15:56 +08:00
|
|
|
return pushcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Handler>
|
2019-02-14 15:40:57 +08:00
|
|
|
bool unqualified_check(lua_State* L, int index, Handler&& handler, record& tracking) {
|
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_check_v<Tu>) {
|
|
|
|
return sol_lua_check(types<Tu>(), L, index, std::forward<Handler>(handler), tracking);
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
|
|
|
else {
|
2019-03-18 19:41:51 +08:00
|
|
|
unqualified_checker<Tu, lua_type_of_v<Tu>> c;
|
2018-12-20 12:17:15 +08:00
|
|
|
// VC++ has a bad warning here: shut it up
|
|
|
|
(void)c;
|
|
|
|
return c.check(L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
2016-07-29 12:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Handler>
|
2019-02-14 15:40:57 +08:00
|
|
|
bool unqualified_check(lua_State* L, int index, Handler&& handler) {
|
2020-03-31 12:22:46 +08:00
|
|
|
record tracking {};
|
2019-02-14 15:40:57 +08:00
|
|
|
return unqualified_check<T>(L, index, std::forward<Handler>(handler), tracking);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-02-14 15:40:57 +08:00
|
|
|
bool unqualified_check(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
|
2021-01-04 18:52:30 +08:00
|
|
|
auto handler = &no_panic;
|
2019-02-14 15:40:57 +08:00
|
|
|
return unqualified_check<T>(L, index, handler);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 01:19:09 +08:00
|
|
|
template <typename T, typename Handler>
|
2019-02-14 15:40:57 +08:00
|
|
|
bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
|
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_check_v<T>) {
|
|
|
|
return sol_lua_check(types<T>(), L, index, std::forward<Handler>(handler), tracking);
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
|
|
|
else {
|
2019-03-18 19:41:51 +08:00
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
qualified_checker<T, lua_type_of_v<Tu>> c;
|
2018-12-20 12:17:15 +08:00
|
|
|
// VC++ has a bad warning here: shut it up
|
|
|
|
(void)c;
|
|
|
|
return c.check(L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Handler>
|
2019-02-14 15:40:57 +08:00
|
|
|
bool check(lua_State* L, int index, Handler&& handler) {
|
2020-03-31 12:22:46 +08:00
|
|
|
record tracking {};
|
2019-02-14 15:40:57 +08:00
|
|
|
return check<T>(L, index, std::forward<Handler>(handler), tracking);
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-02-14 15:40:57 +08:00
|
|
|
bool check(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
|
2021-01-04 18:52:30 +08:00
|
|
|
auto handler = &no_panic;
|
2019-02-14 15:40:57 +08:00
|
|
|
return check<T>(L, index, handler);
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename T, typename Handler>
|
2019-03-23 04:28:17 +08:00
|
|
|
bool check_usertype(lua_State* L, int index, type, Handler&& handler, record& tracking) {
|
2018-12-20 12:17:15 +08:00
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
using detail_t = meta::conditional_t<std::is_pointer_v<T>, detail::as_pointer_tag<Tu>, detail::as_value_tag<Tu>>;
|
|
|
|
return check<detail_t>(L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
|
|
|
|
2017-12-11 04:56:49 +08:00
|
|
|
template <typename T, typename Handler>
|
|
|
|
bool check_usertype(lua_State* L, int index, Handler&& handler, record& tracking) {
|
2018-12-20 12:17:15 +08:00
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
using detail_t = meta::conditional_t<std::is_pointer_v<T>, detail::as_pointer_tag<Tu>, detail::as_value_tag<Tu>>;
|
|
|
|
return check<detail_t>(L, index, std::forward<Handler>(handler), tracking);
|
2017-12-11 04:56:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Handler>
|
|
|
|
bool check_usertype(lua_State* L, int index, Handler&& handler) {
|
2020-03-31 12:22:46 +08:00
|
|
|
record tracking {};
|
2017-12-11 04:56:49 +08:00
|
|
|
return check_usertype<T>(L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool check_usertype(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
|
2021-01-04 18:52:30 +08:00
|
|
|
auto handler = &no_panic;
|
2017-12-11 04:56:49 +08:00
|
|
|
return check_usertype<T>(L, index, handler);
|
|
|
|
}
|
|
|
|
|
2018-06-16 01:19:09 +08:00
|
|
|
template <typename T, typename Handler>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) unqualified_check_get(lua_State* L, int index, Handler&& handler, record& tracking) {
|
2018-12-20 12:17:15 +08:00
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_check_get_v<T>) {
|
|
|
|
return sol_lua_check_get(types<T>(), L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
|
|
|
else if constexpr (meta::meta_detail::is_adl_sol_lua_check_get_v<Tu>) {
|
|
|
|
return sol_lua_check_get(types<Tu>(), L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
|
|
|
else {
|
2020-03-31 12:22:46 +08:00
|
|
|
unqualified_check_getter<Tu> cg {};
|
2018-12-20 12:17:15 +08:00
|
|
|
(void)cg;
|
|
|
|
return cg.get(L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Handler>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) unqualified_check_get(lua_State* L, int index, Handler&& handler) {
|
2020-03-31 12:22:46 +08:00
|
|
|
record tracking {};
|
2018-06-16 01:19:09 +08:00
|
|
|
return unqualified_check_get<T>(L, index, handler, tracking);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) unqualified_check_get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
|
2021-01-04 18:52:30 +08:00
|
|
|
auto handler = &no_panic;
|
2018-06-16 01:19:09 +08:00
|
|
|
return unqualified_check_get<T>(L, index, handler);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename Handler>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) check_get(lua_State* L, int index, Handler&& handler, record& tracking) {
|
2018-12-20 12:17:15 +08:00
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_check_get_v<T>) {
|
|
|
|
return sol_lua_check_get(types<T>(), L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
|
|
|
else {
|
2020-03-31 12:22:46 +08:00
|
|
|
qualified_check_getter<T> cg {};
|
2018-12-20 12:17:15 +08:00
|
|
|
(void)cg;
|
|
|
|
return cg.get(L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
2016-07-29 12:57:47 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename Handler>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) check_get(lua_State* L, int index, Handler&& handler) {
|
2020-03-31 12:22:46 +08:00
|
|
|
record tracking {};
|
2016-07-29 12:57:47 +08:00
|
|
|
return check_get<T>(L, index, handler, tracking);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) check_get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
|
2021-01-04 18:52:30 +08:00
|
|
|
auto handler = &no_panic;
|
2016-06-17 08:15:56 +08:00
|
|
|
return check_get<T>(L, index, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace stack_detail {
|
|
|
|
|
2019-03-18 19:41:51 +08:00
|
|
|
template <typename Handler>
|
2019-05-21 14:13:56 +08:00
|
|
|
bool check_types(lua_State*, int, Handler&&, record&) {
|
2019-03-18 19:41:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-10 23:09:52 +08:00
|
|
|
template <typename T, typename... Args, typename Handler>
|
2019-05-21 14:13:56 +08:00
|
|
|
bool check_types(lua_State* L, int firstargument, Handler&& handler, record& tracking) {
|
2019-03-10 23:09:52 +08:00
|
|
|
if (!stack::check<T>(L, firstargument + tracking.used, handler, tracking))
|
|
|
|
return false;
|
|
|
|
return check_types<Args...>(L, firstargument, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
2016-07-29 12:57:47 +08:00
|
|
|
|
2019-03-10 23:09:52 +08:00
|
|
|
template <typename... Args, typename Handler>
|
2019-05-21 14:13:56 +08:00
|
|
|
bool check_types(types<Args...>, lua_State* L, int index, Handler&& handler, record& tracking) {
|
2019-03-10 23:09:52 +08:00
|
|
|
return check_types<Args...>(L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
2019-02-14 15:40:57 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace stack_detail
|
2016-06-17 08:15:56 +08:00
|
|
|
|
2016-07-29 12:57:47 +08:00
|
|
|
template <typename... Args, typename Handler>
|
|
|
|
bool multi_check(lua_State* L, int index, Handler&& handler, record& tracking) {
|
2019-03-10 23:09:52 +08:00
|
|
|
return stack_detail::check_types<Args...>(L, index, std::forward<Handler>(handler), tracking);
|
2016-07-29 12:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args, typename Handler>
|
|
|
|
bool multi_check(lua_State* L, int index, Handler&& handler) {
|
2020-03-31 12:22:46 +08:00
|
|
|
record tracking {};
|
2019-03-10 23:09:52 +08:00
|
|
|
return multi_check<Args...>(L, index, std::forward<Handler>(handler), tracking);
|
2016-07-29 12:57:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
bool multi_check(lua_State* L, int index) {
|
2019-03-10 23:09:52 +08:00
|
|
|
return multi_check<Args...>(L, index);
|
2016-07-29 12:57:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-11 04:56:49 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
auto unqualified_get(lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_unqualified_get<T>(L, index, tracking)) {
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_SAFE_GETTER_I_)
|
2020-03-31 12:22:46 +08:00
|
|
|
static constexpr bool is_op = meta::is_optional_v<T>;
|
2018-12-20 12:17:15 +08:00
|
|
|
if constexpr (is_op) {
|
|
|
|
return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (is_lua_reference<T>::value) {
|
|
|
|
return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
|
|
|
|
}
|
|
|
|
auto op = unqualified_check_get<T>(L, index, type_panic_c_str, tracking);
|
|
|
|
return *std::move(op);
|
|
|
|
}
|
2017-12-11 04:56:49 +08:00
|
|
|
#else
|
2018-12-20 12:17:15 +08:00
|
|
|
return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
|
2017-12-11 04:56:49 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) unqualified_get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
|
2020-03-31 12:22:46 +08:00
|
|
|
record tracking {};
|
2018-12-20 12:17:15 +08:00
|
|
|
return unqualified_get<T>(L, index, tracking);
|
2017-12-11 04:56:49 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
auto get(lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_get<T>(L, index, tracking)) {
|
2020-08-10 05:17:06 +08:00
|
|
|
#if SOL_IS_ON(SOL_SAFE_GETTER_I_)
|
2020-03-31 12:22:46 +08:00
|
|
|
static constexpr bool is_op = meta::is_optional_v<T>;
|
2018-12-20 12:17:15 +08:00
|
|
|
if constexpr (is_op) {
|
|
|
|
return stack_detail::unchecked_get<T>(L, index, tracking);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (is_lua_reference<T>::value) {
|
|
|
|
return stack_detail::unchecked_get<T>(L, index, tracking);
|
|
|
|
}
|
|
|
|
auto op = check_get<T>(L, index, type_panic_c_str, tracking);
|
|
|
|
return *std::move(op);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return stack_detail::unchecked_get<T>(L, index, tracking);
|
|
|
|
#endif
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
|
2020-03-31 12:22:46 +08:00
|
|
|
record tracking {};
|
2018-12-20 12:17:15 +08:00
|
|
|
return get<T>(L, index, tracking);
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) get_usertype(lua_State* L, int index, record& tracking) {
|
2018-12-20 12:17:15 +08:00
|
|
|
using UT = meta::conditional_t<std::is_pointer<T>::value, detail::as_pointer_tag<std::remove_pointer_t<T>>, detail::as_value_tag<T>>;
|
|
|
|
return get<UT>(L, index, tracking);
|
2016-07-29 12:57:47 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2021-01-04 18:52:30 +08:00
|
|
|
decltype(auto) get_usertype(lua_State* L, int index = -lua_size_v<meta::unqualified_t<T>>) {
|
2020-03-31 12:22:46 +08:00
|
|
|
record tracking {};
|
2018-12-20 12:17:15 +08:00
|
|
|
return get_usertype<T>(L, index, tracking);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
decltype(auto) pop(lua_State* L) {
|
2021-02-03 09:36:49 +08:00
|
|
|
return popper<T> {}.pop(L);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, bool raw = false, typename Key>
|
|
|
|
void get_field(lua_State* L, Key&& key) {
|
2020-03-31 12:22:46 +08:00
|
|
|
field_getter<meta::unqualified_t<Key>, global, raw> {}.get(L, std::forward<Key>(key));
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, bool raw = false, typename Key>
|
|
|
|
void get_field(lua_State* L, Key&& key, int tableindex) {
|
2020-03-31 12:22:46 +08:00
|
|
|
field_getter<meta::unqualified_t<Key>, global, raw> {}.get(L, std::forward<Key>(key), tableindex);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key>
|
|
|
|
void raw_get_field(lua_State* L, Key&& key) {
|
|
|
|
get_field<global, true>(L, std::forward<Key>(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key>
|
|
|
|
void raw_get_field(lua_State* L, Key&& key, int tableindex) {
|
|
|
|
get_field<global, true>(L, std::forward<Key>(key), tableindex);
|
|
|
|
}
|
|
|
|
|
2018-05-10 19:30:51 +08:00
|
|
|
template <bool global = false, bool raw = false, typename C = detail::non_lua_nil_t, typename Key>
|
2016-06-17 08:15:56 +08:00
|
|
|
probe probe_get_field(lua_State* L, Key&& key) {
|
2020-03-31 12:22:46 +08:00
|
|
|
return probe_field_getter<meta::unqualified_t<Key>, C, global, raw> {}.get(L, std::forward<Key>(key));
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:30:51 +08:00
|
|
|
template <bool global = false, bool raw = false, typename C = detail::non_lua_nil_t, typename Key>
|
2016-06-17 08:15:56 +08:00
|
|
|
probe probe_get_field(lua_State* L, Key&& key, int tableindex) {
|
2020-03-31 12:22:46 +08:00
|
|
|
return probe_field_getter<meta::unqualified_t<Key>, C, global, raw> {}.get(L, std::forward<Key>(key), tableindex);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:30:51 +08:00
|
|
|
template <bool global = false, typename C = detail::non_lua_nil_t, typename Key>
|
2016-06-17 08:15:56 +08:00
|
|
|
probe probe_raw_get_field(lua_State* L, Key&& key) {
|
2018-05-10 19:30:51 +08:00
|
|
|
return probe_get_field<global, true, C>(L, std::forward<Key>(key));
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-10 19:30:51 +08:00
|
|
|
template <bool global = false, typename C = detail::non_lua_nil_t, typename Key>
|
2016-06-17 08:15:56 +08:00
|
|
|
probe probe_raw_get_field(lua_State* L, Key&& key, int tableindex) {
|
2018-05-10 19:30:51 +08:00
|
|
|
return probe_get_field<global, true, C>(L, std::forward<Key>(key), tableindex);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, bool raw = false, typename Key, typename Value>
|
|
|
|
void set_field(lua_State* L, Key&& key, Value&& value) {
|
2020-03-31 12:22:46 +08:00
|
|
|
field_setter<meta::unqualified_t<Key>, global, raw> {}.set(L, std::forward<Key>(key), std::forward<Value>(value));
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, bool raw = false, typename Key, typename Value>
|
|
|
|
void set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
|
2020-03-31 12:22:46 +08:00
|
|
|
field_setter<meta::unqualified_t<Key>, global, raw> {}.set(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
|
2016-06-17 08:15:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key, typename Value>
|
|
|
|
void raw_set_field(lua_State* L, Key&& key, Value&& value) {
|
|
|
|
set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key, typename Value>
|
|
|
|
void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
|
|
|
|
set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
|
|
|
|
}
|
2018-03-30 16:51:29 +08:00
|
|
|
|
2018-09-28 13:27:38 +08:00
|
|
|
template <typename T, typename F>
|
2019-05-21 14:13:56 +08:00
|
|
|
void modify_unique_usertype_as(const stack_reference& obj, F&& f) {
|
2018-09-28 13:27:38 +08:00
|
|
|
void* raw = lua_touserdata(obj.lua_state(), obj.stack_index());
|
|
|
|
void* ptr_memory = detail::align_usertype_pointer(raw);
|
|
|
|
void* uu_memory = detail::align_usertype_unique<T>(raw);
|
|
|
|
T& uu = *static_cast<T*>(uu_memory);
|
|
|
|
f(uu);
|
2021-02-03 09:36:49 +08:00
|
|
|
*static_cast<void**>(ptr_memory) = static_cast<void*>(detail::unique_get(obj.lua_state(), uu));
|
2018-03-30 16:51:29 +08:00
|
|
|
}
|
|
|
|
|
2018-09-28 13:27:38 +08:00
|
|
|
template <typename F>
|
2019-05-21 14:13:56 +08:00
|
|
|
void modify_unique_usertype(const stack_reference& obj, F&& f) {
|
2019-02-14 15:40:57 +08:00
|
|
|
using bt = meta::bind_traits<meta::unqualified_t<F>>;
|
|
|
|
using T = typename bt::template arg_at<0>;
|
|
|
|
using Tu = meta::unqualified_t<T>;
|
|
|
|
modify_unique_usertype_as<Tu>(obj, std::forward<F>(f));
|
2018-03-30 16:51:29 +08:00
|
|
|
}
|
2018-12-20 12:17:15 +08:00
|
|
|
|
2021-01-04 18:52:30 +08:00
|
|
|
namespace stack_detail {
|
|
|
|
template <typename T, typename Handler>
|
|
|
|
decltype(auto) check_get_arg(lua_State* L_, int index_, Handler&& handler_, record& tracking_) {
|
2021-01-05 03:58:54 +08:00
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_check_access_v<T>) {
|
|
|
|
sol_lua_check_access(types<meta::unqualified_t<T>>(), L_, index_, tracking_);
|
|
|
|
}
|
2021-01-04 18:52:30 +08:00
|
|
|
return check_get<T>(L_, index_, std::forward<Handler>(handler_), tracking_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
decltype(auto) unchecked_get_arg(lua_State* L_, int index_, record& tracking_) {
|
2021-01-05 03:58:54 +08:00
|
|
|
if constexpr (meta::meta_detail::is_adl_sol_lua_check_access_v<T>) {
|
|
|
|
sol_lua_check_access(types<meta::unqualified_t<T>>(), L_, index_, tracking_);
|
|
|
|
}
|
2021-01-04 18:52:30 +08:00
|
|
|
return unchecked_get<T>(L_, index_, tracking_);
|
|
|
|
}
|
|
|
|
} // namespace stack_detail
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace stack
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
lua_CFunction make_destructor(std::true_type) {
|
2018-12-20 12:17:15 +08:00
|
|
|
if constexpr (is_unique_usertype_v<T>) {
|
2021-01-21 02:55:44 +08:00
|
|
|
return &unique_destroy<T>;
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
|
|
|
else if constexpr (!std::is_pointer_v<T>) {
|
2021-01-21 02:55:44 +08:00
|
|
|
return &usertype_alloc_destroy<T>;
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
2019-01-05 18:48:51 +08:00
|
|
|
else {
|
2021-01-21 02:55:44 +08:00
|
|
|
return &cannot_destroy<T>;
|
2019-01-05 18:48:51 +08:00
|
|
|
}
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
lua_CFunction make_destructor(std::false_type) {
|
2021-01-21 02:55:44 +08:00
|
|
|
return &cannot_destroy<T>;
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
lua_CFunction make_destructor() {
|
2018-12-20 12:17:15 +08:00
|
|
|
return make_destructor<T>(std::is_destructible<T>());
|
|
|
|
}
|
|
|
|
|
|
|
|
struct no_comp {
|
|
|
|
template <typename A, typename B>
|
|
|
|
bool operator()(A&&, B&&) const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
int is_check(lua_State* L) {
|
2018-12-20 12:17:15 +08:00
|
|
|
return stack::push(L, stack::check<T>(L, 1, &no_panic));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
int member_default_to_string(std::true_type, lua_State* L) {
|
2018-12-20 12:17:15 +08:00
|
|
|
decltype(auto) ts = stack::get<T>(L, 1).to_string();
|
|
|
|
return stack::push(L, std::forward<decltype(ts)>(ts));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
int member_default_to_string(std::false_type, lua_State* L) {
|
2018-12-20 12:17:15 +08:00
|
|
|
return luaL_error(L,
|
|
|
|
"cannot perform to_string on '%s': no 'to_string' overload in namespace, 'to_string' member "
|
|
|
|
"function, or operator<<(ostream&, ...) present",
|
|
|
|
detail::demangle<T>().data());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
int adl_default_to_string(std::true_type, lua_State* L) {
|
2018-12-20 12:17:15 +08:00
|
|
|
using namespace std;
|
|
|
|
decltype(auto) ts = to_string(stack::get<T>(L, 1));
|
|
|
|
return stack::push(L, std::forward<decltype(ts)>(ts));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
int adl_default_to_string(std::false_type, lua_State* L) {
|
2018-12-20 12:17:15 +08:00
|
|
|
return member_default_to_string<T>(meta::supports_to_string_member<T>(), L);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
int oss_default_to_string(std::true_type, lua_State* L) {
|
2018-12-20 12:17:15 +08:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << stack::unqualified_get<T>(L, 1);
|
|
|
|
return stack::push(L, oss.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
int oss_default_to_string(std::false_type, lua_State* L) {
|
2018-12-20 12:17:15 +08:00
|
|
|
return adl_default_to_string<T>(meta::supports_adl_to_string<T>(), L);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
int default_to_string(lua_State* L) {
|
2019-12-03 04:54:58 +08:00
|
|
|
return oss_default_to_string<T>(meta::supports_op_left_shift<std::ostream, T>(), L);
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-21 14:13:56 +08:00
|
|
|
int default_size(lua_State* L) {
|
2018-12-20 12:17:15 +08:00
|
|
|
decltype(auto) self = stack::unqualified_get<T>(L, 1);
|
|
|
|
return stack::push(L, self.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Op>
|
|
|
|
int comparsion_operator_wrap(lua_State* L) {
|
2019-01-29 01:33:21 +08:00
|
|
|
if constexpr (std::is_void_v<T>) {
|
2019-01-05 18:48:51 +08:00
|
|
|
return stack::push(L, false);
|
|
|
|
}
|
|
|
|
else {
|
2019-03-10 09:57:49 +08:00
|
|
|
auto maybel = stack::unqualified_check_get<T>(L, 1);
|
2019-01-29 01:33:21 +08:00
|
|
|
if (!maybel) {
|
|
|
|
return stack::push(L, false);
|
|
|
|
}
|
2019-03-10 09:57:49 +08:00
|
|
|
auto mayber = stack::unqualified_check_get<T>(L, 2);
|
2019-01-29 01:33:21 +08:00
|
|
|
if (!mayber) {
|
|
|
|
return stack::push(L, false);
|
|
|
|
}
|
|
|
|
decltype(auto) l = *maybel;
|
|
|
|
decltype(auto) r = *mayber;
|
|
|
|
if constexpr (std::is_same_v<no_comp, Op>) {
|
|
|
|
std::equal_to<> op;
|
|
|
|
return stack::push(L, op(detail::ptr(l), detail::ptr(r)));
|
|
|
|
}
|
|
|
|
else {
|
2019-04-05 09:33:32 +08:00
|
|
|
if constexpr (std::is_same_v<std::equal_to<>, Op> // clang-format hack
|
2019-05-21 14:13:56 +08:00
|
|
|
|| std::is_same_v<std::less_equal<>, Op> //
|
|
|
|
|| std::is_same_v<std::less_equal<>, Op>) { //
|
2019-01-29 01:33:21 +08:00
|
|
|
if (detail::ptr(l) == detail::ptr(r)) {
|
|
|
|
return stack::push(L, true);
|
|
|
|
}
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
2019-01-29 01:33:21 +08:00
|
|
|
Op op;
|
|
|
|
return stack::push(L, op(detail::deref(l), detail::deref(r)));
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename IFx, typename Fx>
|
|
|
|
void insert_default_registrations(IFx&& ifx, Fx&& fx);
|
|
|
|
|
|
|
|
template <typename T, bool, bool>
|
2020-09-06 05:25:15 +08:00
|
|
|
struct get_is_primitive : is_lua_primitive<T> { };
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct get_is_primitive<T, true, false>
|
2020-09-06 05:25:15 +08:00
|
|
|
: meta::neg<std::is_reference<decltype(sol_lua_get(types<T>(), nullptr, -1, std::declval<stack::record&>()))>> { };
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct get_is_primitive<T, false, true>
|
2020-09-06 05:25:15 +08:00
|
|
|
: meta::neg<std::is_reference<decltype(sol_lua_get(types<meta::unqualified_t<T>>(), nullptr, -1, std::declval<stack::record&>()))>> { };
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2020-09-06 05:25:15 +08:00
|
|
|
struct get_is_primitive<T, true, true> : get_is_primitive<T, true, false> { };
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct is_proxy_primitive
|
2020-09-06 05:25:15 +08:00
|
|
|
: detail::get_is_primitive<T, meta::meta_detail::is_adl_sol_lua_get_v<T>, meta::meta_detail::is_adl_sol_lua_get_v<meta::unqualified_t<T>>> { };
|
2018-12-20 12:17:15 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace sol
|
2016-03-25 03:45:44 +08:00
|
|
|
|
|
|
|
#endif // SOL_STACK_CORE_HPP
|