Skip to content

Commit

Permalink
Merge fcbc012 into 70ff6b4
Browse files Browse the repository at this point in the history
  • Loading branch information
petrihakkinen authored May 21, 2022
2 parents 70ff6b4 + fcbc012 commit b7633d7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions VM/include/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ LUA_API const char* lua_tostringatom(lua_State* L, int idx, int* atom);
LUA_API const char* lua_namecallatom(lua_State* L, int* atom);
LUA_API int lua_objlen(lua_State* L, int idx);
LUA_API lua_CFunction lua_tocfunction(lua_State* L, int idx);
LUA_API void* lua_tolightuserdata(lua_State* L, int idx);
LUA_API void* lua_touserdata(lua_State* L, int idx);
LUA_API void* lua_touserdatatagged(lua_State* L, int idx, int tag);
LUA_API int lua_userdatatag(lua_State* L, int idx);
Expand Down
9 changes: 8 additions & 1 deletion VM/src/lapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ lua_CFunction lua_tocfunction(lua_State* L, int idx)
return (!iscfunction(o)) ? NULL : cast_to(lua_CFunction, clvalue(o)->c.f);
}

void* lua_tolightuserdata(lua_State* L, int idx)
{
StkId o = index2addr(L, idx);
return (!ttislightuserdata(o)) ? NULL : pvalue(o);
}

void* lua_touserdata(lua_State* L, int idx)
{
StkId o = index2addr(L, idx);
Expand Down Expand Up @@ -524,8 +530,9 @@ const void* lua_topointer(lua_State* L, int idx)
case LUA_TTHREAD:
return thvalue(o);
case LUA_TUSERDATA:
return uvalue(o)->data;
case LUA_TLIGHTUSERDATA:
return lua_touserdata(L, idx);
return pvalue(o);
default:
return NULL;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Conformance.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,13 +1066,15 @@ TEST_CASE("UserdataApi")
int lud;
lua_pushlightuserdata(L, &lud);

CHECK(lua_tolightuserdata(L, -1) == &lud);
CHECK(lua_touserdata(L, -1) == &lud);
CHECK(lua_topointer(L, -1) == &lud);

// regular user data
int* ud1 = (int*)lua_newuserdata(L, 4);
*ud1 = 42;

CHECK(lua_tolightuserdata(L, -1) == nullptr);
CHECK(lua_touserdata(L, -1) == ud1);
CHECK(lua_topointer(L, -1) == ud1);

Expand Down

0 comments on commit b7633d7

Please sign in to comment.