1 /*
2 	Actiona
3 	Copyright (C) 2005 Jonathan Mercier-Ganady
4 
5 	Actiona is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 3 of the License, or
8 	(at your option) any later version.
9 
10 	Actiona is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 	Contact : jmgr@jmgr.info
19 */
20 
21 #pragma once
22 
23 #include "executer_global.h"
24 
25 #include <QScriptEngineAgent>
26 #include <QStringList>
27 
28 namespace LibExecuter
29 {
30 	class EXECUTERSHARED_EXPORT ScriptAgent : public QObject, public QScriptEngineAgent
31 	{
32 		Q_OBJECT
33 
34 	public:
35 		enum Context
36 		{
37 			Unknown,
38 			ActionInit,
39 			Parameters,
40 			Actions
41 		};
42 
ScriptAgent(QScriptEngine * engine)43 		ScriptAgent(QScriptEngine *engine)
44 			: QScriptEngineAgent(engine),
45 			mCurrentParameter(-1),
46 			mCurrentLine(-1),
47 			mCurrentColumn(-1),
48 			mContext(Unknown),
49 			mPaused(false),
50 			mContinueExecution(true),
51 			mDebuggerAgent(nullptr),
52 			mEngineLevel(0)
53 																			{}
54 
setContext(Context context)55 		void setContext(Context context)									{ mContext = context; }
setCurrentParameter(int currentParameter)56 		void setCurrentParameter(int currentParameter)						{ mCurrentParameter = currentParameter; }
pause(bool pause)57 		void pause(bool pause)												{ mPaused = pause; }
setDebuggerAgent(QScriptEngineAgent * debuggerAgent)58 		void setDebuggerAgent(QScriptEngineAgent *debuggerAgent)			{ mDebuggerAgent = debuggerAgent;  }
59 		void stopExecution(bool emitSignal = true)							{ mContinueExecution = false; if(emitSignal) emit executionStopped(); }
currentLine()60 		int currentLine() const												{ return mCurrentLine; }
currentColumn()61 		int currentColumn() const											{ return mCurrentColumn; }
context()62 		Context context() const												{ return mContext; }
currentParameter()63 		int currentParameter() const										{ return mCurrentParameter; }
currentFile()64 		QString currentFile() const											{ return mFiles.count() > 0 ? mFiles.back() : QString(); }
65 
66 	signals:
67 		void executionStopped();
68 		void evaluationStarted();
69 		void evaluationStopped();
70 
71 	private:
72 		void contextPop() override ;
73 		void contextPush() override ;
74 		void exceptionCatch(qint64 scriptId, const QScriptValue &exception) override ;
75 		void exceptionThrow(qint64 scriptId, const QScriptValue &exception, bool hasHandler) override ;
76 		QVariant extension(Extension extension, const QVariant &argument = QVariant()) override ;
77 		void functionEntry(qint64 scriptId) override ;
78 		void functionExit(qint64 scriptId, const QScriptValue &returnValue) override ;
79 		void positionChange(qint64 scriptId, int lineNumber, int columnNumber) override ;
80 		void scriptLoad(qint64 id, const QString &program, const QString &fileName, int baseLineNumber) override ;
81 		void scriptUnload(qint64 id) override ;
82 		bool supportsExtension(Extension extension) const override ;
83 
84 	private:
85 		int mCurrentParameter;
86 		int mCurrentLine;
87 		int mCurrentColumn;
88 		QStringList mFiles;
89 		Context mContext;
90 		bool mPaused;
91 		bool mContinueExecution;
92 		QScriptEngineAgent *mDebuggerAgent;
93 		int mEngineLevel;
94 	};
95 }
96 
97