Esempio n. 1
0
void EntityLibrary::copyEventHandlers(const EventHandlers *from, EventHandlers *to)
{
    MARK_LUA_STACK(*_luaState);
    if(_luaState->good() && from->_eventHandlersRef != LUA_NOREF && lua_checkstack(*_luaState, 6))
    {
        if (to->_eventHandlersRef == LUA_NOREF)
        {
            lua_newtable (*_luaState);
            lua_pushvalue(*_luaState, -1);
            to->_eventHandlersRef = luaL_ref(*_luaState, LUA_REGISTRYINDEX);
        }
        else
        {
            lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, to->_eventHandlersRef);
        }
        lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, from->_eventHandlersRef);
        lua_pushnil(*_luaState);
        while (lua_next(*_luaState, -2))
        {
            lua_pushvalue(*_luaState, -2);
            lua_pushvalue(*_luaState, -2);
            lua_rawset(*_luaState, -6);
            lua_pop(*_luaState, 1);
        }
        lua_pop(*_luaState, 2);
    }
    CHECK_LUA_STACK(*_luaState);
}
Esempio n. 2
0
void EntityLibrary::fireEvent(EventHandlers *eventHandlers, const char *event, EntityNode *target)
{
    if (!_luaState->good())
    {
        return;
    }

    MARK_LUA_STACK(*_luaState);
    int nbPop = 0;
    if(eventHandlers->_eventHandlersRef != LUA_NOREF && eventHandlers->_eventHandlersRef != LUA_REFNIL)
    {
        lua_rawgeti(*_luaState, LUA_REGISTRYINDEX, eventHandlers->_eventHandlersRef);
        ++nbPop;
        if(lua_istable(*_luaState, -1))
        {
            lua_getfield(*_luaState, -1, event);
            ++nbPop;
            if (lua_isfunction(*_luaState, -1))
            {
                lua_pushNode(*_luaState, target);
                --nbPop;
                if (lua_pcall(*_luaState, 1, 0, 0))
                {
                    ++nbPop;
                }
            }
        }
        lua_pop(*_luaState, nbPop);
    }
    CHECK_LUA_STACK(*_luaState);
}
Esempio n. 3
0
	int SScriptObject::GetUserData(CScriptUserData& scriptUserData)
	{
		scriptUserData.UnRef();

		if(m_scriptHandle.IsNull())
		{
			return 0;
		}

		lua_State* pLua = m_pObjRef->pLua;
		CHECK_LUA_STACK(pLua);

		// Stop gc, because CheckObjectsTable may call gc
		//GUARD_LUA_GC(pLua);

		ScriptExporterManager().CheckObjectsTable(pLua, IsWeaked());
		lua_rawgeti(pLua, -1, m_pObjRef->nRef);

		const char* TEMP_REF = "__bin_temp";
		lua_setglobal(pLua, TEMP_REF);

		m_scriptHandle.Get(TEMP_REF, scriptUserData);
		lua_pushnil(pLua);
		lua_setglobal(pLua, TEMP_REF);
		
		return 1;
	}
Esempio n. 4
0
	void SScriptObjectRef::OnChangeWeakedTo(bool bWeaked)
	{
		if(pObject && pObject->IsWeaked() != bWeaked)	// Weak property is changed
		{
			assert(pLua && nRef!=LUA_NOREF);

			CHECK_LUA_STACK(pLua);

			ScriptExporterManager().CheckObjectsTable(pLua, pObject->IsWeaked());	// oldTbl
			lua_rawgeti(pLua, -1, nRef);									// oldTbl, obj

			ScriptExporterManager().CheckObjectsTable(pLua, bWeaked);		// oldTbl, obj, newTbl
			lua_pushvalue(pLua, -2);										// oldTbl, obj, newTbl, obj

			int nNewRef = luaL_ref(pLua, -2);	// oldTbl, obj, newTbl
			luaL_unref(pLua, -3, nRef);

			nRef = nNewRef;
		}
	}
Esempio n. 5
0
	void SScriptObjectRef::SideUnlink()
	{
		// Remove the object from objects table
		if(pLua && nRef!=LUA_NOREF)
		{
			CHECK_LUA_STACK(pLua);

			ScriptExporterManager().CheckObjectsTable(pLua, pObject->IsWeaked());

			// Avoid re-entrance by CheckObjectsTable
			if(pLua && nRef!=LUA_NOREF)
			{
				luaL_unref(pLua, -1, nRef);
			}
		}

		pObject = NULL;
		pLua    = NULL;
		nRef    = LUA_NOREF;
	}