| 2167 | |
| 2168 | template <typename T> |
| 2169 | T* LuaEngine::getUserData(int handleIndex) { |
| 2170 | int typeRef = m_registeredUserDataTypes.value(typeid(T), LUA_NOREF); |
| 2171 | if (typeRef == LUA_NOREF) |
| 2172 | throw LuaException::format("Cannot convert userdata type of {}, not registered", typeid(T).name()); |
| 2173 | |
| 2174 | lua_checkstack(m_state, 3); |
| 2175 | |
| 2176 | pushHandle(m_state, handleIndex); |
| 2177 | T* userdata = (T*)lua_touserdata(m_state, -1); |
| 2178 | if (lua_getmetatable(m_state, -1) == 0) { |
| 2179 | lua_pop(m_state, 1); |
| 2180 | throw LuaException("Cannot get userdata from lua type, no metatable found"); |
| 2181 | } |
| 2182 | |
| 2183 | lua_rawgeti(m_state, LUA_REGISTRYINDEX, typeRef); |
| 2184 | if (!lua_rawequal(m_state, -1, -2)) { |
| 2185 | lua_pop(m_state, 3); |
| 2186 | throw LuaException::format("Improper conversion from userdata to type {}", typeid(T).name()); |
| 2187 | } |
| 2188 | |
| 2189 | lua_pop(m_state, 3); |
| 2190 | |
| 2191 | return userdata; |
| 2192 | } |
| 2193 | |
| 2194 | inline void LuaEngine::destroyHandle(int handleIndex) { |
| 2195 | // We don't bother setting the entry in the handle stack to nil, we just wait |
nothing calls this directly
no test coverage detected