MCPcopy Create free account
hub / github.com/defold/defold / CreateLuaHashUserdata

Function CreateLuaHashUserdata

engine/script/src/script_hash.cpp:153–202  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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 {

Callers 1

PushHashFunction · 0.85

Calls 14

lua_newuserdataFunction · 0.85
lua_setmetatableFunction · 0.85
lua_rawgetiFunction · 0.85
lua_pushvalueFunction · 0.85
luaL_refFunction · 0.85
PushHashWeakTableKeyFunction · 0.85
lua_rawsetFunction · 0.85
lua_gettopFunction · 0.85
GetScriptContextFunction · 0.70
FullMethod · 0.45
SetCapacityMethod · 0.45
SizeMethod · 0.45

Tested by

no test coverage detected