* Push a pointer with the name of meta table */
| 184 | * Push a pointer with the name of meta table |
| 185 | */ |
| 186 | int32 PushPointer(lua_State *L, void *Value, const char *MetatableName, bool bAlwaysCreate) |
| 187 | { |
| 188 | if (!Value |
| 189 | || !MetatableName) |
| 190 | { |
| 191 | lua_pushnil(L); |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | bool bCreateUserdata = bAlwaysCreate; |
| 196 | if (!bAlwaysCreate) |
| 197 | { |
| 198 | // find the pointer from 'StructMap' first |
| 199 | lua_getfield(L, LUA_REGISTRYINDEX, "StructMap"); |
| 200 | lua_pushlightuserdata(L, Value); |
| 201 | int32 Type = lua_rawget(L, -2); |
| 202 | if (Type == LUA_TUSERDATA) |
| 203 | { |
| 204 | lua_remove(L, -2); |
| 205 | |
| 206 | // check metatable is same? |
| 207 | bool bMTSame = false; |
| 208 | if (lua_getmetatable(L, -1)) |
| 209 | { |
| 210 | luaL_getmetatable(L, MetatableName); |
| 211 | if (lua_rawequal(L,-1,-2)) |
| 212 | { |
| 213 | bMTSame = true; |
| 214 | } |
| 215 | |
| 216 | lua_pop(L, 2); |
| 217 | } |
| 218 | |
| 219 | if (!bMTSame) |
| 220 | { |
| 221 | #if UNLUA_ENABLE_DEBUG != 0 |
| 222 | FString CurMetatableName; |
| 223 | if (lua_getmetatable(L, -1)) |
| 224 | { |
| 225 | lua_pushstring(L, "__name"); |
| 226 | Type = lua_rawget(L,-2); |
| 227 | if (LUA_TSTRING == Type) |
| 228 | { |
| 229 | CurMetatableName = UTF8_TO_TCHAR(lua_tostring(L,-1)); |
| 230 | } |
| 231 | lua_pop(L, 2); |
| 232 | } |
| 233 | UE_LOG(LogTemp, Log, TEXT("%s : userdata with difference metatable finded! need %s,get %s,may be local or stack variable pushed to lua..."), |
| 234 | ANSI_TO_TCHAR(__FUNCTION__), UTF8_TO_TCHAR(MetatableName), *CurMetatableName); |
| 235 | #endif |
| 236 | bool bSuccess = TryToSetMetatable(L, MetatableName); // set metatable |
| 237 | if (!bSuccess) |
| 238 | { |
| 239 | UNLUA_LOGERROR(L, LogUnLua, Warning, TEXT("%s, Invalid metatable, metatable name: %s!"), UTF8_TO_TCHAR(MetatableName)); |
| 240 | return 1; |
| 241 | } |
| 242 | } |
| 243 | } |
no test coverage detected