handle js value convert & function invocation

This commit is contained in:
王劲鹏 2019-12-06 19:47:59 +08:00
parent 75ebd5c0d6
commit 06f96f33fe
9 changed files with 115 additions and 14 deletions

View File

@ -29,6 +29,8 @@ const QString Constant::TEMPLATE_MODULE = QString("Reflect.apply(doric.jsRegiste
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__");

View File

@ -18,9 +18,11 @@ public:
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

View File

@ -24,16 +24,30 @@ public:
this->source = source;
}
void init(double width, double height) {
QJsonObject* params = new QJsonObject();
params->insert("width", width);
params->insert("height", height);
QJsonDocument* jsonDocument = new QJsonDocument();
jsonDocument->setObject(*params);
QString strJson(jsonDocument->toJson(QJsonDocument::Compact));
void show() {
QString* method = new QString(Constant::DORIC_ENTITY_SHOW);
QVector<QString*>* arguments = new QVector<QString*>();
delete params;
delete jsonDocument;
driver->invokeContextEntityMethod(contextId, method, nullptr);
delete arguments;
delete method;
}
void 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;
}
};

View File

@ -42,4 +42,5 @@ HEADERS += \
native/native_empty.h \
native/native_log.h \
native/native_timer.h \
template/singleton.h
template/singleton.h \
utility/utility.h

View File

@ -8,7 +8,9 @@ class Driver {
public:
virtual void createContext(int contextId, QString* script) = 0;
virtual void destroyContext(int contextId) = 0;
virtual void invokeContextEntityMethod(int contextId, QString* method, QVector<QObject>* arguments) = 0;
virtual void invokeContextEntityMethod(int contextId, QString* method, ...) = 0;
virtual void invokeDoricMethod(QString* method, ...) = 0;
virtual ~Driver() = default;
};

View File

@ -1,4 +1,7 @@
#include "stdarg.h"
#include "native_driver.h"
#include "utility/utility.h"
NativeDriver::~NativeDriver() {
qDebug() << "NativeDriver destructor";
@ -12,6 +15,34 @@ void NativeDriver::destroyContext(int contextId) {
jsEngine->destroyContext(contextId);
}
void NativeDriver::invokeContextEntityMethod(int contextId, QString* method, QVector<QObject>* arguments) {
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() << result.toString();
delete arguments;
}
void NativeDriver::invokeDoricMethod(QString *method, ...) {
}

View File

@ -26,10 +26,10 @@ public:
}
void createContext(int contextId, QString *script) override;
void destroyContext(int contextId) override;
void invokeContextEntityMethod(int contextId, QString* method, QVector<QObject>* arguments) override;
void invokeContextEntityMethod(int contextId, QString* method, ...) override;
void invokeDoricMethod(QString* method, ...) override;
};
#endif // NATIVE_DRIVER_H

View File

@ -31,6 +31,7 @@ int main(int argc, char *argv[])
QString* source = new QString("Snake.js");
Context *context = ContextManager::getInstance()->createContext(&script, source);
context->show();
context->init(180, 320);
delete source;
}

48
doric/utility/utility.h Normal file
View 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