aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsapi/qjsmanagedvalue.cpp
blob: 454dc11fd8a48c6644fa7157fdc3b99c2e4ea061 (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
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtQml/qjsmanagedvalue.h>
#include <QtQml/qjsengine.h>
#include <QtQml/private/qv4persistent_p.h>
#include <QtQml/private/qv4engine_p.h>
#include <QtQml/private/qv4mm_p.h>
#include <QtQml/private/qjsvalue_p.h>
#include <QtQml/private/qv4runtime_p.h>
#include <QtQml/private/qv4functionobject_p.h>
#include <QtQml/private/qv4jscall_p.h>
#include <QtQml/private/qv4urlobject_p.h>
#include <QtQml/private/qv4variantobject_p.h>
#include <QtQml/private/qv4qobjectwrapper_p.h>
#include <QtQml/private/qv4regexpobject_p.h>
#include <QtQml/private/qv4dateobject_p.h>
#include <QtQml/private/qv4errorobject_p.h>
#include <QtQml/private/qv4identifiertable_p.h>

#include <QtCore/qregularexpression.h>
#include <QtCore/qurl.h>
#include <QtCore/qdatetime.h>

QT_BEGIN_NAMESPACE

/*!
 * \class QJSManagedValue
 * \inmodule QtQml
 * \since 6.1
 *
 * \brief QJSManagedValue represents a value on the JavaScript heap belonging to a QJSEngine.
 *
 * The QJSManagedValue class allows interaction with JavaScript values in most
 * ways you can interact with them from JavaScript itself. You can get and set
 * properties and prototypes, and you can access arrays. Additionally, you can
 * transform the value into the Qt counterparts of JavaScript objects. For
 * example, a Url object may be transformed into a QUrl.
 *
 * A QJSManagedValue is always bound to a particular QJSEngine. You cannot use
 * it independently. This means that you cannot have a QJSManagedValue from one
 * engine be a property or a proptotype of a QJSManagedValue from a different
 * engine.
 *
 * In contrast to QJSValue, almost all values held by QJSManagedValue live on
 * the JavaScript heap. There is no inline or unmanaged storage. Therefore, you
 * can get the prototype of a primitive value, and you can get the \c length
 * property of a string.
 *
 * Only default-constructed or moved-from QJSManagedValues do not hold a value
 * on the JavaScript heap. They represent \c undefined, which doesn't have any
 * properties or prototypes.
 *
 * Also in contrast to QJSValue, QJSManagedValue does not catch any JavaScript
 * exceptions. If an operation on a QJSManagedValue causes an error, it will
 * generally return an \c undefined value and QJSEngine::hasError() will return
 * \c true afterwards. You can then catch the exception using
 * QJSEngine::catchError(), or pass it up the stack, at your own discretion.
 *
 * \note As the reference to the value on the JavaScript heap has to be freed
 * on destruction, you cannot move a QJSManagedValue to a different thread.
 * The destruction would take place in the new thread, which would create a race
 * condition with the garbage collector on the original thread. This also means
 * that you cannot hold a QJSManagedValue beyond the lifespan of its engine.
 *
 * The recommended way of working with a QJSManagedValue is creating it
 * on the stack, possibly by moving a QJSValue and adding an engine, then
 * performing the necessary operations on it, and finally moving it back into a
 * QJSValue for storage. Moving between QJSManagedValue and QJSValue is fast.
 */

/*!
 * \enum QJSManagedValue::Type
 *
 * This enum represents the JavaScript native types, as specified by
 * \l{ECMA-262}.
 *
 * \value Undefined The \c undefined type
 * \value Boolean   The \c boolean type
 * \value Number    The \c number type
 * \value String    The \c string type
 * \value Object    The \c object type
 * \value Symbol    The \c symbol type
 * \value Function  The \c function type
 *
 * Note that the \c null value is not a type of itself but rather a special kind
 * of object. You can query a QJSManagedValue for this condition using the
 * isNull() method. Furthermore, JavaScript has no integer type, but it knows a
 * special treatment of numbers in preparation for integer only operations. You
 * can query a QJSManagedValue to find out whether it holds the result of such a
 * treatment by using the isInteger() method.
 */

/*!
 * \fn QJSManagedValue::QJSManagedValue()
 *
 * Creates a QJSManagedValue that represents the JavaScript \c undefined value.
 * This is the only value not stored on the JavaScript heap. Calling engine()
 * on a default-constructed QJSManagedValue will return nullptr.
 */

static QV4::ExecutionEngine *v4Engine(QV4::Value *d)
{
    if (!d)
        return nullptr;

    QV4::ExecutionEngine *v4 = QV4::PersistentValueStorage::getEngine(d);
    Q_ASSERT(v4);
    return v4;
}

/*!
 * Creates a QJSManagedValue from \a value, using the heap of \a engine. If
 * \a value is itself managed and the engine it belongs to is not \a  engine,
 * the result is an \c undefined value, and a warning is generated.
 */
QJSManagedValue::QJSManagedValue(QJSValue value, QJSEngine *engine)
{
    QV4::ExecutionEngine *v4 = engine->handle();

    if (QV4::Value *m = QJSValuePrivate::takeManagedValue(&value)) {
        if (Q_UNLIKELY(v4Engine(m) != v4)) {
            qWarning("QJSManagedValue(QJSValue, QJSEngine *) failed: "
                     "Value was created in different engine.");
            QV4::PersistentValueStorage::free(m);
            return;
        }

        d = m;
        return;
    }

    d = v4->memoryManager->m_persistentValues->allocate();

    if (const QString *string = QJSValuePrivate::asQString(&value))
        *d = v4->newString(*string);
    else
        *d = QJSValuePrivate::asReturnedValue(&value);
}

/*!
 * Creates a QJSManagedValue from \a value using the heap of \a engine.
 */
QJSManagedValue::QJSManagedValue(const QJSPrimitiveValue &value, QJSEngine *engine) :
    QJSManagedValue(engine->handle())
{
    switch (value.type()) {
    case QJSPrimitiveValue::Undefined:
        *d = QV4::Encode::undefined();
        return;
    case QJSPrimitiveValue::Null:
        *d = QV4::Encode::null();
        return;
    case QJSPrimitiveValue::Boolean:
        *d = QV4::Encode(value.asBoolean());
        return;
    case QJSPrimitiveValue::Integer:
        *d = QV4::Encode(value.asInteger());
        return;
    case QJSPrimitiveValue::Double:
        *d = QV4::Encode(value.asDouble());
        return;
    case QJSPrimitiveValue::String:
        *d = engine->handle()->newString(value.asString());
        return;
    }

    Q_UNREACHABLE();
}

/*!
 * Creates a QJSManagedValue from \a variant using the heap of \a engine.
 */
QJSManagedValue::QJSManagedValue(const QVariant &variant, QJSEngine *engine) :
    QJSManagedValue(engine->handle())
{
    *d = engine->handle()->fromVariant(variant);
}

/*!
 * Creates a QJSManagedValue from \a string using the heap of \a engine.
 */
QJSManagedValue::QJSManagedValue(const QString &string, QJSEngine *engine) :
    QJSManagedValue(engine->handle())
{
    *d = engine->handle()->newString(string);
}

/*!
 * Destroys the QJSManagedValue.
 *
 * \note This frees the memory slot it holds on the JavaScript heap. You must
 *       not destroy a QJSManagedValue from a different thread than the one
 *       where the QJSEngine it belongs to lives.
 */
QJSManagedValue::~QJSManagedValue()
{
    QV4::PersistentValueStorage::free(d);
}

/*!
 * Move-constructs a QJSManagedValue from \a other. This leaves \a other in
 * the default-constructed state where it represents undefined and does not
 * belong to any engine.
 */
QJSManagedValue::QJSManagedValue(QJSManagedValue &&other)
{
    qSwap(d, other.d);
}

/*!
 * Move-assigns a QJSManagedValue from \a other. This leaves \a other in
 * the default-constructed state where it represents undefined and does not
 * belong to any engine.
 *
 * \note This frees the memory slot this QJSManagedValue holds on the
 *       JavaScript heap. You must not move-assign a QJSManagedValue on a
 *       different thread than the one where the QJSEngine it belongs to lives.
 */
QJSManagedValue &QJSManagedValue::operator=(QJSManagedValue &&other)
{
    if (this != &other) {
        QV4::PersistentValueStorage::free(d);
        d = nullptr;
        qSwap(d, other.d);
    }
    return *this;
}

/*!
 * Invokes the JavaScript '==' operator on this QJSManagedValue and \a other,
 * and returns the result.
 *
 * \sa strictlyEquals
 */
bool QJSManagedValue::equals(const QJSManagedValue &other) const
{
    if (!d)
        return !other.d || other.d->isNullOrUndefined();
    if (!other.d)
        return d->isNullOrUndefined();

    return QV4::Runtime::CompareEqual::call(*d, *other.d);
}

/*!
 * Invokes the JavaScript '===' operator on this QJSManagedValue and \a other,
 * and returns the result.
 *
 * \sa equals
 */
bool QJSManagedValue::strictlyEquals(const QJSManagedValue &other) const
{
    if (!d)
        return !other.d || other.d->isUndefined();
    if (!other.d)
        return d->isUndefined();

    return QV4::RuntimeHelpers::strictEqual(*d, *other.d);
}

/*!
 * Returns the QJSEngine this QJSManagedValue belongs to. Mind that the engine
 * is always valid, unless the QJSManagedValue is default-constructed or moved
 * from. In the latter case a nullptr is returned.
 */
QJSEngine *QJSManagedValue::engine() const
{
    if (!d)
        return nullptr;
    if (QV4::ExecutionEngine *v4 = QV4::PersistentValueStorage::getEngine(d))
        return v4->jsEngine();
    return nullptr;
}

/*!
 * Returns the prototype for this QJSManagedValue. This works on any value. You
 * can, for example retrieve the JavaScript \c boolean prototype from a \c boolean
 * value.
 */
QJSManagedValue QJSManagedValue::prototype() const
{
    if (!d)
        return QJSManagedValue();

    QV4::ExecutionEngine *v4 = v4Engine(d);
    QJSManagedValue result(v4);

    if (auto object = d->as<QV4::Object>())
        *result.d = object->getPrototypeOf();
    else if (auto managed = d->as<QV4::Managed>())
        *result.d = managed->internalClass()->prototype;
    else if (d->isBoolean())
        *result.d = v4->booleanPrototype();
    else if (d->isNumber())
        *result.d = v4->numberPrototype();

    // If the prototype appears to be undefined, then it's actually null in JS terms.
    if (result.d->isUndefined())
        *result.d = QV4::Encode::null();

    return result;
}

/*!
 * Sets the prototype of this QJSManagedValue to \a prototype. A precondition
 * is that \a prototype belongs to the same QJSEngine as this QJSManagedValue
 * and is an object (including null). Furthermore, this QJSManagedValue has to
 * be an object (excluding null), too, and you cannot create prototype cycles.
 */
void QJSManagedValue::setPrototype(const QJSManagedValue &prototype)
{
    auto object = d ? d->as<QV4::Object>() : nullptr;
    if (!object) {
        qWarning("QJSManagedValue::setPrototype() failed: "
                 "Can only set a prototype on an object (excluding null).");
        return;
    }

    // Object includes null ...
    if (prototype.type() != QJSManagedValue::Object) {
        qWarning("QJSManagedValue::setPrototype() failed: "
                 "Can only set objects (including null) as prototypes.");
        return;
    }

    if (Q_UNLIKELY(object->engine() != v4Engine(prototype.d))) {
        qWarning("QJSManagedValue::setPrototype() failed: "
                 "Prototype was created in differen engine.");
        return;
    }

    // ... Null becomes nullptr here. That is why it appears as undefined later.
    if (!object->setPrototypeOf(prototype.d->as<QV4::Object>())) {
        qWarning("QJSManagedValue::setPrototype() failed: "
                 "Prototype cycle detected.");
    }
}

/*!
 * Returns the JavaScript type of this QJSManagedValue.
 */
QJSManagedValue::Type QJSManagedValue::type() const
{
    if (!d || d->isUndefined())
        return Undefined;
    if (d->isBoolean())
        return Boolean;
    if (d->isNumber())
        return Number;
    if (d->isString())
        return String;
    if (d->isSymbol())
        return Symbol;
    if (d->isFunctionObject())
        return Function;
    return Object;
}

/*!
 * \fn QJSManagedValue::isUndefined() const
 *
 * Returns \c true if the type of this QJSManagedValue is \c undefined,
 * or \c false otherwise.
 */

/*!
 * \fn QJSManagedValue::isBoolean() const
 *
 * Returns \c true if the type of this QJSManagedValue is \c boolean,
 * or \c false otherwise.
 */

/*!
 * \fn QJSManagedValue::isNumber() const
 *
 * Returns \c true if the type of this QJSManagedValue is \c number,
 * or \c false otherwise.
 */

/*!
 * \fn QJSManagedValue::isString() const
 *
 * Returns \c true if the type of this QJSManagedValue is \c string,
 * or \c false otherwise.
 */

/*!
 * \fn QJSManagedValue::isSymbol() const
 *
 * Returns \c true if the type of this QJSManagedValue is \c symbol,
 * or \c false otherwise.
 */

/*!
 * \fn QJSManagedValue::isObject() const
 *
 * Returns \c true if the type of this QJSManagedValue is \c object,
 * or \c false otherwise.
 */

/*!
 * \fn QJSManagedValue::isFunction() const
 *
 * Returns \c true if the type of this QJSManagedValue is \c function,
 * \c false otherwise.
 */

/*!
 * Returns \c true if this QJSManagedValue holds the JavaScript \c null value,
 * or \c false otherwise.
 */
bool QJSManagedValue::isNull() const
{
    return d && d->isNull();
}

/*!
 * Returns \c true if this QJSManagedValue holds an integer value, or \c false
 * otherwise. The storage format of a number does not affect the result of any
 * operations performed on it, but if an integer is stored, many operations are
 * faster.
 */
bool QJSManagedValue::isInteger() const
{
    return d && d->isInteger();
}

/*!
 * Returns \c true if this value represents a JavaScript regular expression
 * object, or \c false otherwise.
 */
bool QJSManagedValue::isRegularExpression() const
{
    return d && d->as<QV4::RegExpObject>();
}

/*!
 * Returns \c true if this value represents a JavaScript Array
 * object, or \c false otherwise.
 */
bool QJSManagedValue::isArray() const
{
    return d && d->as<QV4::ArrayObject>();
}

/*!
 * Returns \c true if this value represents a JavaScript Url
 * object, or \c false otherwise.
 */
bool QJSManagedValue::isUrl() const
{
    return d && d->as<QV4::UrlObject>();
}

/*!
 * Returns \c true if this value represents a QVariant managed on the JavaScript
 * heap, or \c false otherwise.
 */
bool QJSManagedValue::isVariant() const
{
    return d && d->as<QV4::VariantObject>();
}

/*!
 * Returns \c true if this value represents a QObject pointer managed on the
 * JavaScript heap, or \c false otherwise.
 */
bool QJSManagedValue::isQObject() const
{
    return d && d->as<QV4::QObjectWrapper>();
}

/*!
 * Returns \c true if this value represents a QMetaObject pointer managed on the
 * JavaScript heap, or \c false otherwise.
 */
bool QJSManagedValue::isQMetaObject() const
{
    return d && d->as<QV4::QMetaObjectWrapper>();
}

/*!
 * Returns \c true if this value represents a JavaScript Date object, or
 * \c false otherwise.
 */
bool QJSManagedValue::isDate() const
{
    return d && d->as<QV4::DateObject>();
}

/*!
 * Returns \c true if this value represents a JavaScript Error object, or
 * \c false otherwise.
 */
bool QJSManagedValue::isError() const
{
    return d && d->as<QV4::ErrorObject>();
}

/*!
 * \internal
 *
 * Returns \c true if this value represents a JavaScript meta type, or \c false
 * otherwise.
 */
bool QJSManagedValue::isJsMetaType() const
{
    return d && d->as<QV4::InternalClass>();
}

/*!
 * Converts the manged value to a string. If the managed value holds a string,
 * that one is returned. Otherwise a string coercion by JavaScript rules is
 * performed.
 *
 * \note Conversion of a managed value to a string can throw an exception. In
 *       particular, symbols cannot be coerced into strings, or a custom
 *       toString() method  may throw. In this case the result is an empty
 *       string and the engine carries an error after the conversion.
 */
QString QJSManagedValue::toString() const
{
    return d ? d->toQString() : QStringLiteral("undefined");
}

/*!
 * Converts the manged value to a number. If the managed value holds a number,
 * that one is returned. Otherwise a number coercion by JavaScript rules is
 * performed.
 *
 * \note Conversion of a managed value to a number can throw an exception. In
 *       particular, symbols cannot be coerced into numbers, or a custom
 *       valueOf() method  may throw. In this case the result is 0 and the
 *       engine carries an error after the conversion.
 */
double QJSManagedValue::toNumber() const
{
    return d ? d->toNumber() : 0;
}

/*!
 * Converts the manged value to a boolean. If the managed value holds a boolean,
 * that one is returned. Otherwise a boolean coercion by JavaScript rules is
 * performed.
 */
bool QJSManagedValue::toBoolean() const
{
    return d ? d->toBoolean() : false;
}

/*!
 * Converts the manged value to an integer. This first converts the value to a
 * number by the rules of toNumber(), and then clamps it into the integer range
 * by the rules given for coercing the arguments to JavaScript bit shift
 * operators into 32bit integers.
 *
 * Internally, the value may already be stored as an integer, in which case a
 * fast path is taken.
 *
 * \note Conversion of a managed value to a number can throw an exception. In
 *       particular, symbols cannot be coerced into numbers, or a custom
 *       valueOf() method  may throw. In this case the result is 0 and the
 *       engine carries an error after the conversion.
 *
 * \note The JavaScript rules for coercing numbers into 32bit integers are
 *       unintuitive.
 */
int QJSManagedValue::toInteger() const
{
    return d ? d->toInt32() : 0;
}

/*!
 * Converts the manged value to a QJSPrimitiveValue. If the managed value holds
 * a type supported by QJSPrimitiveValue, the value is copied. Otherwise the
 * value is converted to a string, and the string is stored in
 * QJSPrimitiveValue.
 *
 * \note Conversion of a managed value to a string can throw an exception. In
 *       particular, symbols cannot be coerced into strings, or a custom
 *       toString() method  may throw. In this case the result is the undefined
 *       value and the engine carries an error after the conversion.
 */
QJSPrimitiveValue QJSManagedValue::toPrimitive() const
{
    if (!d || d->isUndefined())
        return QJSPrimitiveUndefined();
    if (d->isNull())
        return QJSPrimitiveNull();
    if (d->isBoolean())
        return d->booleanValue();
    if (d->isInteger())
        return d->integerValue();
    if (d->isNumber())
        return d->doubleValue();

    bool ok;
    const QString result = d->toQString(&ok);
    return ok ? QJSPrimitiveValue(result) : QJSPrimitiveValue(QJSPrimitiveUndefined());
}

/*!
 * Copies this QJSManagedValue into a new QJSValue. This is less efficient than
 * move-constructing a QJSValue from a QJSManagedValue, but retains the
 * QJSManagedValue.
 */
QJSValue QJSManagedValue::toJSValue() const
{
    return d ? QJSValuePrivate::fromReturnedValue(d->asReturnedValue()) : QJSValue();
}

/*!
 * Copies this QJSManagedValue into a new QVariant. This also creates a useful
 * QVariant if QJSManagedValue::isVariant() returns false. QVariant can hold all
 * types supported by QJSManagedValue.
 */
QVariant QJSManagedValue::toVariant() const
{
    if (!d || d->isUndefined())
        return QVariant();
    if (d->isNull())
        return QVariant(QMetaType::fromType<std::nullptr_t>(), nullptr);
    if (d->isBoolean())
        return QVariant(d->booleanValue());
    if (d->isInteger())
        return QVariant(d->integerValue());
    if (d->isNumber())
        return QVariant(d->doubleValue());
    if (d->isString())
        return QVariant(d->toQString());
    if (QV4::Managed *m = d->as<QV4::Managed>())
        return m->engine()->toVariant(*d, QMetaType{}, true);

    Q_UNREACHABLE();
    return QVariant();
}

/*!
 * If this QJSManagedValue holds a JavaScript regular expression object, returns
 * an equivalent QRegularExpression. Otherwise returns an invalid one.
 */
QRegularExpression QJSManagedValue::toRegularExpression() const
{
    if (const auto *r = d ? d->as<QV4::RegExpObject>() : nullptr)
        return r->toQRegularExpression();
    return {};
}

/*!
 * If this QJSManagedValue holds a JavaScript Url object, returns
 * an equivalent QUrl. Otherwise returns an invalid one.
 */
QUrl QJSManagedValue::toUrl() const
{
    if (const auto *u = d ? d->as<QV4::UrlObject>() : nullptr)
        return u->toQUrl();
    return {};
}

/*!
 * If this QJSManagedValue holds a QObject pointer, returns it. Otherwise
 * returns nullptr.
 */
QObject *QJSManagedValue::toQObject() const
{
    if (const auto *o = d ? d->as<QV4::QObjectWrapper>() : nullptr)
        return o->object();
    return {};
}

/*!
 * If this QJSManagedValue holds a QMetaObject pointer, returns it.
 * Otherwise returns nullptr.
 */
const QMetaObject *QJSManagedValue::toQMetaObject() const
{
    if (const auto *m = d ? d->as<QV4::QMetaObjectWrapper>() : nullptr)
        return m->metaObject();
    return {};
}

/*!
 * If this QJSManagedValue holds a JavaScript Date object, returns an equivalent
 * QDateTime. Otherwise returns an invalid one.
 */
QDateTime QJSManagedValue::toDateTime() const
{
    if (const auto *t = d ? d->as<QV4::DateObject>() : nullptr)
        return t->toQDateTime();
    return {};
}

/*!
 * Returns \c true if this QJSManagedValue has a property \a name, otherwise
 * returns \c false. The properties of the prototype chain are considered.
 */
bool QJSManagedValue::hasProperty(const QString &name) const
{
    if (!d || d->isNullOrUndefined())
        return false;

    if (d->isString() && name == QStringLiteral("length"))
        return true;

    if (QV4::Object *obj = d->as<QV4::Object>()) {
        QV4::Scope scope(obj->engine());
        QV4::ScopedPropertyKey key(scope, scope.engine->identifierTable->asPropertyKey(name));
        return obj->hasProperty(key);
    }

    return prototype().hasProperty(name);
}

/*!
 * Returns \c true if this QJSManagedValue has a property \a name, otherwise
 * returns \c false. The properties of the prototype chain are not considered.
 */
bool QJSManagedValue::hasOwnProperty(const QString &name) const
{
    if (!d || d->isNullOrUndefined())
        return false;

    if (d->isString() && name == QStringLiteral("length"))
        return true;

    if (QV4::Object *obj = d->as<QV4::Object>()) {
        QV4::Scope scope(obj->engine());
        QV4::ScopedPropertyKey key(scope, scope.engine->identifierTable->asPropertyKey(name));
        return obj->getOwnProperty(key) != QV4::Attr_Invalid;
    }

    return false;
}

/*!
 * Returns the property \a name of this QJSManagedValue. The prototype chain
 * is searched if the property is not found on the actual object.
 */
QJSValue QJSManagedValue::property(const QString &name) const
{
    if (!d)
        return QJSValue();

    if (d->isNullOrUndefined()) {
        QV4::ExecutionEngine *e = v4Engine(d);
        e->throwTypeError(QStringLiteral("Cannot read property '%1' of null").arg(name));
        return QJSValue();
    }

    if (QV4::String *string = d->as<QV4::String>()) {
        if (name == QStringLiteral("length"))
            return QJSValue(string->d()->length());
    }

    if (QV4::Object *obj = d->as<QV4::Object>()) {
        QV4::Scope scope(obj->engine());
        QV4::ScopedPropertyKey key(scope, scope.engine->identifierTable->asPropertyKey(name));
        return QJSValuePrivate::fromReturnedValue(obj->get(key));
    }

    return prototype().property(name);
}

/*!
 * Sets the property \a name to \a value on this QJSManagedValue. This can only
 * be done on JavaScript values of type \c object. Furhermore, \a value has to be
 * either a primitive or belong to the same engine as this value.
 */
void QJSManagedValue::setProperty(const QString &name, const QJSValue &value)
{
    if (!d)
        return;

    if (d->isNullOrUndefined()) {
        v4Engine(d)->throwTypeError(
                    QStringLiteral("Value is null and could not be converted to an object"));
    }

    if (QV4::Object *obj = d->as<QV4::Object>()) {
        QV4::Scope scope(obj->engine());
        QV4::ExecutionEngine *v4 = QJSValuePrivate::engine(&value);
        if (Q_UNLIKELY(v4 && v4 != scope.engine)) {
            qWarning("QJSManagedValue::setProperty() failed: "
                     "Value was created in different engine.");
            return;
        }
        QV4::ScopedPropertyKey key(scope, scope.engine->identifierTable->asPropertyKey(name));
        obj->put(key, QJSValuePrivate::convertToReturnedValue(scope.engine, value));
    }
}

/*!
 * Deletes the property \a name from this QJSManagedValue. Returns \c true if
 * the deletion succeeded, or \c false otherwise.
 */
bool QJSManagedValue::deleteProperty(const QString &name)
{
    if (!d)
        return false;

    if (QV4::Object *obj = d->as<QV4::Object>()) {
        QV4::Scope scope(obj->engine());
        QV4::ScopedPropertyKey key(scope, scope.engine->identifierTable->asPropertyKey(name));
        return obj->deleteProperty(key);
    }

    return false;
}

/*!
 * Returns \c true if this QJSManagedValue has an array index \a arrayIndex,
 * otherwise returns \c false. The properties of the prototype chain are
 * considered.
 */
bool QJSManagedValue::hasProperty(quint32 arrayIndex) const
{
    if (!d || d->isNullOrUndefined())
        return false;

    if (QV4::String *string = d->as<QV4::String>())
        return arrayIndex < quint32(string->d()->length());

    if (QV4::Object *obj = d->as<QV4::Object>()) {
        bool hasProperty = false;
        if (arrayIndex == std::numeric_limits<quint32>::max())
            obj->get(obj->engine()->id_uintMax(), &hasProperty);
        else
            obj->get(arrayIndex, &hasProperty);
        return hasProperty;
    }

    return prototype().hasProperty(arrayIndex);
}

/*!
 * Returns \c true if this QJSManagedValue has an array index \a arrayIndex,
 * otherwise returns \c false. The properties of the prototype chain are not
 * considered.
 */
bool QJSManagedValue::hasOwnProperty(quint32 arrayIndex) const
{
    if (!d || d->isNullOrUndefined())
        return false;

    if (QV4::String *string = d->as<QV4::String>())
        return arrayIndex < quint32(string->d()->length());

    if (QV4::Object *obj = d->as<QV4::Object>()) {
        if (arrayIndex == std::numeric_limits<quint32>::max()) {
            return obj->getOwnProperty(obj->engine()->id_uintMax()->toPropertyKey())
                    != QV4::Attr_Invalid;
        } else {
            return obj->getOwnProperty(QV4::PropertyKey::fromArrayIndex(arrayIndex))
                    != QV4::Attr_Invalid;
        }
    }

    return false;
}

/*!
 * Returns the property stored at \a arrayIndex of this QJSManagedValue. The
 * prototype chain is searched if the property is not found on the actual
 * object.
 */
QJSValue QJSManagedValue::property(quint32 arrayIndex) const
{
    if (!d || d->isNullOrUndefined())
        return QJSValue();

    if (QV4::String *string = d->as<QV4::String>()) {
        const QString qString = string->toQString();
        if (arrayIndex < qString.size())
            return qString.sliced(arrayIndex, 1);
        return QJSValue();
    }

    if (QV4::Object *obj = d->as<QV4::Object>()) {
        if (arrayIndex == std::numeric_limits<quint32>::max())
            return QJSValuePrivate::fromReturnedValue(obj->get(obj->engine()->id_uintMax()));
        else
            return QJSValuePrivate::fromReturnedValue(obj->get(arrayIndex));
    }

    return prototype().property(arrayIndex);
}

/*!
 * Stores the \a value at \a arrayIndex in this QJSManagedValue. This can only
 * be done on JavaScript values of type \c object, and it's not recommended if the
 * value is not an array. Furhermore, \a value has to be either a primitive or
 * belong to the same engine as this value.
 */
void QJSManagedValue::setProperty(quint32 arrayIndex, const QJSValue &value)
{
    if (!d)
        return;

    if (QV4::Object *obj = d->as<QV4::Object>()) {
        QV4::ExecutionEngine *v4 = QJSValuePrivate::engine(&value);
        if (Q_UNLIKELY(v4 && v4 != obj->engine())) {
            qWarning("QJSManagedValue::setProperty() failed: "
                     "Value was created in different engine.");
            return;
        }
        obj->put(arrayIndex, QJSValuePrivate::convertToReturnedValue(v4, value));
    }
}

/*!
 * Deletes the value stored at \a arrayIndex from this QJSManagedValue. Returns
 * \c true if the deletion succeeded, or \c false otherwise.
 */
bool QJSManagedValue::deleteProperty(quint32 arrayIndex)
{
    if (!d)
        return false;

    if (QV4::Object *obj = d->as<QV4::Object>())
        return obj->deleteProperty(QV4::PropertyKey::fromArrayIndex(arrayIndex));

    return false;
}

static const QV4::FunctionObject *functionObjectForCall(QV4::Value *d)
{
    if (Q_UNLIKELY(!d)) {
        qWarning("QJSManagedValue: Calling a default-constructed or moved-from managed value"
                 "should throw an exception, but there is no engine to receive it.");
        return nullptr;
    }

    if (const QV4::FunctionObject *f = d->as<QV4::FunctionObject>())
        return f;

    v4Engine(d)->throwTypeError(QStringLiteral("Value is not a function"));
    return nullptr;
}

/*!
 * If this QJSManagedValue represents a JavaScript FunctionObject, calls it with
 * the given \a arguments, and returns the result. Otherwise returns a
 * JavaScript \c undefined value.
 *
 * The \a arguments have to be either primitive values or belong to the same
 * QJSEngine as this QJSManagedValue. Otherwise the call is not carried
 * out and a JavaScript \c undefined value is returned.
 */
QJSValue QJSManagedValue::call(const QJSValueList &arguments) const
{
    const QV4::FunctionObject *f = functionObjectForCall(d);
    if (!f)
        return QJSValue();

    QV4::ExecutionEngine *engine = f->engine();

    QV4::Scope scope(engine);
    QV4::JSCallArguments jsCallData(scope, arguments.length());
    *jsCallData.thisObject = engine->globalObject;
    int i = 0;
    for (const QJSValue &arg : arguments) {
        if (Q_UNLIKELY(!QJSValuePrivate::checkEngine(engine, arg))) {
            qWarning("QJSManagedValue::call() failed: Argument was created in different engine.");
            return QJSValue();
        }
        jsCallData.args[i++] = QJSValuePrivate::convertToReturnedValue(engine, arg);
    }

    return QJSValuePrivate::fromReturnedValue(f->call(jsCallData));
}

/*!
 * If this QJSManagedValue represents a JavaScript FunctionObject, calls it on
 * \a instance with the given \a arguments, and returns the result. Otherwise
 * returns a JavaScript \c undefined value.
 *
 * The \a arguments and the \a instance have to be either primitive values or
 * belong to the same QJSEngine as this QJSManagedValue. Otherwise the call is
 * not carried out and a JavaScript \c undefined value is returned.
 */
QJSValue QJSManagedValue::callWithInstance(const QJSValue &instance,
                                           const QJSValueList &arguments) const
{
    const QV4::FunctionObject *f = functionObjectForCall(d);
    if (!f)
        return QJSValue();

    QV4::ExecutionEngine *engine = f->engine();

    if (Q_UNLIKELY(!QJSValuePrivate::checkEngine(engine, instance))) {
        qWarning("QJSManagedValue::callWithInstance() failed: "
                 "Instance was created in different engine.");
        return QJSValue();
    }

    QV4::Scope scope(engine);
    QV4::JSCallArguments jsCallData(scope, arguments.length());
    *jsCallData.thisObject = QJSValuePrivate::convertToReturnedValue(engine, instance);
    int i = 0;
    for (const QJSValue &arg : arguments) {
        if (Q_UNLIKELY(!QJSValuePrivate::checkEngine(engine, arg))) {
            qWarning("QJSManagedValue::callWithInstance() failed: "
                     "Argument was created in different engine.");
            return QJSValue();
        }
        jsCallData.args[i++] = QJSValuePrivate::convertToReturnedValue(engine, arg);
    }

    return QJSValuePrivate::fromReturnedValue(f->call(jsCallData));
}

/*!
 * If this QJSManagedValue represents a JavaScript FunctionObject, calls it as
 * constructor with the given \a arguments, and returns the result. Otherwise
 * returns a JavaScript \c undefined value.
 *
 * The \a arguments have to be either primitive values or belong to the same
 * QJSEngine as this QJSManagedValue. Otherwise the call is not carried
 * out and a JavaScript \c undefined value is returned.
 */
QJSValue QJSManagedValue::callAsConstructor(const QJSValueList &arguments) const
{
    const QV4::FunctionObject *f = functionObjectForCall(d);
    if (!f)
        return QJSValue();

    QV4::ExecutionEngine *engine = f->engine();

    QV4::Scope scope(engine);
    QV4::JSCallArguments jsCallData(scope, arguments.length());
    int i = 0;
    for (const QJSValue &arg : arguments) {
        if (Q_UNLIKELY(!QJSValuePrivate::checkEngine(engine, arg))) {
            qWarning("QJSManagedValue::callAsConstructor() failed: "
                     "Argument was created in different engine.");
            return QJSValue();
        }
        jsCallData.args[i++] = QJSValuePrivate::convertToReturnedValue(engine, arg);
    }

    return QJSValuePrivate::fromReturnedValue(f->callAsConstructor(jsCallData));
}

/*!
 * \internal
 *
 * Retrieves the JavaScript meta type of this value. The JavaScript meta type
 * represents the layout of members in an object. Instantiating a meta type is
 * faster than re-constructing the same object using a sequence of setProperty()
 * calls on a new object.
 *
 * \sa members(), instantiate()
 */
QJSManagedValue QJSManagedValue::jsMetaType() const
{
    if (!d)
        return QJSManagedValue();

    QJSManagedValue result(v4Engine(d));
    if (QV4::Managed *m = d->as<QV4::Managed>())
        *result.d = m->internalClass();

    return result;
}

/*!
 * \internal
 *
 * If this value is a JavaScript meta type, retrieves the names of its members
 * The ordering of the names corresponds to the ordering of the values to be
 * passed to instantiate().
 *
 * If the value is not a meta type, an empty list is returned.
 *
 * \sa isMetaType(), metaType(), instantiate()
 */
QStringList QJSManagedValue::jsMetaMembers() const
{
    if (!d)
        return {};

    if (QV4::InternalClass *c = d->as<QV4::InternalClass>()) {
        const auto heapClass = c->d();
        const int size = heapClass->size;
        QStringList result;
        result.reserve(size);
        for (int i = 0; i < size; ++i)
            result.append(heapClass->keyAt(i));
        return result;
    }

    return {};
}

/*!
 * \internal
 *
 * If this value is a JavaScript meta type, instantiates it using the
 * \a values, and returns the result. Otherwise returns undefined.
 *
 * The values are expected in the same order as the keys in the return value of
 * members(), and that is the order in which properties were added to the object
 * this meta type originally belongs to.
 *
 * \sa members(), metaType(), isMetaType().
 */
QJSManagedValue QJSManagedValue::jsMetaInstantiate(const QJSValueList &values) const
{
    if (!d)
        return {};

    if (QV4::InternalClass *c = d->as<QV4::InternalClass>()) {
        QV4::ExecutionEngine *engine = c->engine();
        QJSManagedValue result(engine);
        *result.d = c->engine()->newObject(c->d());
        QV4::Object *o = result.d->as<QV4::Object>();

        for (uint i = 0, end = qMin(qsizetype(c->d()->size), values.size()); i < end; ++i) {
            const QJSValue &arg = values[i];
            if (Q_UNLIKELY(!QJSValuePrivate::checkEngine(engine, arg))) {
                qWarning("QJSManagedValue::instantiate() failed: "
                         "Argument was created in different engine.");
                return QJSManagedValue();
            }
            o->setProperty(i, QJSValuePrivate::convertToReturnedValue(engine, arg));
        }

        return result;
    }

    return {};
}

QJSManagedValue::QJSManagedValue(QV4::ExecutionEngine *engine) :
    d(engine->memoryManager->m_persistentValues->allocate())
{
}

QT_END_NAMESPACE