diff options
| author | Alexander Gavrilov | 2012-03-25 15:48:18 +0400 |
|---|---|---|
| committer | Alexander Gavrilov | 2012-03-25 15:48:18 +0400 |
| commit | 0412aaebe4266d9d1ed07ac4d6083ba360b90a45 (patch) | |
| tree | 4f27a00302b7b0837a1b4ea0156056c90987f9ad /library/LuaWrapper.cpp | |
| parent | 7209e4d3f2c3fc51d176131aa9da1654f211fed0 (diff) | |
| download | dfhack-0412aaebe4266d9d1ed07ac4d6083ba360b90a45.tar.gz dfhack-0412aaebe4266d9d1ed07ac4d6083ba360b90a45.tar.bz2 dfhack-0412aaebe4266d9d1ed07ac4d6083ba360b90a45.tar.xz | |
Add a delete() method to the objects in the lua wrapper.
Diffstat (limited to 'library/LuaWrapper.cpp')
| -rw-r--r-- | library/LuaWrapper.cpp | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/library/LuaWrapper.cpp b/library/LuaWrapper.cpp index bd9066a0..c38d6f28 100644 --- a/library/LuaWrapper.cpp +++ b/library/LuaWrapper.cpp @@ -593,10 +593,7 @@ static int meta_new(lua_State *state) void *ptr = id->allocate(); if (!ptr) - { - lua_pushnil(state); - return 1; - } + luaL_error(state, "Cannot allocate %s", id->getFullName().c_str()); if (lua_isuserdata(state, 1)) { @@ -631,6 +628,30 @@ static int meta_assign(lua_State *state) } /** + * Method: deallocation for DF object references. + */ +static int meta_delete(lua_State *state) +{ + int argc = lua_gettop(state); + + if (argc != 1) + luaL_error(state, "Usage: object:delete() or df.delete(object)"); + + if (lua_isnil(state, 1)) + { + lua_pushboolean(state, true); + return 1; + } + + type_identity *id = get_object_identity(state, 1, "df.delete()", false); + + bool ok = id->destroy(get_object_ref(state, 1)); + + lua_pushboolean(state, ok); + return 1; +} + +/** * Verify that the object is a DF ref with UPVAL_METATABLE. * If everything ok, extract the address. */ @@ -741,6 +762,10 @@ void LuaWrapper::SetPtrMethods(lua_State *state, int meta_idx, int read_idx) lua_setfield(state, meta_idx, "new"); EnableMetaField(state, read_idx, "new"); + lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_DELETE_NAME); + lua_setfield(state, meta_idx, "delete"); + EnableMetaField(state, read_idx, "delete"); + lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_ASSIGN_NAME); lua_setfield(state, meta_idx, "assign"); EnableMetaField(state, read_idx, "assign"); @@ -1022,6 +1047,10 @@ static void DoAttach(lua_State *state) lua_pushcclosure(state, meta_assign, 1); lua_setfield(state, LUA_REGISTRYINDEX, DFHACK_ASSIGN_NAME); + lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_TYPETABLE_NAME); + lua_pushcclosure(state, meta_delete, 1); + lua_setfield(state, LUA_REGISTRYINDEX, DFHACK_DELETE_NAME); + luaL_register(state, "df", no_functions); { @@ -1035,6 +1064,8 @@ static void DoAttach(lua_State *state) lua_setfield(state, -2, "sizeof"); lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_NEW_NAME); lua_setfield(state, -2, "new"); + lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_DELETE_NAME); + lua_setfield(state, -2, "delete"); lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_DISPLACE_NAME); lua_setfield(state, -2, "_displace"); lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_ASSIGN_NAME); |
