pointer delete under memory management
This commit is contained in:
parent
dbdd1770ef
commit
b82a00dd90
@ -30,3 +30,5 @@ const QString Constant::TEMPLATE_CONTEXT_DESTROY = QString("doric.jsReleaseConte
|
||||
|
||||
const QString Constant::GLOBAL_DORIC = QString("doric");
|
||||
const QString Constant::DORIC_TIMER_CALLBACK = QString("jsCallbackTimer");
|
||||
|
||||
const QString Constant::DORIC_ENTITY_INIT = QString("__init__");
|
||||
|
@ -19,6 +19,8 @@ public:
|
||||
|
||||
static const QString GLOBAL_DORIC;
|
||||
static const QString DORIC_TIMER_CALLBACK;
|
||||
|
||||
static const QString DORIC_ENTITY_INIT;
|
||||
};
|
||||
|
||||
#endif // CONSTANT_H
|
||||
|
@ -2,6 +2,10 @@
|
||||
#define CONTEXT_H
|
||||
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include "constant.h"
|
||||
#include "driver/driver.h"
|
||||
#include "driver/native_driver.h"
|
||||
|
||||
@ -19,6 +23,18 @@ public:
|
||||
this->contextId = contextId;
|
||||
this->source = source;
|
||||
}
|
||||
|
||||
void init(double width, double height) {
|
||||
QJsonObject* params = new QJsonObject();
|
||||
params->insert("width", width);
|
||||
params->insert("height", height);
|
||||
QJsonDocument* jsonDocument = new QJsonDocument();
|
||||
jsonDocument->setObject(*params);
|
||||
QString strJson(jsonDocument->toJson(QJsonDocument::Compact));
|
||||
|
||||
delete params;
|
||||
delete jsonDocument;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // CONTEXT_H
|
||||
|
@ -8,6 +8,7 @@ class Driver {
|
||||
public:
|
||||
virtual void createContext(int contextId, QString* script) = 0;
|
||||
virtual void destroyContext(int contextId) = 0;
|
||||
virtual void invokeContextEntityMethod(int contextId, QString* method, QVector<QObject>* arguments) = 0;
|
||||
|
||||
virtual ~Driver() = default;
|
||||
};
|
||||
|
@ -11,3 +11,7 @@ void NativeDriver::createContext(int contextId, QString *script) {
|
||||
void NativeDriver::destroyContext(int contextId) {
|
||||
jsEngine->destroyContext(contextId);
|
||||
}
|
||||
|
||||
void NativeDriver::invokeContextEntityMethod(int contextId, QString* method, QVector<QObject>* arguments) {
|
||||
|
||||
}
|
||||
|
@ -28,6 +28,8 @@ public:
|
||||
void createContext(int contextId, QString *script) override;
|
||||
|
||||
void destroyContext(int contextId) override;
|
||||
|
||||
void invokeContextEntityMethod(int contextId, QString* method, QVector<QObject>* arguments) override;
|
||||
};
|
||||
|
||||
#endif // NATIVE_DRIVER_H
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include <QJSEngine>
|
||||
|
||||
#include "constant.h"
|
||||
#include "native/native_bridge.h"
|
||||
#include "native/native_empty.h"
|
||||
#include "native/native_log.h"
|
||||
#include "native/native_timer.h"
|
||||
@ -14,7 +15,8 @@ class JSEngine : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QJSEngine *engine;
|
||||
QJSEngine *engine = new QJSEngine();
|
||||
|
||||
JSEngine(QObject *parent = nullptr) : QObject(parent) {
|
||||
initJSEngine();
|
||||
injectGlobal();
|
||||
@ -41,21 +43,28 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
NativeLog* nativeLog = new NativeLog();
|
||||
NativeTimer* nativeTimer = new NativeTimer(engine);
|
||||
NativeEmpty* nativeEmpty = new NativeEmpty();
|
||||
NativeBridge* nativeBridge = new NativeBridge();
|
||||
|
||||
void initJSEngine() {
|
||||
engine = new QJSEngine();
|
||||
engine->installExtensions(QJSEngine::AllExtensions);
|
||||
}
|
||||
|
||||
void injectGlobal() {
|
||||
QJSValue nativeLog = engine->newQObject(new NativeLog());
|
||||
engine->globalObject().setProperty(Constant::INJECT_LOG, nativeLog.property("function"));
|
||||
QJSValue log = engine->newQObject(nativeLog);
|
||||
engine->globalObject().setProperty(Constant::INJECT_LOG, log.property("function"));
|
||||
|
||||
QJSValue nativeTimer = engine->newQObject(new NativeTimer(engine));
|
||||
engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, nativeTimer.property("setTimer"));
|
||||
engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, nativeTimer.property("clearTimer"));
|
||||
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"));
|
||||
|
||||
QJSValue nativeEmpty = engine->newQObject(new NativeEmpty());
|
||||
engine->globalObject().setProperty(Constant::INJECT_EMPTY, nativeEmpty.property("function"));
|
||||
QJSValue empty = engine->newQObject(nativeEmpty);
|
||||
engine->globalObject().setProperty(Constant::INJECT_EMPTY, empty.property("function"));
|
||||
|
||||
QJSValue bridge = engine->newQObject(nativeBridge);
|
||||
engine->globalObject().setProperty(Constant::INJECT_BRIDGE, bridge.property("function"));
|
||||
}
|
||||
|
||||
void initDoricRuntime() {
|
||||
|
@ -20,6 +20,7 @@ int main(int argc, char *argv[])
|
||||
}, Qt::QueuedConnection);
|
||||
engine.load(url);
|
||||
|
||||
{
|
||||
QFile* file = new QFile("/Users/maverick/Workspace/doric/demo/bundle/src/Snake.js");
|
||||
file->open(QFile::ReadOnly | QFile::Text);
|
||||
QTextStream in(file);
|
||||
@ -28,6 +29,10 @@ int main(int argc, char *argv[])
|
||||
delete file;
|
||||
|
||||
QString* source = new QString("Snake.js");
|
||||
ContextManager::getInstance()->createContext(&script, source);
|
||||
Context *context = ContextManager::getInstance()->createContext(&script, source);
|
||||
context->init(180, 320);
|
||||
delete source;
|
||||
}
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
@ -1,9 +1,23 @@
|
||||
import QtQuick 2.6
|
||||
import QtQuick.Controls 2.13
|
||||
import QtQuick.Window 2.2
|
||||
|
||||
Window {
|
||||
visible: true
|
||||
width: 640
|
||||
height: 480
|
||||
title: qsTr("Hello World")
|
||||
width: 360;
|
||||
height: 640;
|
||||
title: qsTr("Hello Doric");
|
||||
|
||||
StackView {
|
||||
id: stack;
|
||||
anchors.centerIn: parent;
|
||||
initialItem: mainView;
|
||||
width: 360;
|
||||
height: 640;
|
||||
}
|
||||
|
||||
Rectangle {
|
||||
id: mainView;
|
||||
color: "lightgreen";
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ class NativeBridge : public QObject {
|
||||
public:
|
||||
NativeBridge(QObject *parent = nullptr) : QObject(parent) {}
|
||||
|
||||
Q_INVOKABLE void function() {
|
||||
|
||||
Q_INVOKABLE void function(int contextId) {
|
||||
qDebug() << contextId;
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user