| 1 |
#ifndef XMLCUSTOMCODE_H |
| 2 |
#define XMLCUSTOMCODE_H |
| 3 |
|
| 4 |
#include "utilxmltools.h" |
| 5 |
|
| 6 |
#include <ctime> // script execution time calculation |
| 7 |
#include <QScriptEngine> |
| 8 |
#include <QTextStream> |
| 9 |
#include <QCoreApplication> |
| 10 |
|
| 11 |
#include <QThreadPool> |
| 12 |
#include <QtConcurrent/QtConcurrent> |
| 13 |
|
| 14 |
static constexpr double SLOW_SCRIPT_TIME = 0.1; // if a user script takes more than 0.1 seconds to execute give a warning. |
| 15 |
static constexpr int CUSTOM_FILES_PER_THREAD = 4; |
| 16 |
|
| 17 |
// Uses a singleton implementation (based on here: http://www.yolinux.com/TUTORIALS/C++Singleton.html) |
| 18 |
// which allows each thread to keep one script engine without always create/destruct them |
| 19 |
class XmlCustomCode |
| 20 |
{ |
| 21 |
public: |
| 22 |
static XmlCustomCode* getInstance(); |
| 23 |
void executeCustomCode(const QString &jsString, const QVector<QString> &filesToProcess, const bool backupsEnabled, const bool verboseEnabled); |
| 24 |
private: |
| 25 |
static XmlCustomCode* uniqueInstance; |
| 26 |
|
| 27 |
const int numThreads; |
| 28 |
QThreadPool myThreadPool; |
| 29 |
|
| 30 |
struct jsCustomCodeEngine{ |
| 31 |
/* No need to delete these... They will stay alive until program quits. */ |
| 32 |
QScriptEngine* scriptEngine; |
| 33 |
QScriptValue* jsFunction; |
| 34 |
QScriptValue* getXmlDataFunction; |
| 35 |
QScriptValue* setXmlDataFunction; |
| 36 |
QMutex *mutexForEngine; |
| 37 |
}; |
| 38 |
|
| 39 |
QVector<jsCustomCodeEngine> jsScriptEngines; |
| 40 |
|
| 41 |
XmlCustomCode(); // constructor is private (use getInstance) |
| 42 |
XmlCustomCode(XmlCustomCode const&); // copy constructor is private |
| 43 |
XmlCustomCode& operator=(XmlCustomCode const&); // assignment operator is private |
| 44 |
|
| 45 |
void displayJsException(QScriptEngine &engine, QScriptValue &engineResult); |
| 46 |
|
| 47 |
__attribute__((always_inline)) inline void customCodeUnwinding(const QString &fileName, QString &currXmlFileString, |
| 48 |
QScriptEngine &engine, clock_t &begin, double elapsed_secs, QScriptValue &engineResult, QScriptValue &jsFunction, |
| 49 |
QScriptValue &getXmlDataFunction, QScriptValue &setXmlDataFunction, const bool &backupsEnabled, const bool &verboseEnabled){ |
| 50 |
if(backupsEnabled){ |
| 51 |
UtilXmlTools::backupFile(fileName, verboseEnabled); |
| 52 |
} |
| 53 |
|
| 54 |
QFile currXmlFile(fileName); |
| 55 |
|
| 56 |
if(!currXmlFile.open(QFile::ReadOnly | QFile::Text)){ |
| 57 |
UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + fileName + "' file for read operation."); |
| 58 |
} |
| 59 |
|
| 60 |
currXmlFileString=QTextStream(&currXmlFile).readAll(); |
| 61 |
|
| 62 |
currXmlFile.close(); // close reading |
| 63 |
|
| 64 |
setXmlDataFunction.call(setXmlDataFunction,QScriptValueList() << currXmlFileString); |
| 65 |
|
| 66 |
begin = clock(); |
| 67 |
|
| 68 |
engineResult=jsFunction.call(); // main funtion allows to use return to exit prematurely from user code |
| 69 |
|
| 70 |
if(verboseEnabled){ |
| 71 |
elapsed_secs = double(clock() - begin) / CLOCKS_PER_SEC; |
| 72 |
|
| 73 |
// Warn the user if the script took much time |
| 74 |
if(elapsed_secs>SLOW_SCRIPT_TIME){ |
| 75 |
std::cout << "Warning: Slow javascript code detected.\n" << |
| 76 |
"Warning: Script execution seconds: " << elapsed_secs |
| 77 |
<< std::endl; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if (engine.hasUncaughtException()) { |
| 82 |
displayJsException(engine,engineResult); |
| 83 |
} |
| 84 |
|
| 85 |
if(!currXmlFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate)){ |
| 86 |
UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Error loading '" + fileName + "' file for @CUSTOM_CODE write operation."); |
| 87 |
} |
| 88 |
|
| 89 |
engineResult=getXmlDataFunction.call(); |
| 90 |
|
| 91 |
if (engine.hasUncaughtException()) { |
| 92 |
displayJsException(engine,engineResult); |
| 93 |
} |
| 94 |
|
| 95 |
QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file |
| 96 |
|
| 97 |
currXmlFile.close(); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
#endif // XMLCUSTOMCODE_H |