| 151 | } |
| 152 | |
| 153 | static int CreateLuaHashUserdata(lua_State* L, dmhash_t hash) |
| 154 | { |
| 155 | HContext context = dmScript::GetScriptContext(L); |
| 156 | dmhash_t* lua_hash = (dmhash_t*)lua_newuserdata(L, sizeof(dmhash_t)); |
| 157 | *lua_hash = hash; |
| 158 | luaL_getmetatable(L, SCRIPT_TYPE_NAME_HASH); |
| 159 | lua_setmetatable(L, -2); |
| 160 | |
| 161 | // [-1] hash |
| 162 | lua_rawgeti(L, LUA_REGISTRYINDEX, context->m_ContextTableRef); |
| 163 | // [-2] hash |
| 164 | // [-1] Context table |
| 165 | lua_pushvalue(L, -2); |
| 166 | // [-3] hash |
| 167 | // [-2] Context table |
| 168 | // [-1] hash |
| 169 | int ref = luaL_ref(L, -2); |
| 170 | // [-2] hash |
| 171 | // [-1] Context table |
| 172 | lua_pop(L, 1); |
| 173 | // [-1] hash |
| 174 | |
| 175 | dmHashTable64<int>* instances = &context->m_HashInstances; |
| 176 | if (instances->Full()) |
| 177 | { |
| 178 | instances->SetCapacity(instances->Size(), instances->Capacity() + 256); |
| 179 | } |
| 180 | instances->Put(hash, ref); |
| 181 | |
| 182 | // Also store the value in the weak table with the hash as key |
| 183 | lua_rawgeti(L, LUA_REGISTRYINDEX, context->m_ContextWeakTableRef); |
| 184 | // [-2] userdata |
| 185 | // [-1] weak_table |
| 186 | PushHashWeakTableKey(L, hash); |
| 187 | // [-3] userdata |
| 188 | // [-2] weak_table |
| 189 | // [-1] key |
| 190 | lua_pushvalue(L, -3); |
| 191 | // [-4] userdata |
| 192 | // [-3] weak_table |
| 193 | // [-2] key |
| 194 | // [-1] value |
| 195 | lua_rawset(L, -3); // weak_table[hash] = userdata |
| 196 | // [-2] userdata |
| 197 | // [-1] weak_table |
| 198 | lua_pop(L, 1); |
| 199 | // [-1] userdata |
| 200 | |
| 201 | return lua_gettop(L); // return stack index of the new userdata |
| 202 | } |
| 203 | |
| 204 | void PushHash(lua_State* L, dmhash_t hash) |
| 205 | { |
no test coverage detected