aboutsummaryrefslogtreecommitdiffstats
path: root/qmljs_runtime.h
blob: 85ec5ea4c1a105d5d956f7ae6738c2ce7244a9eb (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
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the V4VM 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QMLJS_RUNTIME_H
#define QMLJS_RUNTIME_H

#include <QtCore/QString>
#include <QtCore/qnumeric.h>
#include <QtCore/QDebug>

#include <wtf/Platform.h>

#include <cmath>
#include <cassert>

#ifdef DO_TRACE_INSTR
#  define TRACE1(x) fprintf(stderr, "    %s\n", __FUNCTION__);
#  define TRACE2(x, y) fprintf(stderr, "    %s\n", __FUNCTION__);
#else
#  define TRACE1(x)
#  define TRACE2(x, y)
#endif // TRACE1

namespace QQmlJS {

namespace IR {
struct Function;
}

namespace VM {

enum TypeHint {
    PREFERREDTYPE_HINT,
    NUMBER_HINT,
    STRING_HINT
};

struct Object;
struct String;
struct PropertyDescriptor;
struct Context;
struct FunctionObject;
struct BooleanObject;
struct NumberObject;
struct StringObject;
struct DateObject;
struct RegExpObject;
struct ArrayObject;
struct ErrorObject;
struct ActivationObject;
struct ExecutionEngine;

typedef uint Bool;

struct Value
{
    union {
        quint64 val;
        double dbl;
        struct {
#if Q_BYTE_ORDER != Q_LITTLE_ENDIAN
            uint tag;
#endif
            union {
                uint uint_32;
                int int_32;
#if CPU(X86_64)
#else
                Object *o;
                String *s;
#endif
            };
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
            uint tag;
#endif
        };
    };

    enum Masks {
        NotDouble_Mask = 0xfff80000,
        Type_Mask = 0xffff0000,
        Immediate_Mask = NotDouble_Mask | 0x00040000,
        Tag_Shift = 32
    };
    enum ValueType {
        Undefined_Type = Immediate_Mask | 0x00000,
        Null_Type = Immediate_Mask | 0x10000,
        Boolean_Type = Immediate_Mask | 0x20000,
        Integer_Type = Immediate_Mask | 0x30000,
        Object_Type = NotDouble_Mask | 0x00000,
        String_Type = NotDouble_Mask | 0x10000
    };

    enum ImmediateFlags {
        ConvertibleToInt = Immediate_Mask | (0x1 << 15)
    };

    enum ValueTypeInternal {
        _Undefined_Type = Undefined_Type,
        _Null_Type = Null_Type | ConvertibleToInt,
        _Boolean_Type = Boolean_Type | ConvertibleToInt,
        _Integer_Type = Integer_Type | ConvertibleToInt,
        _Object_Type = Object_Type,
        _String_Type = String_Type

    };

    inline ValueType type() const {
        return (ValueType)(tag & Type_Mask);
    }

    inline bool isUndefined() const { return tag == _Undefined_Type; }
    inline bool isNull() const { return tag == _Null_Type; }
    inline bool isBoolean() const { return tag == _Boolean_Type; }
    inline bool isInteger() const { return tag == _Integer_Type; }
    inline bool isDouble() const { return (tag & NotDouble_Mask) != NotDouble_Mask; }
    inline bool isNumber() const { return tag == _Integer_Type || (tag & NotDouble_Mask) != NotDouble_Mask; }
#if CPU(X86_64)
    inline bool isString() const { return (tag & Type_Mask) == String_Type; }
    inline bool isObject() const { return (tag & Type_Mask) == Object_Type; }
#else
    inline bool isString() const { return tag == String_Type; }
    inline bool isObject() const { return tag == Object_Type; }
#endif
    inline bool isConvertibleToInt() const { return (tag & ConvertibleToInt) == ConvertibleToInt; }

    Bool booleanValue() const {
        return int_32;
    }
    double doubleValue() const {
        return dbl;
    }
    void setDouble(double d) {
        dbl = d;
    }
    double asDouble() const {
        if (tag == _Integer_Type)
            return int_32;
        return dbl;
    }
    int integerValue() const {
        return int_32;
    }

#if CPU(X86_64)
    String *stringValue() const {
        return (String *)(val & ~(quint64(Type_Mask) << Tag_Shift));
    }
    Object *objectValue() const {
        return (Object *)(val & ~(quint64(Type_Mask) << Tag_Shift));
    }
#else
    String *stringValue() const {
        return s;
    }
    Object *objectValue() const {
        return o;
    }
#endif

    quint64 rawValue() const {
        return val;
    }

    static Value undefinedValue();
    static Value nullValue();
    static Value fromBoolean(Bool b);
    static Value fromDouble(double d);
    static Value fromInt32(int i);
    static Value fromString(String *s);
    static Value fromObject(Object *o);

#ifndef QMLJS_LLVM_RUNTIME
    static Value fromString(Context *ctx, const QString &fromString);
#endif

    static double toInteger(double fromNumber);
    static int toInt32(double value);
    static unsigned int toUInt32(double value);

    inline int toUInt16(Context *ctx);
    inline int toInt32(Context *ctx);
    inline unsigned int toUInt32(Context *ctx);
    inline Bool toBoolean(Context *ctx) const;
    inline double toInteger(Context *ctx) const;
    double toNumber(Context *ctx) const;
    inline String *toString(Context *ctx) const;
    inline Value toObject(Context *ctx) const;

    inline bool isPrimitive() const { return !isObject(); }
    static inline bool integerCompatible(Value a, Value b) {
        return ((a.tag & b.tag) & ConvertibleToInt) == ConvertibleToInt;
    }
    inline bool tryIntegerConversion() {
        bool b = isConvertibleToInt();
        if (b)
            tag = _Integer_Type;
        return b;
    }

    Object *asObject() const;
    FunctionObject *asFunctionObject() const;
    BooleanObject *asBooleanObject() const;
    NumberObject *asNumberObject() const;
    StringObject *asStringObject() const;
    DateObject *asDateObject() const;
    RegExpObject *asRegExpObject() const;
    ArrayObject *asArrayObject() const;
    ErrorObject *asErrorObject() const;
    ActivationObject *asArgumentsObject() const;

    Value property(Context *ctx, String *name) const;

    // Section 9.12
    bool sameValue(Value other);
};

extern "C" {

// context
Value __qmljs_call_activation_property(Context *, String *name, Value *args, int argc);
Value __qmljs_call_property(Context *context, Value base, String *name, Value *args, int argc);
Value __qmljs_call_value(Context *context, Value thisObject, Value func, Value *args, int argc);

Value __qmljs_construct_activation_property(Context *, String *name, Value *args, int argc);
Value __qmljs_construct_property(Context *context, Value base, String *name, Value *args, int argc);
Value __qmljs_construct_value(Context *context, Value func, Value *args, int argc);

Value __qmljs_builtin_typeof(Value val, Context *context);
void __qmljs_builtin_throw(Value val, Context *context);

// constructors
Value __qmljs_init_closure(IR::Function *clos, Context *ctx);
Value __qmljs_init_native_function(void (*code)(Context *), Context *ctx);

Bool __qmljs_is_function(Value value);

// string literals
Value __qmljs_string_literal_undefined(Context *ctx);
Value __qmljs_string_literal_null(Context *ctx);
Value __qmljs_string_literal_true(Context *ctx);
Value __qmljs_string_literal_false(Context *ctx);
Value __qmljs_string_literal_object(Context *ctx);
Value __qmljs_string_literal_boolean(Context *ctx);
Value __qmljs_string_literal_number(Context *ctx);
Value __qmljs_string_literal_string(Context *ctx);
Value __qmljs_string_literal_function(Context *ctx);

// strings
String *__qmljs_string_from_utf8(Context *ctx, const char *s);
int __qmljs_string_length(Context *ctx, String *string);
double __qmljs_string_to_number(Context *ctx, String *string);
Value __qmljs_string_from_number(Context *ctx, double number);
Bool __qmljs_string_compare(Context *ctx, String *left, String *right);
Bool __qmljs_string_equal(String *left, String *right);
String *__qmljs_string_concat(Context *ctx, String *first, String *second);
String *__qmljs_identifier_from_utf8(Context *ctx, const char *s);

// objects
Value __qmljs_object_default_value(Context *ctx, Value object, int typeHint);
Value __qmljs_throw_type_error(Context *ctx);
Value __qmljs_new_object(Context *ctx);
Value __qmljs_new_boolean_object(Context *ctx, bool boolean);
Value __qmljs_new_number_object(Context *ctx, double n);
Value __qmljs_new_string_object(Context *ctx, String *string);
void __qmljs_set_activation_property(Context *ctx, String *name, Value value);
void __qmljs_set_property(Context *ctx, Value object, String *name, Value value);
Value __qmljs_get_property(Context *ctx, Value object, String *name);
Value __qmljs_get_activation_property(Context *ctx, String *name);

Value __qmljs_get_element(Context *ctx, Value object, Value index);
void __qmljs_set_element(Context *ctx, Value object, Value index, Value value);

// For each
Value __qmljs_foreach_iterator_object(Value in, Context *ctx);
Value __qmljs_foreach_next_property_name(Value foreach_iterator);

// context
Value __qmljs_get_thisObject(Context *ctx);

// type conversion and testing
Value __qmljs_to_primitive(Value value, Context *ctx, int typeHint);
Bool __qmljs_to_boolean(Value value, Context *ctx);
double __qmljs_to_number(Value value, Context *ctx);
double __qmljs_to_integer(Value value, Context *ctx);
int __qmljs_to_int32(Value value, Context *ctx);
unsigned short __qmljs_to_uint16(Value value, Context *ctx);
Value __qmljs_to_string(Value value, Context *ctx);
Value __qmljs_to_object(Value value, Context *ctx);
//uint __qmljs_check_object_coercible(Context *ctx, Value *result, Value *value);
Bool __qmljs_is_callable(Value value, Context *ctx);
Value __qmljs_default_value(Value value, Context *ctx, int typeHint);

Bool __qmljs_equal(Value x, Value y, Context *ctx);
Bool __qmljs_strict_equal(Value x, Value y);

// unary operators
Value __qmljs_uplus(Value value, Context *ctx);
Value __qmljs_uminus(Value value, Context *ctx);
Value __qmljs_compl(Value value, Context *ctx);
Value __qmljs_not(Value value, Context *ctx);

Value __qmljs_delete_subscript(Context *ctx, Value base, Value index);
Value __qmljs_delete_member(Context *ctx, Value base, String *name);
Value __qmljs_delete_property(Context *ctx, String *name);
Value __qmljs_delete_value(Context *ctx, Value value);

Value __qmljs_typeof(Value value, Context *ctx);
void __qmljs_throw(Value value, Context *context);
// actually returns a jmp_buf *
void *__qmljs_create_exception_handler(Context *context);
void __qmljs_delete_exception_handler(Context *context);
Value __qmljs_get_exception(Context *context);

// binary operators
typedef Value (*BinOp)(Value left, Value right, Context *ctx);

Value __qmljs_instanceof(Value left, Value right, Context *ctx);
Value __qmljs_in(Value left, Value right, Context *ctx);
Value __qmljs_bit_or(Value left, Value right, Context *ctx);
Value __qmljs_bit_xor(Value left, Value right, Context *ctx);
Value __qmljs_bit_and(Value left, Value right, Context *ctx);
Value __qmljs_add(Value left, Value right, Context *ctx);
Value __qmljs_sub(Value left, Value right, Context *ctx);
Value __qmljs_mul(Value left, Value right, Context *ctx);
Value __qmljs_div(Value left, Value right, Context *ctx);
Value __qmljs_mod(Value left, Value right, Context *ctx);
Value __qmljs_shl(Value left, Value right, Context *ctx);
Value __qmljs_shr(Value left, Value right, Context *ctx);
Value __qmljs_ushr(Value left, Value right, Context *ctx);
Value __qmljs_gt(Value left, Value right, Context *ctx);
Value __qmljs_lt(Value left, Value right, Context *ctx);
Value __qmljs_ge(Value left, Value right, Context *ctx);
Value __qmljs_le(Value left, Value right, Context *ctx);
Value __qmljs_eq(Value left, Value right, Context *ctx);
Value __qmljs_ne(Value left, Value right, Context *ctx);
Value __qmljs_se(Value left, Value right, Context *ctx);
Value __qmljs_sne(Value left, Value right, Context *ctx);

Value __qmljs_add_helper(Value left, Value right, Context *ctx);

void __qmljs_inplace_bit_and_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_bit_or_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_bit_xor_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_add_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_sub_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_mul_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_div_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_mod_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_shl_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_shr_name(Value value, String *name, Context *ctx);
void __qmljs_inplace_ushr_name(Value value, String *name, Context *ctx);

void __qmljs_inplace_bit_and_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_bit_or_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_bit_xor_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_add_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_sub_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_mul_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_div_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_mod_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_shl_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_shr_element(Value base, Value index, Value value, Context *ctx);
void __qmljs_inplace_ushr_element(Value base, Value index, Value value, Context *ctx);

void __qmljs_inplace_bit_and_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_bit_or_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_bit_xor_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_add_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_sub_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_mul_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_div_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_mod_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_shl_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_shr_member(Value value, Value base, String *name, Context *ctx);
void __qmljs_inplace_ushr_member(Value value, Value base, String *name, Context *ctx);

Bool __qmljs_cmp_gt(Value left, Value right, Context *ctx);
Bool __qmljs_cmp_lt(Value left, Value right, Context *ctx);
Bool __qmljs_cmp_ge(Value left, Value right, Context *ctx);
Bool __qmljs_cmp_le(Value left, Value right, Context *ctx);
Bool __qmljs_cmp_eq(Value left, Value right, Context *ctx);
Bool __qmljs_cmp_ne(Value left, Value right, Context *ctx);
Bool __qmljs_cmp_se(Value left, Value right, Context *ctx);
Bool __qmljs_cmp_sne(Value left, Value right, Context *ctx);
Bool __qmljs_cmp_instanceof(Value left, Value right, Context *ctx);
Bool __qmljs_cmp_in(Value left, Value right, Context *ctx);

} // extern "C"

inline int Value::toUInt16(Context *ctx)
{
    return __qmljs_to_uint16(*this, ctx);
}

inline int Value::toInt32(Context *ctx)
{
    if (isConvertibleToInt())
        return int_32;

    return Value::toInt32(__qmljs_to_number(*this, ctx));
}

inline unsigned int Value::toUInt32(Context *ctx)
{
    if (isConvertibleToInt())
        return (unsigned) int_32;

    return toUInt32(__qmljs_to_number(*this, ctx));
}

inline Bool Value::toBoolean(Context *ctx) const
{
    return __qmljs_to_boolean(*this, ctx);
}

inline double Value::toInteger(Context *ctx) const
{
    return __qmljs_to_integer(*this, ctx);
}

inline double Value::toNumber(Context *ctx) const
{
    return __qmljs_to_number(*this, ctx);
}

inline String *Value::toString(Context *ctx) const
{
    Value v = __qmljs_to_string(*this, ctx);
    assert(v.isString());
    return v.stringValue();
}

inline Value Value::toObject(Context *ctx) const
{
    return __qmljs_to_object(*this, ctx);
}

inline Value Value::undefinedValue()
{
    Value v;
#if CPU(X86_64)
    v.val = quint64(_Undefined_Type) << Tag_Shift;
#else
    v.tag = _Undefined_Type;
    v.int_32 = 0;
#endif
    return v;
}

inline Value Value::nullValue()
{
    Value v;
#if CPU(X86_64)
    v.val = quint64(_Null_Type) << Tag_Shift;
#else
    v.tag = _Null_Type;
    v.int_32 = 0;
#endif
    return v;
}

inline Value Value::fromBoolean(Bool b)
{
    Value v;
    v.tag = _Boolean_Type;
    v.int_32 = (bool)b;
    return v;
}

inline Value Value::fromDouble(double d)
{
    Value v;
    v.dbl = d;
    return v;
}

inline Value Value::fromInt32(int i)
{
    Value v;
    v.tag = _Integer_Type;
    v.int_32 = i;
    return v;
}

inline Value Value::fromString(String *s)
{
    Value v;
#if CPU(X86_64)
    v.val = (quint64)s;
    v.val |= quint64(_String_Type) << Tag_Shift;
#else
    v.tag = _String_Type;
    v.s = s;
#endif
    return v;
}

inline Value Value::fromObject(Object *o)
{
    Value v;
#if CPU(X86_64)
    v.val = (quint64)o;
    v.val |= quint64(_Object_Type) << Tag_Shift;
#else
    v.tag = _Object_Type;
    v.o = o;
#endif
    return v;
}

inline bool Value::sameValue(Value other) {
    if (val == other.val)
        return true;
    if (isString() && other.isString())
        return __qmljs_string_equal(stringValue(), other.stringValue());
    return false;
}

#include <qmljs_math.h>

struct Context {
    ExecutionEngine *engine;
    Context *parent;
    Value activation;
    Value thisObject;
    Value *arguments;
    unsigned int argumentCount;
    Value *locals;
    Value result;
    String **formals;
    unsigned int formalCount;
    String **vars;
    unsigned int varCount;
    int calledAsConstructor;

    PropertyDescriptor *lookupPropertyDescriptor(String *name, PropertyDescriptor *tmp);
    void inplaceBitOp(Value value, String *name, BinOp op);

    inline Value argument(unsigned int index = 0)
    {
        Value arg;
        getArgument(&arg, index);
        return arg;
    }

    inline void getArgument(Value *result, unsigned int index)
    {
        if (index < argumentCount)
            *result = arguments[index];
        else
            *result = Value::undefinedValue();
    }

    void init(ExecutionEngine *eng);

    void throwError(Value value);
    void throwTypeError();
    void throwReferenceError(Value value);

#ifndef QMLJS_LLVM_RUNTIME
    void throwError(const QString &message);
    void throwUnimplemented(const QString &message);
#endif

    void initCallContext(Context *parent, const Value *object, FunctionObject *f, Value *args, unsigned argc);
    void leaveCallContext();

    void initConstructorContext(Context *parent, Value *object, FunctionObject *f, Value *args, unsigned argc);
    void leaveConstructorContext(FunctionObject *f);
};



extern "C" {

// type conversion and testing
inline Value __qmljs_to_primitive(Value value, Context *ctx, int typeHint)
{
    if (!value.isObject())
        return value;
    return __qmljs_default_value(value, ctx, typeHint);
}

inline Bool __qmljs_to_boolean(Value value, Context *ctx)
{
    switch (value.type()) {
    case Value::Undefined_Type:
    case Value::Null_Type:
        return false;
    case Value::Boolean_Type:
    case Value::Integer_Type:
        return (bool)value.int_32;
    case Value::String_Type:
        return __qmljs_string_length(ctx, value.stringValue()) > 0;
    case Value::Object_Type:
        return true;
    default: // double
        if (! value.doubleValue() || std::isnan(value.doubleValue()))
            return false;
        return true;
    }
}

inline double __qmljs_to_number(Value value, Context *ctx)
{
    switch (value.type()) {
    case Value::Undefined_Type:
        return nan("");
    case Value::Null_Type:
        return 0;
    case Value::Boolean_Type:
        return (value.booleanValue() ? 1. : 0.);
    case Value::Integer_Type:
        return value.int_32;
    case Value::String_Type:
        return __qmljs_string_to_number(ctx, value.stringValue());
    case Value::Object_Type: {
        Value prim = __qmljs_to_primitive(value, ctx, NUMBER_HINT);
        return __qmljs_to_number(prim, ctx);
    }
    default: // double
        return value.doubleValue();
    }
}

inline double __qmljs_to_integer(Value value, Context *ctx)
{
    if (value.isConvertibleToInt())
        return value.int_32;

    return Value::toInteger(__qmljs_to_number(value, ctx));
}

inline int __qmljs_to_int32(Value value, Context *ctx)
{
    if (value.isConvertibleToInt())
        return value.int_32;

    return Value::toInt32(__qmljs_to_number(value, ctx));
}

inline unsigned short __qmljs_to_uint16(Value value, Context *ctx)
{
    if (value.isConvertibleToInt())
        return (ushort)(uint)value.integerValue();

    double number = __qmljs_to_number(value, ctx);

    double D16 = 65536.0;
    if ((number >= 0 && number < D16))
        return static_cast<ushort>(number);

    if (!std::isfinite(number))
        return +0;

    double d = ::floor(::fabs(number));
    if (std::signbit(number))
        d = -d;

    number = ::fmod(d , D16);

    if (number < 0)
        number += D16;

    return (unsigned short)number;
}

inline Value __qmljs_to_string(Value value, Context *ctx)
{
    switch (value.type()) {
    case Value::Undefined_Type:
        return __qmljs_string_literal_undefined(ctx);
        break;
    case Value::Null_Type:
        return __qmljs_string_literal_null(ctx);
        break;
    case Value::Boolean_Type:
        if (value.booleanValue())
            return __qmljs_string_literal_true(ctx);
        else
            return __qmljs_string_literal_false(ctx);
        break;
    case Value::String_Type:
        return value;
        break;
    case Value::Object_Type: {
        Value prim = __qmljs_to_primitive(value, ctx, STRING_HINT);
        if (prim.isPrimitive())
            return __qmljs_to_string(prim, ctx);
        else
            return __qmljs_throw_type_error(ctx);
        break;
    }
    case Value::Integer_Type:
        return __qmljs_string_from_number(ctx, value.int_32);
        break;
    default: // double
        return __qmljs_string_from_number(ctx, value.doubleValue());
        break;

    } // switch
}

inline Value __qmljs_to_object(Value value, Context *ctx)
{
    switch (value.type()) {
    case Value::Undefined_Type:
    case Value::Null_Type:
        return __qmljs_throw_type_error(ctx);
        break;
    case Value::Boolean_Type:
        return __qmljs_new_boolean_object(ctx, value.booleanValue());
        break;
    case Value::String_Type:
        return __qmljs_new_string_object(ctx, value.stringValue());
        break;
    case Value::Object_Type:
        return value;
        break;
    case Value::Integer_Type:
        return __qmljs_new_number_object(ctx, value.int_32);
        break;
    default: // double
        return __qmljs_new_number_object(ctx, value.doubleValue());
        break;
    }
}

/*
inline uint __qmljs_check_object_coercible(Context *ctx, Value *result, Value *value)
{
    switch (value->type()) {
    case Value::Undefined_Type:
    case Value::Null_Type:
        *result = __qmljs_throw_type_error(ctx);
        return false;
    default:
        return true;
    }
}
*/

inline Bool __qmljs_is_callable(Value value, Context * /*ctx*/)
{
    if (value.isObject())
        return __qmljs_is_function(value);
    else
        return false;
}

inline Value __qmljs_default_value(Value value, Context *ctx, int typeHint)
{
    if (value.isObject())
        return __qmljs_object_default_value(ctx, value, typeHint);
    return Value::undefinedValue();
}


// unary operators
inline Value __qmljs_typeof(Value value, Context *ctx)
{
    switch (value.type()) {
    case Value::Undefined_Type:
        return __qmljs_string_literal_undefined(ctx);
        break;
    case Value::Null_Type:
        return __qmljs_string_literal_object(ctx);
        break;
    case Value::Boolean_Type:
        return __qmljs_string_literal_boolean(ctx);
        break;
    case Value::String_Type:
        return __qmljs_string_literal_string(ctx);
        break;
    case Value::Object_Type:
        if (__qmljs_is_callable(value, ctx))
            return __qmljs_string_literal_function(ctx);
        else
            return __qmljs_string_literal_object(ctx); // ### implementation-defined
        break;
    default:
        return __qmljs_string_literal_number(ctx);
        break;
    }
}

inline Value __qmljs_uplus(Value value, Context *ctx)
{
    TRACE1(value);

    if (value.tryIntegerConversion())
        return value;

    double n = __qmljs_to_number(value, ctx);
    return Value::fromDouble(n);
}

inline Value __qmljs_uminus(Value value, Context *ctx)
{
    TRACE1(value);

    if (value.isInteger())
        return Value::fromInt32(-value.integerValue());
    double n = __qmljs_to_number(value, ctx);
    return Value::fromDouble(-n);
}

inline Value __qmljs_compl(Value value, Context *ctx)
{
    TRACE1(value);

    int n;
    if (value.isConvertibleToInt())
        n = ~value.int_32;
    else
        n = Value::toInteger(__qmljs_to_number(value, ctx));

    return Value::fromInt32(~n);
}

inline Value __qmljs_not(Value value, Context *ctx)
{
    TRACE1(value);

    bool b = __qmljs_to_boolean(value, ctx);
    return Value::fromBoolean(!b);
}

// binary operators
inline Value __qmljs_bit_or(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return Value::fromInt32(left.integerValue() | right.integerValue());

    int lval = Value::toInt32(__qmljs_to_number(left, ctx));
    int rval = Value::toInt32(__qmljs_to_number(right, ctx));
    return Value::fromInt32(lval | rval);
}

inline Value __qmljs_bit_xor(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return Value::fromInt32(left.integerValue() ^ right.integerValue());

    int lval = Value::toInt32(__qmljs_to_number(left, ctx));
    int rval = Value::toInt32(__qmljs_to_number(right, ctx));
    return Value::fromInt32(lval ^ rval);
}

inline Value __qmljs_bit_and(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return Value::fromInt32(left.integerValue() & right.integerValue());

    int lval = Value::toInt32(__qmljs_to_number(left, ctx));
    int rval = Value::toInt32(__qmljs_to_number(right, ctx));
    return Value::fromInt32(lval & rval);
}

inline Value __qmljs_add(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return add_int32(left.integerValue(), right.integerValue());

    if (left.isDouble() & right.isDouble())
        return Value::fromDouble(left.doubleValue() + right.doubleValue());

    return __qmljs_add_helper(left, right, ctx);
}

inline Value __qmljs_sub(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return sub_int32(left.integerValue(), right.integerValue());

    double lval = __qmljs_to_number(left, ctx);
    double rval = __qmljs_to_number(right, ctx);
    return Value::fromDouble(lval - rval);
}

inline Value __qmljs_mul(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return mul_int32(left.integerValue(), right.integerValue());

    double lval = __qmljs_to_number(left, ctx);
    double rval = __qmljs_to_number(right, ctx);
    return Value::fromDouble(lval * rval);
}

inline Value __qmljs_div(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    double lval = __qmljs_to_number(left, ctx);
    double rval = __qmljs_to_number(right, ctx);
    return Value::fromDouble(lval / rval);
}

inline Value __qmljs_mod(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return Value::fromInt32(left.integerValue() % right.integerValue());

    double lval = __qmljs_to_number(left, ctx);
    double rval = __qmljs_to_number(right, ctx);
    return Value::fromDouble(fmod(lval, rval));
}

// ### unsigned shl missing?

inline Value __qmljs_shl(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return Value::fromInt32(left.integerValue() << ((uint(right.integerValue()) & 0x1f)));

    int lval = Value::toInt32(__qmljs_to_number(left, ctx));
    unsigned rval = Value::toUInt32(__qmljs_to_number(right, ctx)) & 0x1f;
    return Value::fromInt32(lval << rval);
}

inline Value __qmljs_shr(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return Value::fromInt32(left.integerValue() >> ((uint(right.integerValue()) & 0x1f)));

    int lval = Value::toInt32(__qmljs_to_number(left, ctx));
    unsigned rval = Value::toUInt32(__qmljs_to_number(right, ctx)) & 0x1f;
    return Value::fromInt32(lval >> rval);
}

inline Value __qmljs_ushr(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    if (Value::integerCompatible(left, right))
        return Value::fromInt32(uint(left.integerValue()) >> ((uint(right.integerValue()) & 0x1f)));

    unsigned lval = Value::toUInt32(__qmljs_to_number(left, ctx));
    unsigned rval = Value::toUInt32(__qmljs_to_number(right, ctx)) & 0x1f;
    return Value::fromInt32(lval >> rval);
}

inline Value __qmljs_gt(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    return Value::fromBoolean(__qmljs_cmp_gt(left, right, ctx));
}

inline Value __qmljs_lt(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    return Value::fromBoolean(__qmljs_cmp_lt(left, right, ctx));
}

inline Value __qmljs_ge(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    return Value::fromBoolean(__qmljs_cmp_ge(left, right, ctx));
}

inline Value __qmljs_le(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    return Value::fromBoolean(__qmljs_cmp_le(left, right, ctx));
}

inline Value __qmljs_eq(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    return Value::fromBoolean(__qmljs_cmp_eq(left, right, ctx));
}

inline Value __qmljs_ne(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    return Value::fromBoolean(!__qmljs_cmp_eq(left, right, ctx));
}

inline Value __qmljs_se(Value left, Value right, Context *)
{
    TRACE2(left, right);

    bool r = __qmljs_strict_equal(left, right);
    return Value::fromBoolean(r);
}

inline Value __qmljs_sne(Value left, Value right, Context *)
{
    TRACE2(left, right);

    bool r = ! __qmljs_strict_equal(left, right);
    return Value::fromBoolean(r);
}

inline Bool __qmljs_cmp_gt(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    left = __qmljs_to_primitive(left, ctx, NUMBER_HINT);
    right = __qmljs_to_primitive(right, ctx, NUMBER_HINT);

    if (Value::integerCompatible(left, right))
        return left.integerValue() > right.integerValue();
    if (left.isDouble() && right.isDouble()) {
        return left.doubleValue() > right.doubleValue();
    } else if (left.isString() && right.isString()) {
        return __qmljs_string_compare(ctx, right.stringValue(), left.stringValue());
    } else {
        double l = __qmljs_to_number(left, ctx);
        double r = __qmljs_to_number(right, ctx);
        return l > r;
    }
}

inline Bool __qmljs_cmp_lt(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    left = __qmljs_to_primitive(left, ctx, NUMBER_HINT);
    right = __qmljs_to_primitive(right, ctx, NUMBER_HINT);

    if (Value::integerCompatible(left, right))
        return left.integerValue() < right.integerValue();
    if (left.isDouble() && right.isDouble()) {
        return left.doubleValue() < right.doubleValue();
    } else if (left.isString() && right.isString()) {
        return __qmljs_string_compare(ctx, left.stringValue(), right.stringValue());
    } else {
        double l = __qmljs_to_number(left, ctx);
        double r = __qmljs_to_number(right, ctx);
        return l < r;
    }
}

inline Bool __qmljs_cmp_ge(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    left = __qmljs_to_primitive(left, ctx, NUMBER_HINT);
    right = __qmljs_to_primitive(right, ctx, NUMBER_HINT);

    if (Value::integerCompatible(left, right))
        return left.integerValue() >= right.integerValue();
    if (left.isDouble() && right.isDouble()) {
        return left.doubleValue() >= right.doubleValue();
    } else if (left.isString() && right.isString()) {
        return !__qmljs_string_compare(ctx, left.stringValue(), right.stringValue());
    } else {
        double l = __qmljs_to_number(left, ctx);
        double r = __qmljs_to_number(right, ctx);
        return l >= r;
    }
}

inline Bool __qmljs_cmp_le(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    left = __qmljs_to_primitive(left, ctx, NUMBER_HINT);
    right = __qmljs_to_primitive(right, ctx, NUMBER_HINT);

    if (Value::integerCompatible(left, right))
        return left.integerValue() <= right.integerValue();
    if (left.isDouble() && right.isDouble()) {
        return left.doubleValue() <= right.doubleValue();
    } else if (left.isString() && right.isString()) {
        return !__qmljs_string_compare(ctx, right.stringValue(), left.stringValue());
    } else {
        double l = __qmljs_to_number(left, ctx);
        double r = __qmljs_to_number(right, ctx);
        return l <= r;
    }
}

inline Bool __qmljs_cmp_eq(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    // need to test for doubles first as NaN != NaN
    if (left.isDouble() && right.isDouble())
        return left.doubleValue() == right.doubleValue();
    if (left.val == right.val)
        return true;
    if (left.isString() && right.isString())
        return __qmljs_string_equal(left.stringValue(), right.stringValue());

    return __qmljs_equal(left, right, ctx);
}

inline Bool __qmljs_cmp_ne(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    return !__qmljs_cmp_eq(left, right, ctx);
}

inline Bool __qmljs_cmp_se(Value left, Value right, Context *)
{
    TRACE2(left, right);

    return __qmljs_strict_equal(left, right);
}

inline Bool __qmljs_cmp_sne(Value left, Value right, Context *)
{
    TRACE2(left, right);

    return ! __qmljs_strict_equal(left, right);
}

inline Bool __qmljs_cmp_instanceof(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    Value v = __qmljs_instanceof(left, right, ctx);
    return v.booleanValue();
}

inline uint __qmljs_cmp_in(Value left, Value right, Context *ctx)
{
    TRACE2(left, right);

    Value v = __qmljs_in(left, right, ctx);
    return v.booleanValue();
}

inline Bool __qmljs_strict_equal(Value x, Value y)
{
    TRACE2(x, y);

    if (x.isDouble() || y.isDouble())
        return x.asDouble() == y.asDouble();
    if (x.rawValue() == y.rawValue())
        return true;
    if (x.isString() && y.isString())
        return __qmljs_string_equal(x.stringValue(), y.stringValue());
    return false;
}

} // extern "C"

} // namespace VM
} // namespace QQmlJS

#endif // QMLJS_RUNTIME_H