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

View File

@ -1,16 +1,13 @@
#ifndef CALLBACK_H
#define CALLBACK_H
#include <QException>
#include <QJSValue>
template <class R>
class Callback {
public:
virtual void onResult(QJSValue result) = 0;
virtual void onResult(R result) = 0;
virtual void onError(QException exception) = 0;
virtual void onError(QJSValue error) = 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 "driver/native_driver.h"
#include "native_driver.h"
Context::Context(int contextId, QString *source) {
this->driver = NativeDriver::getInstance();
this->contextId = contextId;
Context::Context(QString contextId, QString source, QString extra)
{
this->mContextId = contextId;
this->source = source;
this->extra = extra;
}
void Context::show() {
QString *method = new QString(Constant::DORIC_ENTITY_SHOW);
QVector<QString*> *arguments = new QVector<QString*>();
driver->invokeContextEntityMethod(contextId, method, nullptr);
delete arguments;
delete method;
}
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;
InterfaceDriver* Context::getDriver()
{
if (driver == NULL)
{
driver = NativeDriver::getInstance();
return driver;
}
return driver;
}

View File

@ -3,23 +3,26 @@
#include <QString>
#include "driver/driver.h"
#include "interface_driver.h"
class Context
{
private:
int contextId;
QString *source;
QString mContextId;
QString source;
QString script;
QString extra;
InterfaceDriver *driver;
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();
void init(double width, double height);
InterfaceDriver* getDriver();
};
#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
#define CONTEXT_MANAGER_H
#ifndef CONTEXTMANAGER_H
#define CONTEXTMANAGER_H
#include <QAtomicInt>
#include <QDebug>
#include <QMap>
#include "context.h"
class ContextManager
{
private:
static ContextManager *local_instance;
ContextManager() {
qDebug() << "ContextManager constructor";
ContextManager()
{
qDebug() << "ContextManager create";
}
~ContextManager() {
qDebug() << "ContextManager destructor";
~ContextManager()
{
qDebug() << "ContextManager destroy";
}
QAtomicInt *counter = new QAtomicInt();
QMap<int, Context*> *contextMap = new QMap<int, Context*>();
QMap<QString, Context*> *contextMap = new QMap<QString, Context*>();
public:
static ContextManager *getInstance() {
static ContextManager locla_s;
return &locla_s;
static ContextManager *getInstance()
{
static ContextManager instance;
return &instance;
}
Context *createContext(QString *script, QString *source) {
Context *createContext(QString script, QString source, QString extra) {
int contextId = counter->fetchAndAddOrdered(1);
Context *context = new Context(contextId, source);
contextMap->insert(contextId, context);
Context *context = new Context(QString::number(contextId), source, extra);
contextMap->insert(QString::number(contextId), context);
context->driver->createContext(contextId, script);
return context;
}
Context *getContext(int contextId) {
return contextMap->take(contextId);
}
};
#endif // CONTEXT_MANAGER_H
#endif // CONTEXTMANAGER_H

View File

@ -1,6 +1,4 @@
QT += \
quick \
widgets
QT += quick
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
SOURCES += \
constant.cpp \
async/async_result.cpp \
async/settable_future.cpp \
context.cpp \
driver/native_driver.cpp \
engine/bridge_extension.cpp \
engine/js_engine.cpp \
engine/native_empty.cpp \
engine/native_jse.cpp \
engine/native_log.cpp \
engine/timer_extension.cpp \
main.cpp \
native/native_bridge.cpp \
native/native_empty.cpp \
native/native_log.cpp \
native/native_timer.cpp \
plugin/shader_plugin.cpp \
registry.cpp
native_driver.cpp \
utils/constant.cpp
RESOURCES += qml.qrc
@ -42,22 +41,22 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
async/async_call.h \
async/async_result.h \
async/callback.h \
constant.h \
async/settable_future.h \
context.h \
context_holder.h \
context_manager.h \
driver/driver.h \
driver/native_driver.h \
engine/bridge_extension.h \
engine/interface_jse.h \
engine/js_engine.h \
native/native_bridge.h \
native/native_empty.h \
native/native_log.h \
native/native_timer.h \
plugin/shader_plugin.h \
registry.h \
shader/layer.h \
template/custom_callback.h \
engine/native_empty.h \
engine/native_jse.h \
engine/native_log.h \
engine/timer_extension.h \
interface_driver.h \
native_driver.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 <QFile>
#include <QResource>
#include <QGuiApplication>
#include <QJsonObject>
#include <QRect>
#include <QScreen>
#include <QSysInfo>
#include <QtConcurrent/QtConcurrent>
#include "constant.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() {
initJSEngine();
injectGlobal();
initDoricRuntime();
JSEngine::JSEngine(QObject *parent) : QObject(parent)
{
mJSThreadPool.setMaxThreadCount(1);
QtConcurrent::run(&mJSThreadPool, [this]{
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);
});
}
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();
QJSValue JSEngine::invokeDoricMethod(QString method, QJSValueList arguments)
{
return mJSE->invokeObject(Constant::GLOBAL_DORIC, method, arguments);
}
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::loadBuiltinJS(QString assetName)
{
QString script = Utils::readAssetFile("/doric", assetName);
QString result = mJSE->loadJS(script, "Assets://" + assetName);
}
void JSEngine::initJSEngine() {
engine->installExtensions(QJSEngine::AllExtensions);
void JSEngine::prepareContext(QString contextId, QString script, QString source)
{
mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source);
}
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"));
QString JSEngine::packageContextScript(QString contextId, QString content)
{
return QString(Constant::TEMPLATE_CONTEXT_CREATE).replace("%s1", content).replace("%s2", contextId).replace("%s3", contextId);
}
void JSEngine::initDoricRuntime() {
{
QResource resource(":/doric/doric-sandbox.js");
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");
qDebug() << "doric-sandbox.js result: " + result.toString();
}
{
QResource resource(":/doric/doric-lib.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 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();
}
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
#define JS_ENGINE_H
#ifndef JSENGINE_H
#define JSENGINE_H
#include <QJSEngine>
#include <QJSValue>
#include <QThreadPool>
#include "native/native_bridge.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);
#include "interface_jse.h"
class JSEngine : public QObject
{
Q_OBJECT
private:
NativeLog *nativeLog = new NativeLog();
NativeTimer *nativeTimer = new NativeTimer(engine);
NativeEmpty *nativeEmpty = new NativeEmpty();
NativeBridge *nativeBridge = new NativeBridge();
QThreadPool mJSThreadPool;
InterfaceJSE *mJSE;
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
#define NATIVE_EMPTY_H
#ifndef NATIVEEMPTY_H
#define NATIVEEMPTY_H
#include <QObject>
#include <QJSValue>
class NativeEmpty : public QObject {
Q_OBJECT
@ -9,7 +10,7 @@ class NativeEmpty : public QObject {
public:
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
#define NATIVELOG_H
#ifndef NATIVE_LOG_H
#define NATIVE_LOG_H
#include <QObject>
@ -12,4 +12,4 @@ public:
Q_INVOKABLE void function(QString level, QString content);
};
#endif // NATIVELOG_H
#endif // NATIVE_LOG_H

View File

@ -1,8 +1,9 @@
#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);
timer->setSingleShot(!repeat);
connect(timer, &QTimer::timeout, this, [=] () {
@ -10,11 +11,7 @@ Q_INVOKABLE void NativeTimer::setTimer(long timerId, int time, bool repeat) {
deletedTimerIds->remove(timerId);
delete timer;
} else {
engine->evaluate(
Constant::GLOBAL_DORIC + "." +
Constant::DORIC_TIMER_CALLBACK + "(" +
QString::number(timerId) + ")"
);
this->method(timerId);
if (!repeat) {
deletedTimerIds->remove(timerId);
@ -25,6 +22,6 @@ Q_INVOKABLE void NativeTimer::setTimer(long timerId, int time, bool repeat) {
timer->start(time);
}
void NativeTimer::clearTimer(long timerId) {
Q_INVOKABLE void TimerExtension::clearTimer(long 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 <QDialog>
#include <QFile>
#include <QResource>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
#include "context_manager.h"
#include "engine/js_engine.h"
#include "async/async_result.h"
#include "template/custom_callback.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
{
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;
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QString *source = new QString("Snake.js");
Context *context = ContextManager::getInstance()->createContext(&script, source);
context->show();
context->init(180, 320);
delete source;
}
QGuiApplication app(argc, argv);
{
// code for test
QJsonValue *a = new QJsonValue();
AsyncResult<QJsonValue> *result = new AsyncResult<QJsonValue>(*a);
CustomCallback<QJsonValue> *callback = new CustomCallback<QJsonValue>();
result->setCallback(callback);
qDebug() << result->hasResult();
qDebug() << result->getResult();
}
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
JSEngine jsEngine;
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>
<qresource prefix="/">
<file>main.qml</file>
<file>qtquickcontrols2.conf</file>
</qresource>
<qresource prefix="/doric">
<file alias="doric-sandbox.js">../../doric-js/bundle/doric-sandbox.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:
static Singleton *local_instance;
Singleton() {
Singleton()
{
qDebug() << "constructor";
}
~Singleton() {
~Singleton()
{
qDebug() << "destructor";
}
public:
static Singleton *getInstance() {
static Singleton *getInstance()
{
static Singleton 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 {
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_EMPTY;
static const QString INJECT_REQUIRE;
static const QString INJECT_TIMER_SET;
static const QString INJECT_TIMER_CLEAR;
static const QString INJECT_BRIDGE;
static const QString INJECT_EMPTY;
static const QString TEMPLATE_CONTEXT_CREATE;
static const QString TEMPLATE_MODULE;
static const QString TEMPLATE_CONTEXT_DESTROY;
static const QString GLOBAL_DORIC;
static const QString DORIC_CONTEXT_INVOKE;
static const QString DORIC_TIMER_CALLBACK;
static const QString DORIC_ENTITY_INIT;
static const QString DORIC_ENTITY_SHOW;
};
#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