2021-03-31 10:36:45 +08:00
|
|
|
#include "JSValueHelper.h"
|
|
|
|
|
2021-04-02 17:23:36 +08:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
std::string JS2String(v8::Local<v8::Value> object) {
|
2021-03-31 10:36:45 +08:00
|
|
|
v8::Isolate *isolate = v8::Isolate::GetCurrent();
|
|
|
|
v8::HandleScope handleScope(isolate);
|
|
|
|
v8::Local<v8::Context> context = isolate->GetEnteredOrMicrotaskContext();
|
|
|
|
if (object->IsString() || object->IsRegExp() || object->IsFunction()) {
|
|
|
|
v8::String::Utf8Value utf8(isolate, object);
|
|
|
|
return std::string(*utf8);
|
|
|
|
}
|
|
|
|
if (object->IsObject()) {
|
|
|
|
v8::MaybeLocal<v8::String> str = v8::JSON::Stringify(
|
|
|
|
context, object->ToObject(context).ToLocalChecked());
|
|
|
|
if (str.IsEmpty()) {
|
|
|
|
return "<string conversion failed>";
|
|
|
|
}
|
|
|
|
v8::Local<v8::String> s = str.ToLocalChecked();
|
|
|
|
v8::String::Utf8Value str2(isolate, s);
|
|
|
|
return std::string(*str2 ? *str2 : "<string conversion failed>");
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::String::Utf8Value str(
|
2021-04-02 15:16:18 +08:00
|
|
|
isolate, v8::JSON::Stringify(context, object).ToLocalChecked());
|
2021-03-31 10:36:45 +08:00
|
|
|
return std::string(*str ? *str : "<string conversion failed>");
|
|
|
|
}
|
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
double JS2Number(v8::Local<v8::Value> value) {
|
|
|
|
v8::HandleScope handleScope(v8::Isolate::GetCurrent());
|
|
|
|
v8::Local<v8::Context> context =
|
|
|
|
v8::Isolate::GetCurrent()->GetCurrentContext();
|
|
|
|
if (value->IsNumber()) {
|
|
|
|
return value->ToNumber(context).ToLocalChecked()->Value();
|
|
|
|
} else {
|
|
|
|
return 0;
|
2021-03-31 10:36:45 +08:00
|
|
|
}
|
2021-04-02 15:16:18 +08:00
|
|
|
}
|
2021-03-31 10:36:45 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
bool JS2Bool(v8::Local<v8::Value> value) {
|
|
|
|
v8::HandleScope handleScope(v8::Isolate::GetCurrent());
|
|
|
|
if (value->IsBoolean()) {
|
|
|
|
return value->ToBoolean(v8::Isolate::GetCurrent())->Value();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 10:36:45 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
v8::Local<v8::Value> String2JS(std::string string) {
|
2021-03-31 10:36:45 +08:00
|
|
|
v8::Isolate *isolate = v8::Isolate::GetCurrent();
|
|
|
|
v8::EscapableHandleScope handleScope(isolate);
|
|
|
|
v8::Local<v8::Context> context = isolate->GetEnteredOrMicrotaskContext();
|
2021-04-02 15:16:18 +08:00
|
|
|
v8::Local<v8::String> jsString = NewV8String(string.c_str());
|
2021-03-31 10:36:45 +08:00
|
|
|
|
2021-04-02 15:16:18 +08:00
|
|
|
v8::Local<v8::Value> ret =
|
|
|
|
v8::JSON::Parse(context, jsString).ToLocalChecked();
|
2021-03-31 10:36:45 +08:00
|
|
|
|
|
|
|
return handleScope.Escape(ret);
|
|
|
|
}
|
2021-04-02 17:23:36 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|