| 291 | } |
| 292 | |
| 293 | static UpdateResult HandleMessage(void* context, ScriptInstance* script_instance, dmMessage::Message* message, int function_ref, bool is_callback, bool deref_function_ref) |
| 294 | { |
| 295 | UpdateResult result = UPDATE_RESULT_OK; |
| 296 | |
| 297 | lua_State* L = GetLuaState(context); |
| 298 | int top = lua_gettop(L); |
| 299 | (void) top; |
| 300 | |
| 301 | lua_rawgeti(L, LUA_REGISTRYINDEX, script_instance->m_InstanceReference); |
| 302 | dmScript::SetInstance(L); |
| 303 | |
| 304 | if (is_callback) |
| 305 | { |
| 306 | dmScript::ResolveInInstance(L, function_ref); |
| 307 | if (!lua_isfunction(L, -1)) |
| 308 | { |
| 309 | // If the script instance is dead we just ignore the callback |
| 310 | lua_pop(L, 1); |
| 311 | lua_pushnil(L); |
| 312 | dmScript::SetInstance(L); |
| 313 | dmLogWarning("Failed to call message response callback function, has it been deleted?"); |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | if (deref_function_ref) |
| 318 | { |
| 319 | // The reason the caller cannot deref this function, is that they've posted the message onto a queue |
| 320 | // and don't know when it's going to be actually called |
| 321 | dmScript::UnrefInInstance(L, function_ref); |
| 322 | } |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | lua_rawgeti(L, LUA_REGISTRYINDEX, function_ref); |
| 327 | } |
| 328 | |
| 329 | assert(lua_isfunction(L, -1)); |
| 330 | |
| 331 | lua_rawgeti(L, LUA_REGISTRYINDEX, script_instance->m_InstanceReference); |
| 332 | |
| 333 | dmScript::PushHash(L, message->m_Id); |
| 334 | |
| 335 | const char* message_name = 0; |
| 336 | if (message->m_Descriptor != 0) |
| 337 | { |
| 338 | // TODO: setjmp/longjmp here... how to handle?!!! We are not running "from lua" here |
| 339 | // lua_cpcall? |
| 340 | message_name = ((const dmDDF::Descriptor*)message->m_Descriptor)->m_Name; |
| 341 | dmScript::PushDDF(L, (const dmDDF::Descriptor*)message->m_Descriptor, (const char*) message->m_Data, true); |
| 342 | } |
| 343 | else |
| 344 | { |
| 345 | if (ProfileIsInitialized()) |
| 346 | { |
| 347 | // Try to find the message name via id and reverse hash |
| 348 | message_name = (const char*)dmHashReverse64(message->m_Id, 0); |
| 349 | } |
| 350 | if (message->m_DataSize > 0) |
no test coverage detected