handle js value convert & function invocation
This commit is contained in:
parent
75ebd5c0d6
commit
06f96f33fe
@ -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::TEMPLATE_CONTEXT_DESTROY = QString("doric.jsReleaseContext(\"%s\")");
|
||||||
|
|
||||||
const QString Constant::GLOBAL_DORIC = QString("doric");
|
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_TIMER_CALLBACK = QString("jsCallbackTimer");
|
||||||
|
|
||||||
const QString Constant::DORIC_ENTITY_INIT = QString("__init__");
|
const QString Constant::DORIC_ENTITY_INIT = QString("__init__");
|
||||||
|
const QString Constant::DORIC_ENTITY_SHOW = QString("__onShow__");
|
||||||
|
@ -18,9 +18,11 @@ public:
|
|||||||
static const QString TEMPLATE_CONTEXT_DESTROY;
|
static const QString TEMPLATE_CONTEXT_DESTROY;
|
||||||
|
|
||||||
static const QString GLOBAL_DORIC;
|
static const QString GLOBAL_DORIC;
|
||||||
|
static const QString DORIC_CONTEXT_INVOKE;
|
||||||
static const QString DORIC_TIMER_CALLBACK;
|
static const QString DORIC_TIMER_CALLBACK;
|
||||||
|
|
||||||
static const QString DORIC_ENTITY_INIT;
|
static const QString DORIC_ENTITY_INIT;
|
||||||
|
static const QString DORIC_ENTITY_SHOW;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONSTANT_H
|
#endif // CONSTANT_H
|
||||||
|
@ -24,16 +24,30 @@ public:
|
|||||||
this->source = source;
|
this->source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init(double width, double height) {
|
void show() {
|
||||||
QJsonObject* params = new QJsonObject();
|
QString* method = new QString(Constant::DORIC_ENTITY_SHOW);
|
||||||
params->insert("width", width);
|
QVector<QString*>* arguments = new QVector<QString*>();
|
||||||
params->insert("height", height);
|
|
||||||
QJsonDocument* jsonDocument = new QJsonDocument();
|
|
||||||
jsonDocument->setObject(*params);
|
|
||||||
QString strJson(jsonDocument->toJson(QJsonDocument::Compact));
|
|
||||||
|
|
||||||
delete params;
|
driver->invokeContextEntityMethod(contextId, method, nullptr);
|
||||||
delete jsonDocument;
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -42,4 +42,5 @@ HEADERS += \
|
|||||||
native/native_empty.h \
|
native/native_empty.h \
|
||||||
native/native_log.h \
|
native/native_log.h \
|
||||||
native/native_timer.h \
|
native/native_timer.h \
|
||||||
template/singleton.h
|
template/singleton.h \
|
||||||
|
utility/utility.h
|
||||||
|
@ -8,7 +8,9 @@ class Driver {
|
|||||||
public:
|
public:
|
||||||
virtual void createContext(int contextId, QString* script) = 0;
|
virtual void createContext(int contextId, QString* script) = 0;
|
||||||
virtual void destroyContext(int contextId) = 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;
|
virtual ~Driver() = default;
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
|
#include "stdarg.h"
|
||||||
|
|
||||||
#include "native_driver.h"
|
#include "native_driver.h"
|
||||||
|
#include "utility/utility.h"
|
||||||
|
|
||||||
NativeDriver::~NativeDriver() {
|
NativeDriver::~NativeDriver() {
|
||||||
qDebug() << "NativeDriver destructor";
|
qDebug() << "NativeDriver destructor";
|
||||||
@ -12,6 +15,34 @@ void NativeDriver::destroyContext(int contextId) {
|
|||||||
jsEngine->destroyContext(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, ...) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,10 +26,10 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void createContext(int contextId, QString *script) override;
|
void createContext(int contextId, QString *script) override;
|
||||||
|
|
||||||
void destroyContext(int contextId) 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
|
#endif // NATIVE_DRIVER_H
|
||||||
|
@ -31,6 +31,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
QString* source = new QString("Snake.js");
|
QString* source = new QString("Snake.js");
|
||||||
Context *context = ContextManager::getInstance()->createContext(&script, source);
|
Context *context = ContextManager::getInstance()->createContext(&script, source);
|
||||||
|
context->show();
|
||||||
context->init(180, 320);
|
context->init(180, 320);
|
||||||
delete source;
|
delete source;
|
||||||
}
|
}
|
||||||
|
48
doric/utility/utility.h
Normal file
48
doric/utility/utility.h
Normal 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
|
Reference in New Issue
Block a user