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/example/doric/engine/v8/JSValueHelper.cpp

101 lines
3.5 KiB
C++
Raw Permalink Normal View History

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();
2021-04-06 19:25:51 +08:00
2021-04-02 17:23:36 +08:00
v8::EscapableHandleScope handle_scope(isolate);
2021-04-06 16:14:43 +08:00
v8::Local<v8::Context> context = isolate->GetEnteredOrMicrotaskContext();
2021-04-02 17:23:36 +08:00
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) {
2021-04-06 16:14:43 +08:00
QStringList list = variant.toStringList();
v8::Handle<v8::Array> array = v8::Array::New(isolate, list.size());
for (int i = 0; i != list.size(); i++) {
v8::Local<v8::Value> value = NewV8String(list.at(i).toUtf8().constData());
auto result = array->Set(context, i, value);
result.ToChecked();
}
jsValue = array;
2021-06-02 11:26:11 +08:00
} else if (variant.type() == QVariant::Int ||
variant.type() == QVariant::Double) {
2021-04-06 19:25:51 +08:00
jsValue = v8::Number::New(isolate, variant.toDouble());
2021-05-27 14:00:19 +08:00
} else if (variant.type() == QVariant::Bool) {
jsValue = v8::Boolean::New(isolate, variant.toBool());
2021-05-06 10:01:56 +08:00
} else if (variant.type() == QVariant::Invalid) {
jsValue = v8::Undefined(isolate);
2021-04-02 17:23:36 +08:00
}
return handle_scope.Escape(jsValue);
}