This commit is contained in:
王劲鹏 2021-01-28 17:06:40 +08:00 committed by osborn
parent 0be7a9679a
commit 93b7cc2457
53 changed files with 1020 additions and 774 deletions

View File

@ -1,73 +0,0 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

View File

@ -0,0 +1,15 @@
#ifndef ASYNC_CALL_H
#define ASYNC_CALL_H
#include <QThreadPool>
class AsyncCall {
public:
static void ensureRunInThreadPool(QThreadPool &threadPool, QRunnable *runnable)
{
}
};
#endif // ASYNC_CALL_H

View File

@ -0,0 +1,56 @@
#include "async_result.h"
AsyncResult::AsyncResult()
{
}
AsyncResult::AsyncResult(QJSValue result)
{
this->result = result;
}
void AsyncResult::setResult(QJSValue result)
{
this->result = result;
if (this->callback != NULL) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
void AsyncResult::setError(QJSValue exception)
{
this->result = exception;
if (this->callback != NULL) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
bool AsyncResult::hasResult()
{
return !result.equals(EMPTY);
}
QJSValue AsyncResult::getResult()
{
return this->result;
}
void AsyncResult::setCallback(Callback *callback)
{
this->callback = callback;
if (this->result.isError()) {
this->callback->onError(result);
this->callback->onFinish();
} else if (!result.equals(EMPTY)) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
SettableFuture* AsyncResult::synchronous()
{
return NULL;
}

View File

@ -1,60 +1,35 @@
#ifndef ASYNC_RESULT_H #ifndef ASYNCRESULT_H
#define ASYNC_RESULT_H #define ASYNCRESULT_H
#include <QDebug> #include <QJSValue>
#include <QVariant>
#include "callback.h" #include "callback.h"
#include "settable_future.h"
template <class R> static QJSValue EMPTY(QJSValue::NullValue);
class AsyncResult {
class AsyncResult
{
private: private:
QVariant result; QJSValue result = EMPTY;
Callback<R> *callback = nullptr; Callback *callback;
public: public:
AsyncResult() {} AsyncResult();
AsyncResult(R result) { AsyncResult(QJSValue result);
this->result.setValue(result);
}
void setResult(R result) { void setResult(QJSValue result);
this->result.setValue(result);
if (callback != nullptr) {
this->callback->onResult(result);
this->callback->onFinish();
}
}
void setError(QException exception) { void setError(QJSValue exception);
this->result->setValue(exception);
if (callback != nullptr) {
this->callback->onError(exception);
this->callback->onFinish();
}
}
bool hasResult() { bool hasResult();
qDebug() << result.typeName();
return !QString(result.typeName()).isEmpty();
}
R *getResult() { QJSValue getResult();
return static_cast<R*>(result.data());
}
void setCallback(Callback<R> *callback) { void setCallback(Callback *callback);
this->callback = callback;
if (QException *exception = static_cast<QException*>(result.data())) { SettableFuture *synchronous();
this->callback->onError(*exception);
this->callback->onFinish();
} else if (hasResult()) {
this->callback->onResult(*getResult());
this->callback->onFinish();
}
}
}; };
#endif // ASYNC_RESULT_H #endif // ASYNCRESULT_H

View File

@ -1,16 +1,13 @@
#ifndef CALLBACK_H #ifndef CALLBACK_H
#define CALLBACK_H #define CALLBACK_H
#include <QException> #include <QJSValue>
template <class R>
class Callback { class Callback {
public: public:
virtual void onResult(QJSValue result) = 0;
virtual void onResult(R result) = 0; virtual void onError(QJSValue error) = 0;
virtual void onError(QException exception) = 0;
virtual void onFinish() = 0; virtual void onFinish() = 0;
}; };

View File

@ -0,0 +1,20 @@
#include <QDebug>
#include "settable_future.h"
void SettableFuture::set(QJSValue result)
{
if (mReadyLatch == NULL)
{
qDebug() << "Result has already been set!";
return;
}
mResult = result;
delete mReadyLatch;
}
QJSValue SettableFuture::get()
{
mReadyLatch->lock();
return mResult;
}

View File

@ -0,0 +1,18 @@
#ifndef SETTABLE_FUTURE_H
#define SETTABLE_FUTURE_H
#include <QJSValue>
#include "utils/count_down_latch.h"
class SettableFuture {
private:
QJSValue mResult;
CountDownLatch *mReadyLatch = new CountDownLatch();
public:
void set(QJSValue result);
QJSValue get();
};
#endif // SETTABLE_FUTURE_H

View File

@ -1,36 +0,0 @@
#include "constant.h"
const QString Constant::INJECT_LOG = "nativeLog";
const QString Constant::INJECT_REQUIRE = "nativeRequire";
const QString Constant::INJECT_TIMER_SET = "nativeSetTimer";
const QString Constant::INJECT_TIMER_CLEAR = "nativeClearTimer";
const QString Constant::INJECT_BRIDGE = "nativeBridge";
const QString Constant::INJECT_EMPTY = "nativeEmpty";
const QString Constant::TEMPLATE_CONTEXT_CREATE = QString("Reflect.apply(") +
QString("function(doric,context,Entry,require,exports){") + QString("\n") +
QString("%s1") + QString("\n") +
QString("},doric.jsObtainContext(\"%s2\"),[") +
QString("undefined,") +
QString("doric.jsObtainContext(\"%s3\"),") +
QString("doric.jsObtainEntry(\"%s4\"),") +
QString("doric.__require__") +
QString(",{}") +
QString("])");
const QString Constant::TEMPLATE_MODULE = QString("Reflect.apply(doric.jsRegisterModule,this,[") +
QString("\"%s1\",") +
QString("Reflect.apply(function(__module){") +
QString("(function(module,exports,require){") + QString("\n") +
QString("%s2") + QString("\n") +
QString("})(__module,__module.exports,doric.__require__);") +
QString("\nreturn __module.exports;") +
QString("},this,[{exports:{}}])") +
QString("])");
const QString Constant::TEMPLATE_CONTEXT_DESTROY = QString("doric.jsReleaseContext(\"%s\")");
const QString Constant::GLOBAL_DORIC = QString("doric");
const QString Constant::DORIC_CONTEXT_INVOKE = QString("jsCallEntityMethod");
const QString Constant::DORIC_TIMER_CALLBACK = QString("jsCallbackTimer");
const QString Constant::DORIC_ENTITY_INIT = QString("__init__");
const QString Constant::DORIC_ENTITY_SHOW = QString("__onShow__");

View File

@ -1,39 +1,19 @@
#include <QJsonObject>
#include <QJsonDocument>
#include "constant.h"
#include "context.h" #include "context.h"
#include "driver/native_driver.h" #include "native_driver.h"
Context::Context(int contextId, QString *source) { Context::Context(QString contextId, QString source, QString extra)
this->driver = NativeDriver::getInstance(); {
this->mContextId = contextId;
this->contextId = contextId;
this->source = source; this->source = source;
this->extra = extra;
} }
void Context::show() { InterfaceDriver* Context::getDriver()
QString *method = new QString(Constant::DORIC_ENTITY_SHOW); {
QVector<QString*> *arguments = new QVector<QString*>(); if (driver == NULL)
{
driver->invokeContextEntityMethod(contextId, method, nullptr); driver = NativeDriver::getInstance();
return driver;
delete arguments;
delete method;
} }
return driver;
void Context::init(double width, double height) {
QJsonObject *jsonObject = new QJsonObject();
jsonObject->insert("width", width);
jsonObject->insert("height", height);
QString *method = new QString(Constant::DORIC_ENTITY_INIT);
QVariant *variant = new QVariant();
variant->setValue(*jsonObject);
driver->invokeContextEntityMethod(contextId, method, variant, nullptr);
delete variant;
delete method;
delete jsonObject;
} }

View File

@ -3,23 +3,26 @@
#include <QString> #include <QString>
#include "driver/driver.h" #include "interface_driver.h"
class Context class Context
{ {
private: private:
int contextId; QString mContextId;
QString *source; QString source;
QString script;
QString extra;
InterfaceDriver *driver;
public: public:
Driver *driver; Context(QString contextId, QString source, QString extra);
Context(int contextId, QString *source); static Context* create(QString script, QString source, QString extra)
{
return NULL;
}
void show(); InterfaceDriver* getDriver();
void init(double width, double height);
}; };
#endif // CONTEXT_H #endif // CONTEXT_H

View File

@ -1,17 +0,0 @@
#ifndef CONTEXT_HOLDER_H
#define CONTEXT_HOLDER_H
#include "context.h"
class ContextHolder : public QObject {
Q_OBJECT
public:
Context *_context;
public:
ContextHolder(Context *context, QObject *parent = nullptr) : QObject(parent) {
this->_context = context;
}
};
#endif // CONTEXT_HOLDER_H

View File

@ -1,45 +1,41 @@
#ifndef CONTEXT_MANAGER_H #ifndef CONTEXTMANAGER_H
#define CONTEXT_MANAGER_H #define CONTEXTMANAGER_H
#include <QAtomicInt>
#include <QDebug> #include <QDebug>
#include <QMap>
#include "context.h" #include "context.h"
class ContextManager class ContextManager
{ {
private: private:
static ContextManager *local_instance; static ContextManager *local_instance;
ContextManager() { ContextManager()
qDebug() << "ContextManager constructor"; {
qDebug() << "ContextManager create";
} }
~ContextManager() { ~ContextManager()
qDebug() << "ContextManager destructor"; {
qDebug() << "ContextManager destroy";
} }
QAtomicInt *counter = new QAtomicInt(); QAtomicInt *counter = new QAtomicInt();
QMap<int, Context*> *contextMap = new QMap<int, Context*>(); QMap<QString, Context*> *contextMap = new QMap<QString, Context*>();
public: public:
static ContextManager *getInstance() { static ContextManager *getInstance()
static ContextManager locla_s; {
return &locla_s; static ContextManager instance;
return &instance;
} }
Context *createContext(QString *script, QString *source) { Context *createContext(QString script, QString source, QString extra) {
int contextId = counter->fetchAndAddOrdered(1); int contextId = counter->fetchAndAddOrdered(1);
Context *context = new Context(contextId, source); Context *context = new Context(QString::number(contextId), source, extra);
contextMap->insert(contextId, context); contextMap->insert(QString::number(contextId), context);
context->driver->createContext(contextId, script); context->driver->createContext(contextId, script);
return context; return context;
} }
Context *getContext(int contextId) {
return contextMap->take(contextId);
}
}; };
#endif // CONTEXT_MANAGER_H #endif // CONTEXTMANAGER_H

View File

@ -1,6 +1,4 @@
QT += \ QT += quick
quick \
widgets
CONFIG += c++11 CONFIG += c++11
@ -16,17 +14,18 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
constant.cpp \ async/async_result.cpp \
async/settable_future.cpp \
context.cpp \ context.cpp \
driver/native_driver.cpp \ engine/bridge_extension.cpp \
engine/js_engine.cpp \ engine/js_engine.cpp \
engine/native_empty.cpp \
engine/native_jse.cpp \
engine/native_log.cpp \
engine/timer_extension.cpp \
main.cpp \ main.cpp \
native/native_bridge.cpp \ native_driver.cpp \
native/native_empty.cpp \ utils/constant.cpp
native/native_log.cpp \
native/native_timer.cpp \
plugin/shader_plugin.cpp \
registry.cpp
RESOURCES += qml.qrc RESOURCES += qml.qrc
@ -42,22 +41,22 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
HEADERS += \ HEADERS += \
async/async_call.h \
async/async_result.h \ async/async_result.h \
async/callback.h \ async/callback.h \
constant.h \ async/settable_future.h \
context.h \ context.h \
context_holder.h \
context_manager.h \ context_manager.h \
driver/driver.h \ engine/bridge_extension.h \
driver/native_driver.h \ engine/interface_jse.h \
engine/js_engine.h \ engine/js_engine.h \
native/native_bridge.h \ engine/native_empty.h \
native/native_empty.h \ engine/native_jse.h \
native/native_log.h \ engine/native_log.h \
native/native_timer.h \ engine/timer_extension.h \
plugin/shader_plugin.h \ interface_driver.h \
registry.h \ native_driver.h \
shader/layer.h \
template/custom_callback.h \
template/singleton.h \ template/singleton.h \
utility/utility.h utils/constant.h \
utils/count_down_latch.h \
utils/utils.h

View File

@ -0,0 +1,320 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2021-01-28T10:15:57. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{824ffc72-3002-4c5f-b578-b99df65133bd}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="int">0</value>
</data>
<data>
<variable>ProjectExplorer.Project.EditorSettings</variable>
<valuemap type="QVariantMap">
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
<value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
<value type="QString" key="language">Cpp</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
</valuemap>
</valuemap>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
<value type="QString" key="language">QmlJS</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
</valuemap>
</valuemap>
<value type="int" key="EditorConfiguration.CodeStyle.Count">2</value>
<value type="QByteArray" key="EditorConfiguration.Codec">UTF-8</value>
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
<value type="int" key="EditorConfiguration.IndentSize">4</value>
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
<value type="int" key="EditorConfiguration.MarginColumn">80</value>
<value type="bool" key="EditorConfiguration.MouseHiding">true</value>
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">true</value>
<value type="bool" key="EditorConfiguration.ShowMargin">false</value>
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">0</value>
<value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value>
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
<value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
<value type="int" key="EditorConfiguration.TabSize">8</value>
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
<value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value>
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
<value type="bool" key="EditorConfiguration.cleanIndentation">true</value>
<value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
<value type="bool" key="EditorConfiguration.inEntireDocument">false</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.PluginSettings</variable>
<valuemap type="QVariantMap">
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey">
<value type="QString">-fno-delayed-template-parsing</value>
</valuelist>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap">
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win64_mingw73_kit</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/admin/Documents/build-doric-Desktop_Qt_5_14_2_MinGW_64_bit-Debug</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibrary">true</value>
<value type="QString" key="QtProjectManager.QMakeBuildStep.QMakeArguments"></value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.SeparateDebugInfo">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.UseQtQuickCompiler">false</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">false</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments"></value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">true</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Debug</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/admin/Documents/build-doric-Desktop_Qt_5_14_2_MinGW_64_bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibrary">false</value>
<value type="QString" key="QtProjectManager.QMakeBuildStep.QMakeArguments"></value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.SeparateDebugInfo">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.UseQtQuickCompiler">true</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">false</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments"></value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">true</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Release</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/admin/Documents/build-doric-Desktop_Qt_5_14_2_MinGW_64_bit-Profile</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibrary">true</value>
<value type="QString" key="QtProjectManager.QMakeBuildStep.QMakeArguments"></value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.SeparateDebugInfo">true</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.UseQtQuickCompiler">true</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">false</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments"></value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.BuildTargets"/>
<value type="bool" key="Qt4ProjectManager.MakeStep.Clean">true</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
<value type="bool" key="Qt4ProjectManager.MakeStep.OverrideMakeflags">false</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Profile</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.BuildConfigurationCount">3</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.PluginSettings"/>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
<value type="QString" key="Analyzer.Perf.CallgraphMode">dwarf</value>
<valuelist type="QVariantList" key="Analyzer.Perf.Events">
<value type="QString">cpu-cycles</value>
</valuelist>
<valuelist type="QVariantList" key="Analyzer.Perf.ExtraArguments"/>
<value type="int" key="Analyzer.Perf.Frequency">250</value>
<valuelist type="QVariantList" key="Analyzer.Perf.RecordArguments">
<value type="QString">-e</value>
<value type="QString">cpu-cycles</value>
<value type="QString">--call-graph</value>
<value type="QString">dwarf,4096</value>
<value type="QString">-F</value>
<value type="QString">250</value>
</valuelist>
<value type="QString" key="Analyzer.Perf.SampleMode">-F</value>
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
<value type="int" key="Analyzer.Perf.StackSize">4096</value>
<value type="bool" key="Analyzer.QmlProfiler.AggregateTraces">false</value>
<value type="bool" key="Analyzer.QmlProfiler.FlushEnabled">false</value>
<value type="uint" key="Analyzer.QmlProfiler.FlushInterval">1000</value>
<value type="QString" key="Analyzer.QmlProfiler.LastTraceFile"></value>
<value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value>
<valuelist type="QVariantList" key="Analyzer.Valgrind.AddedSuppressionFiles"/>
<value type="bool" key="Analyzer.Valgrind.Callgrind.CollectBusEvents">false</value>
<value type="bool" key="Analyzer.Valgrind.Callgrind.CollectSystime">false</value>
<value type="bool" key="Analyzer.Valgrind.Callgrind.EnableBranchSim">false</value>
<value type="bool" key="Analyzer.Valgrind.Callgrind.EnableCacheSim">false</value>
<value type="bool" key="Analyzer.Valgrind.Callgrind.EnableEventToolTips">true</value>
<value type="double" key="Analyzer.Valgrind.Callgrind.MinimumCostRatio">0.01</value>
<value type="double" key="Analyzer.Valgrind.Callgrind.VisualisationMinimumCostRatio">10</value>
<value type="bool" key="Analyzer.Valgrind.FilterExternalIssues">true</value>
<value type="QString" key="Analyzer.Valgrind.KCachegrindExecutable">kcachegrind</value>
<value type="int" key="Analyzer.Valgrind.LeakCheckOnFinish">1</value>
<value type="int" key="Analyzer.Valgrind.NumCallers">25</value>
<valuelist type="QVariantList" key="Analyzer.Valgrind.RemovedSuppressionFiles"/>
<value type="int" key="Analyzer.Valgrind.SelfModifyingCodeDetection">1</value>
<value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.Valgrind.ShowReachable">false</value>
<value type="bool" key="Analyzer.Valgrind.TrackOrigins">true</value>
<value type="QString" key="Analyzer.Valgrind.ValgrindExecutable">valgrind</value>
<valuelist type="QVariantList" key="Analyzer.Valgrind.VisibleErrorKinds">
<value type="int">0</value>
<value type="int">1</value>
<value type="int">2</value>
<value type="int">3</value>
<value type="int">4</value>
<value type="int">5</value>
<value type="int">6</value>
<value type="int">7</value>
<value type="int">8</value>
<value type="int">9</value>
<value type="int">10</value>
<value type="int">11</value>
<value type="int">12</value>
<value type="int">13</value>
<value type="int">14</value>
</valuelist>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">doric2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:C:/Users/admin/Workspace/Doric/doric-Qt/doric/doric.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">C:/Users/admin/Workspace/Doric/doric-Qt/doric/doric.pro</value>
<value type="QString" key="RunConfiguration.Arguments"></value>
<value type="bool" key="RunConfiguration.Arguments.multi">false</value>
<value type="QString" key="RunConfiguration.OverrideDebuggerStartup"></value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseMultiProcess">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/admin/Documents/build-doric-Desktop_Qt_5_14_2_MinGW_64_bit-Debug</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.TargetCount</variable>
<value type="int">1</value>
</data>
<data>
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
<value type="int">22</value>
</data>
<data>
<variable>Version</variable>
<value type="int">22</value>
</data>
</qtcreator>

View File

@ -1,26 +0,0 @@
#ifndef DRIVER_H
#define DRIVER_H
#include <QtPlugin>
#include "registry.h"
class Driver {
public:
virtual void createContext(int contextId, QString *script) = 0;
virtual void destroyContext(int contextId) = 0;
virtual void invokeContextEntityMethod(int contextId, QString *method, ...) = 0;
virtual void invokeDoricMethod(QString *method, ...) = 0;
virtual Registry *getRegistry() = 0;
virtual ~Driver() = default;
};
#define DriverInterface "pub.doric.DriverInterface"
Q_DECLARE_INTERFACE(Driver, DriverInterface)
#endif // DRIVER_H

View File

@ -1,58 +0,0 @@
#include <QDebug>
#include "stdarg.h"
#include "native_driver.h"
#include "utility/utility.h"
NativeDriver::NativeDriver() {
qDebug() << "NativeDriver constructor";
}
NativeDriver::~NativeDriver() {
qDebug() << "NativeDriver destructor";
}
void NativeDriver::createContext(int contextId, QString *script) {
jsEngine->prepareContext(contextId, script);
}
void NativeDriver::destroyContext(int contextId) {
jsEngine->destroyContext(contextId);
}
void NativeDriver::invokeContextEntityMethod(int contextId, QString *method, ...) {
QJSValueList *arguments = new QJSValueList();
arguments->append(QJSValue(QString::number(contextId)));
arguments->append(QJSValue(*method));
va_list vaList;
va_start(vaList, method);
auto argument = va_arg(vaList, QVariant*);
while (argument != nullptr) {
if (QString(typeid(QJsonObject).name()).contains(QString(argument->typeName()))) {
QJsonObject *jsonObject = static_cast<QJsonObject*>(argument->data());
QJsonValue *jsonValue = new QJsonValue(*jsonObject);
QJSValue jsValue = Utility::convert(jsEngine->engine, *jsonValue);
delete jsonValue;
arguments->append(jsValue);
}
argument = va_arg(vaList, QVariant*);
}
va_end(vaList);
QJSValue result = jsEngine->engine->globalObject()
.property(Constant::GLOBAL_DORIC)
.property(Constant::DORIC_CONTEXT_INVOKE)
.call(*arguments);
qDebug() << "invokeContextEntityMethod: " + result.toString();
delete arguments;
}
void NativeDriver::invokeDoricMethod(QString *method, ...) {
}
Registry* NativeDriver::getRegistry() {
return jsEngine->registry;
}

View File

@ -1,33 +0,0 @@
#ifndef NATIVE_DRIVER_H
#define NATIVE_DRIVER_H
#include "driver.h"
#include "engine/js_engine.h"
class NativeDriver : public Driver {
Q_INTERFACES(Driver)
private:
static NativeDriver *local_instance;
NativeDriver();
~NativeDriver() override;
JSEngine *jsEngine = new JSEngine();
public:
static NativeDriver *getInstance() {
static NativeDriver locla_s;
return &locla_s;
}
void createContext(int contextId, QString *script) override;
void destroyContext(int contextId) override;
void invokeContextEntityMethod(int contextId, QString *method, ...) override;
void invokeDoricMethod(QString *method, ...) override;
Registry * getRegistry() override;
};
#endif // NATIVE_DRIVER_H

View File

@ -0,0 +1,16 @@
#include <QDebug>
#include "bridge_extension.h"
BridgeExtension::BridgeExtension(QObject *parent) : QObject(parent)
{
}
void BridgeExtension::callNative(QString contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue)
{
qDebug() << "contextId: " + contextId;
qDebug() << "module: " + module;
qDebug() << "methodName: " + methodName;
qDebug() << "callbackId: " + callbackId;
qDebug() << "jsValue: " + jsValue.toString();
}

View File

@ -0,0 +1,16 @@
#ifndef BRIDGEEXTENSION_H
#define BRIDGEEXTENSION_H
#include <QObject>
#include <QJSValue>
class BridgeExtension : public QObject
{
Q_OBJECT
public:
explicit BridgeExtension(QObject *parent = nullptr);
void callNative(QString contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue);
};
#endif // BRIDGEEXTENSION_H

View File

@ -0,0 +1,19 @@
#ifndef INTERFACE_JSE_H
#define INTERFACE_JSE_H
#include <QString>
#include <QJSValue>
#include <QVariant>
class InterfaceJSE {
public:
virtual QString loadJS(QString script, QString source) = 0;
virtual void injectGlobalJSObject(QString name, QObject *object) = 0;
virtual void injectGlobalJSFunction(QString name, QObject *function, QString property) = 0;
virtual QJSValue invokeObject(QString objectName, QString functionName, QJSValueList arguments) = 0;
};
#endif // INTERFACE_JSE_H

View File

@ -1,81 +1,107 @@
#include <QDebug> #include <QGuiApplication>
#include <QFile> #include <QJsonObject>
#include <QResource> #include <QRect>
#include <QScreen>
#include <QSysInfo>
#include <QtConcurrent/QtConcurrent>
#include "constant.h"
#include "js_engine.h" #include "js_engine.h"
#include "native_jse.h"
#include "../utils/constant.h"
#include "native_log.h"
#include "native_empty.h"
#include "timer_extension.h"
#include "bridge_extension.h"
#include "../utils/utils.h"
JSEngine::JSEngine() { JSEngine::JSEngine(QObject *parent) : QObject(parent)
initJSEngine();
injectGlobal();
initDoricRuntime();
}
void JSEngine::prepareContext(int contextId, QString *script) {
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 JSEngine::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();
}
void JSEngine::initJSEngine() {
engine->installExtensions(QJSEngine::AllExtensions);
}
void JSEngine::injectGlobal() {
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"));
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 JSEngine::initDoricRuntime() {
{ {
QResource resource(":/doric/doric-sandbox.js"); mJSThreadPool.setMaxThreadCount(1);
QFile *file = new QFile(resource.fileName());
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"); QtConcurrent::run(&mJSThreadPool, [this]{
qDebug() << "doric-sandbox.js result: " + result.toString(); mJSE = new NativeJSE();
});
QtConcurrent::run(&mJSThreadPool, [this]{
// inject env
QScreen *screen = QGuiApplication::primaryScreen();
QRect screenGeometry = screen->geometry();
int screenWidth = screenGeometry.width();
int screenHeight = screenGeometry.height();
QObject *envObject = new QObject();
envObject->setProperty("platform", "Qt");
envObject->setProperty("platformVersion", qVersion());
envObject->setProperty("appName", "appName");
envObject->setProperty("appVersion", "appVersion");
envObject->setProperty("screenWidth", screenWidth);
envObject->setProperty("screenHeight", screenHeight);
envObject->setProperty("screenScale", 1);
envObject->setProperty("statusBarHeight", 0);
envObject->setProperty("hasNotch", false);
envObject->setProperty("deviceBrand", QSysInfo::prettyProductName());
envObject->setProperty("deviceModel", QSysInfo::productType());
mJSE->injectGlobalJSObject(Constant::INJECT_ENVIRONMENT, envObject);
// inject log
NativeLog *nativeLog = new NativeLog();
mJSE->injectGlobalJSFunction(Constant::INJECT_LOG, nativeLog, "function");
// inject empty
NativeEmpty *nativeEmpty = new NativeEmpty();
mJSE->injectGlobalJSFunction(Constant::INJECT_EMPTY, nativeEmpty, "function");
// inject timer set & clear
std::function<void(void)> func = [](){};
TimerExtension *timerExtension = new TimerExtension([this](long timerId){
QJSValueList arguments;
arguments.append(QJSValue((int)timerId));
this->invokeDoricMethod(Constant::DORIC_TIMER_CALLBACK, arguments);
});
mJSE->injectGlobalJSFunction(Constant::INJECT_TIMER_SET, timerExtension, "setTimer");
mJSE->injectGlobalJSFunction(Constant::INJECT_TIMER_CLEAR, timerExtension, "clearTimer");
BridgeExtension *bridgeExtension = new BridgeExtension();
mJSE->injectGlobalJSFunction(Constant::INJECT_BRIDGE, bridgeExtension, "callNative");
});
QtConcurrent::run(&mJSThreadPool, [this]{
loadBuiltinJS(Constant::DORIC_BUNDLE_SANDBOX);
QString libName = Constant::DORIC_MODULE_LIB;
QString libJS = Utils::readAssetFile("/doric", Constant::DORIC_BUNDLE_LIB);
QString script = packageModuleScript(libName, libJS);
mJSE->loadJS(script, "Module://" + libName);
});
} }
QJSValue JSEngine::invokeDoricMethod(QString method, QJSValueList arguments)
{ {
QResource resource(":/doric/doric-lib.js"); return mJSE->invokeObject(Constant::GLOBAL_DORIC, method, arguments);
QFile *file = new QFile(resource.fileName()); }
file->open(QFile::ReadOnly | QFile::Text);
QTextStream in(file);
QString script = in.readAll();
file->close();
delete file;
QString lib = QString(Constant::TEMPLATE_MODULE) void JSEngine::loadBuiltinJS(QString assetName)
.replace("%s1", "doric") {
.replace("%s2", script); QString script = Utils::readAssetFile("/doric", assetName);
QJSValue result = engine->evaluate(lib, "doric-lib.js"); QString result = mJSE->loadJS(script, "Assets://" + assetName);
qDebug() << "doric-lib.js result: " + result.toString();
} }
void JSEngine::prepareContext(QString contextId, QString script, QString source)
{
mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source);
}
QString JSEngine::packageContextScript(QString contextId, QString content)
{
return QString(Constant::TEMPLATE_CONTEXT_CREATE).replace("%s1", content).replace("%s2", contextId).replace("%s3", contextId);
}
QString JSEngine::packageModuleScript(QString moduleName, QString content)
{
return QString(Constant::TEMPLATE_MODULE).replace("%s1", moduleName).replace("%s2", content);
}
JSEngine::~JSEngine()
{
} }

View File

@ -1,38 +1,28 @@
#ifndef JS_ENGINE_H #ifndef JSENGINE_H
#define JS_ENGINE_H #define JSENGINE_H
#include <QJSEngine> #include <QJSValue>
#include <QThreadPool>
#include "native/native_bridge.h" #include "interface_jse.h"
#include "native/native_empty.h"
#include "native/native_log.h"
#include "native/native_timer.h"
#include "registry.h"
class JSEngine {
public:
QJSEngine *engine = new QJSEngine();
Registry *registry = new Registry();
JSEngine();
void prepareContext(int contextId, QString *script);
void destroyContext(int contextId);
class JSEngine : public QObject
{
Q_OBJECT
private: private:
NativeLog *nativeLog = new NativeLog(); QThreadPool mJSThreadPool;
NativeTimer *nativeTimer = new NativeTimer(engine); InterfaceJSE *mJSE;
NativeEmpty *nativeEmpty = new NativeEmpty();
NativeBridge *nativeBridge = new NativeBridge();
void initJSEngine(); void loadBuiltinJS(QString assetName);
void prepareContext(QString contextId, QString script, QString source);
QString packageContextScript(QString contextId, QString content);
QString packageModuleScript(QString moduleName, QString content);
public:
explicit JSEngine(QObject *parent = nullptr);
void injectGlobal(); QJSValue invokeDoricMethod(QString method, QJSValueList arguments);
void initDoricRuntime(); ~JSEngine();
}; };
#endif // JS_ENGINE_H #endif // JSENGINE_H

View File

@ -0,0 +1,7 @@
#include "native_empty.h"
#include <QDebug>
Q_INVOKABLE QJSValue NativeEmpty::function() {
qDebug() << "nativeEmpty";
return QJSValue::NullValue;
}

View File

@ -1,7 +1,8 @@
#ifndef NATIVE_EMPTY_H #ifndef NATIVEEMPTY_H
#define NATIVE_EMPTY_H #define NATIVEEMPTY_H
#include <QObject> #include <QObject>
#include <QJSValue>
class NativeEmpty : public QObject { class NativeEmpty : public QObject {
Q_OBJECT Q_OBJECT
@ -9,7 +10,7 @@ class NativeEmpty : public QObject {
public: public:
NativeEmpty(QObject *parent = nullptr) : QObject(parent) {} NativeEmpty(QObject *parent = nullptr) : QObject(parent) {}
Q_INVOKABLE void function(); Q_INVOKABLE QJSValue function();
}; };
#endif // NATIVE_EMPTY_H #endif // NATIVEEMPTY_H

View File

@ -0,0 +1,43 @@
#include "native_jse.h"
#include <QDebug>
NativeJSE::NativeJSE()
{
mJSEngine.installExtensions(QJSEngine::AllExtensions);
}
QString NativeJSE::loadJS(QString script, QString source)
{
return mJSEngine.evaluate(script, source).toString();
}
void NativeJSE::injectGlobalJSObject(QString name, QObject *object)
{
QJSValue jsObject = mJSEngine.newQObject(object);
QList<QByteArray> propertyNames = object->dynamicPropertyNames();
foreach(QByteArray propertyName, propertyNames)
{
QString key = QString::fromStdString(propertyName.toStdString());
if (key == "undefined") {
} else {
jsObject.setProperty(key, mJSEngine.toScriptValue(object->property(propertyName)));
}
}
mJSEngine.globalObject().setProperty(name, jsObject);
}
void NativeJSE::injectGlobalJSFunction(QString name, QObject *function, QString property)
{
QJSValue functionObject = mJSEngine.newQObject(function);
mJSEngine.globalObject().setProperty(name, functionObject.property(property));
}
QJSValue NativeJSE::invokeObject(QString objectName, QString functionName, QJSValueList arguments)
{
QJSValue object = mJSEngine.evaluate(objectName);
QJSValue function = object.property(functionName);
return function.call(arguments);
}

View File

@ -0,0 +1,23 @@
#ifndef NATIVE_JSE_H
#define NATIVE_JSE_H
#include <QJSEngine>
#include "interface_jse.h"
class NativeJSE : public InterfaceJSE
{
private:
QJSEngine mJSEngine;
public:
NativeJSE();
QString loadJS(QString script, QString source) override;
void injectGlobalJSObject(QString name, QObject *object) override;
void injectGlobalJSFunction(QString name, QObject *function, QString property) override;
QJSValue invokeObject(QString objectName, QString functionName, QJSValueList arguments) override;
};
#endif // NATIVE_JSE_H

View File

@ -1,5 +1,5 @@
#ifndef NATIVELOG_H #ifndef NATIVE_LOG_H
#define NATIVELOG_H #define NATIVE_LOG_H
#include <QObject> #include <QObject>
@ -12,4 +12,4 @@ public:
Q_INVOKABLE void function(QString level, QString content); Q_INVOKABLE void function(QString level, QString content);
}; };
#endif // NATIVELOG_H #endif // NATIVE_LOG_H

View File

@ -1,8 +1,9 @@
#include <QTimer> #include <QTimer>
#include "native_timer.h" #include "timer_extension.h"
#include "../utils/constant.h"
Q_INVOKABLE void NativeTimer::setTimer(long timerId, int time, bool repeat) { Q_INVOKABLE void TimerExtension::setTimer(long timerId, int time, bool repeat) {
QTimer *timer = new QTimer(this); QTimer *timer = new QTimer(this);
timer->setSingleShot(!repeat); timer->setSingleShot(!repeat);
connect(timer, &QTimer::timeout, this, [=] () { connect(timer, &QTimer::timeout, this, [=] () {
@ -10,11 +11,7 @@ Q_INVOKABLE void NativeTimer::setTimer(long timerId, int time, bool repeat) {
deletedTimerIds->remove(timerId); deletedTimerIds->remove(timerId);
delete timer; delete timer;
} else { } else {
engine->evaluate( this->method(timerId);
Constant::GLOBAL_DORIC + "." +
Constant::DORIC_TIMER_CALLBACK + "(" +
QString::number(timerId) + ")"
);
if (!repeat) { if (!repeat) {
deletedTimerIds->remove(timerId); deletedTimerIds->remove(timerId);
@ -25,6 +22,6 @@ Q_INVOKABLE void NativeTimer::setTimer(long timerId, int time, bool repeat) {
timer->start(time); timer->start(time);
} }
void NativeTimer::clearTimer(long timerId) { Q_INVOKABLE void TimerExtension::clearTimer(long timerId) {
deletedTimerIds->insert(timerId); deletedTimerIds->insert(timerId);
} }

View File

@ -0,0 +1,24 @@
#ifndef NATIVETIMER_H
#define NATIVETIMER_H
#include <QObject>
#include <QSet>
class TimerExtension : public QObject {
Q_OBJECT
private:
QSet<long> *deletedTimerIds = new QSet<long>();
std::function<void(long)> method;
public:
explicit TimerExtension(std::function<void(long)> method, QObject *parent = nullptr) : QObject(parent) {
this->method = method;
}
Q_INVOKABLE void setTimer(long timerId, int time, bool repeat);
Q_INVOKABLE void clearTimer(long timerId);
};
#endif // NATIVETIMER_H

View File

@ -0,0 +1,19 @@
#ifndef INTERFACEDRIVER_H
#define INTERFACEDRIVER_H
#include <QString>
#include <QObject>
class InterfaceDriver
{
public:
virtual void invokeContextEntityMethod(QString contextId, QString method, QList<QObject> args) = 0;
virtual void invokeDoricMethod(QString method, QList<QObject> args) = 0;
virtual void createContext(QString contextId, QString script, QString source) = 0;
virtual void destroyContext(QString contextId) = 0;
};
#endif // INTERFACEDRIVER_H

View File

@ -1,46 +1,26 @@
#include <QApplication> #include <QGuiApplication>
#include <QDialog> #include <QQmlApplicationEngine>
#include <QFile> #include <QDebug>
#include <QResource>
#include "context_manager.h" #include "engine/js_engine.h"
#include "async/async_result.h" #include "async/async_result.h"
#include "template/custom_callback.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
{
QWidget *widget = new QWidget(nullptr, Qt::WindowType::Window);
widget->setWindowTitle(QString("Hello Doric"));
widget->resize(360, 640);
widget->show();
}
{
QResource resource(":/doric/Snake.js");
QFile *file = new QFile(resource.fileName());
file->open(QFile::ReadOnly | QFile::Text);
QTextStream in(file);
QString script = in.readAll();
file->close();
delete file;
QString *source = new QString("Snake.js"); QGuiApplication app(argc, argv);
Context *context = ContextManager::getInstance()->createContext(&script, source);
context->show();
context->init(180, 320);
delete source;
}
{ QQmlApplicationEngine engine;
// code for test const QUrl url(QStringLiteral("qrc:/main.qml"));
QJsonValue *a = new QJsonValue(); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
AsyncResult<QJsonValue> *result = new AsyncResult<QJsonValue>(*a); &app, [url](QObject *obj, const QUrl &objUrl) {
CustomCallback<QJsonValue> *callback = new CustomCallback<QJsonValue>(); if (!obj && url == objUrl)
result->setCallback(callback); QCoreApplication::exit(-1);
qDebug() << result->hasResult(); }, Qt::QueuedConnection);
qDebug() << result->getResult(); engine.load(url);
}
JSEngine jsEngine;
return app.exec(); return app.exec();
} }

23
doric-Qt/doric/main.qml Normal file
View File

@ -0,0 +1,23 @@
import QtQuick 2.12
import QtQuick.Controls 2.5
ApplicationWindow {
visible: true
width: 960
height: 720
title: qsTr("Scroll")
ScrollView {
anchors.fill: parent
ListView {
width: parent.width
model: 20
delegate: ItemDelegate {
text: "Item " + (index + 1)
width: parent.width
height: 100
}
}
}
}

View File

@ -1,23 +0,0 @@
#include "context_manager.h"
#include "native_bridge.h"
#include "plugin/shader_plugin.h"
Q_INVOKABLE void NativeBridge::function(int contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue) {
qDebug() << "contextId: " + QString::number(contextId) + ", " +
"module: " + module + ", " +
"methodName: " + methodName + ", " +
"callbackId: " + callbackId + ", " +
"arguments: " + jsValue.toString();
Context *context = ContextManager::getInstance()->getContext(contextId);
QString value = context->driver->getRegistry()->acquirePluginInfo(module);
qDebug() << value;
if (value.contains("ShaderPlugin")) {
ShaderPlugin shaderPlugin(context);
QMetaObject::invokeMethod(
&shaderPlugin,
methodName.toStdString().c_str(),
Qt::AutoConnection,
Q_ARG(QJSValue, jsValue));
}
}

View File

@ -1,16 +0,0 @@
#ifndef NATIVE_BRIDGE_H
#define NATIVE_BRIDGE_H
#include <QJSValue>
#include <QObject>
class NativeBridge : public QObject {
Q_OBJECT
public:
NativeBridge(QObject *parent = nullptr) : QObject(parent) {}
Q_INVOKABLE void function(int contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue);
};
#endif // NATIVE_BRIDGE_H

View File

@ -1,7 +0,0 @@
#include <QDebug>
#include "native_empty.h"
Q_INVOKABLE void NativeEmpty::function() {
qDebug() << "nativeEmpty";
}

View File

@ -1,27 +0,0 @@
#ifndef NATIVE_TIMER_H
#define NATIVE_TIMER_H
#include <QJSEngine>
#include <QObject>
#include <QSet>
#include "constant.h"
class NativeTimer : public QObject {
Q_OBJECT
private:
QSet<long> *deletedTimerIds = new QSet<long>();
QJSEngine *engine;
public:
NativeTimer(QJSEngine *engine, QObject *parent = nullptr) : QObject(parent) {
this->engine = engine;
}
Q_INVOKABLE void setTimer(long timerId, int time, bool repeat);
Q_INVOKABLE void clearTimer(long timerId);
};
#endif // NATIVE_TIMER_H

View File

@ -0,0 +1,21 @@
#include "native_driver.h"
void NativeDriver::invokeContextEntityMethod(QString contextId, QString method, QList<QObject> args)
{
}
void NativeDriver::invokeDoricMethod(QString method, QList<QObject> args)
{
}
void NativeDriver::createContext(QString contextId, QString script, QString source)
{
}
void NativeDriver::destroyContext(QString contextId)
{
}

View File

@ -0,0 +1,40 @@
#ifndef NATIVEDRIVER_H
#define NATIVEDRIVER_H
#include <QDebug>
#include "interface_driver.h"
#include "engine/js_engine.h"
class NativeDriver : public InterfaceDriver
{
private:
static NativeDriver *local_instance;
NativeDriver()
{
qDebug() << "constructor";
}
~NativeDriver()
{
qDebug() << "destructor";
}
JSEngine jsEngine;
public:
static NativeDriver *getInstance()
{
static NativeDriver instance;
return &instance;
}
void invokeContextEntityMethod(QString contextId, QString method, QList<QObject> args) override;
void invokeDoricMethod(QString method, QList<QObject> args) override;
void createContext(QString contextId, QString script, QString source) override;
void destroyContext(QString contextId) override;
};
#endif // NATIVEDRIVER_H

View File

@ -1,6 +0,0 @@
#include "shader_plugin.h"
Q_INVOKABLE void ShaderPlugin::render(QJSValue jsValue) {
QString viewId = jsValue.property("id").toString();
qDebug() << viewId;
}

View File

@ -1,18 +0,0 @@
#ifndef SHADER_PLUGIN_H
#define SHADER_PLUGIN_H
#include <QDebug>
#include <QJSValue>
#include "context_holder.h"
class ShaderPlugin : public ContextHolder {
Q_OBJECT
public:
ShaderPlugin(Context *context) : ContextHolder(context) {}
Q_INVOKABLE void render(QJSValue jsValue);
};
#endif // SHADER_PLUGIN_H

View File

@ -1,4 +1,8 @@
<RCC> <RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>qtquickcontrols2.conf</file>
</qresource>
<qresource prefix="/doric"> <qresource prefix="/doric">
<file alias="doric-sandbox.js">../../doric-js/bundle/doric-sandbox.js</file> <file alias="doric-sandbox.js">../../doric-js/bundle/doric-sandbox.js</file>
<file alias="doric-lib.js">../../doric-js/bundle/doric-lib.js</file> <file alias="doric-lib.js">../../doric-js/bundle/doric-lib.js</file>

View File

@ -0,0 +1,6 @@
; This file can be edited to change the style of the application
; Read "Qt Quick Controls 2 Configuration File" for details:
; http://doc.qt.io/qt-5/qtquickcontrols2-configuration.html
[Controls]
Style=Default

View File

@ -1,17 +0,0 @@
#include <QDebug>
#include "registry.h"
#include "plugin/shader_plugin.h"
Registry::Registry() {
registerNativePlugin("shader", typeid(ShaderPlugin).name());
}
void Registry::registerNativePlugin(QString key, QString value) {
qDebug() << key + " " + value;
pluginInfoMap.insert(key, value);
}
QString Registry::acquirePluginInfo(QString key) {
return pluginInfoMap.take(key);
}

View File

@ -1,19 +0,0 @@
#ifndef REGISTRY_H
#define REGISTRY_H
#include <QMap>
class Registry {
private:
QMap<QString, QString> pluginInfoMap;
public:
Registry();
void registerNativePlugin(QString key, QString value);
QString acquirePluginInfo(QString key);
};
#endif // REGISTRY_H

View File

@ -1,33 +0,0 @@
#ifndef LAYER_H
#define LAYER_H
#include <QPainter>
#include <QWidget>
class Layer : public QWidget {
void paintEvent(QPaintEvent *event) override {
QPainter painter(this);
QWidget::paintEvent(event);
}
public:
void setShadow(int sdColor, int sdOpacity, int sdRadius, int offsetX, int offsetY) {
}
void setBorder(int borderWidth, int borderColor) {
}
void setCornerRadius(int corner) {
}
void setCornerRadius(int leftTop, int rightTop, int rightBottom, int leftBottom) {
}
};
#endif // LAYER_H

View File

@ -1,25 +0,0 @@
#ifndef CUSTOM_CALLBACK_H
#define CUSTOM_CALLBACK_H
#include <QDebug>
#include "async/callback.h"
template <class R>
class CustomCallback : public Callback<R> {
public:
void onResult(R result) override {
qDebug() << result;
}
void onError(QException exception) override {
qDebug() << exception.what();
}
void onFinish() override {
}
};
#endif // CUSTOM_CALLBACK_H

View File

@ -7,16 +7,19 @@ class Singleton
{ {
private: private:
static Singleton *local_instance; static Singleton *local_instance;
Singleton() { Singleton()
{
qDebug() << "constructor"; qDebug() << "constructor";
} }
~Singleton() { ~Singleton()
{
qDebug() << "destructor"; qDebug() << "destructor";
} }
public: public:
static Singleton *getInstance() { static Singleton *getInstance()
{
static Singleton locla_s; static Singleton locla_s;
return &locla_s; return &locla_s;
} }

View File

@ -1,48 +0,0 @@
#ifndef UTILITY_H
#define UTILITY_H
#include <QJSEngine>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
class Utility {
public:
static QJSValue convert(QJSEngine *jsEngine, QJsonValue jsonValue) {
if (jsonValue.isBool()) {
return QJSValue(jsonValue.toBool());
} else if (jsonValue.isString()) {
return QJSValue(jsonValue.toString());
} else if (jsonValue.isDouble()) {
return QJSValue(jsonValue.toDouble());
} else if (jsonValue.isNull()) {
return QJSValue(QJSValue::NullValue);
} else if (jsonValue.isUndefined()) {
return QJSValue(QJSValue::UndefinedValue);
} else if (jsonValue.isObject()) {
QJsonObject jsonObject = jsonValue.toObject();
QJSValue jsValue = jsEngine->newObject();
for (auto iterator = jsonObject.begin(); iterator != jsonObject.end(); iterator++) {
QString key = iterator.key();
QJsonValue value = iterator.value();
QJSValue convertedValue = convert(jsEngine, value);
jsValue.setProperty(key, convertedValue);
}
return jsValue;
} else if (jsonValue.isArray()) {
QJsonArray jsonArray = jsonValue.toArray();
QJSValue jsValue = jsEngine->newArray(jsonArray.size());
for (int i = 0; i < jsonArray.size(); i++) {
QJsonValue value = jsonArray[i];
QJSValue convertedValue = convert(jsEngine, value);
jsValue.setProperty(i, convertedValue);
}
return jsValue;
}
return QJSValue(QJSValue::UndefinedValue);
}
};
#endif // UTILITY_H

View File

@ -0,0 +1,37 @@
#include "constant.h"
const QString Constant::DORIC_BUNDLE_SANDBOX = "doric-sandbox.js";
const QString Constant::DORIC_BUNDLE_LIB = "doric-lib.js";
const QString Constant::DORIC_MODULE_LIB = "doric";
const QString Constant::INJECT_ENVIRONMENT = "Environment";
const QString Constant::INJECT_LOG = "nativeLog";
const QString Constant::INJECT_EMPTY = "nativeEmpty";
const QString Constant::INJECT_REQUIRE = "nativeRequire";
const QString Constant::INJECT_TIMER_SET = "nativeSetTimer";
const QString Constant::INJECT_TIMER_CLEAR = "nativeClearTimer";
const QString Constant::INJECT_BRIDGE = "nativeBridge";
const QString Constant::TEMPLATE_CONTEXT_CREATE = QString("Reflect.apply(") +
"function(doric,context,Entry,require,exports){" + "\n" +
"%s1" + "\n" +
"},undefined,[" +
"undefined," +
"doric.jsObtainContext(\"%s2\")," +
"doric.jsObtainEntry(\"%s3\")," +
"doric.__require__" +
",{}" +
"])";
const QString Constant::TEMPLATE_MODULE = QString("Reflect.apply(doric.jsRegisterModule,this,[") +
"\"%s1\"," +
"Reflect.apply(function(__module){" +
"(function(module,exports,require){" + "\n" +
"%s2" + "\n" +
"})(__module,__module.exports,doric.__require__);" +
"\nreturn __module.exports;" +
"},this,[{exports:{}}])" +
"])";
const QString Constant::TEMPLATE_CONTEXT_DESTROY = QString("doric.jsReleaseContext(\"%s\")");
const QString Constant::GLOBAL_DORIC = "doric";
const QString Constant::DORIC_TIMER_CALLBACK = "jsCallbackTimer";

View File

@ -6,23 +6,25 @@
class Constant { class Constant {
public: public:
static const QString DORIC_BUNDLE_SANDBOX;
static const QString DORIC_BUNDLE_LIB;
static const QString DORIC_MODULE_LIB;
static const QString INJECT_ENVIRONMENT;
static const QString INJECT_LOG; static const QString INJECT_LOG;
static const QString INJECT_EMPTY;
static const QString INJECT_REQUIRE; static const QString INJECT_REQUIRE;
static const QString INJECT_TIMER_SET; static const QString INJECT_TIMER_SET;
static const QString INJECT_TIMER_CLEAR; static const QString INJECT_TIMER_CLEAR;
static const QString INJECT_BRIDGE; static const QString INJECT_BRIDGE;
static const QString INJECT_EMPTY;
static const QString TEMPLATE_CONTEXT_CREATE; static const QString TEMPLATE_CONTEXT_CREATE;
static const QString TEMPLATE_MODULE; static const QString TEMPLATE_MODULE;
static const QString TEMPLATE_CONTEXT_DESTROY; static const QString TEMPLATE_CONTEXT_DESTROY;
static const QString GLOBAL_DORIC; static const QString GLOBAL_DORIC;
static const QString DORIC_CONTEXT_INVOKE;
static const QString DORIC_TIMER_CALLBACK; static const QString DORIC_TIMER_CALLBACK;
static const QString DORIC_ENTITY_INIT;
static const QString DORIC_ENTITY_SHOW;
}; };
#endif // CONSTANT_H #endif // CONSTANT_H

View File

@ -0,0 +1,27 @@
#ifndef COUNTDOWNLATCH_H
#define COUNTDOWNLATCH_H
#include <climits>
#include <QSemaphore>
class CountDownLatch {
Q_DISABLE_COPY(CountDownLatch)
QSemaphore m_sem{INT_MAX};
public:
CountDownLatch() {}
~CountDownLatch() {
m_sem.acquire(INT_MAX);
m_sem.release(INT_MAX);
}
class Locker {
CountDownLatch * sem;
public:
Locker(const Locker & other) : sem{other.sem} { sem->m_sem.acquire(); }
Locker(Locker && other) : sem{other.sem} { other.sem = nullptr; }
Locker(CountDownLatch * sem) : sem{sem} { sem->m_sem.acquire(); }
~Locker() { if (sem) sem->m_sem.release(); }
};
Locker lock() { return Locker{this}; }
};
#endif // COUNTDOWNLATCH_H

View File

@ -0,0 +1,25 @@
#ifndef UTILS_H
#define UTILS_H
#include <QString>
#include <QResource>
#include <QFile>
#include <QTextStream>
class Utils
{
public:
static QString readAssetFile(QString preffix, QString assetName) {
QResource resource(":" + preffix + "/" + assetName);
QFile *file = new QFile(resource.fileName());
file->open(QFile::ReadOnly | QFile::Text);
QTextStream in(file);
QString content = in.readAll();
file->close();
delete file;
return content;
}
};
#endif // UTILS_H