@@ -59,7 +59,6 @@ ScriptEngineWorker::ScriptEngineWorker(trikControl::Brick &brick, QString const
5959 , mBrick(brick)
6060 , mThreadingVariable(*this )
6161 , mStartDirPath(startDirPath)
62- , mCloned(false )
6362{
6463 connect (&mBrick , SIGNAL (quitSignal ()), this , SLOT (onScriptRequestingToQuit ()));
6564}
@@ -86,7 +85,6 @@ ScriptEngineWorker &ScriptEngineWorker::clone()
8685 QScriptValue globalObject = result->mEngine ->globalObject ();
8786 Utils::copyRecursivelyTo (mEngine ->globalObject (), globalObject, result->mEngine );
8887 result->mEngine ->setGlobalObject (globalObject);
89- result->mCloned = true ;
9088 return *result;
9189}
9290
@@ -104,11 +102,15 @@ void ScriptEngineWorker::run(QString const &script, bool inEventDrivenMode, QStr
104102 }
105103
106104 mThreadingVariable .setCurrentScript (script);
107- mEngine ->evaluate (function.isEmpty () ? script : QString (" %1\n %2();" ).arg (script, function));
105+ QRegExp const functionRegexp (QString (
106+ " (.*%1\\ s*=\\ s*\\ w*\\ s*function\\ (.*\\ ).*)|(.*function\\ s+%1\\ s*\\ (.*\\ ).*)" ).arg (function));
107+ bool const needCallFunction = !function.isEmpty () && functionRegexp.exactMatch (script)
108+ && !script.trimmed ().endsWith (function + " ();" );
109+ mEngine ->evaluate (needCallFunction ? QString (" %1\n %2();" ).arg (script, function) : script);
108110
109111 if (!mBrick .isInEventDrivenMode ()) {
110112 mBrick .stop ();
111- if (!mCloned ) {
113+ if (!dynamic_cast <ScriptEngineWorker *>( parent ()) ) {
112114 // Only main thread must wait for others
113115 mThreadingVariable .waitForAll ();
114116 }
0 commit comments