History

Further work on evaluateInClosure

ksuprynowicz committed 3 years ago

Commit ed968e11 parent 70d4a435 files · +63 −10Browse files

Showing the changes to libraries/script-engine/src/v8/ScriptEngineV8_cast.cpp (1 of 5 files). Show all

Unified · Split

libraries/script-engine/src/v8/ScriptEngineV8_cast.cpp+40−0
@@ -383,6 +383,12 @@ bool ScriptEngineV8::castValueToVariant(const V8ScriptValue& v8Val, QVariant& de · 382 unmodified lines
383383 break;
384384 }
385385 }
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+ }
386392 // V8TODO
387393 errorMessage = QString() + "Conversion failure: " + QString(*v8::String::Utf8Value(_v8Isolate, val->ToDetailString(getConstContext()).ToLocalChecked()))
388394 + "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
487493 return true;
488494 }
489495 }
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?
490502 errorMessage = QString() + "Conversion to variant failed: " + QString(*v8::String::Utf8Value(_v8Isolate, val->ToDetailString(getConstContext()).ToLocalChecked()))
491503 + " Destination type: " + QMetaType::typeName(destTypeId) + " Value details: " + scriptValueDebugDetailsV8(v8Val);
492504 qDebug() << errorMessage;
@@ -532,6 +544,34 @@ bool ScriptEngineV8::castValueToVariant(const V8ScriptValue& v8Val, QVariant& de · 39 unmodified lines
532544 return destTypeId == QMetaType::UnknownType || dest.userType() == destTypeId || dest.convert(destTypeId);
533545 }
534546
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+
535575 QString ScriptEngineV8::valueType(const V8ScriptValue& v8Val) {
536576 // V8TODO
537577 v8::HandleScope handleScope(const_cast<v8::Isolate*>(v8Val.constGetIsolate()));