-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlcustomcode.cpp
147 lines (102 loc) · 6.2 KB
/
xmlcustomcode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "xmlcustomcode.h"
XmlCustomCode* XmlCustomCode::uniqueInstance = NULL;
QScriptValue echo(QScriptContext *context, QScriptEngine*)
{
std::cout << context->argument(0).toString().toUtf8().constData() << std::endl;
return "";
}
XmlCustomCode::XmlCustomCode(): numThreads(QThread::idealThreadCount())
{
myThreadPool.setMaxThreadCount(numThreads);
myThreadPool.setExpiryTimeout(-1); // don't let threads expire
// create individual thread script engines
this->jsScriptEngines.reserve(this->numThreads);
QString jsxmlString;
QFile jsxmlfile(":/resources/libs/jsxml.js");
jsxmlfile.open(QFile::ReadOnly | QFile::Text);
jsxmlString=QTextStream(&jsxmlfile).readAll();
for(int i=0; i<this->numThreads; i++){
jsCustomCodeEngine e;
e.scriptEngine = new QScriptEngine();
e.jsFunction = new QScriptValue();
// main needs to be called so the user code is evaluated
// alternatively you can do: myFunc=engine.evaluate('(function main(){})'); myFunc.call();
// Note the () around the function
e.getXmlDataFunction = new QScriptValue(e.scriptEngine->evaluate("(function getXmlData() { return $xmlData; })"));
e.setXmlDataFunction = new QScriptValue(e.scriptEngine->evaluate("(function setXmlData(newXmlData) { $xmlData=newXmlData; })"));
e.mutexForEngine = new QMutex;
// Add echo function so user can debug the code
QScriptValue echoFunction = e.scriptEngine->newFunction(echo);
e.scriptEngine->globalObject().setProperty("echo", echoFunction);
// Add the js library for XmlEditing
e.scriptEngine->evaluate(jsxmlString);
this->jsScriptEngines.append(e);
}
}
XmlCustomCode* XmlCustomCode::getInstance(){
if (uniqueInstance==NULL){ // allow only one instance
uniqueInstance = new XmlCustomCode();
}
return uniqueInstance;
}
void XmlCustomCode::executeCustomCode(const QString &jsString, const QVector<QString> &filesToProcess, const bool backupsEnabled, const bool verboseEnabled){
// Reconstruct script functions
for(int i=0; i<this->numThreads; i++){
*(jsScriptEngines[i].jsFunction) = jsScriptEngines.at(i).scriptEngine->evaluate("(function main() {"+jsString+"})");
}
QString currXmlFileString;
QScriptValue engineResult; // variable to check for js_errors
double elapsed_secs=0; // elapsed seconds that a user script took
clock_t begin; // seconds that a script started
// Single tread if small number of files or number of threads = 1
if(filesToProcess.size()<=CUSTOM_FILES_PER_THREAD || numThreads == 1){
jsCustomCodeEngine &jsEngine = this->jsScriptEngines.first();
// Process all XmlFiles
for(int i=0; i<filesToProcess.size(); i++){
customCodeUnwinding(filesToProcess.at(i),currXmlFileString,*jsEngine.scriptEngine,begin,elapsed_secs,engineResult,
*jsEngine.jsFunction,*jsEngine.getXmlDataFunction,*jsEngine.setXmlDataFunction,backupsEnabled,verboseEnabled);
}
}
else{ // Multithread if there are many files
// Process all XmlFiles
QVector<QFuture<void>> executingThreads;
for(int i=0, iteration = 0; i<=filesToProcess.size()-CUSTOM_FILES_PER_THREAD; i+=CUSTOM_FILES_PER_THREAD, ++iteration){
executingThreads << QtConcurrent::run(&this->myThreadPool, [=]()
{
const int indexJsEngine = iteration % this->numThreads;
jsCustomCodeEngine &jsEngine = this->jsScriptEngines[indexJsEngine];
jsEngine.mutexForEngine->lock();
QString currXmlFileStringThread;
QScriptValue engineResultThread; // variable to check for js_errors
double elapsedSecsThread=0; // elapsed seconds that a user script took
clock_t beginThread; // seconds that a script started
customCodeUnwinding(filesToProcess.at(i),currXmlFileStringThread,*jsEngine.scriptEngine,beginThread,elapsedSecsThread,engineResultThread,
*jsEngine.jsFunction,*jsEngine.getXmlDataFunction,*jsEngine.setXmlDataFunction,backupsEnabled,verboseEnabled);
customCodeUnwinding(filesToProcess.at(i+1),currXmlFileStringThread,*jsEngine.scriptEngine,beginThread,elapsedSecsThread,engineResultThread,
*jsEngine.jsFunction,*jsEngine.getXmlDataFunction,*jsEngine.setXmlDataFunction,backupsEnabled,verboseEnabled);
customCodeUnwinding(filesToProcess.at(i+2),currXmlFileStringThread,*jsEngine.scriptEngine,beginThread,elapsedSecsThread,engineResultThread,
*jsEngine.jsFunction,*jsEngine.getXmlDataFunction,*jsEngine.setXmlDataFunction,backupsEnabled,verboseEnabled);
customCodeUnwinding(filesToProcess.at(i+3),currXmlFileStringThread,*jsEngine.scriptEngine,beginThread,elapsedSecsThread,engineResultThread,
*jsEngine.jsFunction,*jsEngine.getXmlDataFunction,*jsEngine.setXmlDataFunction,backupsEnabled,verboseEnabled);
jsEngine.mutexForEngine->unlock();
});
}
// Wait for all threads to finish
for(QFuture<void> &f :executingThreads){
f.waitForFinished();
}
if(filesToProcess.size()%CUSTOM_FILES_PER_THREAD!=0){
int alreadyProcessedFiles=(filesToProcess.size()/CUSTOM_FILES_PER_THREAD)*CUSTOM_FILES_PER_THREAD;
jsCustomCodeEngine &jsEngine = this->jsScriptEngines.first();
for(int i=alreadyProcessedFiles; i<filesToProcess.size(); i++){
customCodeUnwinding(filesToProcess.at(i),currXmlFileString,*jsEngine.scriptEngine,begin,elapsed_secs,engineResult,
*jsEngine.jsFunction,*jsEngine.getXmlDataFunction,*jsEngine.setXmlDataFunction,backupsEnabled,verboseEnabled);
}
}
}
}
void XmlCustomCode::displayJsException(QScriptEngine &engine, QScriptValue &engineResult){
if (engine.hasUncaughtException()) {
UtilXmlTools::displayErrorMessage("@CUSTOM_CODE","Uncaught js exception (user code) at line " +QString::number(engine.uncaughtExceptionLineNumber()) + ":\n" + engineResult.toString());
}
}