2021-02-04 16:59:58 +08:00
|
|
|
#include <QDebug>
|
2021-04-02 15:16:18 +08:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2021-02-08 14:36:05 +08:00
|
|
|
|
|
|
|
#include "../utils/DoricUtils.h"
|
|
|
|
#include "DoricNativeJSE.h"
|
2021-02-04 16:59:58 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
DoricNativeJSE::DoricNativeJSE(JSEType type) {
|
|
|
|
mType = type;
|
|
|
|
if (mType == JSEType::V8) {
|
|
|
|
v8Executor = new V8Executor();
|
|
|
|
} else if (mType == JSEType::Native) {
|
|
|
|
nativeExecutor = new NativeExecutor();
|
|
|
|
}
|
2021-02-04 16:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString DoricNativeJSE::loadJS(QString script, QString source) {
|
2021-04-02 15:16:18 +08:00
|
|
|
if (mType == JSEType::V8) {
|
|
|
|
return v8Executor->loadJS(script, source);
|
|
|
|
} else if (mType == JSEType::Native) {
|
|
|
|
return nativeExecutor->loadJS(script, source);
|
|
|
|
}
|
2021-02-04 16:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DoricNativeJSE::injectGlobalJSObject(QString name, QObject *object) {
|
2021-04-02 15:16:18 +08:00
|
|
|
if (mType == JSEType::V8) {
|
|
|
|
QJsonObject jsonObject;
|
|
|
|
|
|
|
|
QList<QByteArray> propertyNames = object->dynamicPropertyNames();
|
|
|
|
foreach (QByteArray propertyName, propertyNames) {
|
|
|
|
QString key = QString::fromStdString(propertyName.toStdString());
|
|
|
|
object->property(propertyName).toString();
|
|
|
|
if (key == "undefined" || key.isEmpty()) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
jsonObject[key] =
|
|
|
|
QJsonValue::fromVariant(object->property(propertyName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument doc(jsonObject);
|
|
|
|
QString strJson(doc.toJson(QJsonDocument::Compact));
|
|
|
|
|
|
|
|
v8Executor->injectGlobalJSObject(name, strJson.toUtf8().constData());
|
|
|
|
} else if (mType == JSEType::Native) {
|
|
|
|
nativeExecutor->injectGlobalJSObject(name, object);
|
|
|
|
}
|
2021-02-04 16:59:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DoricNativeJSE::injectGlobalJSFunction(QString name, QObject *function,
|
|
|
|
QString property) {
|
2021-04-02 15:16:18 +08:00
|
|
|
if (mType == JSEType::V8) {
|
|
|
|
v8Executor->injectGlobalJSFunction(name, function, property);
|
|
|
|
} else if (mType == JSEType::Native) {
|
|
|
|
nativeExecutor->injectGlobalJSFunction(name, function, property);
|
|
|
|
}
|
2021-02-04 16:59:58 +08:00
|
|
|
}
|
|
|
|
|
2021-04-02 17:23:36 +08:00
|
|
|
void DoricNativeJSE::invokeObject(QString objectName, QString functionName,
|
|
|
|
QVariantList arguments) {
|
2021-04-02 15:16:18 +08:00
|
|
|
if (mType == JSEType::V8) {
|
2021-04-02 17:23:36 +08:00
|
|
|
v8Executor->invokeObject(objectName, functionName, arguments);
|
2021-04-02 15:16:18 +08:00
|
|
|
} else if (mType == JSEType::Native) {
|
2021-04-02 17:23:36 +08:00
|
|
|
nativeExecutor->invokeObject(objectName, functionName, arguments);
|
2021-04-02 15:16:18 +08:00
|
|
|
}
|
2021-02-04 16:59:58 +08:00
|
|
|
}
|