WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZScriptWorker.cpp
Go to the documentation of this file.
1 /*
2  * Zuble - A run-time system for QML/Javascript applications
3  * Copyright (C) 2013, 2014 Bob Dinitto
4  *
5  * Filename: ZScriptWorker.cpp
6  * Created on: 11/9/2014
7  * Author: Bob Dinitto
8  *
9  * Zuble is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include "ZScriptWorker.h"
26 #include "zglobal.h"
27 #include "ZblApp.h"
28 #include "ZblException.h"
29 #include "ZblThreadErr.h"
30 #include "ZblJsonHelper.h"
31 
32 #include <QDebug>
33 //#include <QVariant>
34 //#include <QJSValueIterator>
35 #include <QJsonDocument>
36 #include <QJsonObject>
37 #include <QJsonArray>
38 #include <QJsonValue>
39 
40 namespace Zbl
41 {
42 
43 ZScriptWorker::ZScriptWorker(ZScriptThread* container, QObject *parent) :
44  QObject(parent)
45 {
46  m_engine = new QJSEngine(this);
47  m_app = new ZblApp(m_engine, container, this);
48  m_error = m_engine->evaluate("new Error();");
50 
52 }
53 
55 {
56  delete m_json;
57 }
58 
60 {
61  m_engine->globalObject().setProperty("Zbl",m_engine->newQObject(m_app));
62 }
63 
65 {
67 }
68 
69 void ZScriptWorker::execScript(qulonglong requestID, const QString& script, QString userData)
70 {
71  m_script = script;
72 
73  m_result = QJSValue();
74 
75  QJSValue glob = m_engine->globalObject();
76 
77  try
78  {
79  QJSValue jsData = m_json->JsonTextToValue(userData);
80  glob.setProperty("userData", jsData);
81 
82  m_result = m_engine->evaluate(m_script, "ZScriptWorker", 1);
83 
84  if(m_result.isError())
85  {
86  // evaluation error
88  }
89 
90  if(m_app->isError())
91  {
92  // Zuble custom QObject exception error
93  m_result = m_app->error();
94  }
95 
96  // TBD: we should provide a fast path for string arrays to avoid
97  // the overhead of JSON conversion.
98 
99  // TBD: should we pass primitives like int and string?
100 
101  QString result = m_json->ValueToJson(m_result);
102  QJSValue jsUserData = glob.property("userData");
103  QString userReturnData = m_json->ValueToJson(jsUserData);
104 
105  glob.deleteProperty("userData");
106 
107  emit execComplete(requestID, result, userReturnData);
108  }
109  catch(ZblException ex)
110  {
111  // normal exception error
112 
113  m_result = m_app->error();
114  }
115 }
116 
117 } // Zbl
QString ValueToJson(QJSValue &value)
ZblApp * m_app
An object to represent the running Zuble application.
ZblJsonHelper * m_json
An object to perform QJSValue/JSON data conversions on the fly.
QJSValue m_error
A template javascript Error object.
void execScript(qulonglong requestID, const QString &script, QString userData=QString())
Executes the specified javascript program in the current thread and sends the execComplete signal whe...
QString m_script
The most recent script that was executed.
ZScriptWorker(ZScriptThread *container=nullptr, QObject *parent=nullptr)
virtual ~ZScriptWorker()
A thread class to support Zuble&#39;s background Javascript processing.
Definition: ZScriptThread.h:62
bool isError()
Determines if an error state exists for this thread.
Definition: ZblApp.cpp:291
QJSValue m_result
Result returned by the most recent script execution.
Definition: ZAndGate.cpp:6
static void zInit()
This method must be called once at the start of each thread that throws or catches ZblException objec...
void execComplete(qulonglong requestID, QString result, QString userData)
Sent when the a javascript program execution started with execScript() has been completed.
void onStarted()
Called when QThread has started running.
QJSValue JsonTextToValue(QString jsonText)
Converts objects between JSON and QJSValue domains for a specific QJSEngine instance.
Definition: ZblJsonHelper.h:17
The primary QML API to the Zuble plugin library. Zuble applications access this object through the ja...
Definition: ZblApp.h:81
QJSValue error()
returns the internal Error object for this thread saved in thread local storage.
Definition: ZblApp.cpp:306
QJSValue convertEvaluateError(const QJSValue &evaluateError)
Javascript syntax errors appear to be passing back a string object with the QJSValue error bit set...
Definition: ZblApp.cpp:317
QJSEngine * m_engine
The javascript engine for background processing.
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45