Showing the changes to libraries/script-engine/src/v8/ScriptEngineV8_cast.cpp (1 of 5 files). Show all
Unified · Split
| @@ -383,6 +383,12 @@ bool ScriptEngineV8::castValueToVariant(const V8ScriptValue& v8Val, QVariant& de · 382 unmodified lines | ||
| 383 | 383 | break; |
| 384 | 384 | } |
| 385 | 385 | } |
| 386 | + // This is for generic JS objects | |
| 387 | + if (val->IsObject()) { | |
| 388 | + if (convertJSObjectToVariant(v8::Local<v8::Object>::Cast(val), dest)) { | |
| 389 | + break; | |
| 390 | + } | |
| 391 | + } | |
| 386 | 392 | // V8TODO |
| 387 | 393 | errorMessage = QString() + "Conversion failure: " + QString(*v8::String::Utf8Value(_v8Isolate, val->ToDetailString(getConstContext()).ToLocalChecked())) |
| 388 | 394 | + "to variant. Destination type: " + QMetaType::typeName(destTypeId) +" details: "+ scriptValueDebugDetailsV8(v8Val); |
| @@ -487,6 +493,12 @@ bool ScriptEngineV8::castValueToVariant(const V8ScriptValue& v8Val, QVariant& de · 98 unmodified lines | ||
| 487 | 493 | return true; |
| 488 | 494 | } |
| 489 | 495 | } |
| 496 | + if (val->IsObject()) { | |
| 497 | + if (convertJSObjectToVariant(v8::Local<v8::Object>::Cast(val), dest)) { | |
| 498 | + return true; | |
| 499 | + } | |
| 500 | + } | |
| 501 | + //V8TODO is v8::Array to QVariant conversion used anywhere? | |
| 490 | 502 | errorMessage = QString() + "Conversion to variant failed: " + QString(*v8::String::Utf8Value(_v8Isolate, val->ToDetailString(getConstContext()).ToLocalChecked())) |
| 491 | 503 | + " Destination type: " + QMetaType::typeName(destTypeId) + " Value details: " + scriptValueDebugDetailsV8(v8Val); |
| 492 | 504 | qDebug() << errorMessage; |
| @@ -532,6 +544,34 @@ bool ScriptEngineV8::castValueToVariant(const V8ScriptValue& v8Val, QVariant& de · 39 unmodified lines | ||
| 532 | 544 | return destTypeId == QMetaType::UnknownType || dest.userType() == destTypeId || dest.convert(destTypeId); |
| 533 | 545 | } |
| 534 | 546 | |
| 547 | +bool ScriptEngineV8::convertJSObjectToVariant(v8::Local<v8::Object> object, QVariant &dest) { | |
| 548 | + auto context = getContext(); | |
| 549 | + v8::Local<v8::Array> names; | |
| 550 | + if(object->GetPropertyNames(context).ToLocal(&names)) { | |
| 551 | + qDebug() << "ScriptEngineV8::convertJSObjectToVariant could not get property names"; | |
| 552 | + return false; | |
| 553 | + } | |
| 554 | + int length = names->Length(); | |
| 555 | + QHash<QString, QVariant> properties; | |
| 556 | + for (int i = 0; i < length; i++) { | |
| 557 | + v8::Local<v8::Value> v8Property; | |
| 558 | + QString name = *v8::String::Utf8Value(_v8Isolate, names->Get(context, i).ToLocalChecked()); | |
| 559 | + if (!object->Get(context, names->Get(context, i).ToLocalChecked()).ToLocal(&v8Property)) { | |
| 560 | + qDebug() << "ScriptEngineV8::convertJSObjectToVariant could not get property: " + name; | |
| 561 | + continue; | |
| 562 | + } | |
| 563 | + QVariant property; | |
| 564 | + // Maybe QMetaType::QVariant? | |
| 565 | + if (castValueToVariant(V8ScriptValue(_v8Isolate, v8Property), property, QMetaType::UnknownType)) { | |
| 566 | + properties.insert( name, property); | |
| 567 | + } else { | |
| 568 | + qDebug() << "ScriptEngineV8::convertJSObjectToVariant could cast property to variant: " + name; | |
| 569 | + ; | |
| 570 | + } | |
| 571 | + } | |
| 572 | + dest = QVariant(properties); | |
| 573 | +} | |
| 574 | + | |
| 535 | 575 | QString ScriptEngineV8::valueType(const V8ScriptValue& v8Val) { |
| 536 | 576 | // V8TODO |
| 537 | 577 | v8::HandleScope handleScope(const_cast<v8::Isolate*>(v8Val.constGetIsolate())); |