csp::WorkResult::Enum csp::OpLua::Work( Host& host, CspTime_t dt ) { lua::LuaStack& stack = host.LuaState().GetStack(); stack.PushNumber( dt ); if ( !CallLua( stack, "Work", 1, 1 ) ) return WorkResult::FINISH; WorkResult::Enum workResult = WorkResult::FINISH; lua::LuaStackValue callResult = stack.GetTopValue(); if( callResult.IsNumber() ) { switch( callResult.GetInteger() ) { case WorkResult::FINISH: workResult = WorkResult::FINISH; break; case WorkResult::YIELD: workResult = WorkResult::YIELD; break; default: break; } } stack.Pop( 1 ); return workResult; }
csp::WorkResult::Enum csp::OpAlt::Evaluate( Host& host ) { lua::LuaStack& stack = host.LuaState().GetStack(); if( m_pCaseTriggered == NULL ) { bool allCasesClosed = SelectChannelProcessToTrigger( host ); if( allCasesClosed ) return WorkResult::FINISH; } else { if( m_argumentsMoved ) { m_argumentsMoved = false; StartTriggeredProcess( host ); UnrefArguments( stack ); UnrefChannels( stack ); } else if( m_process.IsRunning() ) { } else if( m_processRefKey != lua::LUA_NO_REF ) { UnrefProcess( stack ); return WorkResult::FINISH; } } return WorkResult::YIELD; }
void csp::OpAlt::Terminate( Host& host ) { lua::LuaStack stack = host.LuaState().GetStack(); m_process.Terminate( host ); DetachChannels(); UnrefChannels( stack ); UnrefArguments( stack ); UnrefClosures( stack ); UnrefProcess( stack ); }
csp::WorkResult::Enum csp::OpAlt::StartTriggeredProcess( Host& host ) { CORE_ASSERT( m_pCaseTriggered ); lua::LuaStack& stack = host.LuaState().GetStack(); lua::LuaState thread = stack.NewThread(); stack.GetTopValue().PushValue(); m_processRefKey = stack.RefInRegistry(); m_process.SetLuaThread( thread ); m_process.SetParentProcess( ThisProcess() ); lua::LuaStack threadStack = thread.GetStack(); threadStack.PushRegistryReferenced( m_pCaseTriggered->m_closureRefKey ); for( int i = 0; i < m_numArguments; ++i ) threadStack.PushRegistryReferenced( m_arguments[i].refKey ); int numArguments = m_numArguments == CSP_NO_ARGS ? 0 : m_numArguments; WorkResult::Enum result = m_process.StartEvaluation( host, numArguments ); UnrefClosures( stack ); return result; }
void csp::OpLua::Terminate( Host& host ) { lua::LuaStack& stack = host.LuaState().GetStack(); CallLua( stack, "Terminate", 0, 0 ); UnrefSelf( stack ); }