@@ -87,7 +87,7 @@ ScriptEngineWorker::ScriptEngineWorker(trikControl::BrickInterface &brick
8787 , mMailbox(mailbox)
8888 , mGamepad(gamepad)
8989 , mScriptControl(scriptControl)
90- , mThreadingVariable (this , scriptControl)
90+ , mThreading (this , scriptControl)
9191 , mDirectScriptsEngine(nullptr )
9292 , mScriptId(0 )
9393 , mState(ready)
@@ -102,48 +102,65 @@ void ScriptEngineWorker::brickBeep()
102102 mBrick .playSound (trikKernel::Paths::mediaPath () + " media/beep_soft.wav" );
103103}
104104
105- void ScriptEngineWorker::reset ()
105+ void ScriptEngineWorker::stopScript ()
106106{
107- if (mState == resetting) {
107+ if (mState == stopping) {
108+ // Already stopping, so we can do nothing.
108109 return ;
109110 }
110111
111- while (mState == starting) {
112- QThread::yieldCurrentThread ();
113- }
114-
115112 if (mState == ready) {
116- // / Engine is ready for execution, but we need to clear brick state before we go.
117- QLOG_INFO () << " ScriptEngineWorker: 'soft' reset" ;
118- mState = resetting;
119- clearMailboxAndGamepadState ();
120- clearBrickState ();
121- mState = ready;
113+ // Engine is ready for execution.
122114 return ;
123115 }
124116
125- QLOG_INFO () << " ScriptEngineWorker: reset started" ;
117+ while (mState == starting) {
118+ // Some script is starting right now, so we are in inconsistent state. Let it start, then stop it.
119+ QThread::yieldCurrentThread ();
120+ }
121+
122+ QLOG_INFO () << " ScriptEngineWorker: stopping script" ;
126123
127- mState = resetting ;
124+ mState = stopping ;
128125
129126 mScriptControl .reset ();
130127
131- mThreadingVariable .reset ();
132- clearMailboxAndGamepadState ();
128+ mThreading .reset ();
129+ mMailbox -> stopWaiting ();
133130
134131 if (mDirectScriptsEngine ) {
135132 mDirectScriptsEngine ->abortEvaluation ();
136133 QLOG_INFO () << " ScriptEngineWorker : ending interpretation" ;
137134 emit completed (mDirectScriptsEngine ->hasUncaughtException ()
138- ? mDirectScriptsEngine ->uncaughtException ().toString ()
139- : " " , mScriptId );
135+ ? mDirectScriptsEngine ->uncaughtException ().toString ()
136+ : " "
137+ , mScriptId );
138+
140139 mDirectScriptsEngine ->deleteLater ();
141140 mDirectScriptsEngine = nullptr ;
142141 }
143142
144- clearBrickState ();
145143 mState = ready;
146- QLOG_INFO () << " ScriptEngineWorker: reset complete" ;
144+
145+ // / @todo: is it actually stopped?
146+
147+ QLOG_INFO () << " ScriptEngineWorker: stopping complete" ;
148+ }
149+
150+ void ScriptEngineWorker::resetBrick ()
151+ {
152+ QLOG_INFO () << " Stopping robot" ;
153+
154+ if (mMailbox ) {
155+ mMailbox ->stopWaiting ();
156+ mMailbox ->clearQueue ();
157+ }
158+
159+ if (mGamepad ) {
160+ mGamepad ->reset ();
161+ }
162+
163+ mBrick .reset ();
147164}
148165
149166void ScriptEngineWorker::run (const QString &script, int scriptId)
@@ -154,11 +171,10 @@ void ScriptEngineWorker::run(const QString &script, int scriptId)
154171
155172void ScriptEngineWorker::doRun (const QString &script)
156173{
157- mThreadingVariable .startMainThread (script);
174+ mThreading .startMainThread (script);
158175 mState = running;
159- mThreadingVariable .waitForAll ();
160- const QString error = mThreadingVariable .errorMessage ();
161- reset ();
176+ mThreading .waitForAll ();
177+ const QString error = mThreading .errorMessage ();
162178 QLOG_INFO () << " ScriptEngineWorker: evaluation ended with message" << error;
163179 emit completed (error, mScriptId );
164180}
@@ -167,7 +183,7 @@ void ScriptEngineWorker::runDirect(const QString &command, int scriptId)
167183{
168184 if (!mScriptControl .isInEventDrivenMode ()) {
169185 QLOG_INFO () << " ScriptEngineWorker: starting interpretation" ;
170- reset ();
186+ stopScript ();
171187 startScriptEvaluation (scriptId);
172188 mDirectScriptsEngine = createScriptEngine (false );
173189 mScriptControl .run ();
@@ -182,7 +198,12 @@ void ScriptEngineWorker::doRunDirect(const QString &command)
182198 if (mDirectScriptsEngine ) {
183199 mDirectScriptsEngine ->evaluate (command);
184200 if (mDirectScriptsEngine ->hasUncaughtException ()) {
185- reset ();
201+ QLOG_INFO () << " ScriptEngineWorker : ending interpretation of direct script" ;
202+ emit completed (mDirectScriptsEngine ->hasUncaughtException ()
203+ ? mDirectScriptsEngine ->uncaughtException ().toString ()
204+ : " " , mScriptId );
205+ mDirectScriptsEngine ->deleteLater ();
206+ mDirectScriptsEngine = nullptr ;
186207 }
187208 }
188209}
@@ -203,7 +224,7 @@ void ScriptEngineWorker::onScriptRequestingToQuit()
203224 mScriptControl .run ();
204225 }
205226
206- reset ();
227+ stopScript ();
207228}
208229
209230QScriptEngine * ScriptEngineWorker::createScriptEngine (bool supportThreads)
@@ -242,7 +263,7 @@ QScriptEngine * ScriptEngineWorker::createScriptEngine(bool supportThreads)
242263 }
243264
244265 if (supportThreads) {
245- engine->globalObject ().setProperty (" Threading" , engine->newQObject (&mThreadingVariable ));
266+ engine->globalObject ().setProperty (" Threading" , engine->newQObject (&mThreading ));
246267 }
247268
248269 evalSystemJs (engine);
@@ -290,20 +311,3 @@ void ScriptEngineWorker::evalSystemJs(QScriptEngine * const engine) const
290311 engine->globalObject ().setProperty (functionName, functionValue);
291312 }
292313}
293-
294- void ScriptEngineWorker::clearBrickState ()
295- {
296- mBrick .reset ();
297- }
298-
299- void ScriptEngineWorker::clearMailboxAndGamepadState ()
300- {
301- if (mMailbox ) {
302- mMailbox ->reset ();
303- }
304-
305- if (mGamepad ) {
306- mGamepad ->reset ();
307- }
308- }
309-
0 commit comments