Merge branch 'qt_master' into combine
This commit is contained in:
commit
7641aabc45
2
doric-Qt/.gitignore
vendored
Normal file
2
doric-Qt/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/build-doric-*/
|
||||
.DS_Store
|
1
doric-Qt/README.md
Normal file
1
doric-Qt/README.md
Normal file
@ -0,0 +1 @@
|
||||
# Doric Qt SDK
|
73
doric-Qt/doric/.gitignore
vendored
Normal file
73
doric-Qt/doric/.gitignore
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
# 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
|
||||
|
60
doric-Qt/doric/async/async_result.h
Normal file
60
doric-Qt/doric/async/async_result.h
Normal file
@ -0,0 +1,60 @@
|
||||
#ifndef ASYNC_RESULT_H
|
||||
#define ASYNC_RESULT_H
|
||||
|
||||
#include <QDebug>
|
||||
#include <QVariant>
|
||||
|
||||
#include "callback.h"
|
||||
|
||||
template <class R>
|
||||
class AsyncResult {
|
||||
|
||||
private:
|
||||
QVariant result;
|
||||
Callback<R> *callback = nullptr;
|
||||
|
||||
public:
|
||||
AsyncResult() {}
|
||||
|
||||
AsyncResult(R result) {
|
||||
this->result.setValue(result);
|
||||
}
|
||||
|
||||
void setResult(R result) {
|
||||
this->result.setValue(result);
|
||||
if (callback != nullptr) {
|
||||
this->callback->onResult(result);
|
||||
this->callback->onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
void setError(QException exception) {
|
||||
this->result->setValue(exception);
|
||||
if (callback != nullptr) {
|
||||
this->callback->onError(exception);
|
||||
this->callback->onFinish();
|
||||
}
|
||||
}
|
||||
|
||||
bool hasResult() {
|
||||
qDebug() << result.typeName();
|
||||
return !QString(result.typeName()).isEmpty();
|
||||
}
|
||||
|
||||
R *getResult() {
|
||||
return static_cast<R*>(result.data());
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // ASYNC_RESULT_H
|
18
doric-Qt/doric/async/callback.h
Normal file
18
doric-Qt/doric/async/callback.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef CALLBACK_H
|
||||
#define CALLBACK_H
|
||||
|
||||
#include <QException>
|
||||
|
||||
template <class R>
|
||||
class Callback {
|
||||
|
||||
public:
|
||||
|
||||
virtual void onResult(R result) = 0;
|
||||
|
||||
virtual void onError(QException exception) = 0;
|
||||
|
||||
virtual void onFinish() = 0;
|
||||
};
|
||||
|
||||
#endif // CALLBACK_H
|
36
doric-Qt/doric/constant.cpp
Normal file
36
doric-Qt/doric/constant.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#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__");
|
28
doric-Qt/doric/constant.h
Normal file
28
doric-Qt/doric/constant.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef CONSTANT_H
|
||||
#define CONSTANT_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
class Constant {
|
||||
|
||||
public:
|
||||
static const QString INJECT_LOG;
|
||||
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
|
39
doric-Qt/doric/context.cpp
Normal file
39
doric-Qt/doric/context.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
|
||||
#include "constant.h"
|
||||
#include "context.h"
|
||||
#include "driver/native_driver.h"
|
||||
|
||||
Context::Context(int contextId, QString *source) {
|
||||
this->driver = NativeDriver::getInstance();
|
||||
|
||||
this->contextId = contextId;
|
||||
this->source = source;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
25
doric-Qt/doric/context.h
Normal file
25
doric-Qt/doric/context.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef CONTEXT_H
|
||||
#define CONTEXT_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include "driver/driver.h"
|
||||
|
||||
class Context
|
||||
{
|
||||
|
||||
private:
|
||||
int contextId;
|
||||
QString *source;
|
||||
|
||||
public:
|
||||
Driver *driver;
|
||||
|
||||
Context(int contextId, QString *source);
|
||||
|
||||
void show();
|
||||
|
||||
void init(double width, double height);
|
||||
};
|
||||
|
||||
#endif // CONTEXT_H
|
17
doric-Qt/doric/context_holder.h
Normal file
17
doric-Qt/doric/context_holder.h
Normal file
@ -0,0 +1,17 @@
|
||||
#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
|
45
doric-Qt/doric/context_manager.h
Normal file
45
doric-Qt/doric/context_manager.h
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef CONTEXT_MANAGER_H
|
||||
#define CONTEXT_MANAGER_H
|
||||
|
||||
#include <QAtomicInt>
|
||||
#include <QDebug>
|
||||
#include <QMap>
|
||||
|
||||
#include "context.h"
|
||||
|
||||
class ContextManager
|
||||
{
|
||||
private:
|
||||
|
||||
static ContextManager *local_instance;
|
||||
ContextManager() {
|
||||
qDebug() << "ContextManager constructor";
|
||||
}
|
||||
|
||||
~ContextManager() {
|
||||
qDebug() << "ContextManager destructor";
|
||||
}
|
||||
|
||||
QAtomicInt *counter = new QAtomicInt();
|
||||
QMap<int, Context*> *contextMap = new QMap<int, Context*>();
|
||||
|
||||
public:
|
||||
static ContextManager *getInstance() {
|
||||
static ContextManager locla_s;
|
||||
return &locla_s;
|
||||
}
|
||||
|
||||
Context *createContext(QString *script, QString *source) {
|
||||
int contextId = counter->fetchAndAddOrdered(1);
|
||||
Context *context = new Context(contextId, source);
|
||||
contextMap->insert(contextId, context);
|
||||
context->driver->createContext(contextId, script);
|
||||
return context;
|
||||
}
|
||||
|
||||
Context *getContext(int contextId) {
|
||||
return contextMap->take(contextId);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // CONTEXT_MANAGER_H
|
63
doric-Qt/doric/doric.pro
Normal file
63
doric-Qt/doric/doric.pro
Normal file
@ -0,0 +1,63 @@
|
||||
QT += \
|
||||
quick \
|
||||
widgets
|
||||
|
||||
CONFIG += c++11
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any Qt feature that has been marked deprecated (the exact warnings
|
||||
# depend on your compiler). Refer to the documentation for the
|
||||
# deprecated API to know how to port your code away from it.
|
||||
DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
# You can also make your code fail to compile if it uses deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
constant.cpp \
|
||||
context.cpp \
|
||||
driver/native_driver.cpp \
|
||||
engine/js_engine.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
|
||||
|
||||
RESOURCES += qml.qrc
|
||||
|
||||
# Additional import path used to resolve QML modules in Qt Creator's code model
|
||||
QML_IMPORT_PATH =
|
||||
|
||||
# Additional import path used to resolve QML modules just for Qt Quick Designer
|
||||
QML_DESIGNER_IMPORT_PATH =
|
||||
|
||||
# Default rules for deployment.
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||
!isEmpty(target.path): INSTALLS += target
|
||||
|
||||
HEADERS += \
|
||||
async/async_result.h \
|
||||
async/callback.h \
|
||||
constant.h \
|
||||
context.h \
|
||||
context_holder.h \
|
||||
context_manager.h \
|
||||
driver/driver.h \
|
||||
driver/native_driver.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 \
|
||||
template/singleton.h \
|
||||
utility/utility.h
|
26
doric-Qt/doric/driver/driver.h
Normal file
26
doric-Qt/doric/driver/driver.h
Normal file
@ -0,0 +1,26 @@
|
||||
#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
|
58
doric-Qt/doric/driver/native_driver.cpp
Normal file
58
doric-Qt/doric/driver/native_driver.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#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;
|
||||
}
|
33
doric-Qt/doric/driver/native_driver.h
Normal file
33
doric-Qt/doric/driver/native_driver.h
Normal file
@ -0,0 +1,33 @@
|
||||
#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
|
81
doric-Qt/doric/engine/js_engine.cpp
Normal file
81
doric-Qt/doric/engine/js_engine.cpp
Normal file
@ -0,0 +1,81 @@
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QResource>
|
||||
|
||||
#include "constant.h"
|
||||
#include "js_engine.h"
|
||||
|
||||
JSEngine::JSEngine() {
|
||||
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");
|
||||
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();
|
||||
}
|
||||
}
|
38
doric-Qt/doric/engine/js_engine.h
Normal file
38
doric-Qt/doric/engine/js_engine.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef JS_ENGINE_H
|
||||
#define JS_ENGINE_H
|
||||
|
||||
#include <QJSEngine>
|
||||
|
||||
#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);
|
||||
|
||||
private:
|
||||
NativeLog *nativeLog = new NativeLog();
|
||||
NativeTimer *nativeTimer = new NativeTimer(engine);
|
||||
NativeEmpty *nativeEmpty = new NativeEmpty();
|
||||
NativeBridge *nativeBridge = new NativeBridge();
|
||||
|
||||
void initJSEngine();
|
||||
|
||||
void injectGlobal();
|
||||
|
||||
void initDoricRuntime();
|
||||
};
|
||||
|
||||
#endif // JS_ENGINE_H
|
46
doric-Qt/doric/main.cpp
Normal file
46
doric-Qt/doric/main.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include <QApplication>
|
||||
#include <QDialog>
|
||||
#include <QFile>
|
||||
#include <QResource>
|
||||
|
||||
#include "context_manager.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;
|
||||
|
||||
QString *source = new QString("Snake.js");
|
||||
Context *context = ContextManager::getInstance()->createContext(&script, source);
|
||||
context->show();
|
||||
context->init(180, 320);
|
||||
delete source;
|
||||
}
|
||||
|
||||
{
|
||||
// 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();
|
||||
}
|
||||
|
||||
return app.exec();
|
||||
}
|
23
doric-Qt/doric/native/native_bridge.cpp
Normal file
23
doric-Qt/doric/native/native_bridge.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#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));
|
||||
}
|
||||
}
|
16
doric-Qt/doric/native/native_bridge.h
Normal file
16
doric-Qt/doric/native/native_bridge.h
Normal file
@ -0,0 +1,16 @@
|
||||
#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
|
7
doric-Qt/doric/native/native_empty.cpp
Normal file
7
doric-Qt/doric/native/native_empty.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "native_empty.h"
|
||||
|
||||
Q_INVOKABLE void NativeEmpty::function() {
|
||||
qDebug() << "nativeEmpty";
|
||||
}
|
15
doric-Qt/doric/native/native_empty.h
Normal file
15
doric-Qt/doric/native/native_empty.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef NATIVE_EMPTY_H
|
||||
#define NATIVE_EMPTY_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class NativeEmpty : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
NativeEmpty(QObject *parent = nullptr) : QObject(parent) {}
|
||||
|
||||
Q_INVOKABLE void function();
|
||||
};
|
||||
|
||||
#endif // NATIVE_EMPTY_H
|
13
doric-Qt/doric/native/native_log.cpp
Normal file
13
doric-Qt/doric/native/native_log.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include <QDebug>
|
||||
|
||||
#include "native_log.h"
|
||||
|
||||
Q_INVOKABLE void NativeLog::function(QString level, QString content) {
|
||||
if (level == 'w') {
|
||||
qWarning() << content;
|
||||
} else if (level == 'd') {
|
||||
qDebug() << content;
|
||||
} else if (level == 'e') {
|
||||
qCritical() << content;
|
||||
}
|
||||
}
|
15
doric-Qt/doric/native/native_log.h
Normal file
15
doric-Qt/doric/native/native_log.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef NATIVELOG_H
|
||||
#define NATIVELOG_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class NativeLog : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
NativeLog(QObject *parent = nullptr) : QObject(parent) {}
|
||||
|
||||
Q_INVOKABLE void function(QString level, QString content);
|
||||
};
|
||||
|
||||
#endif // NATIVELOG_H
|
30
doric-Qt/doric/native/native_timer.cpp
Normal file
30
doric-Qt/doric/native/native_timer.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include <QTimer>
|
||||
|
||||
#include "native_timer.h"
|
||||
|
||||
Q_INVOKABLE void NativeTimer::setTimer(long timerId, int time, bool repeat) {
|
||||
QTimer *timer = new QTimer(this);
|
||||
timer->setSingleShot(!repeat);
|
||||
connect(timer, &QTimer::timeout, this, [=] () {
|
||||
if (deletedTimerIds->contains(timerId)) {
|
||||
deletedTimerIds->remove(timerId);
|
||||
delete timer;
|
||||
} else {
|
||||
engine->evaluate(
|
||||
Constant::GLOBAL_DORIC + "." +
|
||||
Constant::DORIC_TIMER_CALLBACK + "(" +
|
||||
QString::number(timerId) + ")"
|
||||
);
|
||||
|
||||
if (!repeat) {
|
||||
deletedTimerIds->remove(timerId);
|
||||
delete timer;
|
||||
}
|
||||
}
|
||||
});
|
||||
timer->start(time);
|
||||
}
|
||||
|
||||
void NativeTimer::clearTimer(long timerId) {
|
||||
deletedTimerIds->insert(timerId);
|
||||
}
|
27
doric-Qt/doric/native/native_timer.h
Normal file
27
doric-Qt/doric/native/native_timer.h
Normal file
@ -0,0 +1,27 @@
|
||||
#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
|
6
doric-Qt/doric/plugin/shader_plugin.cpp
Normal file
6
doric-Qt/doric/plugin/shader_plugin.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include "shader_plugin.h"
|
||||
|
||||
Q_INVOKABLE void ShaderPlugin::render(QJSValue jsValue) {
|
||||
QString viewId = jsValue.property("id").toString();
|
||||
qDebug() << viewId;
|
||||
}
|
18
doric-Qt/doric/plugin/shader_plugin.h
Normal file
18
doric-Qt/doric/plugin/shader_plugin.h
Normal file
@ -0,0 +1,18 @@
|
||||
#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
|
7
doric-Qt/doric/qml.qrc
Normal file
7
doric-Qt/doric/qml.qrc
Normal file
@ -0,0 +1,7 @@
|
||||
<RCC>
|
||||
<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>
|
||||
<file alias="Snake.js">../../doric-demo/bundle/src/Snake.js</file>
|
||||
</qresource>
|
||||
</RCC>
|
17
doric-Qt/doric/registry.cpp
Normal file
17
doric-Qt/doric/registry.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#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);
|
||||
}
|
19
doric-Qt/doric/registry.h
Normal file
19
doric-Qt/doric/registry.h
Normal file
@ -0,0 +1,19 @@
|
||||
#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
|
33
doric-Qt/doric/shader/layer.h
Normal file
33
doric-Qt/doric/shader/layer.h
Normal file
@ -0,0 +1,33 @@
|
||||
#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
|
25
doric-Qt/doric/template/custom_callback.h
Normal file
25
doric-Qt/doric/template/custom_callback.h
Normal file
@ -0,0 +1,25 @@
|
||||
#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
|
25
doric-Qt/doric/template/singleton.h
Normal file
25
doric-Qt/doric/template/singleton.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef SINGLETON_H
|
||||
#define SINGLETON_H
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
class Singleton
|
||||
{
|
||||
private:
|
||||
static Singleton *local_instance;
|
||||
Singleton() {
|
||||
qDebug() << "constructor";
|
||||
}
|
||||
|
||||
~Singleton() {
|
||||
qDebug() << "destructor";
|
||||
}
|
||||
|
||||
public:
|
||||
static Singleton *getInstance() {
|
||||
static Singleton locla_s;
|
||||
return &locla_s;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // SINGLETON_H
|
48
doric-Qt/doric/utility/utility.h
Normal file
48
doric-Qt/doric/utility/utility.h
Normal file
@ -0,0 +1,48 @@
|
||||
#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
|
Reference in New Issue
Block a user