Add lua_tolightuserdata, optimized lua_topointer (#496)

Co-authored-by: Petri Häkkinen <petrih@rmd.remedy.fi>
This commit is contained in:
Petri Häkkinen 2022-05-24 18:59:12 +03:00 committed by GitHub
parent 70ff6b4347
commit fb9c4311d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 7 deletions

View File

@ -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 const char* lua_namecallatom(lua_State* L, int* atom);
LUA_API int lua_objlen(lua_State* L, int idx); LUA_API int lua_objlen(lua_State* L, int idx);
LUA_API lua_CFunction lua_tocfunction(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_touserdata(lua_State* L, int idx);
LUA_API void* lua_touserdatatagged(lua_State* L, int idx, int tag); LUA_API void* lua_touserdatatagged(lua_State* L, int idx, int tag);
LUA_API int lua_userdatatag(lua_State* L, int idx); LUA_API int lua_userdatatag(lua_State* L, int idx);

View File

@ -478,19 +478,23 @@ lua_CFunction lua_tocfunction(lua_State* L, int idx)
return (!iscfunction(o)) ? NULL : cast_to(lua_CFunction, clvalue(o)->c.f); 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) void* lua_touserdata(lua_State* L, int idx)
{ {
StkId o = index2addr(L, idx); StkId o = index2addr(L, idx);
switch (ttype(o)) // fast-path: check userdata first since it is most likely the expected result
{ if (ttisuserdata(o))
case LUA_TUSERDATA:
return uvalue(o)->data; return uvalue(o)->data;
case LUA_TLIGHTUSERDATA: else if (ttislightuserdata(o))
return pvalue(o); return pvalue(o);
default: else
return NULL; return NULL;
} }
}
void* lua_touserdatatagged(lua_State* L, int idx, int tag) void* lua_touserdatatagged(lua_State* L, int idx, int tag)
{ {
@ -524,8 +528,9 @@ const void* lua_topointer(lua_State* L, int idx)
case LUA_TTHREAD: case LUA_TTHREAD:
return thvalue(o); return thvalue(o);
case LUA_TUSERDATA: case LUA_TUSERDATA:
return uvalue(o)->data;
case LUA_TLIGHTUSERDATA: case LUA_TLIGHTUSERDATA:
return lua_touserdata(L, idx); return pvalue(o);
default: default:
return NULL; return NULL;
} }

View File

@ -1066,6 +1066,7 @@ TEST_CASE("UserdataApi")
int lud; int lud;
lua_pushlightuserdata(L, &lud); lua_pushlightuserdata(L, &lud);
CHECK(lua_tolightuserdata(L, -1) == &lud);
CHECK(lua_touserdata(L, -1) == &lud); CHECK(lua_touserdata(L, -1) == &lud);
CHECK(lua_topointer(L, -1) == &lud); CHECK(lua_topointer(L, -1) == &lud);
@ -1073,6 +1074,7 @@ TEST_CASE("UserdataApi")
int* ud1 = (int*)lua_newuserdata(L, 4); int* ud1 = (int*)lua_newuserdata(L, 4);
*ud1 = 42; *ud1 = 42;
CHECK(lua_tolightuserdata(L, -1) == nullptr);
CHECK(lua_touserdata(L, -1) == ud1); CHECK(lua_touserdata(L, -1) == ud1);
CHECK(lua_topointer(L, -1) == ud1); CHECK(lua_topointer(L, -1) == ud1);