Skip to content

Commit

Permalink
Avoid luaL_checkint and luaL_optint, they're not available in Lua 5.4.
Browse files Browse the repository at this point in the history
Fixes #2102.
  • Loading branch information
slime73 committed Jan 19, 2025
1 parent 4e8291f commit 5b4ced2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
15 changes: 15 additions & 0 deletions src/common/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ inline float luax_checkfloat(lua_State *L, int idx)
return static_cast<float>(luaL_checknumber(L, idx));
}

inline int luax_toint(lua_State *L, int idx)
{
return (int)lua_tointeger(L, idx);
}

inline int luax_checkint(lua_State *L, int idx)
{
return (int)luaL_checkinteger(L, idx);
}

inline int luax_optint(lua_State *L, int idx, int def)
{
return (int)luaL_optinteger(L, idx, (lua_Integer)def);
}

inline lua_Number luax_checknumberclamped(lua_State *L, int idx, double minv, double maxv)
{
return std::min(std::max(luaL_checknumber(L, idx), minv), maxv);
Expand Down
14 changes: 7 additions & 7 deletions src/modules/graphics/wrap_Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,22 +1301,22 @@ int w_newTextureView(lua_State *L)

lua_getfield(L, 2, "mipmapstart");
if (!lua_isnoneornil(L, -1))
settings.mipmapStart.set(luaL_checkint(L, -1) - 1);
settings.mipmapStart.set(luax_checkint(L, -1) - 1);
lua_pop(L, 1);

lua_getfield(L, 2, "mipmapcount");
if (!lua_isnoneornil(L, -1))
settings.mipmapCount.set(luaL_checkint(L, -1));
settings.mipmapCount.set(luax_checkint(L, -1));
lua_pop(L, 1);

lua_getfield(L, 2, "layerstart");
if (!lua_isnoneornil(L, -1))
settings.layerStart.set(luaL_checkint(L, -1) - 1);
settings.layerStart.set(luax_checkint(L, -1) - 1);
lua_pop(L, 1);

lua_getfield(L, 2, "layers");
if (!lua_isnoneornil(L, -1))
settings.layerCount.set(luaL_checkint(L, -1));
settings.layerCount.set(luax_checkint(L, -1));
lua_pop(L, 1);

lua_getfield(L, 2, "debugname");
Expand Down Expand Up @@ -1771,7 +1771,7 @@ static Buffer::DataDeclaration luax_checkdatadeclaration(lua_State* L, int forma
luaL_argerror(L, formattableidx, str.c_str());
}
else if (!lua_isnoneornil(L, -1))
decl.bindingLocation = luaL_checkint(L, -1);
decl.bindingLocation = luax_checkint(L, -1);
lua_pop(L, 1);

return decl;
Expand Down Expand Up @@ -2215,7 +2215,7 @@ static Mesh::BufferAttribute luax_checkbufferattributetable(lua_State *L, int id
lua_pop(L, 1);

lua_getfield(L, idx, "location");
attrib.bindingLocation = luaL_checkint(L, -1);
attrib.bindingLocation = luax_checkint(L, -1);
lua_pop(L, 1);

lua_getfield(L, idx, "name");
Expand All @@ -2234,7 +2234,7 @@ static Mesh::BufferAttribute luax_checkbufferattributetable(lua_State *L, int id

lua_getfield(L, idx, "locationinbuffer");
if (!lua_isnoneornil(L, -1))
attrib.bindingLocationInBuffer = luaL_checkint(L, -1);
attrib.bindingLocationInBuffer = luax_checkint(L, -1);
else
attrib.bindingLocationInBuffer = attrib.bindingLocation;
lua_pop(L, 1);
Expand Down
8 changes: 4 additions & 4 deletions src/modules/graphics/wrap_Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ int w_Mesh_setAttributeEnabled(lua_State *L)
}
else
{
int location = luaL_checkint(L, 2);
int location = luax_checkint(L, 2);
luax_catchexcept(L, [&](){ t->setAttributeEnabled(location, enable); });
}
return 0;
Expand All @@ -308,7 +308,7 @@ int w_Mesh_isAttributeEnabled(lua_State *L)
}
else
{
int location = luaL_checkint(L, 2);
int location = luax_checkint(L, 2);
luax_catchexcept(L, [&](){ enabled = t->isAttributeEnabled(location); });
}
lua_pushboolean(L, enabled);
Expand All @@ -324,7 +324,7 @@ int w_Mesh_attachAttribute(lua_State *L)
if (lua_type(L, 2) == LUA_TSTRING)
name = luaL_checkstring(L, 2);
else
location = luaL_checkint(L, 2);
location = luax_checkint(L, 2);

Buffer *buffer = nullptr;
Mesh *mesh = nullptr;
Expand All @@ -350,7 +350,7 @@ int w_Mesh_attachAttribute(lua_State *L)
if (name != nullptr)
attachname = luaL_optstring(L, 5, name);
else
attachlocation = luaL_optint(L, 5, location);
attachlocation = luax_optint(L, 5, location);

int startindex = (int) luaL_optinteger(L, 6, 1) - 1;

Expand Down
8 changes: 4 additions & 4 deletions src/modules/image/wrap_ImageData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ int w_ImageData_mapPixel(lua_State *L)
ImageData *t = luax_checkimagedata(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION);

int sx = luaL_optint(L, 3, 0);
int sy = luaL_optint(L, 4, 0);
int w = luaL_optint(L, 5, t->getWidth());
int h = luaL_optint(L, 6, t->getHeight());
int sx = luax_optint(L, 3, 0);
int sy = luax_optint(L, 4, 0);
int w = luax_optint(L, 5, t->getWidth());
int h = luax_optint(L, 6, t->getHeight());

if (!(t->inside(sx, sy) && t->inside(sx+w-1, sy+h-1)))
return luaL_error(L, "Invalid rectangle dimensions.");
Expand Down

0 comments on commit 5b4ced2

Please sign in to comment.