From b82a00dd902dd82b6da34ce30a49729286aeab1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Wed, 4 Dec 2019 16:44:30 +0800 Subject: [PATCH] pointer delete under memory management --- doric/constant.cpp | 2 ++ doric/constant.h | 2 ++ doric/context.h | 16 ++++++++++++++++ doric/driver/driver.h | 1 + doric/driver/native_driver.cpp | 4 ++++ doric/driver/native_driver.h | 2 ++ doric/engine/js_engine.h | 27 ++++++++++++++++++--------- doric/main.cpp | 21 +++++++++++++-------- doric/main.qml | 20 +++++++++++++++++--- doric/native/native_bridge.h | 4 ++-- 10 files changed, 77 insertions(+), 22 deletions(-) diff --git a/doric/constant.cpp b/doric/constant.cpp index e22a425a..b6452a0c 100644 --- a/doric/constant.cpp +++ b/doric/constant.cpp @@ -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__"); diff --git a/doric/constant.h b/doric/constant.h index a0d27026..e6d32a66 100644 --- a/doric/constant.h +++ b/doric/constant.h @@ -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 diff --git a/doric/context.h b/doric/context.h index a2b15369..e53d8956 100644 --- a/doric/context.h +++ b/doric/context.h @@ -2,6 +2,10 @@ #define CONTEXT_H #include +#include +#include + +#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 diff --git a/doric/driver/driver.h b/doric/driver/driver.h index 23923616..42a2c829 100644 --- a/doric/driver/driver.h +++ b/doric/driver/driver.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* arguments) = 0; virtual ~Driver() = default; }; diff --git a/doric/driver/native_driver.cpp b/doric/driver/native_driver.cpp index 295c6d7f..716d88a4 100644 --- a/doric/driver/native_driver.cpp +++ b/doric/driver/native_driver.cpp @@ -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* arguments) { + +} diff --git a/doric/driver/native_driver.h b/doric/driver/native_driver.h index cac8528c..542ae4eb 100644 --- a/doric/driver/native_driver.h +++ b/doric/driver/native_driver.h @@ -28,6 +28,8 @@ public: void createContext(int contextId, QString *script) override; void destroyContext(int contextId) override; + + void invokeContextEntityMethod(int contextId, QString* method, QVector* arguments) override; }; #endif // NATIVE_DRIVER_H diff --git a/doric/engine/js_engine.h b/doric/engine/js_engine.h index e415ebb3..1534f827 100644 --- a/doric/engine/js_engine.h +++ b/doric/engine/js_engine.h @@ -6,6 +6,7 @@ #include #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() { diff --git a/doric/main.cpp b/doric/main.cpp index b45d3fc1..a366b549 100644 --- a/doric/main.cpp +++ b/doric/main.cpp @@ -20,14 +20,19 @@ 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); - QString script = in.readAll(); - file->close(); - delete file; + { + QFile* file = new QFile("/Users/maverick/Workspace/doric/demo/bundle/src/Snake.js"); + file->open(QFile::ReadOnly | QFile::Text); + QTextStream in(file); + QString script = in.readAll(); + 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(); } diff --git a/doric/main.qml b/doric/main.qml index 45ee20a2..0c5a78dc 100644 --- a/doric/main.qml +++ b/doric/main.qml @@ -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"; + } } diff --git a/doric/native/native_bridge.h b/doric/native/native_bridge.h index 6dd83041..e64a64c2 100644 --- a/doric/native/native_bridge.h +++ b/doric/native/native_bridge.h @@ -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; } };