complete invoke method on v8

This commit is contained in:
王劲鹏
2021-04-02 17:23:36 +08:00
committed by osborn
parent dada2e4e0d
commit 7458d0f4c0
12 changed files with 125 additions and 19 deletions

View File

@@ -1,5 +1,8 @@
#include "JSValueHelper.h"
#include <QJsonDocument>
#include <QJsonObject>
std::string JS2String(v8::Local<v8::Value> object) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::HandleScope handleScope(isolate);
@@ -55,3 +58,27 @@ v8::Local<v8::Value> String2JS(std::string string) {
return handleScope.Escape(ret);
}
v8::Local<v8::Value> Variant2JS(QVariant variant) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::EscapableHandleScope handle_scope(isolate);
v8::Local<v8::Value> jsValue;
if (variant.type() == QVariant::String) {
jsValue = NewV8String(variant.toString().toUtf8().constData());
} else if (variant.type() == QVariant::Map) {
QMap<QString, QVariant> map = variant.toMap();
QJsonObject jsonObject;
foreach (QString key, map.keys()) {
QVariant value = map.value(key);
jsonObject.insert(key, QJsonValue::fromVariant(value));
}
QJsonDocument doc(jsonObject);
QString strJson(doc.toJson(QJsonDocument::Compact));
jsValue = String2JS(strJson.toUtf8().constData());
} else if (variant.type() == QVariant::StringList) {
qDebug() << "";
}
return handle_scope.Escape(jsValue);
}