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

feat(citizen-scripting-lua): table.size function #3093

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions code/components/citizen-scripting-lua/src/LuaScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,25 @@ static int Lua_Require(lua_State* L)
return luaL_error(L, "module '%s' not found", name);
}

static int Lua_TableSize(lua_state* L)
{
if (lua_gettop(L) != 1 || !lua_istable(L, 1))
{
return luaL_error(L, "table expected got %s", lua_typename(L, lua_type(L, 1)));
}

lua_Integer count = 0;
lua_pushnil(L);
while (lua_next(L, 1))
{
count++;
lua_pop(L, 1);
}

lua_pushinteger(L, count);
return 1;
}

//
// Scheduling
//
Expand Down Expand Up @@ -1411,6 +1430,14 @@ result_t LuaScriptRuntime::Create(IScriptHost* scriptHost)
lua_pushcfunction(m_state, Lua_Require);
lua_setglobal(m_state, "require");

lua_getglobal(m_state, "table");
if (lua_istable(m_state, -1))
{
lua_pushcfunction(m_state, Lua_TableSize);
lua_setfield(m_state, -2, "size");
}
lua_pop(m_state, 1);

#if LUA_VERSION_NUM >= 504
lua_setwarnf(m_state, Lua_Warn, nullptr);
#endif
Expand Down
Loading