pointer delete under memory management

This commit is contained in:
王劲鹏 2019-12-04 16:44:30 +08:00
parent dbdd1770ef
commit b82a00dd90
10 changed files with 77 additions and 22 deletions

View File

@ -30,3 +30,5 @@ const QString Constant::TEMPLATE_CONTEXT_DESTROY = QString("doric.jsReleaseConte
const QString Constant::GLOBAL_DORIC = QString("doric"); const QString Constant::GLOBAL_DORIC = QString("doric");
const QString Constant::DORIC_TIMER_CALLBACK = QString("jsCallbackTimer"); const QString Constant::DORIC_TIMER_CALLBACK = QString("jsCallbackTimer");
const QString Constant::DORIC_ENTITY_INIT = QString("__init__");

View File

@ -19,6 +19,8 @@ public:
static const QString GLOBAL_DORIC; static const QString GLOBAL_DORIC;
static const QString DORIC_TIMER_CALLBACK; static const QString DORIC_TIMER_CALLBACK;
static const QString DORIC_ENTITY_INIT;
}; };
#endif // CONSTANT_H #endif // CONSTANT_H

View File

@ -2,6 +2,10 @@
#define CONTEXT_H #define CONTEXT_H
#include <QString> #include <QString>
#include <QJsonObject>
#include <QJsonDocument>
#include "constant.h"
#include "driver/driver.h" #include "driver/driver.h"
#include "driver/native_driver.h" #include "driver/native_driver.h"
@ -19,6 +23,18 @@ public:
this->contextId = contextId; this->contextId = contextId;
this->source = source; 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 #endif // CONTEXT_H

View File

@ -8,6 +8,7 @@ class Driver {
public: public:
virtual void createContext(int contextId, QString* script) = 0; virtual void createContext(int contextId, QString* script) = 0;
virtual void destroyContext(int contextId) = 0; virtual void destroyContext(int contextId) = 0;
virtual void invokeContextEntityMethod(int contextId, QString* method, QVector<QObject>* arguments) = 0;
virtual ~Driver() = default; virtual ~Driver() = default;
}; };

View File

@ -11,3 +11,7 @@ void NativeDriver::createContext(int contextId, QString *script) {
void NativeDriver::destroyContext(int contextId) { void NativeDriver::destroyContext(int contextId) {
jsEngine->destroyContext(contextId); jsEngine->destroyContext(contextId);
} }
void NativeDriver::invokeContextEntityMethod(int contextId, QString* method, QVector<QObject>* arguments) {
}

View File

@ -28,6 +28,8 @@ public:
void createContext(int contextId, QString *script) override; void createContext(int contextId, QString *script) override;
void destroyContext(int contextId) override; void destroyContext(int contextId) override;
void invokeContextEntityMethod(int contextId, QString* method, QVector<QObject>* arguments) override;
}; };
#endif // NATIVE_DRIVER_H #endif // NATIVE_DRIVER_H

View File

@ -6,6 +6,7 @@
#include <QJSEngine> #include <QJSEngine>
#include "constant.h" #include "constant.h"
#include "native/native_bridge.h"
#include "native/native_empty.h" #include "native/native_empty.h"
#include "native/native_log.h" #include "native/native_log.h"
#include "native/native_timer.h" #include "native/native_timer.h"
@ -14,7 +15,8 @@ class JSEngine : public QObject {
Q_OBJECT Q_OBJECT
public: public:
QJSEngine *engine; QJSEngine *engine = new QJSEngine();
JSEngine(QObject *parent = nullptr) : QObject(parent) { JSEngine(QObject *parent = nullptr) : QObject(parent) {
initJSEngine(); initJSEngine();
injectGlobal(); injectGlobal();
@ -41,21 +43,28 @@ public:
} }
private: private:
NativeLog* nativeLog = new NativeLog();
NativeTimer* nativeTimer = new NativeTimer(engine);
NativeEmpty* nativeEmpty = new NativeEmpty();
NativeBridge* nativeBridge = new NativeBridge();
void initJSEngine() { void initJSEngine() {
engine = new QJSEngine();
engine->installExtensions(QJSEngine::AllExtensions); engine->installExtensions(QJSEngine::AllExtensions);
} }
void injectGlobal() { void injectGlobal() {
QJSValue nativeLog = engine->newQObject(new NativeLog()); QJSValue log = engine->newQObject(nativeLog);
engine->globalObject().setProperty(Constant::INJECT_LOG, nativeLog.property("function")); engine->globalObject().setProperty(Constant::INJECT_LOG, log.property("function"));
QJSValue nativeTimer = engine->newQObject(new NativeTimer(engine)); QJSValue timer = engine->newQObject(nativeTimer);
engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, nativeTimer.property("setTimer")); engine->globalObject().setProperty(Constant::INJECT_TIMER_SET, timer.property("setTimer"));
engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, nativeTimer.property("clearTimer")); engine->globalObject().setProperty(Constant::INJECT_TIMER_CLEAR, timer.property("clearTimer"));
QJSValue nativeEmpty = engine->newQObject(new NativeEmpty()); QJSValue empty = engine->newQObject(nativeEmpty);
engine->globalObject().setProperty(Constant::INJECT_EMPTY, nativeEmpty.property("function")); 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() { void initDoricRuntime() {

View File

@ -20,14 +20,19 @@ int main(int argc, char *argv[])
}, Qt::QueuedConnection); }, Qt::QueuedConnection);
engine.load(url); engine.load(url);
QFile* file = new QFile("/Users/maverick/Workspace/doric/demo/bundle/src/Snake.js"); {
file->open(QFile::ReadOnly | QFile::Text); QFile* file = new QFile("/Users/maverick/Workspace/doric/demo/bundle/src/Snake.js");
QTextStream in(file); file->open(QFile::ReadOnly | QFile::Text);
QString script = in.readAll(); QTextStream in(file);
file->close(); QString script = in.readAll();
delete file; file->close();
delete file;
QString* source = new QString("Snake.js");
Context *context = ContextManager::getInstance()->createContext(&script, source);
context->init(180, 320);
delete source;
}
QString* source = new QString("Snake.js");
ContextManager::getInstance()->createContext(&script, source);
return app.exec(); return app.exec();
} }

View File

@ -1,9 +1,23 @@
import QtQuick 2.6 import QtQuick 2.6
import QtQuick.Controls 2.13
import QtQuick.Window 2.2 import QtQuick.Window 2.2
Window { Window {
visible: true visible: true
width: 640 width: 360;
height: 480 height: 640;
title: qsTr("Hello World") title: qsTr("Hello Doric");
StackView {
id: stack;
anchors.centerIn: parent;
initialItem: mainView;
width: 360;
height: 640;
}
Rectangle {
id: mainView;
color: "lightgreen";
}
} }

View File

@ -10,8 +10,8 @@ class NativeBridge : public QObject {
public: public:
NativeBridge(QObject *parent = nullptr) : QObject(parent) {} NativeBridge(QObject *parent = nullptr) : QObject(parent) {}
Q_INVOKABLE void function() { Q_INVOKABLE void function(int contextId) {
qDebug() << contextId;
} }
}; };