Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support call-by-reference in wrapped functions #5185

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions library/include/DataFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,28 @@ namespace df {
template<typename RT>
concept isPrimitive = identity_traits<RT>::is_primitive || std::is_enum_v<RT> || std::is_void_v<RT>;

template<typename T>
T get_from_lua_state(lua_State* L, int idx) {
T val;
// handle call-by-reference function arguments by converting pointers to a reference
// will throw if lua code tries to pass a null pointer
template<typename T> requires std::is_reference_v<T>
T get_from_lua_state(lua_State* L, int idx)
{
using DFHack::LuaWrapper::field_error;
using Ptr = std::add_pointer_t<std::remove_reference_t<T>>;
Ptr ptr{};
df::identity_traits<Ptr>::get()->lua_write(L, UPVAL_METHOD_NAME, &ptr, idx);
if (ptr == nullptr)
{
field_error(L, UPVAL_METHOD_NAME, "cannot convert null pointer to reference", "call");
}
return *ptr;
}

// handle call-by-value function arguments. only semi-regular types are allowed
// (semi-regular covers copyable and default-constructible)
template<std::semiregular T>
T get_from_lua_state(lua_State* L, int idx)
{
T val{};
df::identity_traits<T>::get()->lua_write(L, UPVAL_METHOD_NAME, &val, idx);
return val;
}
Expand Down
Loading