Skip to content

Commit

Permalink
Lua API: add return types to table getters (#389)
Browse files Browse the repository at this point in the history
Co-authored-by: Petri Häkkinen <petrih@rmd.remedy.fi>
  • Loading branch information
petrihakkinen and Petri Häkkinen committed Feb 23, 2022
1 parent cd18adc commit 0bc7c51
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
10 changes: 5 additions & 5 deletions VM/include/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ LUA_API int lua_pushthread(lua_State* L);
/*
** get functions (Lua -> stack)
*/
LUA_API void lua_gettable(lua_State* L, int idx);
LUA_API void lua_getfield(lua_State* L, int idx, const char* k);
LUA_API void lua_rawgetfield(lua_State* L, int idx, const char* k);
LUA_API void lua_rawget(lua_State* L, int idx);
LUA_API void lua_rawgeti(lua_State* L, int idx, int n);
LUA_API int lua_gettable(lua_State* L, int idx);
LUA_API int lua_getfield(lua_State* L, int idx, const char* k);
LUA_API int lua_rawgetfield(lua_State* L, int idx, const char* k);
LUA_API int lua_rawget(lua_State* L, int idx);
LUA_API int lua_rawgeti(lua_State* L, int idx, int n);
LUA_API void lua_createtable(lua_State* L, int narr, int nrec);

LUA_API void lua_setreadonly(lua_State* L, int idx, int enabled);
Expand Down
20 changes: 10 additions & 10 deletions VM/src/lapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,16 @@ int lua_pushthread(lua_State* L)
** get functions (Lua -> stack)
*/

void lua_gettable(lua_State* L, int idx)
int lua_gettable(lua_State* L, int idx)
{
luaC_checkthreadsleep(L);
StkId t = index2addr(L, idx);
api_checkvalidindex(L, t);
luaV_gettable(L, t, L->top - 1, L->top - 1);
return;
return ttype(L->top - 1);
}

void lua_getfield(lua_State* L, int idx, const char* k)
int lua_getfield(lua_State* L, int idx, const char* k)
{
luaC_checkthreadsleep(L);
StkId t = index2addr(L, idx);
Expand All @@ -677,10 +677,10 @@ void lua_getfield(lua_State* L, int idx, const char* k)
setsvalue(L, &key, luaS_new(L, k));
luaV_gettable(L, t, &key, L->top);
api_incr_top(L);
return;
return ttype(L->top - 1);
}

void lua_rawgetfield(lua_State* L, int idx, const char* k)
int lua_rawgetfield(lua_State* L, int idx, const char* k)
{
luaC_checkthreadsleep(L);
StkId t = index2addr(L, idx);
Expand All @@ -689,26 +689,26 @@ void lua_rawgetfield(lua_State* L, int idx, const char* k)
setsvalue(L, &key, luaS_new(L, k));
setobj2s(L, L->top, luaH_getstr(hvalue(t), tsvalue(&key)));
api_incr_top(L);
return;
return ttype(L->top - 1);
}

void lua_rawget(lua_State* L, int idx)
int lua_rawget(lua_State* L, int idx)
{
luaC_checkthreadsleep(L);
StkId t = index2addr(L, idx);
api_check(L, ttistable(t));
setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
return;
return ttype(L->top - 1);
}

void lua_rawgeti(lua_State* L, int idx, int n)
int lua_rawgeti(lua_State* L, int idx, int n)
{
luaC_checkthreadsleep(L);
StkId t = index2addr(L, idx);
api_check(L, ttistable(t));
setobj2s(L, L->top, luaH_getnum(hvalue(t), n));
api_incr_top(L);
return;
return ttype(L->top - 1);
}

void lua_createtable(lua_State* L, int narray, int nrec)
Expand Down
41 changes: 41 additions & 0 deletions tests/Conformance.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,47 @@ TEST_CASE("Reference")
CHECK(dtorhits == 2);
}

TEST_CASE("ApiTables")
{
StateRef globalState(luaL_newstate(), lua_close);
lua_State* L = globalState.get();

lua_newtable(L);
lua_pushnumber(L, 123.0);
lua_setfield(L, -2, "key");
lua_pushstring(L, "test");
lua_rawseti(L, -2, 5);

// lua_gettable
lua_pushstring(L, "key");
CHECK(lua_gettable(L, -2) == LUA_TNUMBER);
CHECK(lua_tonumber(L, -1) == 123.0);
lua_pop(L, 1);

// lua_getfield
CHECK(lua_getfield(L, -1, "key") == LUA_TNUMBER);
CHECK(lua_tonumber(L, -1) == 123.0);
lua_pop(L, 1);

// lua_rawgetfield
CHECK(lua_rawgetfield(L, -1, "key") == LUA_TNUMBER);
CHECK(lua_tonumber(L, -1) == 123.0);
lua_pop(L, 1);

// lua_rawget
lua_pushstring(L, "key");
CHECK(lua_rawget(L, -2) == LUA_TNUMBER);
CHECK(lua_tonumber(L, -1) == 123.0);
lua_pop(L, 1);

// lua_rawgeti
CHECK(lua_rawgeti(L, -1, 5) == LUA_TSTRING);
CHECK(strcmp(lua_tostring(L, -1), "test") == 0);
lua_pop(L, 1);

lua_pop(L, 1);
}

TEST_CASE("ApiFunctionCalls")
{
StateRef globalState = runConformance("apicalls.lua");
Expand Down

0 comments on commit 0bc7c51

Please sign in to comment.