2021-02-04 16:59:58 +08:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QRect>
|
|
|
|
#include <QScreen>
|
|
|
|
#include <QSysInfo>
|
|
|
|
#include <QtConcurrent/QtConcurrent>
|
|
|
|
|
|
|
|
#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);
|
2021-04-15 10:47:49 +08:00
|
|
|
mJSThreadPool.setExpiryTimeout(-1);
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
{
|
|
|
|
auto result = QtConcurrent::run(
|
|
|
|
&mJSThreadPool, [this] { mJSE = new DoricNativeJSE(JSEType::V8); });
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto result = QtConcurrent::run(&mJSThreadPool, [this] {
|
|
|
|
// inject env
|
|
|
|
QScreen *screen = QGuiApplication::primaryScreen();
|
|
|
|
QRect screenGeometry = screen->geometry();
|
|
|
|
int screenWidth = screenGeometry.width();
|
|
|
|
int screenHeight = screenGeometry.height();
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
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());
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
mJSE->injectGlobalJSObject(DoricConstant::INJECT_ENVIRONMENT, envObject);
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
// inject log
|
|
|
|
DoricNativeLog *nativeLog = new DoricNativeLog();
|
|
|
|
mJSE->injectGlobalJSFunction(DoricConstant::INJECT_LOG, nativeLog,
|
|
|
|
"function");
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
// inject require
|
|
|
|
DoricNativeRequire *nativeRequire = new DoricNativeRequire();
|
|
|
|
mJSE->injectGlobalJSFunction(DoricConstant::INJECT_REQUIRE, nativeRequire,
|
|
|
|
"function");
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
// inject timer set & clear
|
|
|
|
DoricTimerExtension *timerExtension =
|
|
|
|
new DoricTimerExtension([this](long timerId) {
|
2021-04-06 19:25:51 +08:00
|
|
|
auto result = QtConcurrent::run(&mJSThreadPool, [this, timerId] {
|
|
|
|
QVariantList arguments;
|
|
|
|
arguments.push_back(QVariant((int)timerId));
|
|
|
|
this->invokeDoricMethod(DoricConstant::DORIC_TIMER_CALLBACK,
|
|
|
|
arguments);
|
|
|
|
});
|
2021-04-02 15:16:18 +08:00
|
|
|
});
|
|
|
|
mJSE->injectGlobalJSFunction(DoricConstant::INJECT_TIMER_SET,
|
|
|
|
timerExtension, "setTimer");
|
|
|
|
mJSE->injectGlobalJSFunction(DoricConstant::INJECT_TIMER_CLEAR,
|
|
|
|
timerExtension, "clearTimer");
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
DoricBridgeExtension *bridgeExtension = new DoricBridgeExtension();
|
|
|
|
mJSE->injectGlobalJSFunction(DoricConstant::INJECT_BRIDGE,
|
|
|
|
bridgeExtension, "callNative");
|
|
|
|
});
|
|
|
|
}
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
{
|
|
|
|
auto result = QtConcurrent::run(&mJSThreadPool, [this] {
|
|
|
|
loadBuiltinJS(DoricConstant::DORIC_BUNDLE_SANDBOX);
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2021-02-04 16:59:58 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 10:45:45 +08:00
|
|
|
QString DoricJSEngine::prepareContext(QString contextId, QString script,
|
|
|
|
QString source) {
|
|
|
|
return mJSE->loadJS(packageContextScript(contextId, script),
|
|
|
|
"Context://" + source);
|
2021-02-04 16:59:58 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 10:45:45 +08:00
|
|
|
QString DoricJSEngine::destroyContext(QString contextId) {
|
2021-04-09 16:39:43 +08:00
|
|
|
QString script =
|
|
|
|
QString(DoricConstant::TEMPLATE_CONTEXT_DESTROY).replace("%s", contextId);
|
2021-05-25 10:45:45 +08:00
|
|
|
return mJSE->loadJS(script, "_Context://" + contextId);
|
2021-04-09 16:39:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 10:45:45 +08:00
|
|
|
QString DoricJSEngine::invokeDoricMethod(QString method,
|
|
|
|
QVariantList arguments) {
|
2021-09-16 13:22:47 +08:00
|
|
|
QString ret = mJSE->invokeObject(DoricConstant::GLOBAL_DORIC, method, arguments);
|
|
|
|
|
|
|
|
if (method != DoricConstant::DORIC_CONTEXT_INVOKE_PURE) {
|
|
|
|
QVariantList newArguments;
|
|
|
|
newArguments.append(0);
|
|
|
|
newArguments.append(false);
|
|
|
|
mJSE->invokeObject(DoricConstant::GLOBAL_DORIC, DoricConstant::DORIC_HOOK_NATIVE_CALL, newArguments);
|
|
|
|
}
|
|
|
|
return ret;
|
2021-02-04 16:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DoricJSEngine::loadBuiltinJS(QString assetName) {
|
|
|
|
QString script = DoricUtils::readAssetFile("/doric", assetName);
|
|
|
|
QString result = mJSE->loadJS(script, "Assets://" + assetName);
|
2021-03-31 16:25:11 +08:00
|
|
|
qDebug() << result;
|
2021-02-04 16:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {}
|