private void Execute(int pExecutions)
{
if (!compilationTurnedOn)
{
return;
}
#if LOG
D.Log("Time to execute program " + this.ToString() + " " + pExecutions + " steps, returnFromExternalFunctionCall: " + _sprakRunner.returnFromExternalFunctionCall);
#endif
//System.Diagnostics.StackTrace t = new System.Diagnostics.StackTrace();
//D.Log("Stacktrace: " + t.ToString());
if (!_sprakRunner.isStarted)
{
bool success = _sprakRunner.Start();
if (!success)
{
D.Log(this.ToString() + " failed to start, will not execute");
//StopAndReset(); // TODO: this is new
PrintErrorsToD();
return;
}
#if LOG
else
{
D.Log(this + " Started!");
}
#endif
}
for (int i = 0; i < pExecutions; i++)
{
InterpreterTwo.Status s = _sprakRunner.Step();
if (sleepTimer > 0f)
{
#if LOG
D.Log("Program " + this.ToString() + " starts sleeping");
#endif
break;
}
if (waitingForInput)
{
#if LOG
D.Log("Program " + this.ToString() + " starts waiting for input");
#endif
break;
}
if (waitForNextFrame)
{
#if LOG
D.Log("Program " + this.ToString() + " starts waiting for next frame");
#endif
waitForNextFrame = false;
break;
}
if (s == InterpreterTwo.Status.FINISHED || _sprakRunner.returnFromExternalFunctionCall)
{
#if LOG
D.Log("Execution of program " + this.ToString() + " finished");
#endif
if (remoteCaller != null)
{
if (remoteCaller.uniqueCompilationId != callersUniqueCompilationId)
{
D.Log("The uniqueExecutionId of " + this.ToString() + " has changed after it called the remote function on " + remoteCaller.ToString());
}
else
{
var retVal = _sprakRunner.GetFinalReturnValue();
#if LOG
D.Log("Has remoteCaller, retVal: " + retVal + " of type " + retVal.GetType());
#endif
remoteCaller.OnReturnValue(retVal);
}
}
else if (_mockProgram != null)
{
var retVal = _sprakRunner.GetFinalReturnValue();
#if LOG
D.Log("Has mock remoteCaller, retVal: " + retVal);
#endif
_mockProgram.OnReturnValue(retVal);
}
else
{
#if LOG
D.Log(this.ToString() + " has no remoteCaller, this function has no listener");
#endif
}
StopAndReset();
break;
}
else if (s == InterpreterTwo.Status.ERROR)
{
isOn = false;
//StopAndReset(); // TODO: this is new
PrintErrorsToD();
break;
}
}
}