diff --git a/doric-Qt/doric/DoricContext.cpp b/doric-Qt/doric/DoricContext.cpp new file mode 100644 index 00000000..0e265176 --- /dev/null +++ b/doric-Qt/doric/DoricContext.cpp @@ -0,0 +1,69 @@ +#include "DoricContext.h" +#include "DoricContextManager.h" +#include "DoricNativeDriver.h" +#include "utils/DoricConstant.h" + +DoricContext::DoricContext(QString contextId, QString source, QString extra) { + this->mRootNode = new DoricRootNode(); + + this->mContextId = contextId; + this->source = source; + this->extra = extra; +} + +DoricContext *DoricContext::create(QString script, QString source, + QString extra) { + DoricContext *context = + DoricContextManager::getInstance()->createContext(script, source, extra); + context->script = script; + context->init(extra); + + QVariantList args; + context->callEntity(DoricConstant::DORIC_ENTITY_CREATE, args); + return context; +} + +void DoricContext::init(QString initData) { + this->extra = initData; + if (!initData.isEmpty()) { + QVariantList args; + args.push_back(initData); + callEntity(DoricConstant::DORIC_ENTITY_INIT, args); + } +} + +void DoricContext::build(int width, int height) { + QMap map; + map.insert("width", QVariant(width)); + map.insert("height", QVariant(height)); + QVariant jsValue(map); + this->initParams = jsValue; + + QVariantList args; + args.push_back(this->initParams); + callEntity(DoricConstant::DORIC_ENTITY_BUILD, args); +} + +void DoricContext::callEntity(QString methodName, QVariantList args) { + return getDriver()->invokeContextEntityMethod(this->mContextId, methodName, + args); +} + +DoricInterfaceDriver *DoricContext::getDriver() { + if (driver == NULL) { + driver = DoricNativeDriver::getInstance(); + return driver; + } + return driver; +} + +QObject *DoricContext::obtainPlugin(QString name) { + if (mPluginMap.keys().contains(name)) { + return mPluginMap.value(name); + } else { + QObject *plugin = + getDriver()->getRegistry()->pluginInfoMap.createObject(name); + mPluginMap.insert(name, plugin); + return plugin; + } +} diff --git a/doric-Qt/doric/DoricContext.h b/doric-Qt/doric/DoricContext.h new file mode 100644 index 00000000..ddb33465 --- /dev/null +++ b/doric-Qt/doric/DoricContext.h @@ -0,0 +1,36 @@ +#ifndef CONTEXT_H +#define CONTEXT_H + +#include + +#include "DoricInterfaceDriver.h" +#include "shader/DoricRootNode.h" + +class DoricContext { +private: + QString mContextId; + QMap mPluginMap; + DoricRootNode *mRootNode; + QString source; + QString script; + QString extra; + QVariant initParams; + DoricInterfaceDriver *driver = NULL; + +public: + DoricContext(QString contextId, QString source, QString extra); + + static DoricContext *create(QString script, QString source, QString extra); + + void init(QString initData); + + void build(int width, int height); + + void callEntity(QString methodName, QVariantList args); + + DoricInterfaceDriver *getDriver(); + + QObject *obtainPlugin(QString name); +}; + +#endif // CONTEXT_H diff --git a/doric-Qt/doric/DoricContextManager.cpp b/doric-Qt/doric/DoricContextManager.cpp new file mode 100644 index 00000000..ecb8746a --- /dev/null +++ b/doric-Qt/doric/DoricContextManager.cpp @@ -0,0 +1,16 @@ +#include "DoricContextManager.h" + +DoricContext *DoricContextManager::createContext(QString script, QString source, + QString extra) { + int contextId = counter->fetchAndAddOrdered(1); + DoricContext *context = + new DoricContext(QString::number(contextId), source, extra); + contextMap->insert(QString::number(contextId), context); + context->getDriver()->createContext(QString::number(contextId), script, + source); + return context; +} + +DoricContext *DoricContextManager::getContext(QString contextId) { + return contextMap->take(contextId); +} diff --git a/doric-Qt/doric/DoricContextManager.h b/doric-Qt/doric/DoricContextManager.h new file mode 100644 index 00000000..f1e0d60b --- /dev/null +++ b/doric-Qt/doric/DoricContextManager.h @@ -0,0 +1,30 @@ +#ifndef CONTEXTMANAGER_H +#define CONTEXTMANAGER_H + +#include + +#include "DoricContext.h" + +class DoricContextManager { +private: + static DoricContextManager *local_instance; + DoricContextManager() {} + + ~DoricContextManager() {} + + QAtomicInt *counter = new QAtomicInt(); + QMap *contextMap = + new QMap(); + +public: + static DoricContextManager *getInstance() { + static DoricContextManager instance; + return &instance; + } + + DoricContext *createContext(QString script, QString source, QString extra); + + DoricContext *getContext(QString contextId); +}; + +#endif // CONTEXTMANAGER_H diff --git a/doric-Qt/doric/DoricInterfaceDriver.h b/doric-Qt/doric/DoricInterfaceDriver.h new file mode 100644 index 00000000..6b4829d5 --- /dev/null +++ b/doric-Qt/doric/DoricInterfaceDriver.h @@ -0,0 +1,24 @@ +#ifndef INTERFACEDRIVER_H +#define INTERFACEDRIVER_H + +#include +#include + +#include "DoricRegistry.h" + +class DoricInterfaceDriver { +public: + virtual void invokeContextEntityMethod(QString contextId, QString method, + QVariantList args) = 0; + + virtual void invokeDoricMethod(QString method, QVariantList args) = 0; + + virtual void createContext(QString contextId, QString script, + QString source) = 0; + + virtual void destroyContext(QString contextId) = 0; + + virtual DoricRegistry *getRegistry() = 0; +}; + +#endif // INTERFACEDRIVER_H diff --git a/doric-Qt/doric/DoricNativeDriver.cpp b/doric-Qt/doric/DoricNativeDriver.cpp new file mode 100644 index 00000000..fd56a360 --- /dev/null +++ b/doric-Qt/doric/DoricNativeDriver.cpp @@ -0,0 +1,34 @@ +#include + +#include "DoricNativeDriver.h" +#include "async/DoricAsyncCall.h" +#include "utils/DoricConstant.h" + +void DoricNativeDriver::invokeContextEntityMethod(QString contextId, + QString method, + QVariantList args) { + args.insert(0, QVariant(contextId)); + args.insert(1, QVariant(method)); + invokeDoricMethod(DoricConstant::DORIC_CONTEXT_INVOKE, args); +} + +void DoricNativeDriver::invokeDoricMethod(QString method, QVariantList args) { + return DoricAsyncCall::ensureRunInThreadPool( + &jsEngine.mJSThreadPool, [this, method, args] { + this->jsEngine.invokeDoricMethod(method, args).toString(); + }); +} + +void DoricNativeDriver::createContext(QString contextId, QString script, + QString source) { + DoricAsyncCall::ensureRunInThreadPool( + &jsEngine.mJSThreadPool, [this, contextId, script, source] { + this->jsEngine.prepareContext(contextId, script, source); + }); +} + +void DoricNativeDriver::destroyContext(QString contextId) {} + +DoricRegistry *DoricNativeDriver::getRegistry() { + return this->jsEngine.getRegistry(); +} diff --git a/doric-Qt/doric/DoricNativeDriver.h b/doric-Qt/doric/DoricNativeDriver.h new file mode 100644 index 00000000..cec3d0ca --- /dev/null +++ b/doric-Qt/doric/DoricNativeDriver.h @@ -0,0 +1,36 @@ +#ifndef NATIVEDRIVER_H +#define NATIVEDRIVER_H + +#include + +#include "DoricInterfaceDriver.h" +#include "engine/DoricJSEngine.h" + +class DoricNativeDriver : public DoricInterfaceDriver { +private: + static DoricNativeDriver *local_instance; + DoricNativeDriver() {} + + ~DoricNativeDriver() {} + + DoricJSEngine jsEngine; + +public: + static DoricNativeDriver *getInstance() { + static DoricNativeDriver instance; + return &instance; + } + + void invokeContextEntityMethod(QString contextId, QString method, + QVariantList args) override; + + void invokeDoricMethod(QString method, QVariantList args) override; + + void createContext(QString contextId, QString script, + QString source) override; + + void destroyContext(QString contextId) override; + + DoricRegistry *getRegistry() override; +}; +#endif // NATIVEDRIVER_H diff --git a/doric-Qt/doric/DoricPanel.cpp b/doric-Qt/doric/DoricPanel.cpp new file mode 100644 index 00000000..ad95560c --- /dev/null +++ b/doric-Qt/doric/DoricPanel.cpp @@ -0,0 +1,13 @@ +#include "DoricPanel.h" + +DoricPanel::DoricPanel() {} + +void DoricPanel::config(QString script, QString alias, QString extra) { + DoricContext *context = DoricContext::create(script, alias, extra); + config(context); +} + +void DoricPanel::config(DoricContext *context) { + this->mContext = context; + this->mContext->build(960, 720); +} diff --git a/doric-Qt/doric/DoricPanel.h b/doric-Qt/doric/DoricPanel.h new file mode 100644 index 00000000..12fc0137 --- /dev/null +++ b/doric-Qt/doric/DoricPanel.h @@ -0,0 +1,20 @@ +#ifndef PANEL_H +#define PANEL_H + +#include "DoricContext.h" + +class DoricPanel { +private: + DoricContext *mContext; + int renderedWidth = -1; + int renderedHeight = -1; + +public: + DoricPanel(); + + void config(QString script, QString alias, QString extra); + + void config(DoricContext *context); +}; + +#endif // PANEL_H diff --git a/doric-Qt/doric/DoricRegistry.cpp b/doric-Qt/doric/DoricRegistry.cpp new file mode 100644 index 00000000..17751f89 --- /dev/null +++ b/doric-Qt/doric/DoricRegistry.cpp @@ -0,0 +1,11 @@ +#include "DoricRegistry.h" + +#include "plugin/DoricShaderPlugin.h" + +DoricRegistry::DoricRegistry() { + registerNativePlugin("shader"); +} + +bool DoricRegistry::acquirePluginInfo(QString name) { + return pluginInfoMap.acquireClass(name); +} diff --git a/doric-Qt/doric/DoricRegistry.h b/doric-Qt/doric/DoricRegistry.h new file mode 100644 index 00000000..0d867735 --- /dev/null +++ b/doric-Qt/doric/DoricRegistry.h @@ -0,0 +1,21 @@ +#ifndef REGISTRY_H +#define REGISTRY_H + +#include + +#include "utils/DoricObjectFactory.h" + +class DoricRegistry { +public: + DoricObjectFactory pluginInfoMap; + + DoricRegistry(); + + template void registerNativePlugin(QString name) { + pluginInfoMap.registerClass(name); + } + + bool acquirePluginInfo(QString name); +}; + +#endif // REGISTRY_H diff --git a/doric-Qt/doric/async/DoricAsyncCall.h b/doric-Qt/doric/async/DoricAsyncCall.h new file mode 100644 index 00000000..047efca1 --- /dev/null +++ b/doric-Qt/doric/async/DoricAsyncCall.h @@ -0,0 +1,16 @@ +#ifndef ASYNC_CALL_H +#define ASYNC_CALL_H + +#include +#include + +class DoricAsyncCall { + +public: + static void ensureRunInThreadPool(QThreadPool *threadPool, + std::function lambda) { + QtConcurrent::run(threadPool, lambda); + } +}; + +#endif // ASYNC_CALL_H diff --git a/doric-Qt/doric/async/DoricAsyncResult.cpp b/doric-Qt/doric/async/DoricAsyncResult.cpp new file mode 100644 index 00000000..3bc37885 --- /dev/null +++ b/doric-Qt/doric/async/DoricAsyncResult.cpp @@ -0,0 +1,38 @@ +#include "DoricAsyncResult.h" + +DoricAsyncResult::DoricAsyncResult() {} + +DoricAsyncResult::DoricAsyncResult(QJSValue result) { this->result = result; } + +void DoricAsyncResult::setResult(QJSValue result) { + this->result = result; + if (this->callback != NULL) { + this->callback->onResult(result); + this->callback->onFinish(); + } +} + +void DoricAsyncResult::setError(QJSValue exception) { + this->result = exception; + if (this->callback != NULL) { + this->callback->onResult(result); + this->callback->onFinish(); + } +} + +bool DoricAsyncResult::hasResult() { return !result.equals(EMPTY); } + +QJSValue DoricAsyncResult::getResult() { return this->result; } + +void DoricAsyncResult::setCallback(DoricCallback *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(); + } +} + +DoricSettableFuture *DoricAsyncResult::synchronous() { return NULL; } diff --git a/doric-Qt/doric/async/DoricAsyncResult.h b/doric-Qt/doric/async/DoricAsyncResult.h new file mode 100644 index 00000000..9228a6ba --- /dev/null +++ b/doric-Qt/doric/async/DoricAsyncResult.h @@ -0,0 +1,34 @@ +#ifndef ASYNCRESULT_H +#define ASYNCRESULT_H + +#include + +#include "DoricCallback.h" +#include "DoricSettableFuture.h" + +static QJSValue EMPTY(QJSValue::NullValue); + +class DoricAsyncResult { +private: + QJSValue result = EMPTY; + DoricCallback *callback; + +public: + DoricAsyncResult(); + + DoricAsyncResult(QJSValue result); + + void setResult(QJSValue result); + + void setError(QJSValue exception); + + bool hasResult(); + + QJSValue getResult(); + + void setCallback(DoricCallback *callback); + + DoricSettableFuture *synchronous(); +}; + +#endif // ASYNCRESULT_H diff --git a/doric-Qt/doric/async/DoricCallback.h b/doric-Qt/doric/async/DoricCallback.h new file mode 100644 index 00000000..3938fc6c --- /dev/null +++ b/doric-Qt/doric/async/DoricCallback.h @@ -0,0 +1,15 @@ +#ifndef CALLBACK_H +#define CALLBACK_H + +#include + +class DoricCallback { +public: + virtual void onResult(QJSValue result) = 0; + + virtual void onError(QJSValue error) = 0; + + virtual void onFinish() = 0; +}; + +#endif // CALLBACK_H diff --git a/doric-Qt/doric/async/DoricSettableFuture.cpp b/doric-Qt/doric/async/DoricSettableFuture.cpp new file mode 100644 index 00000000..a5e8cf9b --- /dev/null +++ b/doric-Qt/doric/async/DoricSettableFuture.cpp @@ -0,0 +1,17 @@ +#include + +#include "DoricSettableFuture.h" + +void DoricSettableFuture::set(QJSValue result) { + if (mReadyLatch == NULL) { + qDebug() << "Result has already been set!"; + return; + } + mResult = result; + delete mReadyLatch; +} + +QJSValue DoricSettableFuture::get() { + mReadyLatch->lock(); + return mResult; +} diff --git a/doric-Qt/doric/async/DoricSettableFuture.h b/doric-Qt/doric/async/DoricSettableFuture.h new file mode 100644 index 00000000..fce4f8ea --- /dev/null +++ b/doric-Qt/doric/async/DoricSettableFuture.h @@ -0,0 +1,19 @@ +#ifndef SETTABLE_FUTURE_H +#define SETTABLE_FUTURE_H + +#include + +#include "utils/DoricCountDownLatch.h" + +class DoricSettableFuture { +private: + QJSValue mResult; + DoricCountDownLatch *mReadyLatch = new DoricCountDownLatch(); + +public: + void set(QJSValue result); + + QJSValue get(); +}; + +#endif // SETTABLE_FUTURE_H diff --git a/doric-Qt/doric/async/async_call.h b/doric-Qt/doric/async/async_call.h deleted file mode 100644 index 117ee5cc..00000000 --- a/doric-Qt/doric/async/async_call.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef ASYNC_CALL_H -#define ASYNC_CALL_H - -#include -#include - -class AsyncCall { - -public: - static void ensureRunInThreadPool(QThreadPool *threadPool, std::function lambda) - { - QtConcurrent::run(threadPool, lambda); - } -}; - -#endif // ASYNC_CALL_H diff --git a/doric-Qt/doric/async/async_result.cpp b/doric-Qt/doric/async/async_result.cpp deleted file mode 100644 index a1d20f90..00000000 --- a/doric-Qt/doric/async/async_result.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#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; -} diff --git a/doric-Qt/doric/async/async_result.h b/doric-Qt/doric/async/async_result.h deleted file mode 100644 index 922414e2..00000000 --- a/doric-Qt/doric/async/async_result.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef ASYNCRESULT_H -#define ASYNCRESULT_H - -#include - -#include "callback.h" -#include "settable_future.h" - -static QJSValue EMPTY(QJSValue::NullValue); - -class AsyncResult -{ -private: - QJSValue result = EMPTY; - Callback *callback; - -public: - AsyncResult(); - - AsyncResult(QJSValue result); - - void setResult(QJSValue result); - - void setError(QJSValue exception); - - bool hasResult(); - - QJSValue getResult(); - - void setCallback(Callback *callback); - - SettableFuture *synchronous(); -}; - -#endif // ASYNCRESULT_H diff --git a/doric-Qt/doric/async/callback.h b/doric-Qt/doric/async/callback.h deleted file mode 100644 index 92ed77bd..00000000 --- a/doric-Qt/doric/async/callback.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef CALLBACK_H -#define CALLBACK_H - -#include - -class Callback { -public: - virtual void onResult(QJSValue result) = 0; - - virtual void onError(QJSValue error) = 0; - - virtual void onFinish() = 0; -}; - -#endif // CALLBACK_H diff --git a/doric-Qt/doric/async/settable_future.cpp b/doric-Qt/doric/async/settable_future.cpp deleted file mode 100644 index 8141ba43..00000000 --- a/doric-Qt/doric/async/settable_future.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include - -#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; -} diff --git a/doric-Qt/doric/async/settable_future.h b/doric-Qt/doric/async/settable_future.h deleted file mode 100644 index e24751e8..00000000 --- a/doric-Qt/doric/async/settable_future.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef SETTABLE_FUTURE_H -#define SETTABLE_FUTURE_H - -#include -#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 diff --git a/doric-Qt/doric/context.cpp b/doric-Qt/doric/context.cpp deleted file mode 100644 index d735e003..00000000 --- a/doric-Qt/doric/context.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "context.h" -#include "native_driver.h" -#include "context_manager.h" -#include "utils/constant.h" - -Context::Context(QString contextId, QString source, QString extra) -{ - this->mRootNode = new RootNode(); - - this->mContextId = contextId; - this->source = source; - this->extra = extra; -} - -Context* Context::create(QString script, QString source, QString extra) -{ - Context *context = ContextManager::getInstance()->createContext(script, source, extra); - context->script = script; - context->init(extra); - - QVariantList args; - context->callEntity(Constant::DORIC_ENTITY_CREATE, args); - return context; -} - -void Context::init(QString initData) -{ - this->extra = initData; - if (!initData.isEmpty()) { - QVariantList args; - args.push_back(initData); - callEntity(Constant::DORIC_ENTITY_INIT, args); - } -} - -void Context::build(int width, int height) -{ - QMap map; - map.insert("width", QVariant(width)); - map.insert("height", QVariant(height)); - QVariant jsValue(map); - this->initParams = jsValue; - - QVariantList args; - args.push_back(this->initParams); - callEntity(Constant::DORIC_ENTITY_BUILD, args); -} - -void Context::callEntity(QString methodName, QVariantList args) -{ - return getDriver()->invokeContextEntityMethod(this->mContextId, methodName, args); -} - -InterfaceDriver* Context::getDriver() -{ - if (driver == NULL) - { - driver = NativeDriver::getInstance(); - return driver; - } - return driver; -} - -QObject* Context::obtainPlugin(QString name) -{ - if (mPluginMap.keys().contains(name)) { - return mPluginMap.value(name); - } else { - QObject *plugin = getDriver()->getRegistry()->pluginInfoMap.createObject(name); - mPluginMap.insert(name, plugin); - return plugin; - } -} diff --git a/doric-Qt/doric/context.h b/doric-Qt/doric/context.h deleted file mode 100644 index 6c5198d2..00000000 --- a/doric-Qt/doric/context.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef CONTEXT_H -#define CONTEXT_H - -#include - -#include "interface_driver.h" -#include "shader/root_node.h" - -class Context -{ -private: - QString mContextId; - QMap mPluginMap; - RootNode *mRootNode; - QString source; - QString script; - QString extra; - QVariant initParams; - InterfaceDriver *driver = NULL; - -public: - Context(QString contextId, QString source, QString extra); - - static Context* create(QString script, QString source, QString extra); - - void init(QString initData); - - void build(int width, int height); - - void callEntity(QString methodName, QVariantList args); - - InterfaceDriver* getDriver(); - - QObject* obtainPlugin(QString name); -}; - -#endif // CONTEXT_H diff --git a/doric-Qt/doric/context_manager.cpp b/doric-Qt/doric/context_manager.cpp deleted file mode 100644 index 02d30f49..00000000 --- a/doric-Qt/doric/context_manager.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "context_manager.h" - -Context *ContextManager::createContext(QString script, QString source, QString extra) -{ - int contextId = counter->fetchAndAddOrdered(1); - Context *context = new Context(QString::number(contextId), source, extra); - contextMap->insert(QString::number(contextId), context); - context->getDriver()->createContext(QString::number(contextId), script, source); - return context; -} - -Context *ContextManager::getContext(QString contextId) -{ - return contextMap->take(contextId); -} diff --git a/doric-Qt/doric/context_manager.h b/doric-Qt/doric/context_manager.h deleted file mode 100644 index 447693cd..00000000 --- a/doric-Qt/doric/context_manager.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef CONTEXTMANAGER_H -#define CONTEXTMANAGER_H - -#include - -#include "context.h" - -class ContextManager -{ -private: - static ContextManager *local_instance; - ContextManager() - { - } - - ~ContextManager() - { - } - - QAtomicInt *counter = new QAtomicInt(); - QMap *contextMap = new QMap(); - -public: - static ContextManager *getInstance() - { - static ContextManager instance; - return &instance; - } - - Context *createContext(QString script, QString source, QString extra); - - Context *getContext(QString contextId); -}; - -#endif // CONTEXTMANAGER_H diff --git a/doric-Qt/doric/demo/DoricDemoBridge.cpp b/doric-Qt/doric/demo/DoricDemoBridge.cpp new file mode 100644 index 00000000..09e24f80 --- /dev/null +++ b/doric-Qt/doric/demo/DoricDemoBridge.cpp @@ -0,0 +1,19 @@ +#include + +#include "DoricDemoBridge.h" +#include "DoricPanel.h" +#include "utils/DoricUtils.h" + +DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {} + +void DoricDemoBridge::navigate(QVariant route) { + switch (route.toInt()) { + case 0: + QString name = "Snake.es5.js"; + QString script = DoricUtils::readAssetFile("/doric/bundles", name); + + DoricPanel panel; + panel.config(script, name, NULL); + break; + } +} diff --git a/doric-Qt/doric/demo/DoricDemoBridge.h b/doric-Qt/doric/demo/DoricDemoBridge.h new file mode 100644 index 00000000..76a42e02 --- /dev/null +++ b/doric-Qt/doric/demo/DoricDemoBridge.h @@ -0,0 +1,17 @@ +#ifndef DEMOBRIDGE_H +#define DEMOBRIDGE_H + +#include +#include + +class DoricDemoBridge : public QObject { + Q_OBJECT +public: + explicit DoricDemoBridge(QObject *parent = nullptr); + + Q_INVOKABLE + void navigate(QVariant route); +signals: +}; + +#endif // DEMOBRIDGE_H diff --git a/doric-Qt/doric/demo/demo_bridge.cpp b/doric-Qt/doric/demo/demo_bridge.cpp deleted file mode 100644 index 2ae30f49..00000000 --- a/doric-Qt/doric/demo/demo_bridge.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include - -#include "demo_bridge.h" -#include "utils/utils.h" -#include "panel.h" - -DemoBridge::DemoBridge(QObject *parent) : QObject(parent) -{ - -} - -void DemoBridge::navigate(QVariant route) -{ - switch (route.toInt()) { - case 0: - QString name = "Snake.es5.js"; - QString script = Utils::readAssetFile("/doric/bundles", name); - - Panel panel; - panel.config(script, name, NULL); - break; - } -} diff --git a/doric-Qt/doric/demo/demo_bridge.h b/doric-Qt/doric/demo/demo_bridge.h deleted file mode 100644 index 258da1c6..00000000 --- a/doric-Qt/doric/demo/demo_bridge.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef DEMOBRIDGE_H -#define DEMOBRIDGE_H - -#include -#include - -class DemoBridge : public QObject -{ - Q_OBJECT -public: - explicit DemoBridge(QObject *parent = nullptr); - - Q_INVOKABLE - void navigate(QVariant route); -signals: - -}; - -#endif // DEMOBRIDGE_H diff --git a/doric-Qt/doric/doric.pro b/doric-Qt/doric/doric.pro index 0c8eee98..339b9d94 100644 --- a/doric-Qt/doric/doric.pro +++ b/doric-Qt/doric/doric.pro @@ -14,25 +14,25 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ - async/async_result.cpp \ - async/settable_future.cpp \ - context.cpp \ - context_manager.cpp \ - demo/demo_bridge.cpp \ - engine/bridge_extension.cpp \ - engine/js_engine.cpp \ - engine/native_empty.cpp \ - engine/native_jse.cpp \ - engine/native_log.cpp \ - engine/native_require.cpp \ - engine/timer_extension.cpp \ + DoricContext.cpp \ + DoricContextManager.cpp \ + DoricNativeDriver.cpp \ + DoricPanel.cpp \ + DoricRegistry.cpp \ + async/DoricAsyncResult.cpp \ + async/DoricSettableFuture.cpp \ + demo/DoricDemoBridge.cpp \ + engine/DoricBridgeExtension.cpp \ + engine/DoricJSEngine.cpp \ + engine/DoricNativeEmpty.cpp \ + engine/DoricNativeJSE.cpp \ + engine/DoricNativeLog.cpp \ + engine/DoricNativeRequire.cpp \ + engine/DoricTimerExtension.cpp \ main.cpp \ - native_driver.cpp \ - panel.cpp \ - plugin/shader_plugin.cpp \ - registry.cpp \ - shader/root_node.cpp \ - utils/constant.cpp + plugin/DoricShaderPlugin.cpp \ + shader/DoricRootNode.cpp \ + utils/DoricConstant.cpp RESOURCES += qml.qrc @@ -48,29 +48,29 @@ 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 \ - async/settable_future.h \ - context.h \ - context_manager.h \ - demo/demo_bridge.h \ - engine/bridge_extension.h \ - engine/interface_jse.h \ - engine/js_engine.h \ - engine/native_empty.h \ - engine/native_jse.h \ - engine/native_log.h \ - engine/native_require.h \ - engine/timer_extension.h \ - interface_driver.h \ - native_driver.h \ - panel.h \ - plugin/shader_plugin.h \ - registry.h \ - shader/root_node.h \ - template/singleton.h \ - utils/constant.h \ - utils/count_down_latch.h \ - utils/object_factory.h \ - utils/utils.h + DoricContext.h \ + DoricContextManager.h \ + DoricInterfaceDriver.h \ + DoricNativeDriver.h \ + DoricPanel.h \ + DoricRegistry.h \ + async/DoricAsyncCall.h \ + async/DoricAsyncResult.h \ + async/DoricCallback.h \ + async/DoricSettableFuture.h \ + demo/DoricDemoBridge.h \ + engine/DoricBridgeExtension.h \ + engine/DoricInterfaceJSE.h \ + engine/DoricJSEngine.h \ + engine/DoricNativeEmpty.h \ + engine/DoricNativeJSE.h \ + engine/DoricNativeLog.h \ + engine/DoricNativeRequire.h \ + engine/DoricTimerExtension.h \ + plugin/DoricShaderPlugin.h \ + shader/DoricRootNode.h \ + template/DoricSingleton.h \ + utils/DoricConstant.h \ + utils/DoricCountDownLatch.h \ + utils/DoricObjectFactory.h \ + utils/DoricUtils.h diff --git a/doric-Qt/doric/doric.pro.user b/doric-Qt/doric/doric.pro.user index 01231fa4..b8013ae0 100644 --- a/doric-Qt/doric/doric.pro.user +++ b/doric-Qt/doric/doric.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/doric-Qt/doric/engine/DoricBridgeExtension.cpp b/doric-Qt/doric/engine/DoricBridgeExtension.cpp new file mode 100644 index 00000000..0f284ec2 --- /dev/null +++ b/doric-Qt/doric/engine/DoricBridgeExtension.cpp @@ -0,0 +1,29 @@ +#include +#include + +#include "../DoricContextManager.h" +#include "DoricBridgeExtension.h" + +DoricBridgeExtension::DoricBridgeExtension(QObject *parent) : QObject(parent) {} + +void DoricBridgeExtension::callNative(QString contextId, QString module, + QString methodName, QString callbackId, + QJSValue jsValue) { + DoricContext *context = + DoricContextManager::getInstance()->getContext(contextId); + bool classRegistered = + context->getDriver()->getRegistry()->acquirePluginInfo(module); + if (classRegistered) { + QObject *plugin = context->obtainPlugin(module); + QMetaObject::invokeMethod(plugin, methodName.toStdString().c_str(), + Qt::DirectConnection, QGenericReturnArgument(), + Q_ARG(QJSValue, jsValue), + Q_ARG(QString, callbackId)); + qDebug() << plugin; + } + qDebug() << "contextId: " + contextId; + qDebug() << "module: " + module; + qDebug() << "methodName: " + methodName; + qDebug() << "callbackId: " + callbackId; + qDebug() << "jsValue: " + jsValue.toString(); +} diff --git a/doric-Qt/doric/engine/DoricBridgeExtension.h b/doric-Qt/doric/engine/DoricBridgeExtension.h new file mode 100644 index 00000000..a2901d1e --- /dev/null +++ b/doric-Qt/doric/engine/DoricBridgeExtension.h @@ -0,0 +1,17 @@ +#ifndef BRIDGEEXTENSION_H +#define BRIDGEEXTENSION_H + +#include +#include + +class DoricBridgeExtension : public QObject { + Q_OBJECT +public: + explicit DoricBridgeExtension(QObject *parent = nullptr); + + Q_INVOKABLE void callNative(QString contextId, QString module, + QString methodName, QString callbackId, + QJSValue jsValue); +}; + +#endif // BRIDGEEXTENSION_H diff --git a/doric-Qt/doric/engine/DoricInterfaceJSE.h b/doric-Qt/doric/engine/DoricInterfaceJSE.h new file mode 100644 index 00000000..68a878e1 --- /dev/null +++ b/doric-Qt/doric/engine/DoricInterfaceJSE.h @@ -0,0 +1,21 @@ +#ifndef INTERFACE_JSE_H +#define INTERFACE_JSE_H + +#include +#include +#include + +class DoricInterfaceJSE { +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, + QVariantList arguments) = 0; +}; + +#endif // INTERFACE_JSE_H diff --git a/doric-Qt/doric/engine/DoricJSEngine.cpp b/doric-Qt/doric/engine/DoricJSEngine.cpp new file mode 100644 index 00000000..ca442155 --- /dev/null +++ b/doric-Qt/doric/engine/DoricJSEngine.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include +#include + +#include "../utils/DoricConstant.h" +#include "../utils/DoricUtils.h" +#include "DoricBridgeExtension.h" +#include "DoricJSEngine.h" +#include "DoricNativeEmpty.h" +#include "DoricNativeJSE.h" +#include "DoricNativeLog.h" +#include "DoricNativeRequire.h" +#include "DoricTimerExtension.h" + +DoricJSEngine::DoricJSEngine(QObject *parent) : QObject(parent) { + mJSThreadPool.setMaxThreadCount(1); + + QtConcurrent::run(&mJSThreadPool, [this] { mJSE = new DoricNativeJSE(); }); + 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(DoricConstant::INJECT_ENVIRONMENT, envObject); + + // inject log + DoricNativeLog *nativeLog = new DoricNativeLog(); + mJSE->injectGlobalJSFunction(DoricConstant::INJECT_LOG, nativeLog, + "function"); + + // inject empty + DoricNativeEmpty *nativeEmpty = new DoricNativeEmpty(); + mJSE->injectGlobalJSFunction(DoricConstant::INJECT_EMPTY, nativeEmpty, + "function"); + + // inject require + DoricNativeRequire *nativeRequire = new DoricNativeRequire(); + mJSE->injectGlobalJSFunction(DoricConstant::INJECT_REQUIRE, nativeRequire, + "function"); + + // inject timer set & clear + DoricTimerExtension *timerExtension = + new DoricTimerExtension([this](long timerId) { + QVariantList arguments; + arguments.push_back(QVariant((int)timerId)); + this->invokeDoricMethod(DoricConstant::DORIC_TIMER_CALLBACK, + arguments); + }); + mJSE->injectGlobalJSFunction(DoricConstant::INJECT_TIMER_SET, + timerExtension, "setTimer"); + mJSE->injectGlobalJSFunction(DoricConstant::INJECT_TIMER_CLEAR, + timerExtension, "clearTimer"); + + DoricBridgeExtension *bridgeExtension = new DoricBridgeExtension(); + mJSE->injectGlobalJSFunction(DoricConstant::INJECT_BRIDGE, bridgeExtension, + "callNative"); + }); + QtConcurrent::run(&mJSThreadPool, [this] { + loadBuiltinJS(DoricConstant::DORIC_BUNDLE_SANDBOX); + + QString libName = DoricConstant::DORIC_MODULE_LIB; + QString libJS = + DoricUtils::readAssetFile("/doric", DoricConstant::DORIC_BUNDLE_LIB); + QString script = packageModuleScript(libName, libJS); + + mJSE->loadJS(script, "Module://" + libName); + }); +} + +void DoricJSEngine::prepareContext(QString contextId, QString script, + QString source) { + mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source); +} + +QJSValue DoricJSEngine::invokeDoricMethod(QString method, + QVariantList arguments) { + return mJSE->invokeObject(DoricConstant::GLOBAL_DORIC, method, arguments); +} + +void DoricJSEngine::loadBuiltinJS(QString assetName) { + QString script = DoricUtils::readAssetFile("/doric", assetName); + QString result = mJSE->loadJS(script, "Assets://" + assetName); +} + +QString DoricJSEngine::packageContextScript(QString contextId, + QString content) { + return QString(DoricConstant::TEMPLATE_CONTEXT_CREATE) + .replace("%s1", content) + .replace("%s2", contextId) + .replace("%s3", contextId); +} + +QString DoricJSEngine::packageModuleScript(QString moduleName, + QString content) { + return QString(DoricConstant::TEMPLATE_MODULE) + .replace("%s1", moduleName) + .replace("%s2", content); +} + +DoricRegistry *DoricJSEngine::getRegistry() { return this->mRegistry; } + +DoricJSEngine::~DoricJSEngine() {} diff --git a/doric-Qt/doric/engine/DoricJSEngine.h b/doric-Qt/doric/engine/DoricJSEngine.h new file mode 100644 index 00000000..8ad5a1cc --- /dev/null +++ b/doric-Qt/doric/engine/DoricJSEngine.h @@ -0,0 +1,32 @@ +#ifndef JSENGINE_H +#define JSENGINE_H + +#include +#include + +#include "../DoricRegistry.h" +#include "DoricInterfaceJSE.h" + +class DoricJSEngine : public QObject { + Q_OBJECT +private: + DoricInterfaceJSE *mJSE; + DoricRegistry *mRegistry = new DoricRegistry(); + + void loadBuiltinJS(QString assetName); + QString packageContextScript(QString contextId, QString content); + QString packageModuleScript(QString moduleName, QString content); + +public: + QThreadPool mJSThreadPool; + + explicit DoricJSEngine(QObject *parent = nullptr); + + ~DoricJSEngine(); + + void prepareContext(QString contextId, QString script, QString source); + QJSValue invokeDoricMethod(QString method, QVariantList arguments); + DoricRegistry *getRegistry(); +}; + +#endif // JSENGINE_H diff --git a/doric-Qt/doric/engine/DoricNativeEmpty.cpp b/doric-Qt/doric/engine/DoricNativeEmpty.cpp new file mode 100644 index 00000000..84d2ea9d --- /dev/null +++ b/doric-Qt/doric/engine/DoricNativeEmpty.cpp @@ -0,0 +1,7 @@ +#include "DoricNativeEmpty.h" +#include + +Q_INVOKABLE QJSValue DoricNativeEmpty::function() { + qDebug() << "nativeEmpty"; + return QJSValue::NullValue; +} diff --git a/doric-Qt/doric/engine/DoricNativeEmpty.h b/doric-Qt/doric/engine/DoricNativeEmpty.h new file mode 100644 index 00000000..52240d11 --- /dev/null +++ b/doric-Qt/doric/engine/DoricNativeEmpty.h @@ -0,0 +1,16 @@ +#ifndef NATIVEEMPTY_H +#define NATIVEEMPTY_H + +#include +#include + +class DoricNativeEmpty : public QObject { + Q_OBJECT + +public: + DoricNativeEmpty(QObject *parent = nullptr) : QObject(parent) {} + + Q_INVOKABLE QJSValue function(); +}; + +#endif // NATIVEEMPTY_H diff --git a/doric-Qt/doric/engine/DoricNativeJSE.cpp b/doric-Qt/doric/engine/DoricNativeJSE.cpp new file mode 100644 index 00000000..b019835b --- /dev/null +++ b/doric-Qt/doric/engine/DoricNativeJSE.cpp @@ -0,0 +1,65 @@ +#include "DoricNativeJSE.h" +#include + +DoricNativeJSE::DoricNativeJSE() { + mJSEngine.installExtensions(QJSEngine::AllExtensions); +} + +QString DoricNativeJSE::loadJS(QString script, QString source) { + return mJSEngine.evaluate(script, source).toString(); +} + +void DoricNativeJSE::injectGlobalJSObject(QString name, QObject *object) { + QJSValue jsObject = mJSEngine.newQObject(object); + + QList 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 DoricNativeJSE::injectGlobalJSFunction(QString name, QObject *function, + QString property) { + QJSValue functionObject = mJSEngine.newQObject(function); + mJSEngine.globalObject().setProperty(name, functionObject.property(property)); +} + +QJSValue DoricNativeJSE::invokeObject(QString objectName, QString functionName, + QVariantList arguments) { + QJSValue object = mJSEngine.evaluate(objectName); + QJSValue function = object.property(functionName); + + QJSValueList args; + foreach (QVariant variant, arguments) { + if (variant.type() == QVariant::String) { + args.push_back(QJSValue(variant.toString())); + } else if (variant.type() == QVariant::Map) { + QJSValue arg = mJSEngine.newObject(); + QMap map = variant.toMap(); + foreach (QString key, map.keys()) { + QVariant value = map.value(key); + if (value.type() == QVariant::String) { + arg.setProperty(key, value.toString()); + } else if (value.type() == QVariant::Int) { + arg.setProperty(key, value.toInt()); + } + } + args.push_back(arg); + } + } + + QJSValue result = function.call(args); + if (result.isError()) + qDebug() << "Uncaught exception at line" + << result.property("lineNumber").toInt() << ":" + << result.toString(); + return result; +} diff --git a/doric-Qt/doric/engine/DoricNativeJSE.h b/doric-Qt/doric/engine/DoricNativeJSE.h new file mode 100644 index 00000000..564fad55 --- /dev/null +++ b/doric-Qt/doric/engine/DoricNativeJSE.h @@ -0,0 +1,25 @@ +#ifndef NATIVE_JSE_H +#define NATIVE_JSE_H + +#include "DoricInterfaceJSE.h" +#include + +class DoricNativeJSE : public DoricInterfaceJSE { +private: + QJSEngine mJSEngine; + +public: + DoricNativeJSE(); + + 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, + QVariantList arguments) override; +}; + +#endif // NATIVE_JSE_H diff --git a/doric-Qt/doric/engine/DoricNativeLog.cpp b/doric-Qt/doric/engine/DoricNativeLog.cpp new file mode 100644 index 00000000..e15334b3 --- /dev/null +++ b/doric-Qt/doric/engine/DoricNativeLog.cpp @@ -0,0 +1,13 @@ +#include + +#include "DoricNativeLog.h" + +Q_INVOKABLE void DoricNativeLog::function(QString level, QString content) { + if (level == 'w') { + qWarning() << content; + } else if (level == 'd') { + qDebug() << content; + } else if (level == 'e') { + qCritical() << content; + } +} diff --git a/doric-Qt/doric/engine/DoricNativeLog.h b/doric-Qt/doric/engine/DoricNativeLog.h new file mode 100644 index 00000000..89fdad5e --- /dev/null +++ b/doric-Qt/doric/engine/DoricNativeLog.h @@ -0,0 +1,15 @@ +#ifndef NATIVE_LOG_H +#define NATIVE_LOG_H + +#include + +class DoricNativeLog : public QObject { + Q_OBJECT + +public: + DoricNativeLog(QObject *parent = nullptr) : QObject(parent) {} + + Q_INVOKABLE void function(QString level, QString content); +}; + +#endif // NATIVE_LOG_H diff --git a/doric-Qt/doric/engine/DoricNativeRequire.cpp b/doric-Qt/doric/engine/DoricNativeRequire.cpp new file mode 100644 index 00000000..5830fdd7 --- /dev/null +++ b/doric-Qt/doric/engine/DoricNativeRequire.cpp @@ -0,0 +1,8 @@ +#include "DoricNativeRequire.h" + +#include + +Q_INVOKABLE QJSValue DoricNativeRequire::function(QString name) { + qDebug() << "nativeRequire"; + return QJSValue::NullValue; +} diff --git a/doric-Qt/doric/engine/DoricNativeRequire.h b/doric-Qt/doric/engine/DoricNativeRequire.h new file mode 100644 index 00000000..70f1206a --- /dev/null +++ b/doric-Qt/doric/engine/DoricNativeRequire.h @@ -0,0 +1,16 @@ +#ifndef NATIVE_REQUIRE_H +#define NATIVE_REQUIRE_H + +#include +#include + +class DoricNativeRequire : public QObject { + Q_OBJECT + +public: + DoricNativeRequire(QObject *parent = nullptr) : QObject(parent) {} + + Q_INVOKABLE QJSValue function(QString name); +}; + +#endif // NATIVE_REQUIRE_H diff --git a/doric-Qt/doric/engine/DoricTimerExtension.cpp b/doric-Qt/doric/engine/DoricTimerExtension.cpp new file mode 100644 index 00000000..0842ea6a --- /dev/null +++ b/doric-Qt/doric/engine/DoricTimerExtension.cpp @@ -0,0 +1,28 @@ +#include + +#include "../utils/DoricConstant.h" +#include "DoricTimerExtension.h" + +Q_INVOKABLE void DoricTimerExtension::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 { + this->method(timerId); + + if (!repeat) { + deletedTimerIds->remove(timerId); + delete timer; + } + } + }); + timer->start(time); +} + +Q_INVOKABLE void DoricTimerExtension::clearTimer(long timerId) { + deletedTimerIds->insert(timerId); +} diff --git a/doric-Qt/doric/engine/DoricTimerExtension.h b/doric-Qt/doric/engine/DoricTimerExtension.h new file mode 100644 index 00000000..08a8406b --- /dev/null +++ b/doric-Qt/doric/engine/DoricTimerExtension.h @@ -0,0 +1,25 @@ +#ifndef NATIVETIMER_H +#define NATIVETIMER_H + +#include +#include + +class DoricTimerExtension : public QObject { + Q_OBJECT + +private: + QSet *deletedTimerIds = new QSet(); + std::function method; + +public: + explicit DoricTimerExtension(std::function 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 diff --git a/doric-Qt/doric/engine/bridge_extension.cpp b/doric-Qt/doric/engine/bridge_extension.cpp deleted file mode 100644 index b8fa133c..00000000 --- a/doric-Qt/doric/engine/bridge_extension.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -#include "bridge_extension.h" -#include "../context_manager.h" - -BridgeExtension::BridgeExtension(QObject *parent) : QObject(parent) -{ - -} - -void BridgeExtension::callNative(QString contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue) -{ - Context *context = ContextManager::getInstance()->getContext(contextId); - bool classRegistered = context->getDriver()->getRegistry()->acquirePluginInfo(module); - if (classRegistered) { - QObject *plugin = context->obtainPlugin(module); - QMetaObject::invokeMethod( - plugin, - methodName.toStdString().c_str(), - Qt::DirectConnection, QGenericReturnArgument(), - Q_ARG(QJSValue, jsValue), Q_ARG(QString, callbackId)); - qDebug() << plugin; - } - qDebug() << "contextId: " + contextId; - qDebug() << "module: " + module; - qDebug() << "methodName: " + methodName; - qDebug() << "callbackId: " + callbackId; - qDebug() << "jsValue: " + jsValue.toString(); -} diff --git a/doric-Qt/doric/engine/bridge_extension.h b/doric-Qt/doric/engine/bridge_extension.h deleted file mode 100644 index 0a309c42..00000000 --- a/doric-Qt/doric/engine/bridge_extension.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef BRIDGEEXTENSION_H -#define BRIDGEEXTENSION_H - -#include -#include - -class BridgeExtension : public QObject -{ - Q_OBJECT -public: - explicit BridgeExtension(QObject *parent = nullptr); - - Q_INVOKABLE void callNative(QString contextId, QString module, QString methodName, QString callbackId, QJSValue jsValue); -}; - -#endif // BRIDGEEXTENSION_H diff --git a/doric-Qt/doric/engine/interface_jse.h b/doric-Qt/doric/engine/interface_jse.h deleted file mode 100644 index 7f740b2f..00000000 --- a/doric-Qt/doric/engine/interface_jse.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef INTERFACE_JSE_H -#define INTERFACE_JSE_H - -#include -#include -#include - -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, QVariantList arguments) = 0; -}; - -#endif // INTERFACE_JSE_H diff --git a/doric-Qt/doric/engine/js_engine.cpp b/doric-Qt/doric/engine/js_engine.cpp deleted file mode 100644 index 56bf957e..00000000 --- a/doric-Qt/doric/engine/js_engine.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "js_engine.h" -#include "native_jse.h" -#include "../utils/constant.h" -#include "native_log.h" -#include "native_empty.h" -#include "native_require.h" -#include "timer_extension.h" -#include "bridge_extension.h" -#include "../utils/utils.h" - -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 require - NativeRequire *nativeRequire = new NativeRequire(); - mJSE->injectGlobalJSFunction(Constant::INJECT_REQUIRE, nativeRequire, "function"); - - // inject timer set & clear - TimerExtension *timerExtension = new TimerExtension([this](long timerId){ - QVariantList arguments; - arguments.push_back(QVariant((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(QString contextId, QString script, QString source) -{ - mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source); -} - -QJSValue JSEngine::invokeDoricMethod(QString method, QVariantList arguments) -{ - return mJSE->invokeObject(Constant::GLOBAL_DORIC, method, arguments); -} - -void JSEngine::loadBuiltinJS(QString assetName) -{ - QString script = Utils::readAssetFile("/doric", assetName); - QString result = mJSE->loadJS(script, "Assets://" + assetName); -} - -QString JSEngine::packageContextScript(QString contextId, QString content) -{ - return QString(Constant::TEMPLATE_CONTEXT_CREATE).replace("%s1", content).replace("%s2", contextId).replace("%s3", contextId); -} - -QString JSEngine::packageModuleScript(QString moduleName, QString content) -{ - return QString(Constant::TEMPLATE_MODULE).replace("%s1", moduleName).replace("%s2", content); -} - -Registry *JSEngine::getRegistry() -{ - return this->mRegistry; -} - -JSEngine::~JSEngine() -{ - -} diff --git a/doric-Qt/doric/engine/js_engine.h b/doric-Qt/doric/engine/js_engine.h deleted file mode 100644 index fdc62b1c..00000000 --- a/doric-Qt/doric/engine/js_engine.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef JSENGINE_H -#define JSENGINE_H - -#include -#include - -#include "interface_jse.h" -#include "../registry.h" - -class JSEngine : public QObject -{ - Q_OBJECT -private: - InterfaceJSE *mJSE; - Registry *mRegistry = new Registry(); - - void loadBuiltinJS(QString assetName); - QString packageContextScript(QString contextId, QString content); - QString packageModuleScript(QString moduleName, QString content); -public: - QThreadPool mJSThreadPool; - - explicit JSEngine(QObject *parent = nullptr); - - ~JSEngine(); - - void prepareContext(QString contextId, QString script, QString source); - QJSValue invokeDoricMethod(QString method, QVariantList arguments); - Registry *getRegistry(); -}; - -#endif // JSENGINE_H diff --git a/doric-Qt/doric/engine/native_empty.cpp b/doric-Qt/doric/engine/native_empty.cpp deleted file mode 100644 index 06c2d8d2..00000000 --- a/doric-Qt/doric/engine/native_empty.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "native_empty.h" -#include - -Q_INVOKABLE QJSValue NativeEmpty::function() { - qDebug() << "nativeEmpty"; - return QJSValue::NullValue; -} diff --git a/doric-Qt/doric/engine/native_empty.h b/doric-Qt/doric/engine/native_empty.h deleted file mode 100644 index db0bc24d..00000000 --- a/doric-Qt/doric/engine/native_empty.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef NATIVEEMPTY_H -#define NATIVEEMPTY_H - -#include -#include - -class NativeEmpty : public QObject { - Q_OBJECT - -public: - NativeEmpty(QObject *parent = nullptr) : QObject(parent) {} - - Q_INVOKABLE QJSValue function(); -}; - -#endif // NATIVEEMPTY_H diff --git a/doric-Qt/doric/engine/native_jse.cpp b/doric-Qt/doric/engine/native_jse.cpp deleted file mode 100644 index b6ea870c..00000000 --- a/doric-Qt/doric/engine/native_jse.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "native_jse.h" -#include - -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 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, QVariantList arguments) -{ - QJSValue object = mJSEngine.evaluate(objectName); - QJSValue function = object.property(functionName); - - QJSValueList args; - foreach(QVariant variant, arguments) { - if (variant.type() == QVariant::String) { - args.push_back(QJSValue(variant.toString())); - } else if (variant.type() == QVariant::Map) { - QJSValue arg = mJSEngine.newObject(); - QMap map = variant.toMap(); - foreach (QString key, map.keys()) { - QVariant value = map.value(key); - if (value.type() == QVariant::String) { - arg.setProperty(key, value.toString()); - } else if (value.type() == QVariant::Int) { - arg.setProperty(key, value.toInt()); - } - } - args.push_back(arg); - } - } - - QJSValue result = function.call(args); - if (result.isError()) - qDebug() - << "Uncaught exception at line" - << result.property("lineNumber").toInt() - << ":" << result.toString(); - return result; -} diff --git a/doric-Qt/doric/engine/native_jse.h b/doric-Qt/doric/engine/native_jse.h deleted file mode 100644 index dbdb62f2..00000000 --- a/doric-Qt/doric/engine/native_jse.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef NATIVE_JSE_H -#define NATIVE_JSE_H - -#include -#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, QVariantList arguments) override; -}; - -#endif // NATIVE_JSE_H diff --git a/doric-Qt/doric/engine/native_log.cpp b/doric-Qt/doric/engine/native_log.cpp deleted file mode 100644 index cce84723..00000000 --- a/doric-Qt/doric/engine/native_log.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include - -#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; - } -} diff --git a/doric-Qt/doric/engine/native_log.h b/doric-Qt/doric/engine/native_log.h deleted file mode 100644 index 56d9ae7f..00000000 --- a/doric-Qt/doric/engine/native_log.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef NATIVE_LOG_H -#define NATIVE_LOG_H - -#include - -class NativeLog : public QObject { - Q_OBJECT - -public: - NativeLog(QObject *parent = nullptr) : QObject(parent) {} - - Q_INVOKABLE void function(QString level, QString content); -}; - -#endif // NATIVE_LOG_H diff --git a/doric-Qt/doric/engine/native_require.cpp b/doric-Qt/doric/engine/native_require.cpp deleted file mode 100644 index 407a956e..00000000 --- a/doric-Qt/doric/engine/native_require.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "native_require.h" - -#include - -Q_INVOKABLE QJSValue NativeRequire::function(QString name) { - qDebug() << "nativeRequire"; - return QJSValue::NullValue; -} diff --git a/doric-Qt/doric/engine/native_require.h b/doric-Qt/doric/engine/native_require.h deleted file mode 100644 index 3044ad2f..00000000 --- a/doric-Qt/doric/engine/native_require.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef NATIVE_REQUIRE_H -#define NATIVE_REQUIRE_H - - -#include -#include - -class NativeRequire : public QObject { - Q_OBJECT - -public: - NativeRequire(QObject *parent = nullptr) : QObject(parent) {} - - Q_INVOKABLE QJSValue function(QString name); -}; - - -#endif // NATIVE_REQUIRE_H diff --git a/doric-Qt/doric/engine/timer_extension.cpp b/doric-Qt/doric/engine/timer_extension.cpp deleted file mode 100644 index eade7260..00000000 --- a/doric-Qt/doric/engine/timer_extension.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include - -#include "timer_extension.h" -#include "../utils/constant.h" - -Q_INVOKABLE void TimerExtension::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 { - this->method(timerId); - - if (!repeat) { - deletedTimerIds->remove(timerId); - delete timer; - } - } - }); - timer->start(time); -} - -Q_INVOKABLE void TimerExtension::clearTimer(long timerId) { - deletedTimerIds->insert(timerId); -} diff --git a/doric-Qt/doric/engine/timer_extension.h b/doric-Qt/doric/engine/timer_extension.h deleted file mode 100644 index 0125c9dd..00000000 --- a/doric-Qt/doric/engine/timer_extension.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef NATIVETIMER_H -#define NATIVETIMER_H - -#include -#include - -class TimerExtension : public QObject { - Q_OBJECT - -private: - QSet *deletedTimerIds = new QSet(); - std::function method; - -public: - explicit TimerExtension(std::function 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 diff --git a/doric-Qt/doric/interface_driver.h b/doric-Qt/doric/interface_driver.h deleted file mode 100644 index 9e2ba10a..00000000 --- a/doric-Qt/doric/interface_driver.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef INTERFACEDRIVER_H -#define INTERFACEDRIVER_H - -#include -#include - -#include "registry.h" - -class InterfaceDriver -{ -public: - virtual void invokeContextEntityMethod(QString contextId, QString method, QVariantList args) = 0; - - virtual void invokeDoricMethod(QString method, QVariantList args) = 0; - - virtual void createContext(QString contextId, QString script, QString source) = 0; - - virtual void destroyContext(QString contextId) = 0; - - virtual Registry* getRegistry() = 0; -}; - -#endif // INTERFACEDRIVER_H diff --git a/doric-Qt/doric/main.cpp b/doric-Qt/doric/main.cpp index 6a67adf4..fa86e62f 100644 --- a/doric-Qt/doric/main.cpp +++ b/doric-Qt/doric/main.cpp @@ -2,26 +2,26 @@ #include #include -#include "demo/demo_bridge.h" +#include "demo/DoricDemoBridge.h" -int main(int argc, char *argv[]) -{ - QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); +int main(int argc, char *argv[]) { + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - QGuiApplication app(argc, argv); + QGuiApplication app(argc, argv); - QQmlApplicationEngine engine; - const QUrl url(QStringLiteral("qrc:/main.qml")); - QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, - &app, [url](QObject *obj, const QUrl &objUrl) { + 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); + QCoreApplication::exit(-1); + }, + Qt::QueuedConnection); - - DemoBridge *demoBridge = new DemoBridge(); - auto context = engine.rootContext(); - context->setContextProperty("demoBridge", demoBridge); - engine.load(url); - return app.exec(); + DoricDemoBridge *demoBridge = new DoricDemoBridge(); + auto context = engine.rootContext(); + context->setContextProperty("demoBridge", demoBridge); + engine.load(url); + return app.exec(); } diff --git a/doric-Qt/doric/native_driver.cpp b/doric-Qt/doric/native_driver.cpp deleted file mode 100644 index eb1a3407..00000000 --- a/doric-Qt/doric/native_driver.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include - -#include "native_driver.h" -#include "async/async_call.h" -#include "utils/constant.h" - -void NativeDriver::invokeContextEntityMethod(QString contextId, QString method, QVariantList args) -{ - args.insert(0, QVariant(contextId)); - args.insert(1, QVariant(method)); - invokeDoricMethod(Constant::DORIC_CONTEXT_INVOKE, args); -} - -void NativeDriver::invokeDoricMethod(QString method, QVariantList args) -{ - return AsyncCall::ensureRunInThreadPool(&jsEngine.mJSThreadPool, [this, method, args]{ - this->jsEngine.invokeDoricMethod(method, args).toString(); - }); -} - -void NativeDriver::createContext(QString contextId, QString script, QString source) -{ - AsyncCall::ensureRunInThreadPool(&jsEngine.mJSThreadPool, [this, contextId, script, source]{ - this->jsEngine.prepareContext(contextId, script, source); - }); -} - -void NativeDriver::destroyContext(QString contextId) -{ - -} - -Registry *NativeDriver::getRegistry() -{ - return this->jsEngine.getRegistry(); -} diff --git a/doric-Qt/doric/native_driver.h b/doric-Qt/doric/native_driver.h deleted file mode 100644 index aed5588f..00000000 --- a/doric-Qt/doric/native_driver.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef NATIVEDRIVER_H -#define NATIVEDRIVER_H - -#include - -#include "interface_driver.h" -#include "engine/js_engine.h" - -class NativeDriver : public InterfaceDriver -{ -private: - static NativeDriver *local_instance; - NativeDriver() - { - } - - ~NativeDriver() - { - } - - JSEngine jsEngine; - -public: - static NativeDriver *getInstance() - { - static NativeDriver instance; - return &instance; - } - - void invokeContextEntityMethod(QString contextId, QString method, QVariantList args) override; - - void invokeDoricMethod(QString method, QVariantList args) override; - - void createContext(QString contextId, QString script, QString source) override; - - void destroyContext(QString contextId) override; - - Registry * getRegistry() override; -}; -#endif // NATIVEDRIVER_H diff --git a/doric-Qt/doric/panel.cpp b/doric-Qt/doric/panel.cpp deleted file mode 100644 index 749c034e..00000000 --- a/doric-Qt/doric/panel.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "panel.h" - -Panel::Panel() -{ - -} - -void Panel::config(QString script, QString alias, QString extra) -{ - Context *context = Context::create(script, alias, extra); - config(context); -} - -void Panel::config(Context *context) -{ - this->mContext = context; - this->mContext->build(960, 720); -} diff --git a/doric-Qt/doric/panel.h b/doric-Qt/doric/panel.h deleted file mode 100644 index 363d44b7..00000000 --- a/doric-Qt/doric/panel.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef PANEL_H -#define PANEL_H - -#include "context.h" - -class Panel -{ -private: - Context *mContext; - int renderedWidth = -1; - int renderedHeight = -1; - -public: - Panel(); - - void config(QString script, QString alias, QString extra); - - void config(Context *context); -}; - -#endif // PANEL_H diff --git a/doric-Qt/doric/plugin/DoricShaderPlugin.cpp b/doric-Qt/doric/plugin/DoricShaderPlugin.cpp new file mode 100644 index 00000000..2cbcebb9 --- /dev/null +++ b/doric-Qt/doric/plugin/DoricShaderPlugin.cpp @@ -0,0 +1,9 @@ +#include "DoricShaderPlugin.h" + +#include + +DoricShaderPlugin::DoricShaderPlugin(QObject *parent) : QObject(parent) {} + +void DoricShaderPlugin::render(QJSValue jsValue, QString callbackId) { + qDebug() << ""; +} diff --git a/doric-Qt/doric/plugin/DoricShaderPlugin.h b/doric-Qt/doric/plugin/DoricShaderPlugin.h new file mode 100644 index 00000000..7021ca22 --- /dev/null +++ b/doric-Qt/doric/plugin/DoricShaderPlugin.h @@ -0,0 +1,15 @@ +#ifndef SHADERPLUGIN_H +#define SHADERPLUGIN_H + +#include +#include + +class DoricShaderPlugin : public QObject { + Q_OBJECT +public: + DoricShaderPlugin(QObject *parent); + + Q_INVOKABLE void render(QJSValue jsValue, QString callbackId); +}; + +#endif // SHADERPLUGIN_H diff --git a/doric-Qt/doric/plugin/shader_plugin.cpp b/doric-Qt/doric/plugin/shader_plugin.cpp deleted file mode 100644 index c9a0c3b1..00000000 --- a/doric-Qt/doric/plugin/shader_plugin.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "shader_plugin.h" - -#include - -ShaderPlugin::ShaderPlugin(QObject* parent) : QObject (parent) -{ - -} - -void ShaderPlugin::render(QJSValue jsValue, QString callbackId) -{ - qDebug() << ""; -} diff --git a/doric-Qt/doric/plugin/shader_plugin.h b/doric-Qt/doric/plugin/shader_plugin.h deleted file mode 100644 index 467e1c36..00000000 --- a/doric-Qt/doric/plugin/shader_plugin.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef SHADERPLUGIN_H -#define SHADERPLUGIN_H - -#include -#include - -class ShaderPlugin : public QObject -{ - Q_OBJECT -public: - ShaderPlugin(QObject* parent); - - Q_INVOKABLE void render(QJSValue jsValue, QString callbackId); -}; - -#endif // SHADERPLUGIN_H diff --git a/doric-Qt/doric/registry.cpp b/doric-Qt/doric/registry.cpp deleted file mode 100644 index a5022ecf..00000000 --- a/doric-Qt/doric/registry.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "registry.h" - -#include "plugin/shader_plugin.h" - -Registry::Registry() -{ - registerNativePlugin("shader"); -} - -bool Registry::acquirePluginInfo(QString name) -{ - return pluginInfoMap.acquireClass(name); -} diff --git a/doric-Qt/doric/registry.h b/doric-Qt/doric/registry.h deleted file mode 100644 index 20441e4d..00000000 --- a/doric-Qt/doric/registry.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef REGISTRY_H -#define REGISTRY_H - -#include - -#include "utils/object_factory.h" - -class Registry -{ -public: - ObjectFactory pluginInfoMap; - - Registry(); - - template - void registerNativePlugin(QString name) - { - pluginInfoMap.registerClass(name); - } - - bool acquirePluginInfo(QString name); -}; - -#endif // REGISTRY_H diff --git a/doric-Qt/doric/shader/DoricRootNode.cpp b/doric-Qt/doric/shader/DoricRootNode.cpp new file mode 100644 index 00000000..d030916f --- /dev/null +++ b/doric-Qt/doric/shader/DoricRootNode.cpp @@ -0,0 +1,3 @@ +#include "DoricRootNode.h" + +DoricRootNode::DoricRootNode() {} diff --git a/doric-Qt/doric/shader/root_node.h b/doric-Qt/doric/shader/DoricRootNode.h similarity index 63% rename from doric-Qt/doric/shader/root_node.h rename to doric-Qt/doric/shader/DoricRootNode.h index 11643b6d..a6b68138 100644 --- a/doric-Qt/doric/shader/root_node.h +++ b/doric-Qt/doric/shader/DoricRootNode.h @@ -1,10 +1,9 @@ #ifndef ROOTNODE_H #define ROOTNODE_H -class RootNode -{ +class DoricRootNode { public: - RootNode(); + DoricRootNode(); }; #endif // ROOTNODE_H diff --git a/doric-Qt/doric/shader/root_node.cpp b/doric-Qt/doric/shader/root_node.cpp deleted file mode 100644 index 66e79a9b..00000000 --- a/doric-Qt/doric/shader/root_node.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "root_node.h" - -RootNode::RootNode() -{ - -} diff --git a/doric-Qt/doric/template/DoricSingleton.h b/doric-Qt/doric/template/DoricSingleton.h new file mode 100644 index 00000000..4c4eea1d --- /dev/null +++ b/doric-Qt/doric/template/DoricSingleton.h @@ -0,0 +1,20 @@ +#ifndef SINGLETON_H +#define SINGLETON_H + +#include + +class DoricSingleton { +private: + static DoricSingleton *local_instance; + DoricSingleton() { qDebug() << "constructor"; } + + ~DoricSingleton() { qDebug() << "destructor"; } + +public: + static DoricSingleton *getInstance() { + static DoricSingleton locla_s; + return &locla_s; + } +}; + +#endif // SINGLETON_H diff --git a/doric-Qt/doric/template/singleton.h b/doric-Qt/doric/template/singleton.h deleted file mode 100644 index c2655879..00000000 --- a/doric-Qt/doric/template/singleton.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef SINGLETON_H -#define SINGLETON_H - -#include - -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 diff --git a/doric-Qt/doric/utils/DoricConstant.cpp b/doric-Qt/doric/utils/DoricConstant.cpp new file mode 100644 index 00000000..3128019f --- /dev/null +++ b/doric-Qt/doric/utils/DoricConstant.cpp @@ -0,0 +1,34 @@ +#include "DoricConstant.h" + +const QString DoricConstant::DORIC_BUNDLE_SANDBOX = "doric-sandbox.es5.js"; +const QString DoricConstant::DORIC_BUNDLE_LIB = "doric-lib.es5.js"; +const QString DoricConstant::DORIC_MODULE_LIB = "doric"; + +const QString DoricConstant::INJECT_ENVIRONMENT = "Environment"; +const QString DoricConstant::INJECT_LOG = "nativeLog"; +const QString DoricConstant::INJECT_EMPTY = "nativeEmpty"; +const QString DoricConstant::INJECT_REQUIRE = "nativeRequire"; +const QString DoricConstant::INJECT_TIMER_SET = "nativeSetTimer"; +const QString DoricConstant::INJECT_TIMER_CLEAR = "nativeClearTimer"; +const QString DoricConstant::INJECT_BRIDGE = "nativeBridge"; + +const QString DoricConstant::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 DoricConstant::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 DoricConstant::TEMPLATE_CONTEXT_DESTROY = + QString("doric.jsReleaseContext(\"%s\")"); + +const QString DoricConstant::GLOBAL_DORIC = "doric"; +const QString DoricConstant::DORIC_CONTEXT_INVOKE = "jsCallEntityMethod"; +const QString DoricConstant::DORIC_TIMER_CALLBACK = "jsCallbackTimer"; + +const QString DoricConstant::DORIC_ENTITY_INIT = "__init__"; +const QString DoricConstant::DORIC_ENTITY_CREATE = "__onCreate__"; +const QString DoricConstant::DORIC_ENTITY_BUILD = "__build__"; diff --git a/doric-Qt/doric/utils/DoricConstant.h b/doric-Qt/doric/utils/DoricConstant.h new file mode 100644 index 00000000..aa3fdba0 --- /dev/null +++ b/doric-Qt/doric/utils/DoricConstant.h @@ -0,0 +1,34 @@ +#ifndef CONSTANT_H +#define CONSTANT_H + +#include + +class DoricConstant { + +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 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_CREATE; + static const QString DORIC_ENTITY_INIT; + static const QString DORIC_ENTITY_BUILD; +}; + +#endif // CONSTANT_H diff --git a/doric-Qt/doric/utils/DoricCountDownLatch.h b/doric-Qt/doric/utils/DoricCountDownLatch.h new file mode 100644 index 00000000..cc947adb --- /dev/null +++ b/doric-Qt/doric/utils/DoricCountDownLatch.h @@ -0,0 +1,32 @@ +#ifndef COUNTDOWNLATCH_H +#define COUNTDOWNLATCH_H + +#include +#include + +class DoricCountDownLatch { + Q_DISABLE_COPY(DoricCountDownLatch) + QSemaphore m_sem{INT_MAX}; + +public: + DoricCountDownLatch() {} + ~DoricCountDownLatch() { + m_sem.acquire(INT_MAX); + m_sem.release(INT_MAX); + } + class Locker { + DoricCountDownLatch *sem; + + public: + Locker(const Locker &other) : sem{other.sem} { sem->m_sem.acquire(); } + Locker(Locker &&other) : sem{other.sem} { other.sem = nullptr; } + Locker(DoricCountDownLatch *sem) : sem{sem} { sem->m_sem.acquire(); } + ~Locker() { + if (sem) + sem->m_sem.release(); + } + }; + Locker lock() { return Locker{this}; } +}; + +#endif // COUNTDOWNLATCH_H diff --git a/doric-Qt/doric/utils/DoricObjectFactory.h b/doric-Qt/doric/utils/DoricObjectFactory.h new file mode 100644 index 00000000..8936fa32 --- /dev/null +++ b/doric-Qt/doric/utils/DoricObjectFactory.h @@ -0,0 +1,38 @@ +#include +#include +#include + +#ifndef OBJECTFACTORY_H +#define OBJECTFACTORY_H + +class DoricObjectFactory { +public: + template static void registerClass(QString name) { + constructors().insert(name, &constructorHelper); + } + + static bool acquireClass(QString name) { + return constructors().keys().contains(name); + } + + static QObject *createObject(const QString &name, QObject *parent = NULL) { + Constructor constructor = constructors().value(name); + if (constructor == NULL) + return NULL; + return (*constructor)(parent); + } + +private: + typedef QObject *(*Constructor)(QObject *parent); + + template static QObject *constructorHelper(QObject *parent) { + return new T(parent); + } + + static QHash &constructors() { + static QHash instance; + return instance; + } +}; + +#endif // OBJECTFACTORY_H diff --git a/doric-Qt/doric/utils/DoricUtils.h b/doric-Qt/doric/utils/DoricUtils.h new file mode 100644 index 00000000..dc593d29 --- /dev/null +++ b/doric-Qt/doric/utils/DoricUtils.h @@ -0,0 +1,25 @@ +#ifndef UTILS_H +#define UTILS_H + +#include +#include +#include +#include + +class DoricUtils { +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); + in.setCodec("UTF-8"); + QString content = in.readAll(); + file->close(); + delete file; + + return content; + } +}; + +#endif // UTILS_H diff --git a/doric-Qt/doric/utils/constant.cpp b/doric-Qt/doric/utils/constant.cpp deleted file mode 100644 index 3269d623..00000000 --- a/doric-Qt/doric/utils/constant.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "constant.h" - -const QString Constant::DORIC_BUNDLE_SANDBOX = "doric-sandbox.es5.js"; -const QString Constant::DORIC_BUNDLE_LIB = "doric-lib.es5.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_CONTEXT_INVOKE = "jsCallEntityMethod"; -const QString Constant::DORIC_TIMER_CALLBACK = "jsCallbackTimer"; - -const QString Constant::DORIC_ENTITY_INIT = "__init__"; -const QString Constant::DORIC_ENTITY_CREATE = "__onCreate__"; -const QString Constant::DORIC_ENTITY_BUILD = "__build__"; diff --git a/doric-Qt/doric/utils/constant.h b/doric-Qt/doric/utils/constant.h deleted file mode 100644 index af236335..00000000 --- a/doric-Qt/doric/utils/constant.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef CONSTANT_H -#define CONSTANT_H - -#include - -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 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_CREATE; - static const QString DORIC_ENTITY_INIT; - static const QString DORIC_ENTITY_BUILD; -}; - - -#endif // CONSTANT_H diff --git a/doric-Qt/doric/utils/count_down_latch.h b/doric-Qt/doric/utils/count_down_latch.h deleted file mode 100644 index 6618ed20..00000000 --- a/doric-Qt/doric/utils/count_down_latch.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef COUNTDOWNLATCH_H -#define COUNTDOWNLATCH_H - -#include -#include - -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 diff --git a/doric-Qt/doric/utils/object_factory.h b/doric-Qt/doric/utils/object_factory.h deleted file mode 100644 index bc3e622f..00000000 --- a/doric-Qt/doric/utils/object_factory.h +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#include - -#ifndef OBJECTFACTORY_H -#define OBJECTFACTORY_H - -class ObjectFactory -{ -public: - template - static void registerClass(QString name) - { - constructors().insert(name, &constructorHelper); - } - - static bool acquireClass(QString name) - { - return constructors().keys().contains(name); - } - - static QObject* createObject(const QString& name, QObject* parent = NULL) - { - Constructor constructor = constructors().value(name); - if ( constructor == NULL ) - return NULL; - return (*constructor)(parent); - } - -private: - typedef QObject* (*Constructor)(QObject* parent); - - template - static QObject* constructorHelper(QObject* parent) - { - return new T(parent); - } - - static QHash& constructors() - { - static QHash instance; - return instance; - } -}; - -#endif // OBJECTFACTORY_H diff --git a/doric-Qt/doric/utils/utils.h b/doric-Qt/doric/utils/utils.h deleted file mode 100644 index 2996a7aa..00000000 --- a/doric-Qt/doric/utils/utils.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef UTILS_H -#define UTILS_H - -#include -#include -#include -#include - -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); - in.setCodec("UTF-8"); - QString content = in.readAll(); - file->close(); - delete file; - - return content; - } -}; - -#endif // UTILS_H