This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-Qt/doric/engine/DoricNativeJSE.cpp

67 lines
2.1 KiB
C++
Raw Normal View History

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
}
QJSValue DoricNativeJSE::invokeObject(QString objectName, QString functionName,
QVariantList arguments) {
2021-04-02 15:16:18 +08:00
if (mType == JSEType::V8) {
2021-03-31 10:36:45 +08:00
return QJSValue::UndefinedValue;
2021-04-02 15:16:18 +08:00
} else if (mType == JSEType::Native) {
return nativeExecutor->invokeObject(objectName, functionName, arguments);
}
2021-02-04 16:59:58 +08:00
}