Allow assigning NULL lightuserdata to pointers, and export a global.

Otherwise there is no way to specify NULL via recursive lua table assign.
develop
Alexander Gavrilov 2012-03-29 14:47:33 +04:00
parent 85c91c92d8
commit a9b7c74a23
2 changed files with 14 additions and 9 deletions

@ -215,12 +215,19 @@ static void autovivify_ptr(lua_State *state, int fname_idx, void **pptr,
lua_pop(state, 1); lua_pop(state, 1);
} }
static bool is_null(lua_State *state, int val_index)
{
return lua_isnil(state, val_index) ||
(lua_islightuserdata(state, val_index) &&
!lua_touserdata(state, val_index));
}
void df::pointer_identity::lua_write(lua_State *state, int fname_idx, void *ptr, void df::pointer_identity::lua_write(lua_State *state, int fname_idx, void *ptr,
type_identity *target, int val_index) type_identity *target, int val_index)
{ {
auto pptr = (void**)ptr; auto pptr = (void**)ptr;
if (lua_isnil(state, val_index)) if (is_null(state, val_index))
*pptr = NULL; *pptr = NULL;
else if (lua_istable(state, val_index)) else if (lua_istable(state, val_index))
{ {

@ -496,14 +496,7 @@ static int meta_sizeof(lua_State *state)
luaL_error(state, "Usage: object:sizeof() or df.sizeof(object)"); luaL_error(state, "Usage: object:sizeof() or df.sizeof(object)");
// Two special cases: nil and lightuserdata for NULL and void* // Two special cases: nil and lightuserdata for NULL and void*
if (lua_isnil(state, 1)) if (lua_isnil(state, 1) || lua_islightuserdata(state, 1))
{
lua_pushnil(state);
lua_pushinteger(state, 0);
return 2;
}
if (lua_islightuserdata(state, 1))
{ {
lua_pushnil(state); lua_pushnil(state);
lua_pushnumber(state, (size_t)lua_touserdata(state, 1)); lua_pushnumber(state, (size_t)lua_touserdata(state, 1));
@ -1314,6 +1307,11 @@ static void DoAttach(lua_State *state)
lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_ASSIGN_NAME); lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_ASSIGN_NAME);
lua_setfield(state, -2, "assign"); lua_setfield(state, -2, "assign");
lua_pushlightuserdata(state, NULL);
lua_setfield(state, -2, "NULL");
lua_pushlightuserdata(state, NULL);
lua_setglobal(state, "NULL");
freeze_table(state, true, "df"); freeze_table(state, true, "df");
lua_remove(state, -2); lua_remove(state, -2);
lua_setmetatable(state, -2); lua_setmetatable(state, -2);