aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4engine_p.h
blob: 8e1bd24f6be28301d8e1227b860d0576d7c7bc71 (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
// 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
#ifndef QV4ENGINE_H
#define QV4ENGINE_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <private/qintrusivelist_p.h>
#include <private/qqmldelayedcallqueue_p.h>
#include <private/qqmlrefcount_p.h>
#include <private/qv4compileddata_p.h>
#include <private/qv4context_p.h>
#include <private/qv4enginebase_p.h>
#include <private/qv4executablecompilationunit_p.h>
#include <private/qv4function_p.h>
#include <private/qv4global_p.h>
#include <private/qv4stacklimits_p.h>

#include <QtCore/qelapsedtimer.h>
#include <QtCore/qmutex.h>
#include <QtCore/qprocessordetection.h>
#include <QtCore/qset.h>

namespace WTF {
class BumpPointerAllocator;
class PageAllocation;
}

#define V4_DEFINE_EXTENSION(dataclass, datafunction) \
    static inline dataclass *datafunction(QV4::ExecutionEngine *engine) \
    { \
        static int extensionId = -1; \
        if (extensionId == -1) { \
            QV4::ExecutionEngine::registrationMutex()->lock(); \
            if (extensionId == -1) \
                extensionId = QV4::ExecutionEngine::registerExtension(); \
            QV4::ExecutionEngine::registrationMutex()->unlock(); \
        } \
        dataclass *rv = (dataclass *)engine->extensionData(extensionId); \
        if (!rv) { \
            rv = new dataclass(engine); \
            engine->setExtensionData(extensionId, rv); \
        } \
        return rv; \
    } \


QT_BEGIN_NAMESPACE

#if QT_CONFIG(qml_network)
class QNetworkAccessManager;

namespace QV4 {
struct QObjectMethod;
namespace detail {
QNetworkAccessManager *getNetworkAccessManager(ExecutionEngine *engine);
}
}
#else
namespace QV4 { struct QObjectMethod; }
#endif // qml_network

// Used to allow a QObject method take and return raw V4 handles without having to expose
// 48 in the public API.
// Use like this:
//     class MyClass : public QObject {
//         Q_OBJECT
//         ...
//         Q_INVOKABLE void myMethod(QQmlV4FunctionPtr);
//     };
// The QQmlV8Function - and consequently the arguments and return value - only remains
// valid during the call.  If the return value isn't set within myMethod(), the will return
// undefined.

class QQmlV4Function
{
public:
    int length() const { return callData->argc(); }
    QV4::ReturnedValue operator[](int idx) const { return (idx < callData->argc() ? callData->args[idx].asReturnedValue() : QV4::Encode::undefined()); }
    void setReturnValue(QV4::ReturnedValue rv) { *retVal = rv; }
    QV4::ExecutionEngine *v4engine() const { return e; }
private:
    friend struct QV4::QObjectMethod;
    QQmlV4Function();
    QQmlV4Function(const QQmlV4Function &);
    QQmlV4Function &operator=(const QQmlV4Function &);

    QQmlV4Function(QV4::CallData *callData, QV4::Value *retVal, QV4::ExecutionEngine *e)
        : callData(callData), retVal(retVal), e(e)
    {
        callData->thisObject = QV4::Encode::undefined();
    }

    QV4::CallData *callData;
    QV4::Value *retVal;
    QV4::ExecutionEngine *e;
};

class QQmlError;
class QJSEngine;
class QQmlEngine;
class QQmlContextData;
class QQmlTypeLoader;

namespace QV4 {
namespace Debugging {
class Debugger;
} // namespace Debugging
namespace Profiling {
class Profiler;
} // namespace Profiling
namespace CompiledData {
struct CompilationUnit;
}

namespace Heap {
struct Module;
};

struct Function;

namespace Promise {
class ReactionHandler;
};

struct Q_QML_EXPORT ExecutionEngine : public EngineBase
{
private:
    friend struct ExecutionContextSaver;
    friend struct ExecutionContext;
    friend struct Heap::ExecutionContext;
public:
    enum class DiskCache {
        Disabled    = 0,
        AotByteCode = 1 << 0,
        AotNative   = 1 << 1,
        QmlcRead    = 1 << 2,
        QmlcWrite   = 1 << 3,
        Aot         = AotByteCode | AotNative,
        Qmlc        = QmlcRead | QmlcWrite,
        Enabled     = Aot | Qmlc,

    };

    Q_DECLARE_FLAGS(DiskCacheOptions, DiskCache);

    ExecutableAllocator *executableAllocator;
    ExecutableAllocator *regExpAllocator;

    WTF::BumpPointerAllocator *bumperPointerAllocator; // Used by Yarr Regex engine.

    WTF::PageAllocation *jsStack;

    WTF::PageAllocation *gcStack;

    QML_NEARLY_ALWAYS_INLINE Value *jsAlloca(int nValues) {
        Value *ptr = jsStackTop;
        jsStackTop = ptr + nValues;
        return ptr;
    }

    Function *globalCode;

    QJSEngine *jsEngine() const { return publicEngine; }
    QQmlEngine *qmlEngine() const { return m_qmlEngine; }
    QJSEngine *publicEngine;

    template<typename TypeLoader = QQmlTypeLoader>
    TypeLoader *typeLoader()
    {
        if (m_qmlEngine)
            return TypeLoader::get(m_qmlEngine);
        return nullptr;
    }

    enum JSObjects {
        RootContext,
        ScriptContext,
        IntegerNull, // Has to come after the RootContext to make the context stack safe
        ObjectProto,
        SymbolProto,
        ArrayProto,
        ArrayProtoValues,
        PropertyListProto,
        StringProto,
        NumberProto,
        BooleanProto,
        DateProto,
        FunctionProto,
        GeneratorProto,
        RegExpProto,
        ErrorProto,
        EvalErrorProto,
        RangeErrorProto,
        ReferenceErrorProto,
        SyntaxErrorProto,
        TypeErrorProto,
        URIErrorProto,
        PromiseProto,
        VariantProto,
        SequenceProto,
        SharedArrayBufferProto,
        ArrayBufferProto,
        DataViewProto,
        WeakSetProto,
        SetProto,
        WeakMapProto,
        MapProto,
        IntrinsicTypedArrayProto,
        ValueTypeProto,
        SignalHandlerProto,
        IteratorProto,
        ForInIteratorProto,
        SetIteratorProto,
        MapIteratorProto,
        ArrayIteratorProto,
        StringIteratorProto,
        UrlProto,
        UrlSearchParamsProto,

        Object_Ctor,
        String_Ctor,
        Symbol_Ctor,
        Number_Ctor,
        Boolean_Ctor,
        Array_Ctor,
        Function_Ctor,
        GeneratorFunction_Ctor,
        Date_Ctor,
        RegExp_Ctor,
        Error_Ctor,
        EvalError_Ctor,
        RangeError_Ctor,
        ReferenceError_Ctor,
        SyntaxError_Ctor,
        TypeError_Ctor,
        URIError_Ctor,
        SharedArrayBuffer_Ctor,
        Promise_Ctor,
        ArrayBuffer_Ctor,
        DataView_Ctor,
        WeakSet_Ctor,
        Set_Ctor,
        WeakMap_Ctor,
        Map_Ctor,
        IntrinsicTypedArray_Ctor,
        Url_Ctor,
        UrlSearchParams_Ctor,

        GetSymbolSpecies,

        Eval_Function,
        GetStack_Function,
        ThrowerObject,
        NJSObjects
    };
    Value *jsObjects;
    enum { NTypedArrayTypes = 9 }; // == TypedArray::NValues, avoid header dependency

    ExecutionContext *rootContext() const { return reinterpret_cast<ExecutionContext *>(jsObjects + RootContext); }
    ExecutionContext *scriptContext() const { return reinterpret_cast<ExecutionContext *>(jsObjects + ScriptContext); }
    void setScriptContext(ReturnedValue c) { jsObjects[ScriptContext] = c; }
    FunctionObject *objectCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Object_Ctor); }
    FunctionObject *stringCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + String_Ctor); }
    FunctionObject *symbolCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Symbol_Ctor); }
    FunctionObject *numberCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Number_Ctor); }
    FunctionObject *booleanCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Boolean_Ctor); }
    FunctionObject *arrayCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Array_Ctor); }
    FunctionObject *functionCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Function_Ctor); }
    FunctionObject *generatorFunctionCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + GeneratorFunction_Ctor); }
    FunctionObject *dateCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Date_Ctor); }
    FunctionObject *regExpCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + RegExp_Ctor); }
    FunctionObject *errorCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Error_Ctor); }
    FunctionObject *evalErrorCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + EvalError_Ctor); }
    FunctionObject *rangeErrorCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + RangeError_Ctor); }
    FunctionObject *referenceErrorCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + ReferenceError_Ctor); }
    FunctionObject *syntaxErrorCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + SyntaxError_Ctor); }
    FunctionObject *typeErrorCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + TypeError_Ctor); }
    FunctionObject *uRIErrorCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + URIError_Ctor); }
    FunctionObject *sharedArrayBufferCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + SharedArrayBuffer_Ctor); }
    FunctionObject *promiseCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Promise_Ctor); }
    FunctionObject *arrayBufferCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + ArrayBuffer_Ctor); }
    FunctionObject *dataViewCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + DataView_Ctor); }
    FunctionObject *weakSetCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + WeakSet_Ctor); }
    FunctionObject *setCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Set_Ctor); }
    FunctionObject *weakMapCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + WeakMap_Ctor); }
    FunctionObject *mapCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Map_Ctor); }
    FunctionObject *intrinsicTypedArrayCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + IntrinsicTypedArray_Ctor); }
    FunctionObject *urlCtor() const
    {
        return reinterpret_cast<FunctionObject *>(jsObjects + Url_Ctor);
    }
    FunctionObject *urlSearchParamsCtor() const
    {
        return reinterpret_cast<FunctionObject *>(jsObjects + UrlSearchParams_Ctor);
    }
    FunctionObject *typedArrayCtors;

    FunctionObject *getSymbolSpecies() const { return reinterpret_cast<FunctionObject *>(jsObjects + GetSymbolSpecies); }

    Object *objectPrototype() const { return reinterpret_cast<Object *>(jsObjects + ObjectProto); }
    Object *symbolPrototype() const { return reinterpret_cast<Object *>(jsObjects + SymbolProto); }
    Object *arrayPrototype() const { return reinterpret_cast<Object *>(jsObjects + ArrayProto); }
    Object *arrayProtoValues() const { return reinterpret_cast<Object *>(jsObjects + ArrayProtoValues); }
    Object *propertyListPrototype() const { return reinterpret_cast<Object *>(jsObjects + PropertyListProto); }
    Object *stringPrototype() const { return reinterpret_cast<Object *>(jsObjects + StringProto); }
    Object *numberPrototype() const { return reinterpret_cast<Object *>(jsObjects + NumberProto); }
    Object *booleanPrototype() const { return reinterpret_cast<Object *>(jsObjects + BooleanProto); }
    Object *datePrototype() const { return reinterpret_cast<Object *>(jsObjects + DateProto); }
    Object *functionPrototype() const { return reinterpret_cast<Object *>(jsObjects + FunctionProto); }
    Object *generatorPrototype() const { return reinterpret_cast<Object *>(jsObjects + GeneratorProto); }
    Object *regExpPrototype() const { return reinterpret_cast<Object *>(jsObjects + RegExpProto); }
    Object *errorPrototype() const { return reinterpret_cast<Object *>(jsObjects + ErrorProto); }
    Object *evalErrorPrototype() const { return reinterpret_cast<Object *>(jsObjects + EvalErrorProto); }
    Object *rangeErrorPrototype() const { return reinterpret_cast<Object *>(jsObjects + RangeErrorProto); }
    Object *referenceErrorPrototype() const { return reinterpret_cast<Object *>(jsObjects + ReferenceErrorProto); }
    Object *syntaxErrorPrototype() const { return reinterpret_cast<Object *>(jsObjects + SyntaxErrorProto); }
    Object *typeErrorPrototype() const { return reinterpret_cast<Object *>(jsObjects + TypeErrorProto); }
    Object *uRIErrorPrototype() const { return reinterpret_cast<Object *>(jsObjects + URIErrorProto); }
    Object *promisePrototype() const { return reinterpret_cast<Object *>(jsObjects + PromiseProto); }
    Object *variantPrototype() const { return reinterpret_cast<Object *>(jsObjects + VariantProto); }
    Object *sequencePrototype() const { return reinterpret_cast<Object *>(jsObjects + SequenceProto); }

    Object *sharedArrayBufferPrototype() const { return reinterpret_cast<Object *>(jsObjects + SharedArrayBufferProto); }
    Object *arrayBufferPrototype() const { return reinterpret_cast<Object *>(jsObjects + ArrayBufferProto); }
    Object *dataViewPrototype() const { return reinterpret_cast<Object *>(jsObjects + DataViewProto); }
    Object *weakSetPrototype() const { return reinterpret_cast<Object *>(jsObjects + WeakSetProto); }
    Object *setPrototype() const { return reinterpret_cast<Object *>(jsObjects + SetProto); }
    Object *weakMapPrototype() const { return reinterpret_cast<Object *>(jsObjects + WeakMapProto); }
    Object *mapPrototype() const { return reinterpret_cast<Object *>(jsObjects + MapProto); }
    Object *intrinsicTypedArrayPrototype() const { return reinterpret_cast<Object *>(jsObjects + IntrinsicTypedArrayProto); }
    Object *typedArrayPrototype;

    Object *valueTypeWrapperPrototype() const { return reinterpret_cast<Object *>(jsObjects + ValueTypeProto); }
    Object *signalHandlerPrototype() const { return reinterpret_cast<Object *>(jsObjects + SignalHandlerProto); }
    Object *iteratorPrototype() const { return reinterpret_cast<Object *>(jsObjects + IteratorProto); }
    Object *forInIteratorPrototype() const { return reinterpret_cast<Object *>(jsObjects + ForInIteratorProto); }
    Object *setIteratorPrototype() const { return reinterpret_cast<Object *>(jsObjects + SetIteratorProto); }
    Object *mapIteratorPrototype() const { return reinterpret_cast<Object *>(jsObjects + MapIteratorProto); }
    Object *arrayIteratorPrototype() const { return reinterpret_cast<Object *>(jsObjects + ArrayIteratorProto); }
    Object *stringIteratorPrototype() const { return reinterpret_cast<Object *>(jsObjects + StringIteratorProto); }
    Object *urlPrototype() const { return reinterpret_cast<Object *>(jsObjects + UrlProto); }
    Object *urlSearchParamsPrototype() const { return reinterpret_cast<Object *>(jsObjects + UrlSearchParamsProto); }

    EvalFunction *evalFunction() const { return reinterpret_cast<EvalFunction *>(jsObjects + Eval_Function); }
    FunctionObject *getStackFunction() const { return reinterpret_cast<FunctionObject *>(jsObjects + GetStack_Function); }
    FunctionObject *thrower() const { return reinterpret_cast<FunctionObject *>(jsObjects + ThrowerObject); }

#if QT_CONFIG(qml_network)
    QNetworkAccessManager* (*networkAccessManager)(ExecutionEngine*)  = detail::getNetworkAccessManager;
#endif

    enum JSStrings {
        String_Empty,
        String_undefined,
        String_null,
        String_true,
        String_false,
        String_boolean,
        String_number,
        String_string,
        String_default,
        String_symbol,
        String_object,
        String_function,
        String_length,
        String_prototype,
        String_constructor,
        String_arguments,
        String_caller,
        String_callee,
        String_this,
        String___proto__,
        String_enumerable,
        String_configurable,
        String_writable,
        String_value,
        String_get,
        String_set,
        String_eval,
        String_uintMax,
        String_name,
        String_index,
        String_input,
        String_toString,
        String_toLocaleString,
        String_destroy,
        String_valueOf,
        String_byteLength,
        String_byteOffset,
        String_buffer,
        String_lastIndex,
        String_next,
        String_done,
        String_return,
        String_throw,
        String_global,
        String_ignoreCase,
        String_multiline,
        String_unicode,
        String_sticky,
        String_source,
        String_flags,

        NJSStrings
    };
    Value *jsStrings;

    enum JSSymbols {
        Symbol_hasInstance,
        Symbol_isConcatSpreadable,
        Symbol_iterator,
        Symbol_match,
        Symbol_replace,
        Symbol_search,
        Symbol_species,
        Symbol_split,
        Symbol_toPrimitive,
        Symbol_toStringTag,
        Symbol_unscopables,
        Symbol_revokableProxy,
        NJSSymbols
    };
    Value *jsSymbols;

    String *id_empty() const { return reinterpret_cast<String *>(jsStrings + String_Empty); }
    String *id_undefined() const { return reinterpret_cast<String *>(jsStrings + String_undefined); }
    String *id_null() const { return reinterpret_cast<String *>(jsStrings + String_null); }
    String *id_true() const { return reinterpret_cast<String *>(jsStrings + String_true); }
    String *id_false() const { return reinterpret_cast<String *>(jsStrings + String_false); }
    String *id_boolean() const { return reinterpret_cast<String *>(jsStrings + String_boolean); }
    String *id_number() const { return reinterpret_cast<String *>(jsStrings + String_number); }
    String *id_string() const { return reinterpret_cast<String *>(jsStrings + String_string); }
    String *id_default() const { return reinterpret_cast<String *>(jsStrings + String_default); }
    String *id_symbol() const { return reinterpret_cast<String *>(jsStrings + String_symbol); }
    String *id_object() const { return reinterpret_cast<String *>(jsStrings + String_object); }
    String *id_function() const { return reinterpret_cast<String *>(jsStrings + String_function); }
    String *id_length() const { return reinterpret_cast<String *>(jsStrings + String_length); }
    String *id_prototype() const { return reinterpret_cast<String *>(jsStrings + String_prototype); }
    String *id_constructor() const { return reinterpret_cast<String *>(jsStrings + String_constructor); }
    String *id_arguments() const { return reinterpret_cast<String *>(jsStrings + String_arguments); }
    String *id_caller() const { return reinterpret_cast<String *>(jsStrings + String_caller); }
    String *id_callee() const { return reinterpret_cast<String *>(jsStrings + String_callee); }
    String *id_this() const { return reinterpret_cast<String *>(jsStrings + String_this); }
    String *id___proto__() const { return reinterpret_cast<String *>(jsStrings + String___proto__); }
    String *id_enumerable() const { return reinterpret_cast<String *>(jsStrings + String_enumerable); }
    String *id_configurable() const { return reinterpret_cast<String *>(jsStrings + String_configurable); }
    String *id_writable() const { return reinterpret_cast<String *>(jsStrings + String_writable); }
    String *id_value() const { return reinterpret_cast<String *>(jsStrings + String_value); }
    String *id_get() const { return reinterpret_cast<String *>(jsStrings + String_get); }
    String *id_set() const { return reinterpret_cast<String *>(jsStrings + String_set); }
    String *id_eval() const { return reinterpret_cast<String *>(jsStrings + String_eval); }
    String *id_uintMax() const { return reinterpret_cast<String *>(jsStrings + String_uintMax); }
    String *id_name() const { return reinterpret_cast<String *>(jsStrings + String_name); }
    String *id_index() const { return reinterpret_cast<String *>(jsStrings + String_index); }
    String *id_input() const { return reinterpret_cast<String *>(jsStrings + String_input); }
    String *id_toString() const { return reinterpret_cast<String *>(jsStrings + String_toString); }
    String *id_toLocaleString() const { return reinterpret_cast<String *>(jsStrings + String_toLocaleString); }
    String *id_destroy() const { return reinterpret_cast<String *>(jsStrings + String_destroy); }
    String *id_valueOf() const { return reinterpret_cast<String *>(jsStrings + String_valueOf); }
    String *id_byteLength() const { return reinterpret_cast<String *>(jsStrings + String_byteLength); }
    String *id_byteOffset() const { return reinterpret_cast<String *>(jsStrings + String_byteOffset); }
    String *id_buffer() const { return reinterpret_cast<String *>(jsStrings + String_buffer); }
    String *id_lastIndex() const { return reinterpret_cast<String *>(jsStrings + String_lastIndex); }
    String *id_next() const { return reinterpret_cast<String *>(jsStrings + String_next); }
    String *id_done() const { return reinterpret_cast<String *>(jsStrings + String_done); }
    String *id_return() const { return reinterpret_cast<String *>(jsStrings + String_return); }
    String *id_throw() const { return reinterpret_cast<String *>(jsStrings + String_throw); }
    String *id_global() const { return reinterpret_cast<String *>(jsStrings + String_global); }
    String *id_ignoreCase() const { return reinterpret_cast<String *>(jsStrings + String_ignoreCase); }
    String *id_multiline() const { return reinterpret_cast<String *>(jsStrings + String_multiline); }
    String *id_unicode() const { return reinterpret_cast<String *>(jsStrings + String_unicode); }
    String *id_sticky() const { return reinterpret_cast<String *>(jsStrings + String_sticky); }
    String *id_source() const { return reinterpret_cast<String *>(jsStrings + String_source); }
    String *id_flags() const { return reinterpret_cast<String *>(jsStrings + String_flags); }

    Symbol *symbol_hasInstance() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_hasInstance); }
    Symbol *symbol_isConcatSpreadable() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_isConcatSpreadable); }
    Symbol *symbol_iterator() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_iterator); }
    Symbol *symbol_match() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_match); }
    Symbol *symbol_replace() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_replace); }
    Symbol *symbol_search() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_search); }
    Symbol *symbol_species() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_species); }
    Symbol *symbol_split() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_split); }
    Symbol *symbol_toPrimitive() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_toPrimitive); }
    Symbol *symbol_toStringTag() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_toStringTag); }
    Symbol *symbol_unscopables() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_unscopables); }
    Symbol *symbol_revokableProxy() const { return reinterpret_cast<Symbol *>(jsSymbols + Symbol_revokableProxy); }

    quint32 m_engineId;

    RegExpCache *regExpCache;

    // Scarce resources are "exceptionally high cost" QVariant types where allowing the
    // normal JavaScript GC to clean them up is likely to lead to out-of-memory or other
    // out-of-resource situations.  When such a resource is passed into JavaScript we
    // add it to the scarceResources list and it is destroyed when we return from the
    // JavaScript execution that created it.  The user can prevent this behavior by
    // calling preserve() on the object which removes it from this scarceResource list.
    class ScarceResourceData {
    public:
        ScarceResourceData() = default;
        ScarceResourceData(const QMetaType type, const void *data) : data(type, data) {}
        QVariant data;
        QIntrusiveListNode node;
    };
    QIntrusiveList<ScarceResourceData, &ScarceResourceData::node> scarceResources;

    // Normally the JS wrappers for QObjects are stored in the QQmlData/QObjectPrivate,
    // but any time a QObject is wrapped a second time in another engine, we have to do
    // bookkeeping.
    MultiplyWrappedQObjectMap *m_multiplyWrappedQObjects;
#if QT_CONFIG(qml_jit)
    const bool m_canAllocateExecutableMemory;
#endif

    quintptr protoIdCount = 1;

    ExecutionEngine(QJSEngine *jsEngine = nullptr);
    ~ExecutionEngine();

#if !QT_CONFIG(qml_debug)
    QV4::Debugging::Debugger *debugger() const { return nullptr; }
    QV4::Profiling::Profiler *profiler() const { return nullptr; }

    void setDebugger(Debugging::Debugger *) {}
    void setProfiler(Profiling::Profiler *) {}
#else
    QV4::Debugging::Debugger *debugger() const { return m_debugger.data(); }
    QV4::Profiling::Profiler *profiler() const { return m_profiler.data(); }

    void setDebugger(Debugging::Debugger *debugger);
    void setProfiler(Profiling::Profiler *profiler);
#endif // QT_CONFIG(qml_debug)

    // We don't want to #include <private/qv4stackframe_p.h> here, but we still want
    // currentContext() to be inline. Therefore we shift the requirement to provide the
    // complete type of CppStackFrame to the caller by making this a template.
    template<typename StackFrame = CppStackFrame>
    ExecutionContext *currentContext() const
    {
        return static_cast<const StackFrame *>(currentStackFrame)->context();
    }

    // ensure we always get odd prototype IDs. This helps make marking in QV4::Lookup fast
    quintptr newProtoId() { return (protoIdCount += 2); }

    Heap::InternalClass *newInternalClass(const VTable *vtable, Object *prototype);

    Heap::Object *newObject();
    Heap::Object *newObject(Heap::InternalClass *internalClass);

    Heap::String *newString(char16_t c) { return newString(QChar(c)); }
    Heap::String *newString(const QString &s = QString());
    Heap::String *newIdentifier(const QString &text);

    Heap::Object *newStringObject(const String *string);
    Heap::Object *newSymbolObject(const Symbol *symbol);
    Heap::Object *newNumberObject(double value);
    Heap::Object *newBooleanObject(bool b);

    Heap::ArrayObject *newArrayObject(int count = 0);
    Heap::ArrayObject *newArrayObject(const Value *values, int length);
    Heap::ArrayObject *newArrayObject(const QStringList &list);
    Heap::ArrayObject *newArrayObject(Heap::InternalClass *ic);

    Heap::ArrayBuffer *newArrayBuffer(const QByteArray &array);
    Heap::ArrayBuffer *newArrayBuffer(size_t length);

    Heap::DateObject *newDateObject(double dateTime);
    Heap::DateObject *newDateObject(const QDateTime &dateTime);
    Heap::DateObject *newDateObject(QDate date, Heap::Object *parent, int index, uint flags);
    Heap::DateObject *newDateObject(QTime time, Heap::Object *parent, int index, uint flags);
    Heap::DateObject *newDateObject(QDateTime dateTime, Heap::Object *parent, int index, uint flags);

    Heap::RegExpObject *newRegExpObject(const QString &pattern, int flags);
    Heap::RegExpObject *newRegExpObject(RegExp *re);
#if QT_CONFIG(regularexpression)
    Heap::RegExpObject *newRegExpObject(const QRegularExpression &re);
#endif

    Heap::UrlObject *newUrlObject();
    Heap::UrlObject *newUrlObject(const QUrl &url);
    Heap::UrlSearchParamsObject *newUrlSearchParamsObject();

    Heap::Object *newErrorObject(const Value &value);
    Heap::Object *newErrorObject(const QString &message);
    Heap::Object *newSyntaxErrorObject(const QString &message, const QString &fileName, int line, int column);
    Heap::Object *newSyntaxErrorObject(const QString &message);
    Heap::Object *newReferenceErrorObject(const QString &message);
    Heap::Object *newReferenceErrorObject(const QString &message, const QString &fileName, int line, int column);
    Heap::Object *newTypeErrorObject(const QString &message);
    Heap::Object *newRangeErrorObject(const QString &message);
    Heap::Object *newURIErrorObject(const QString &message);
    Heap::Object *newURIErrorObject(const Value &message);
    Heap::Object *newEvalErrorObject(const QString &message);

    Heap::PromiseObject *newPromiseObject();
    Heap::Object *newPromiseObject(const QV4::FunctionObject *thisObject, const QV4::PromiseCapability *capability);
    Promise::ReactionHandler *getPromiseReactionHandler();

    Heap::Object *newVariantObject(const QMetaType type, const void *data);

    Heap::Object *newForInIteratorObject(Object *o);
    Heap::Object *newSetIteratorObject(Object *o);
    Heap::Object *newMapIteratorObject(Object *o);
    Heap::Object *newArrayIteratorObject(Object *o);

    static Heap::ExecutionContext *qmlContext(Heap::ExecutionContext *ctx)
    {
        Heap::ExecutionContext *outer = ctx->outer;

        if (ctx->type != Heap::ExecutionContext::Type_QmlContext && !outer)
            return nullptr;

        while (outer && outer->type != Heap::ExecutionContext::Type_GlobalContext) {
            ctx = outer;
            outer = ctx->outer;
        }

        Q_ASSERT(ctx);
        if (ctx->type != Heap::ExecutionContext::Type_QmlContext)
            return nullptr;

        return ctx;
    }

    Heap::QmlContext *qmlContext() const;
    QObject *qmlScopeObject() const;
    QQmlRefPointer<QQmlContextData> callingQmlContext() const;


    StackTrace stackTrace(int frameLimit = -1) const;
    QUrl resolvedUrl(const QString &file);

    void markObjects(MarkStack *markStack);

    void initRootContext();

    Heap::InternalClass *newClass(Heap::InternalClass *other);

    StackTrace exceptionStackTrace;

    ReturnedValue throwError(const Value &value);
    ReturnedValue catchException(StackTrace *trace = nullptr);

    ReturnedValue throwError(const QString &message);
    ReturnedValue throwSyntaxError(const QString &message);
    ReturnedValue throwSyntaxError(const QString &message, const QString &fileName, int lineNumber, int column);
    ReturnedValue throwTypeError();
    ReturnedValue throwTypeError(const QString &message);
    ReturnedValue throwReferenceError(const Value &value);
    ReturnedValue throwReferenceError(const QString &name);
    ReturnedValue throwReferenceError(const QString &value, const QString &fileName, int lineNumber, int column);
    ReturnedValue throwRangeError(const Value &value);
    ReturnedValue throwRangeError(const QString &message);
    ReturnedValue throwURIError(const Value &msg);
    ReturnedValue throwUnimplemented(const QString &message);

    // Use only inside catch(...) -- will re-throw if no JS exception
    QQmlError catchExceptionAsQmlError();

    // variant conversions
    static QVariant toVariant(
        const QV4::Value &value, QMetaType typeHint, bool createJSValueForObjectsAndSymbols = true);
    static QVariant toVariantLossy(const QV4::Value &value);
    QV4::ReturnedValue fromVariant(const QVariant &);
    QV4::ReturnedValue fromVariant(
            const QVariant &variant, Heap::Object *parent, int property, uint flags);

    static QVariantMap variantMapFromJS(const QV4::Object *o);

    static bool metaTypeFromJS(const Value &value, QMetaType type, void *data);
    QV4::ReturnedValue metaTypeToJS(QMetaType type, const void *data);

    int maxJSStackSize() const;
    int maxGCStackSize() const;

    bool checkStackLimits();
    int safeForAllocLength(qint64 len64);

    bool canJIT(Function *f = nullptr)
    {
#if QT_CONFIG(qml_jit)
        if (!m_canAllocateExecutableMemory)
            return false;
        if (f) {
            return f->kind != Function::AotCompiled
                    && !f->isGenerator()
                    && f->interpreterCallCount >= s_jitCallCountThreshold;
        }
        return true;
#else
        Q_UNUSED(f);
        return false;
#endif
    }

    QV4::ReturnedValue global();
    void initQmlGlobalObject();
    void initializeGlobal();
    void createQtObject();

    void freezeObject(const QV4::Value &value);
    void lockObject(const QV4::Value &value);

    // Return the list of illegal id names (the names of the properties on the global object)
    const QSet<QString> &illegalNames() const;

#if QT_CONFIG(qml_xml_http_request)
    void *xmlHttpRequestData() const { return m_xmlHttpRequestData; }
#endif

    void setQmlEngine(QQmlEngine *engine);

    QQmlDelayedCallQueue *delayedCallQueue() { return &m_delayedCallQueue; }

    // used for console.time(), console.timeEnd()
    void startTimer(const QString &timerName);
    qint64 stopTimer(const QString &timerName, bool *wasRunning);

    // used for console.count()
    int consoleCountHelper(const QString &file, quint16 line, quint16 column);

    struct Deletable {
        virtual ~Deletable() {}
    };

    static QMutex *registrationMutex();
    static int registerExtension();

    void setExtensionData(int, Deletable *);
    Deletable *extensionData(int index) const
    {
        if (index < m_extensionData.size())
            return m_extensionData[index];
        else
            return nullptr;
    }

    double localTZA = 0.0; // local timezone, initialized at startup

    QQmlRefPointer<ExecutableCompilationUnit> compileModule(const QUrl &url);
    QQmlRefPointer<ExecutableCompilationUnit> compileModule(
            const QUrl &url, const QString &sourceCode, const QDateTime &sourceTimeStamp);

    QQmlRefPointer<ExecutableCompilationUnit> compilationUnitForUrl(const QUrl &url) const;

    QQmlRefPointer<ExecutableCompilationUnit> executableCompilationUnit(
            QQmlRefPointer<QV4::CompiledData::CompilationUnit> &&unit);

    QQmlRefPointer<ExecutableCompilationUnit> insertCompilationUnit(
            QQmlRefPointer<QV4::CompiledData::CompilationUnit> &&unit);

    QMultiHash<QUrl, QQmlRefPointer<ExecutableCompilationUnit>> compilationUnits() const
    {
        return m_compilationUnits;
    }
    void clearCompilationUnits() { m_compilationUnits.clear(); }
    void trimCompilationUnits();

    QV4::Value *registerNativeModule(const QUrl &url, const QV4::Value &module);

    struct Module {
        QQmlRefPointer<ExecutableCompilationUnit> compiled;

        // We can pass a raw value pointer here, but nowhere else. See below.
        Value *native = nullptr;
    };

    Module moduleForUrl(const QUrl &_url, const ExecutableCompilationUnit *referrer = nullptr) const;
    Module loadModule(const QUrl &_url, const ExecutableCompilationUnit *referrer = nullptr);

    DiskCacheOptions diskCacheOptions() const;

    void callInContext(QV4::Function *function, QObject *self, QV4::ExecutionContext *ctxt,
                       int argc, void **args, QMetaType *types);
    QV4::ReturnedValue callInContext(QV4::Function *function, QObject *self,
                                     QV4::ExecutionContext *ctxt, int argc, const QV4::Value *argv);

    QV4::ReturnedValue fromData(
            QMetaType type, const void *ptr,
            Heap::Object *parent = nullptr, int property = -1, uint flags = 0);


    static void setMaxCallDepth(int maxCallDepth) { s_maxCallDepth = maxCallDepth; }
    static int maxCallDepth() { return s_maxCallDepth; }

    template<typename Value>
    static QJSPrimitiveValue createPrimitive(const Value &v)
    {
        if (v->isUndefined())
            return QJSPrimitiveValue(QJSPrimitiveUndefined());
        if (v->isNull())
            return QJSPrimitiveValue(QJSPrimitiveNull());
        if (v->isBoolean())
            return QJSPrimitiveValue(v->toBoolean());
        if (v->isInteger())
            return QJSPrimitiveValue(v->integerValue());
        if (v->isDouble())
            return QJSPrimitiveValue(v->doubleValue());
        bool ok;
        const QString result = v->toQString(&ok);
        return ok ? QJSPrimitiveValue(result) : QJSPrimitiveValue(QJSPrimitiveUndefined());
    }

private:
    template<int Frames>
    friend struct ExecutionEngineCallDepthRecorder;

    static void initializeStaticMembers();

    bool inStack(const void *current) const
    {
#if Q_STACK_GROWTH_DIRECTION > 0
        return current < cppStackLimit && current >= cppStackBase;
#else
        return current > cppStackLimit && current <= cppStackBase;
#endif
    }

    bool hasCppStackOverflow()
    {
        if (s_maxCallDepth >= 0)
            return callDepth >= s_maxCallDepth;

        if (inStack(currentStackPointer()))
            return false;

        // Double check the stack limits on failure.
        // We may have moved to a different thread.
        const StackProperties stack = stackProperties();
        cppStackBase = stack.base;
        cppStackLimit = stack.softLimit;
        return !inStack(currentStackPointer());
    }

    bool hasJsStackOverflow() const
    {
        return jsStackTop > jsStackLimit;
    }

    bool hasStackOverflow()
    {
        return hasJsStackOverflow() || hasCppStackOverflow();
    }

    static int s_maxCallDepth;
    static int s_jitCallCountThreshold;
    static int s_maxJSStackSize;
    static int s_maxGCStackSize;

#if QT_CONFIG(qml_debug)
    QScopedPointer<QV4::Debugging::Debugger> m_debugger;
    QScopedPointer<QV4::Profiling::Profiler> m_profiler;
#endif
    QSet<QString> m_illegalNames;

    // used by generated Promise objects to handle 'then' events
    QScopedPointer<QV4::Promise::ReactionHandler> m_reactionHandler;

#if QT_CONFIG(qml_xml_http_request)
    void *m_xmlHttpRequestData;
#endif

    QQmlEngine *m_qmlEngine;

    QQmlDelayedCallQueue m_delayedCallQueue;

    QElapsedTimer m_time;
    QHash<QString, qint64> m_startedTimers;

    QHash<QString, quint32> m_consoleCount;

    QVector<Deletable *> m_extensionData;

    QMultiHash<QUrl, QQmlRefPointer<ExecutableCompilationUnit>> m_compilationUnits;

    // QV4::PersistentValue would be preferred, but using QHash will create copies,
    // and QV4::PersistentValue doesn't like creating copies.
    // Instead, we allocate a raw pointer using the same manual memory management
    // technique in QV4::PersistentValue.
    QHash<QUrl, Value *> nativeModules;
};

#define CHECK_STACK_LIMITS(v4) \
    if (v4->checkStackLimits()) \
        return Encode::undefined(); \
    ExecutionEngineCallDepthRecorder _executionEngineCallDepthRecorder(v4);

template<int Frames = 1>
struct ExecutionEngineCallDepthRecorder
{
    ExecutionEngine *ee;

    ExecutionEngineCallDepthRecorder(ExecutionEngine *e): ee(e)
    {
        if (ExecutionEngine::s_maxCallDepth >= 0)
            ee->callDepth += Frames;
    }

    ~ExecutionEngineCallDepthRecorder()
    {
        if (ExecutionEngine::s_maxCallDepth >= 0)
            ee->callDepth -= Frames;
    }

    bool hasOverflow() const
    {
        return ee->hasCppStackOverflow();
    }
};

inline bool ExecutionEngine::checkStackLimits()
{
    if (Q_UNLIKELY(hasStackOverflow())) {
        throwRangeError(QStringLiteral("Maximum call stack size exceeded."));
        return true;
    }

    return false;
}

Q_DECLARE_OPERATORS_FOR_FLAGS(ExecutionEngine::DiskCacheOptions);

} // namespace QV4

QT_END_NAMESPACE

#endif // QV4ENGINE_H