2019-12-04 15:51:46 +08:00
|
|
|
#ifndef JS_ENGINE_H
|
|
|
|
#define JS_ENGINE_H
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QJSEngine>
|
2019-12-13 17:45:27 +08:00
|
|
|
#include <QObject>
|
2019-12-04 17:39:41 +08:00
|
|
|
#include <QResource>
|
2019-12-04 15:51:46 +08:00
|
|
|
|
|
|
|
#include "constant.h"
|
2019-12-04 16:44:30 +08:00
|
|
|
#include "native/native_bridge.h"
|
2019-12-04 15:51:46 +08:00
|
|
|
#include "native/native_empty.h"
|
|
|
|
#include "native/native_log.h"
|
|
|
|
#include "native/native_timer.h"
|
|
|
|
|
|
|
|
class JSEngine : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2019-12-04 16:44:30 +08:00
|
|
|
QJSEngine *engine = new QJSEngine();
|
|
|
|
|
2019-12-04 15:51:46 +08:00
|
|
|
JSEngine(QObject *parent = nullptr) : QObject(parent) {
|
|
|
|
initJSEngine();
|
|
|
|
injectGlobal();
|
|
|
|
initDoricRuntime();
|
|
|
|
}
|
|
|
|
|
2019-12-13 17:45:27 +08:00
|
|
|
void prepareContext(int contextId, QString *script) {
|
2019-12-04 15:51:46 +08:00
|
|
|
QString contextIdString = QString::number(contextId);
|
|
|
|
QString source = QString(Constant::TEMPLATE_CONTEXT_CREATE)
|
|
|
|
.replace("%s1", *script)
|
|
|
|
.replace("%s2", contextIdString)
|
|
|
|
.replace("%s3", contextIdString)
|
|
|
|
.replace("%s4", contextIdString);
|
|
|
|
QJSValue result = engine->evaluate(source, "context://" + contextIdString);
|
|
|
|
qDebug() << "context://" + contextIdString + " result: " + result.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyContext(int contextId) {
|
|
|
|
QString contextIdString = QString::number(contextId);
|
|
|
|
QString source = QString(Constant::TEMPLATE_CONTEXT_DESTROY)
|
|
|
|
.replace("%s", contextIdString);
|
|
|
|
QJSValue result = engine->evaluate(source, "_context://" + contextIdString);
|
|
|
|
qDebug() << "context://" + contextIdString + " result: " + result.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-12-04 16:44:30 +08:00
|
|
|
NativeLog* nativeLog = new NativeLog();
|
|
|
|
NativeTimer* nativeTimer = new NativeTimer(engine);
|
|
|
|
NativeEmpty* nativeEmpty = new NativeEmpty();
|
|
|
|
NativeBridge* nativeBridge = new NativeBridge();
|
|
|
|
|
2019-12-04 15:51:46 +08:00
|
|
|
void initJSEngine() {
|
|
|
|
engine->installExtensions(QJSEngine::AllExtensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void injectGlobal() {
|
2019-12-04 16:44:30 +08:00
|
|
|
QJSValue log = engine->newQObject(nativeLog);
|
|
|
|
engine->globalObject().setProperty(Constant::INJECT_LOG, log.property("function"));
|
|
|
|
|
|
|
|
QJSValue timer = engine->newQObject(nativeTimer);
|
|
|
|
engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, timer.property("setTimer"));
|
|
|
|
engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, timer.property("clearTimer"));
|
2019-12-04 15:51:46 +08:00
|
|
|
|
2019-12-04 16:44:30 +08:00
|
|
|
QJSValue empty = engine->newQObject(nativeEmpty);
|
|
|
|
engine->globalObject().setProperty(Constant::INJECT_EMPTY, empty.property("function"));
|
2019-12-04 15:51:46 +08:00
|
|
|
|
2019-12-04 16:44:30 +08:00
|
|
|
QJSValue bridge = engine->newQObject(nativeBridge);
|
|
|
|
engine->globalObject().setProperty(Constant::INJECT_BRIDGE, bridge.property("function"));
|
2019-12-04 15:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void initDoricRuntime() {
|
|
|
|
{
|
2019-12-04 17:39:41 +08:00
|
|
|
QResource resource(":/doric/doric-sandbox.js");
|
|
|
|
QFile *file = new QFile(resource.fileName());
|
2019-12-04 15:51:46 +08:00
|
|
|
file->open(QFile::ReadOnly | QFile::Text);
|
|
|
|
QTextStream in(file);
|
|
|
|
QString script = in.readAll();
|
|
|
|
file->close();
|
|
|
|
delete file;
|
|
|
|
|
|
|
|
QJSValue result = engine->evaluate(script, "doric-sandbox.js");
|
|
|
|
qDebug() << "doric-sandbox.js result: " + result.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-12-04 17:39:41 +08:00
|
|
|
QResource resource(":/doric/doric-lib.js");
|
|
|
|
QFile *file = new QFile(resource.fileName());
|
2019-12-04 15:51:46 +08:00
|
|
|
file->open(QFile::ReadOnly | QFile::Text);
|
|
|
|
QTextStream in(file);
|
|
|
|
QString script = in.readAll();
|
|
|
|
file->close();
|
|
|
|
delete file;
|
|
|
|
|
|
|
|
QString lib = QString(Constant::TEMPLATE_MODULE)
|
|
|
|
.replace("%s1", "doric")
|
|
|
|
.replace("%s2", script);
|
|
|
|
QJSValue result = engine->evaluate(lib, "doric-lib.js");
|
|
|
|
qDebug() << "doric-lib.js result: " + result.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // JS_ENGINE_H
|