aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlglobal.cpp
blob: 1273067187e3e328dae44961ae1bcc41f8b28a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QtQml/private/qjsvalue_p.h>
#include <QtQml/private/qqmlglobal_p.h>
#include <QtQml/private/qqmlmetatype_p.h>
#include <QtQml/qqmlengine.h>

#include <QtCore/private/qvariant_p.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qdebug.h>
#include <QtCore/qstringlist.h>

QT_BEGIN_NAMESPACE

// Pre-filter the metatype before poking QQmlMetaType::qmlType() and locking its mutex.
static bool isConstructibleMetaType(const QMetaType metaType)
{
    switch (metaType.id()) {
        // The builtins are not constructible this way.
        case QMetaType::Void:
        case QMetaType::Nullptr:
        case QMetaType::QVariant:
        case QMetaType::Int:
        case QMetaType::UInt:
        case QMetaType::LongLong:
        case QMetaType::ULongLong:
        case QMetaType::Float:
        case QMetaType::Double:
        case QMetaType::Long:
        case QMetaType::ULong:
        case QMetaType::Short:
        case QMetaType::UShort:
        case QMetaType::Char:
        case QMetaType::SChar:
        case QMetaType::UChar:
        case QMetaType::QChar:
        case QMetaType::QString:
        case QMetaType::Bool:
        case QMetaType::QDateTime:
        case QMetaType::QDate:
        case QMetaType::QTime:
        case QMetaType::QUrl:
        case QMetaType::QRegularExpression:
        case QMetaType::QByteArray:
        case QMetaType::QLocale:
        return false;
    default:
        break;
    }

    // QJSValue is also builtin
    if (metaType == QMetaType::fromType<QJSValue>())
        return false;

    // We also don't want to construct pointers of any kind, or lists, or enums.
    if (metaType.flags() &
            (QMetaType::PointerToQObject
             | QMetaType::IsEnumeration
             | QMetaType::SharedPointerToQObject
             | QMetaType::WeakPointerToQObject
             | QMetaType::TrackingPointerToQObject
             | QMetaType::IsUnsignedEnumeration
             | QMetaType::PointerToGadget
             | QMetaType::IsPointer
             | QMetaType::IsQmlList)) {
        return false;
    }

    return true;
}

static void *createVariantData(QMetaType type, QVariant *variant)
{
    const QtPrivate::QMetaTypeInterface *iface = type.iface();
    QVariant::Private *d = &variant->data_ptr();
    Q_ASSERT(d->is_null && !d->is_shared);
    *d = QVariant::Private(iface);
    if (QVariant::Private::canUseInternalSpace(iface))
        return d->data.data;

    // This is not exception safe.
    // If your value type throws an exception from its ctor bad things will happen anyway.
    d->data.shared = QVariant::PrivateShared::create(iface->size, iface->alignment);
    d->is_shared = true;
    return d->data.shared->data();
}

static void callConstructor(
        const QMetaObject *targetMetaObject, int i, void *source, void *target)
{
    void *p[] = { target, source };
    targetMetaObject->static_metacall(QMetaObject::ConstructInPlace, i, p);
}

template<typename Allocate>
static void fromVerifiedType(
        const QMetaObject *targetMetaObject, int ctorIndex, void *source, Allocate &&allocate)
{
    const QMetaMethod ctor = targetMetaObject->constructor(ctorIndex);
    Q_ASSERT_X(ctor.parameterCount() == 1, "fromVerifiedType",
               "Value type constructor must take exactly one argument");
    callConstructor(targetMetaObject, ctorIndex, source, allocate());
}


template<typename Allocate, typename Retrieve>
static bool fromMatchingType(
        const QMetaObject *targetMetaObject, Allocate &&allocate, Retrieve &&retrieve)
{
    for (int i = 0, end = targetMetaObject->constructorCount(); i < end; ++i) {
        const QMetaMethod ctor = targetMetaObject->constructor(i);
        if (ctor.parameterCount() != 1)
            continue;

        const QMetaType parameterType = ctor.parameterMetaType(0);

        if (retrieve(parameterType, [&](QMetaType sourceMetaType, void *sourceData) {
            if (sourceMetaType == parameterType) {
                callConstructor(targetMetaObject, i, sourceData, allocate());
                return true;
            }

            if (const QMetaObject *parameterMetaObject = parameterType.metaObject()) {
                if (const QMetaObject *sourceMetaObject = sourceMetaType.metaObject();
                        sourceMetaObject && sourceMetaObject->inherits(parameterMetaObject)) {
                    // Allow construction from derived types.
                    callConstructor(targetMetaObject, i, sourceData, allocate());
                    return true;
                }
            }

            // Do not recursively try to create parameters here. This may end up in infinite recursion.

            // At this point, s should be a builtin type. For builtin types
            // the QMetaType converters are good enough.
            QVariant converted(parameterType);
            if (QMetaType::convert(sourceMetaType, sourceData, parameterType, converted.data())) {
                callConstructor(targetMetaObject, i, converted.data(), allocate());
                return true;
            }

            return false;
        })) {
            return true;
        }
    }

    return false;
}

template<typename Allocate>
static bool fromMatchingType(
    const QMetaObject *targetMetaObject, const QV4::Value &source, Allocate &&allocate)
{
    return fromMatchingType(
            targetMetaObject, std::forward<Allocate>(allocate),
            [&](QMetaType parameterType, auto callback) {
        QVariant variant = QV4::ExecutionEngine::toVariant(source, parameterType);
        return callback(variant.metaType(), variant.data());
    });
}

template<typename Allocate>
static bool fromMatchingType(
    const QMetaObject *targetMetaObject, QVariant source, Allocate &&allocate)
{
    return fromMatchingType(targetMetaObject, std::forward<Allocate>(allocate),
                            [&](QMetaType, auto callback) {
        return callback(source.metaType(), source.data());
    });
}

template<typename Allocate>
static bool fromString(const QMetaObject *mo, QString s, Allocate &&allocate)
{
    for (int i = 0, end = mo->constructorCount(); i < end; ++i) {
        const QMetaMethod ctor = mo->constructor(i);
        if (ctor.parameterCount() != 1)
            continue;

        if (ctor.parameterMetaType(0) == QMetaType::fromType<QString>()) {
            callConstructor(mo, i, &s, allocate());
            return true;
        }
    }

    return false;
}

template<typename Get, typename Convert>
static bool doWriteProperty(const QMetaProperty &metaProperty, void *target,
                            Get &&get, Convert &&convert)
{
    const QMetaType propertyType = metaProperty.metaType();
    QVariant property = get(propertyType);
    if (property.metaType() == propertyType) {
        metaProperty.writeOnGadget(target, std::move(property));
        return true;
    }

    QVariant converted = convert(propertyType);
    if (converted.isValid()) {
        metaProperty.writeOnGadget(target, std::move(converted));
        return true;
    }

    converted = QVariant(propertyType);
    if (QMetaType::convert(property.metaType(), property.constData(),
                           propertyType, converted.data())) {
        metaProperty.writeOnGadget(target, std::move(converted));
        return true;
    }

    return false;
}

static void doWriteProperties(
    const QMetaObject *targetMetaObject, void *target, const QV4::Value &source)
{
    const QV4::Object *o = static_cast<const QV4::Object *>(&source);
    QV4::Scope scope(o->engine());
    QV4::ScopedObject object(scope, o);

    for (int i = 0; i < targetMetaObject->propertyCount(); ++i) {
        const QMetaProperty metaProperty = targetMetaObject->property(i);
        const QString propertyName = QString::fromUtf8(metaProperty.name());

        QV4::ScopedString v4PropName(scope, scope.engine->newString(propertyName));
        QV4::ScopedValue v4PropValue(scope, object->get(v4PropName));

        // We assume that data is freshly constructed.
        // There is no point in reset()'ing properties of a freshly created object.
        if (v4PropValue->isUndefined())
            continue;

        if (doWriteProperty(metaProperty, target, [&](const QMetaType &propertyType) {
            return QV4::ExecutionEngine::toVariant(v4PropValue, propertyType);
        }, [&](const QMetaType &propertyType) {
            return QQmlValueTypeProvider::createValueType(v4PropValue, propertyType);
        })) {
            continue;
        }

        const QMetaType propertyType = metaProperty.metaType();
        QVariant property = QV4::ExecutionEngine::toVariant(v4PropValue, propertyType);
        if (property.metaType() == propertyType) {
            metaProperty.writeOnGadget(target, std::move(property));
            continue;
        }

        QVariant converted = QQmlValueTypeProvider::createValueType(v4PropValue, propertyType);
        if (converted.isValid()) {
            metaProperty.writeOnGadget(target, std::move(converted));
            continue;
        }

        converted = QVariant(propertyType);
        if (QMetaType::convert(property.metaType(), property.constData(),
                               propertyType, converted.data())) {
            metaProperty.writeOnGadget(target, std::move(converted));
            continue;
        }

        qWarning().noquote()
                << QLatin1String("Could not convert %1 to %2 for property %3")
                   .arg(v4PropValue->toQStringNoThrow(), QString::fromUtf8(propertyType.name()),
                        propertyName);
    }
}

static QVariant byProperties(
    const QMetaObject *targetMetaObject, QMetaType metaType, const QV4::Value &source)
{
    if (!source.isObject() || !targetMetaObject)
        return QVariant();

    QVariant result(metaType);
    doWriteProperties(targetMetaObject, result.data(), source);
    return result;
}

template<typename Read>
static void doWriteProperties(
    const QMetaObject *targetMetaObject, void *target,
    const QMetaObject *sourceMetaObject, Read &&read)
{
    for (int i = 0; i < targetMetaObject->propertyCount(); ++i) {
        const QMetaProperty metaProperty = targetMetaObject->property(i);

        const int sourceProperty = sourceMetaObject->indexOfProperty(metaProperty.name());

        // We assume that data is freshly constructed.
        // There is no point in reset()'ing properties of a freshly created object.
        if (sourceProperty == -1)
            continue;

        const QMetaType propertyType = metaProperty.metaType();
        QVariant property = read(sourceMetaObject, sourceProperty);
        if (property.metaType() == propertyType) {
            metaProperty.writeOnGadget(target, std::move(property));
            continue;
        }

        QVariant converted = QQmlValueTypeProvider::createValueType(property, propertyType);
        if (converted.isValid()) {
            metaProperty.writeOnGadget(target, std::move(converted));
            continue;
        }

        converted = QVariant(propertyType);
        if (QMetaType::convert(property.metaType(), property.constData(),
                               propertyType, converted.data())) {
            metaProperty.writeOnGadget(target, std::move(converted));
            continue;
        }

        qWarning().noquote()
            << QLatin1String("Could not convert %1 to %2 for property %3")
                   .arg(property.toString(), QString::fromUtf8(propertyType.name()),
                        QString::fromUtf8(metaProperty.name()));
    }
}


static void doWriteProperties(const QMetaObject *targetMeta, void *target, QObject *source)
{
    doWriteProperties(
            targetMeta, target, source->metaObject(),
            [source](const QMetaObject *sourceMetaObject, int sourceProperty) {
        return sourceMetaObject->property(sourceProperty).read(source);
    });
}

static QVariant byProperties(
    const QMetaObject *targetMetaObject, QMetaType targetMetaType, QObject *source)
{
    if (!source || !targetMetaObject)
        return QVariant();

    QVariant result(targetMetaType);
    doWriteProperties(targetMetaObject, result.data(), source);
    return result;
}

static QVariant byProperties(
    const QMetaObject *targetMetaObject, QMetaType targetMetaType,
    const QMetaObject *sourceMetaObject, const void *source)
{
    if (!source || !sourceMetaObject || !targetMetaObject)
        return QVariant();

    QVariant result(targetMetaType);
    doWriteProperties(
            targetMetaObject, result.data(), sourceMetaObject,
            [source](const QMetaObject *sourceMetaObject, int sourceProperty) {
        return sourceMetaObject->property(sourceProperty).readOnGadget(source);
    });
    return result;
}

template<typename Map>
void doWriteProperties(const QMetaObject *targetMetaObject, void *target, const Map &source)
{
    for (int i = 0; i < targetMetaObject->propertyCount(); ++i) {
        const QMetaProperty metaProperty = targetMetaObject->property(i);

        // We assume that data is freshly constructed.
        // There is no point in reset()'ing properties of a freshly created object.
        const auto it = source.constFind(QString::fromUtf8(metaProperty.name()));
        if (it == source.constEnd())
            continue;

        const QMetaType propertyType = metaProperty.metaType();
        QVariant property = *it;
        if (property.metaType() == propertyType) {
            metaProperty.writeOnGadget(target, std::move(property));
            continue;
        }

        QVariant converted = QQmlValueTypeProvider::createValueType(property, propertyType);
        if (converted.isValid()) {
            metaProperty.writeOnGadget(target, std::move(converted));
            continue;
        }

        converted = QVariant(propertyType);
        if (QMetaType::convert(property.metaType(), property.constData(),
                               propertyType, converted.data())) {
            metaProperty.writeOnGadget(target, std::move(converted));
            continue;
        }

        qWarning().noquote()
            << QLatin1String("Could not convert %1 to %2 for property %3")
                   .arg(property.toString(), QString::fromUtf8(propertyType.name()),
                        QString::fromUtf8(metaProperty.name()));
    }
}

template<typename Map>
QVariant byProperties(
    const QMetaObject *targetMetaObject, QMetaType targetMetaType, const Map &source)
{
    QVariant result(targetMetaType);
    doWriteProperties(targetMetaObject, result.data(), source);
    return result;
}

static QVariant byProperties(
    const QMetaObject *targetMetaObject, QMetaType targetMetaType, const QVariant &source)
{
    if (!targetMetaObject)
        return QVariant();

    if (source.metaType() == QMetaType::fromType<QJSValue>()) {
        QJSValue val = source.value<QJSValue>();
        return byProperties(
            targetMetaObject, targetMetaType, QV4::Value(QJSValuePrivate::asReturnedValue(&val)));
    }

    if (source.metaType() == QMetaType::fromType<QVariantMap>()) {
        return byProperties(
            targetMetaObject, targetMetaType,
            *static_cast<const QVariantMap *>(source.constData()));
    }

    if (source.metaType() == QMetaType::fromType<QVariantHash>()) {
        return byProperties(
            targetMetaObject, targetMetaType,
            *static_cast<const QVariantHash *>(source.constData()));
    }

    if (source.metaType().flags() & QMetaType::PointerToQObject)
        return byProperties(targetMetaObject, targetMetaType, source.value<QObject *>());

    if (const QMetaObject *sourceMeta = QQmlMetaType::metaObjectForValueType(source.metaType()))
        return byProperties(targetMetaObject, targetMetaType, sourceMeta, source.constData());

    return QVariant();
}

template<typename Allocate, typename DefaultConstruct>
bool createOrConstructValueType(
        const QQmlType &targetType, const QV4::Value &source,
        Allocate &&allocate, DefaultConstruct &&defaultConstruct)
{
    const auto warn = [&](const QMetaObject *targetMetaObject) {
        qWarning().noquote()
                << "Could not find any constructor for value type"
                << targetMetaObject->className() << "to call with value"
                << source.toQStringNoThrow();
    };

    if (targetType.canPopulateValueType()) {
        if (const QMetaObject *targetMetaObject = targetType.metaObjectForValueType()) {
            if (source.isObject()) {
                doWriteProperties(targetMetaObject, defaultConstruct(), source);
                return true;
            }
            if (targetType.canConstructValueType()) {
                if (fromMatchingType(targetMetaObject, source, allocate))
                    return true;
                warn(targetMetaObject);
            }
        }
    } else if (targetType.canConstructValueType()) {
        if (const QMetaObject *targetMetaObject = targetType.metaObjectForValueType()) {
            if (fromMatchingType(targetMetaObject, source, allocate))
                return true;
            warn(targetMetaObject);
        }
    }

    if (const auto valueTypeFunction = targetType.createValueTypeFunction()) {
        const QVariant result
            = valueTypeFunction(QJSValuePrivate::fromReturnedValue(source.asReturnedValue()));
        const QMetaType resultType = result.metaType();
        if (resultType == targetType.typeId()) {
            resultType.construct(allocate(), result.constData());
            return true;
        }
    }

    return false;
}

template<typename Allocate, typename DefaultConstruct>
bool createOrConstructValueType(
        const QQmlType &targetType, QMetaType sourceMetaType, void *source,
        Allocate &&allocate, DefaultConstruct &&defaultConstruct)
{

    const auto warn = [&](const QMetaObject *targetMetaObject) {
        qWarning().noquote()
            << "Could not find any constructor for value type"
            << targetMetaObject->className() << "to call with value" << source;
    };

    if (targetType.canPopulateValueType()) {
        if (const QMetaObject *targetMetaObject = targetType.metaObjectForValueType()) {
            if (const QMetaObject *sourceMetaObject
                    = QQmlMetaType::metaObjectForValueType(sourceMetaType)) {
                doWriteProperties(
                    targetMetaObject, defaultConstruct(), sourceMetaObject,
                    [&source](const QMetaObject *sourceMetaObject, int sourceProperty) {
                        return sourceMetaObject->property(sourceProperty).readOnGadget(source);
                    });
                return true;
            }

            if (sourceMetaType == QMetaType::fromType<QVariantMap>()) {
                doWriteProperties(
                        targetMetaObject, defaultConstruct(),
                        *static_cast<const QVariantMap *>(source));
                return true;
            }

            if (sourceMetaType == QMetaType::fromType<QVariantHash>()) {
                doWriteProperties(
                        targetMetaObject, defaultConstruct(),
                        *static_cast<const QVariantHash *>(source));
                return true;
            }

            if (sourceMetaType.flags() & QMetaType::PointerToQObject) {
                doWriteProperties(
                        targetMetaObject, defaultConstruct(),
                        *static_cast<QObject *const *>(source));
                return true;
            }
        }
    }

    if (targetType.canConstructValueType()) {
        if (const QMetaObject *targetMetaObject = targetType.metaObjectForValueType()) {
            if (fromMatchingType(targetMetaObject, std::forward<Allocate>(allocate),
                                 [&](QMetaType, auto callback) {
                    return callback(sourceMetaType, source);
                })) {
                return true;
            }
            warn(targetMetaObject);
        }
    }

    return false;
}

/*!
 * \internal
 * Populate the value type in place at \a target, which is expected to be
 * allocated and default-constructed, for example the result of a QVariant(QMetaType).
 * This is efficient if we can do byProperties() since it can use the pre-constructed object.
 * It also avoids the creation of a QVariant in most cases. It is not
 * efficient if you're going to create a QVariant anyway.
 */
bool QQmlValueTypeProvider::populateValueType(
    QMetaType targetMetaType, void *target, QMetaType sourceMetaType, void *source)
{
    if (sourceMetaType == QMetaType::fromType<QJSValue>()) {
        const QJSValue *val = static_cast<const QJSValue *>(source);
        return populateValueType(
            targetMetaType, target, QV4::Value(QJSValuePrivate::asReturnedValue(val)));
    }

    if (!isConstructibleMetaType(targetMetaType))
        return false;

    return createOrConstructValueType(
            QQmlMetaType::qmlType(targetMetaType), sourceMetaType, source,
            [targetMetaType, target]() {
                targetMetaType.destruct(target);
                return target;
            }, [target]() {
                return target;
            });
}

/*!
 * \internal
 * Populate the value type in place at \a target, which is expected to be
 * allocated and default-constructed, for example the result of a QVariant(QMetaType).
 * This is efficient if we can do byProperties() since it can use the pre-constructed object.
 * It also avoids the creation of a QVariant in most cases. It is not
 * efficient if you're going to create a QVariant anyway.
 */
bool QQmlValueTypeProvider::populateValueType(
        QMetaType targetMetaType, void *target, const QV4::Value &source)
{
    if (!isConstructibleMetaType(targetMetaType))
        return false;

    return createOrConstructValueType(
        QQmlMetaType::qmlType(targetMetaType), source, [targetMetaType, target]() {
            targetMetaType.destruct(target);
            return target;
        }, [target]() {
            return target;
        });
}

/*!
 * \internal
 * Specialization that constructs the value type on the heap using new and returns a pointer to it.
 */
void *QQmlValueTypeProvider::heapCreateValueType(
        const QQmlType &targetType, const QV4::Value &source)
{
    void *target = nullptr;
    if (createOrConstructValueType(
        targetType, source, [&]() {
            const QMetaType metaType = targetType.typeId();
            const ushort align = metaType.alignOf();
            target = align > __STDCPP_DEFAULT_NEW_ALIGNMENT__
                     ? operator new(metaType.sizeOf(), std::align_val_t(align))
                     : operator new(metaType.sizeOf());
            return target;
        }, [&]() {
            target = targetType.typeId().create();
            return target;
        })) {
        Q_ASSERT(target != nullptr);
    }

    return target;
}

QVariant QQmlValueTypeProvider::constructValueType(
        QMetaType targetMetaType, const QMetaObject *targetMetaObject,
        int ctorIndex, void *ctorArg)
{
    QVariant result;
    fromVerifiedType(targetMetaObject, ctorIndex, ctorArg,
                     [&]() { return createVariantData(targetMetaType, &result); });
    return result;
}

static QVariant fromJSValue(const QQmlType &type, const QJSValue &s, QMetaType metaType)
{
    if (const auto valueTypeFunction = type.createValueTypeFunction()) {
        const QVariant result = valueTypeFunction(s);
        if (result.metaType() == metaType)
            return result;
    }

    return QVariant();
}

QVariant QQmlValueTypeProvider::createValueType(const QJSValue &s, QMetaType metaType)
{
    if (!isConstructibleMetaType(metaType))
        return QVariant();
    return fromJSValue(QQmlMetaType::qmlType(metaType), s, metaType);
}

QVariant QQmlValueTypeProvider::createValueType(const QString &s, QMetaType metaType)
{
    if (!isConstructibleMetaType(metaType))
        return QVariant();
    const QQmlType type = QQmlMetaType::qmlType(metaType);
    if (type.canConstructValueType()) {
        if (const QMetaObject *mo = type.metaObjectForValueType()) {
            QVariant result;
            if (fromString(mo, s, [&]() { return createVariantData(metaType, &result); }))
                return result;
        }
    }

    return fromJSValue(type, s, metaType);
}

QVariant QQmlValueTypeProvider::createValueType(const QV4::Value &s, QMetaType metaType)
{
    if (!isConstructibleMetaType(metaType))
        return QVariant();
    const QQmlType type = QQmlMetaType::qmlType(metaType);
    const auto warn = [&](const QMetaObject *mo) {
        qWarning().noquote()
                << "Could not find any constructor for value type"
                << mo->className() << "to call with value" << s.toQStringNoThrow();
    };

    if (type.canPopulateValueType()) {
        if (const QMetaObject *mo = type.metaObject()) {
            QVariant result = byProperties(mo, metaType, s);
            if (result.isValid())
                return result;
            if (type.canConstructValueType()) {
                if (fromMatchingType(mo, s, [&]() { return createVariantData(metaType, &result); }))
                    return result;
                warn(mo);
            }
        }
    } else if (type.canConstructValueType()) {
        if (const QMetaObject *mo = type.metaObject()) {
            QVariant result;
            if (fromMatchingType(mo, s, [&]() { return createVariantData(metaType, &result); }))
                return result;
            warn(mo);
        }
    }

    return fromJSValue(type, QJSValuePrivate::fromReturnedValue(s.asReturnedValue()), metaType);

}

/*!
 * \internal
 * This should only be called with either builtin types or wrapped QJSValues as source.
 */
QVariant QQmlValueTypeProvider::createValueType(const QVariant &s, QMetaType metaType)
{
    if (!isConstructibleMetaType(metaType))
        return QVariant();
    const QQmlType type = QQmlMetaType::qmlType(metaType);
    const auto warn = [&](const QMetaObject *mo) {
        qWarning().noquote()
                << "Could not find any constructor for value type"
                << mo->className() << "to call with value" << s;
    };

    if (type.canPopulateValueType()) {
        if (const QMetaObject *mo = type.metaObjectForValueType()) {
            QVariant result = byProperties(mo, metaType, s);
            if (result.isValid())
                return result;
            if (type.canConstructValueType()) {
                if (fromMatchingType(mo, s, [&]() { return createVariantData(metaType, &result); }))
                    return result;
                warn(mo);
            }
        }
    } else if (type.canConstructValueType()) {
        if (const QMetaObject *mo = type.metaObjectForValueType()) {
            QVariant result;
            if (fromMatchingType(mo, s, [&]() { return createVariantData(metaType, &result); }))
                return result;
            warn(mo);
        }
    }

    return QVariant();
}

QQmlColorProvider::~QQmlColorProvider() {}
QVariant QQmlColorProvider::colorFromString(const QString &, bool *ok) { if (ok) *ok = false; return QVariant(); }
unsigned QQmlColorProvider::rgbaFromString(const QString &, bool *ok) { if (ok) *ok = false; return 0; }
QVariant QQmlColorProvider::fromRgbF(double, double, double, double) { return QVariant(); }
QVariant QQmlColorProvider::fromHslF(double, double, double, double) { return QVariant(); }
QVariant QQmlColorProvider::fromHsvF(double, double, double, double) { return QVariant(); }
QVariant QQmlColorProvider::lighter(const QVariant &, qreal) { return QVariant(); }
QVariant QQmlColorProvider::darker(const QVariant &, qreal) { return QVariant(); }
QVariant QQmlColorProvider::alpha(const QVariant &, qreal)
{
    return QVariant();
}
QVariant QQmlColorProvider::tint(const QVariant &, const QVariant &) { return QVariant(); }

static QQmlColorProvider *colorProvider = nullptr;

Q_QML_EXPORT QQmlColorProvider *QQml_setColorProvider(QQmlColorProvider *newProvider)
{
    QQmlColorProvider *old = colorProvider;
    colorProvider = newProvider;
    return old;
}

static QQmlColorProvider **getColorProvider(void)
{
    if (colorProvider == nullptr) {
        qWarning() << "Warning: QQml_colorProvider: no color provider has been set!";
        static QQmlColorProvider nullColorProvider;
        colorProvider = &nullColorProvider;
    }

    return &colorProvider;
}

Q_AUTOTEST_EXPORT QQmlColorProvider *QQml_colorProvider(void)
{
    static QQmlColorProvider **providerPtr = getColorProvider();
    return *providerPtr;
}


QQmlGuiProvider::~QQmlGuiProvider() {}
QQmlApplication *QQmlGuiProvider::application(QObject *parent)
{
    return new QQmlApplication(parent);
}
QStringList QQmlGuiProvider::fontFamilies() { return QStringList(); }
bool QQmlGuiProvider::openUrlExternally(const QUrl &) { return false; }

QObject *QQmlGuiProvider::inputMethod()
{
    // We don't have any input method code by default
    QObject *o = new QObject();
    o->setObjectName(QStringLiteral("No inputMethod available"));
    QQmlEngine::setObjectOwnership(o, QQmlEngine::JavaScriptOwnership);
    return o;
}

QObject *QQmlGuiProvider::styleHints()
{
    QObject *o = new QObject();
    o->setObjectName(QStringLiteral("No styleHints available"));
    QQmlEngine::setObjectOwnership(o, QQmlEngine::JavaScriptOwnership);
    return o;
}

QString QQmlGuiProvider::pluginName() const { return QString(); }

static QQmlGuiProvider *guiProvider = nullptr;

Q_QML_EXPORT QQmlGuiProvider *QQml_setGuiProvider(QQmlGuiProvider *newProvider)
{
    QQmlGuiProvider *old = guiProvider;
    guiProvider = newProvider;
    return old;
}

static QQmlGuiProvider **getGuiProvider(void)
{
    if (guiProvider == nullptr) {
        static QQmlGuiProvider nullGuiProvider; //Still provides an application with no GUI support
        guiProvider = &nullGuiProvider;
    }

    return &guiProvider;
}

Q_AUTOTEST_EXPORT QQmlGuiProvider *QQml_guiProvider(void)
{
    static QQmlGuiProvider **providerPtr = getGuiProvider();
    return *providerPtr;
}

//Docs in qqmlengine.cpp
QQmlApplication::QQmlApplication(QObject *parent)
    : QQmlApplication(*(new QQmlApplicationPrivate), parent)
{
}

QQmlApplication::QQmlApplication(QQmlApplicationPrivate &dd, QObject *parent)
    : QObject(dd, parent)
{
    connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()),
            this, SIGNAL(aboutToQuit()));
    connect(QCoreApplication::instance(), SIGNAL(applicationNameChanged()),
            this, SIGNAL(nameChanged()));
    connect(QCoreApplication::instance(), SIGNAL(applicationVersionChanged()),
            this, SIGNAL(versionChanged()));
    connect(QCoreApplication::instance(), SIGNAL(organizationNameChanged()),
            this, SIGNAL(organizationChanged()));
    connect(QCoreApplication::instance(), SIGNAL(organizationDomainChanged()),
            this, SIGNAL(domainChanged()));
}

QStringList QQmlApplication::args()
{
    Q_D(QQmlApplication);
    if (!d->argsInit) {
        d->argsInit = true;
        d->args = QCoreApplication::arguments();
    }
    return d->args;
}

QString QQmlApplication::name() const
{
    return QCoreApplication::instance()->applicationName();
}

QString QQmlApplication::version() const
{
    return QCoreApplication::instance()->applicationVersion();
}

QString QQmlApplication::organization() const
{
    return QCoreApplication::instance()->organizationName();
}

QString QQmlApplication::domain() const
{
    return QCoreApplication::instance()->organizationDomain();
}

void QQmlApplication::setName(const QString &arg)
{
    QCoreApplication::instance()->setApplicationName(arg);
}

void QQmlApplication::setVersion(const QString &arg)
{
    QCoreApplication::instance()->setApplicationVersion(arg);
}

void QQmlApplication::setOrganization(const QString &arg)
{
    QCoreApplication::instance()->setOrganizationName(arg);
}

void QQmlApplication::setDomain(const QString &arg)
{
    QCoreApplication::instance()->setOrganizationDomain(arg);
}

static const QQmlData *ddata_for_cast(QObject *object)
{
    Q_ASSERT(object);
    auto ddata = QQmlData::get(object, false);
    return (ddata && ddata->propertyCache) ? ddata : nullptr;
}

bool qmlobject_can_cpp_cast(QObject *object, const QMetaObject *mo)
{
    Q_ASSERT(mo);
    if (const QQmlData *ddata = ddata_for_cast(object))
        return ddata->propertyCache->firstCppMetaObject()->inherits(mo);
    return object->metaObject()->inherits(mo);
}

bool qmlobject_can_qml_cast(QObject *object, const QQmlType &type)
{
    Q_ASSERT(type.isValid());

    // A non-composite type will always have a metaobject.
    const QMetaObject *typeMetaObject = type.metaObject();
    const QQmlPropertyCache::ConstPtr typePropertyCache = typeMetaObject
            ? QQmlPropertyCache::ConstPtr()
            : QQmlMetaType::findPropertyCacheInCompositeTypes(type.typeId());

    if (const QQmlData *ddata = ddata_for_cast(object)) {
        for (const QQmlPropertyCache *propertyCache = ddata->propertyCache.data(); propertyCache;
             propertyCache = propertyCache->parent().data()) {

            if (typeMetaObject) {
                // Prefer the metaobject inheritance mechanism, since it is more accurate.
                //
                // Assume the object can be casted to the type. Then, if we have a type metaobject,
                // the object's property cache inheritance has to contain it. Otherwise we would
                // end up with diverging metaobject hierarchies if we created the object's
                // metaobject. This would be a disaster.
                if (const QMetaObject *objectMetaObject = propertyCache->metaObject())
                    return objectMetaObject->inherits(typeMetaObject);
            } else {
                // This is a best effort attempt. There are a number of ways for the
                // property caches to be unrelated but the types still convertible.
                // Multiple property caches can hold the same metaobject, for example for
                // versions of non-composite types.
                if (propertyCache == typePropertyCache.data())
                    return true;
            }
        }
    }

    // If nothing else works, we have to create the metaobjects.

    return object->metaObject()->inherits(typeMetaObject
            ? typeMetaObject
            : (typePropertyCache ? typePropertyCache->createMetaObject() : nullptr));
}

QT_END_NAMESPACE

#include "moc_qqmlglobal_p.cpp"