Skip to content

Commit

Permalink
Use int instead of size_t as index in CLuaFunctionParser as it use ne…
Browse files Browse the repository at this point in the history
…gative indices (#2180)
  • Loading branch information
Pirulax committed Apr 8, 2023
1 parent 7ada789 commit e9351a6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
20 changes: 10 additions & 10 deletions Shared/mods/deathmatch/logic/lua/CLuaFunctionParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CLuaArgument;
struct CLuaFunctionParserBase
{
// iIndex is passed around by reference
std::size_t iIndex = 1;
int iIndex = 1;

std::string strError = "";
std::string strErrorFoundType = "";
Expand Down Expand Up @@ -94,7 +94,7 @@ struct CLuaFunctionParserBase

// Reads the parameter type (& value in some cases) at a given index
// For example a 42 on the Lua stack is returned as 'number (42)'
static SString ReadParameterAsString(lua_State* L, std::size_t index)
static SString ReadParameterAsString(lua_State* L, int index)
{
switch (lua_type(L, index))
{
Expand Down Expand Up @@ -143,7 +143,7 @@ struct CLuaFunctionParserBase

// Pop should remove a T from the Lua Stack after verifying that it is a valid type
template <typename T>
T Pop(lua_State* L, std::size_t& index)
T Pop(lua_State* L, int& index)
{
if (!TypeMatch<T>(L, index))
{
Expand All @@ -158,7 +158,7 @@ struct CLuaFunctionParserBase
// Special type matcher for variants. Returns -1 if the type does not match
// returns n if the nth type of the variant matches
template <typename T>
int TypeMatchVariant(lua_State* L, std::size_t index)
int TypeMatchVariant(lua_State* L, int index)
{
// If the variant is empty, we have exhausted all options
// The type therefore doesn't match the variant
Expand Down Expand Up @@ -186,7 +186,7 @@ struct CLuaFunctionParserBase
// should only check for obvious type violations (e.g. false is not a string) but not
// for internal type errors (passing a vehicle to a function expecting a ped)
template <typename T>
bool TypeMatch(lua_State* L, std::size_t index)
bool TypeMatch(lua_State* L, int index)
{
int iArgument = lua_type(L, index);
// primitive types
Expand Down Expand Up @@ -289,8 +289,8 @@ struct CLuaFunctionParserBase
}

// Special PopUnsafe for variants
template <typename T, std::size_t currIndex = 0>
T PopUnsafeVariant(lua_State* L, std::size_t& index, int vindex)
template <typename T, int currIndex = 0>
T PopUnsafeVariant(lua_State* L, int& index, int vindex)
{
// As std::variant<> cannot be constructed, we simply return the first value
// in the error case. This is actually unreachable in the regular path,
Expand Down Expand Up @@ -344,7 +344,7 @@ struct CLuaFunctionParserBase
// as this condition cannot be caught before actually reading the userdata from the Lua stack
// On success, this function may also increment `index`
template <typename T>
T PopUnsafe(lua_State* L, std::size_t& index)
T PopUnsafe(lua_State* L, int& index)
{
// Expect no change in stack size
LUA_STACK_EXPECT(0);
Expand Down Expand Up @@ -417,7 +417,7 @@ struct CLuaFunctionParserBase
continue;
}

std::size_t i = -1;
int i = -1;
vecData.emplace_back(PopUnsafe<param>(L, i));
lua_pop(L, 1); // drop value, keep key for lua_next
}
Expand All @@ -439,7 +439,7 @@ struct CLuaFunctionParserBase
continue;
}

std::size_t i = -2;
int i = -2;
auto k = PopUnsafe<key_t>(L, i);
auto v = PopUnsafe<value_t>(L, i);
map.emplace(std::move(k), std::move(v));
Expand Down
28 changes: 14 additions & 14 deletions Shared/mods/deathmatch/logic/lua/LuaBasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace lua
{
template <>
std::string PopPrimitive<std::string>(lua_State* L, std::size_t& index)
std::string PopPrimitive<std::string>(lua_State* L, int& index)
{
uint uiLength = lua_strlen(L, index);
std::string outValue;
Expand All @@ -22,81 +22,81 @@ namespace lua
}

template <>
std::string_view PopPrimitive<std::string_view>(lua_State* L, std::size_t& index)
std::string_view PopPrimitive<std::string_view>(lua_State* L, int& index)
{
uint uiLength = lua_strlen(L, index);
std::string_view outValue(lua_tostring(L, index++), uiLength);
return outValue;
}

template <>
int PopPrimitive<int>(lua_State* L, std::size_t& index)
int PopPrimitive<int>(lua_State* L, int& index)
{
return static_cast<int>(lua_tonumber(L, index++));
}

template <>
unsigned int PopPrimitive<unsigned int>(lua_State* L, std::size_t& index)
unsigned int PopPrimitive<unsigned int>(lua_State* L, int& index)
{
return static_cast<unsigned int>(lua_tonumber(L, index++));
}

template <>
short PopPrimitive<short>(lua_State* L, std::size_t& index)
short PopPrimitive<short>(lua_State* L, int& index)
{
return static_cast<short>(lua_tonumber(L, index++));
}

template <>
unsigned short PopPrimitive<unsigned short>(lua_State* L, std::size_t& index)
unsigned short PopPrimitive<unsigned short>(lua_State* L, int& index)
{
return static_cast<unsigned short>(lua_tonumber(L, index++));
}

template <>
char PopPrimitive<char>(lua_State* L, std::size_t& index)
char PopPrimitive<char>(lua_State* L, int& index)
{
return static_cast<char>(lua_tonumber(L, index++));
}

template <>
unsigned char PopPrimitive<unsigned char>(lua_State* L, std::size_t& index)
unsigned char PopPrimitive<unsigned char>(lua_State* L, int& index)
{
return static_cast<unsigned char>(lua_tonumber(L, index++));
}

template <>
int64_t PopPrimitive<int64_t>(lua_State* L, std::size_t& index)
int64_t PopPrimitive<int64_t>(lua_State* L, int& index)
{
return static_cast<int64_t>(lua_tonumber(L, index++));
}

template <>
uint64_t PopPrimitive<uint64_t>(lua_State* L, std::size_t& index)
uint64_t PopPrimitive<uint64_t>(lua_State* L, int& index)
{
return static_cast<uint64_t>(lua_tonumber(L, index++));
}

template <>
float PopPrimitive<float>(lua_State* L, std::size_t& index)
float PopPrimitive<float>(lua_State* L, int& index)
{
return static_cast<float>(lua_tonumber(L, index++));
}

template <>
double PopPrimitive<double>(lua_State* L, std::size_t& index)
double PopPrimitive<double>(lua_State* L, int& index)
{
return static_cast<double>(lua_tonumber(L, index++));
}

template <>
bool PopPrimitive<bool>(lua_State* L, std::size_t& index)
bool PopPrimitive<bool>(lua_State* L, int& index)
{
return static_cast<bool>(lua_toboolean(L, index++));
}

template <>
void* PopPrimitive<void*>(lua_State* L, std::size_t& index)
void* PopPrimitive<void*>(lua_State* L, int& index)
{
return lua_touserdata(L, index++);
}
Expand Down
4 changes: 2 additions & 2 deletions Shared/mods/deathmatch/logic/lua/LuaBasic.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
/*
Basic Lua operations:
void Push(L, T value)
T PopPrimitive(L, std::size_t stackIndex)
T PopPrimitive(L, int& stackIndex)
*/

namespace lua
{
// PopTrival should read a simple value of type T from the stack without extra type checks
// If whatever is at that point in the stack is not convertible to T, the behavior is undefined
template <typename T>
inline T PopPrimitive(lua_State* L, std::size_t& index);
inline T PopPrimitive(lua_State* L, int& index);

// Push should push a value of type T to the Lua Stack
// This will always increase the stack size by 1
Expand Down

0 comments on commit e9351a6

Please sign in to comment.