aboutsummaryrefslogtreecommitdiffstats
path: root/qmljs_runtime.cpp
blob: 65ca1ae04cd135a85a526b38bdf2db57b132b5cf (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
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
/****************************************************************************
**
** 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$
**
****************************************************************************/


#include "qmljs_runtime.h"
#include "qmljs_objects.h"
#include "qv4ir_p.h"
#include "qv4ecmaobjects_p.h"

#include <QtCore/qmath.h>
#include <QtCore/qnumeric.h>
#include <QtCore/QDebug>
#include <cstdio>
#include <cassert>
#include <typeinfo>

namespace QQmlJS {
namespace VM {

QString numberToString(double num, int radix = 10)
{
    if (qIsNaN(num)) {
        return QStringLiteral("NaN");
    } else if (qIsInf(num)) {
        return QLatin1String(num < 0 ? "-Infinity" : "Infinity");
    }

    if (radix == 10)
        return QString::number(num, 'g', 16);

    QString str;
    bool negative = false;

    if (num < 0) {
        negative = true;
        num = -num;
    }

    double frac = num - ::floor(num);
    num = Value::toInteger(num);

    do {
        char c = (char)::fmod(num, radix);
        c = (c < 10) ? (c + '0') : (c - 10 + 'a');
        str.prepend(QLatin1Char(c));
        num = ::floor(num / radix);
    } while (num != 0);

    if (frac != 0) {
        str.append(QLatin1Char('.'));
        do {
            frac = frac * radix;
            char c = (char)::floor(frac);
            c = (c < 10) ? (c + '0') : (c - 10 + 'a');
            str.append(QLatin1Char(c));
            frac = frac - ::floor(frac);
        } while (frac != 0);
    }

    if (negative)
        str.prepend(QLatin1Char('-'));

    return str;
}


Value Value::fromString(Context *ctx, const QString &s)
{
    return fromString(ctx->engine->newString(s));
}

int Value::toInt32(double number)
{
    if (! number || std::isnan(number) || std::isinf(number))
        return +0;
    return (int) trunc(number); // ###
}

unsigned int Value::toUInt32(double number)
{
    if (! number || std::isnan(number) || std::isinf(number))
        return +0;
    return (uint) trunc(number); // ###
}

int Value::toInteger(double number)
{
    if (std::isnan(number))
        return +0;
    else if (! number || std::isinf(number))
        return number;
    const double v = floor(fabs(number));
    return std::signbit(number) ? -v : v;
}

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

int Value::toInt32(Context *ctx)
{
    return __qmljs_to_int32(*this, ctx);
}

unsigned int Value::toUInt32(Context *ctx)
{
    return __qmljs_to_uint32(*this, ctx);
}

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

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

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

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

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

bool Value::isFunctionObject() const
{
    return isObject() ? objectValue()->asFunctionObject() != 0 : false;
}

bool Value::isBooleanObject() const
{
    return isObject() ? objectValue()->asBooleanObject() != 0 : false;
}

bool Value::isNumberObject() const
{
    return isObject() ? objectValue()->asNumberObject() != 0 : false;
}

bool Value::isStringObject() const
{
    return isObject() ? objectValue()->asStringObject() != 0 : false;
}

bool Value::isDateObject() const
{
    return isObject() ? objectValue()->asDateObject() != 0 : false;
}

bool Value::isArrayObject() const
{
    return isObject() ? objectValue()->asArrayObject() != 0 : false;
}

bool Value::isErrorObject() const
{
    return isObject() ? objectValue()->asErrorObject() != 0 : false;
}

bool Value::isArgumentsObject() const
{
    return isObject() ? objectValue()->asActivationObject() != 0 : false;
}

Object *Value::asObject() const
{
    return isObject() ? objectValue() : 0;
}

FunctionObject *Value::asFunctionObject() const
{
    return isObject() ? objectValue()->asFunctionObject() : 0;
}

BooleanObject *Value::asBooleanObject() const
{
    return isObject() ? objectValue()->asBooleanObject() : 0;
}

NumberObject *Value::asNumberObject() const
{
    return isObject() ? objectValue()->asNumberObject() : 0;
}

StringObject *Value::asStringObject() const
{
    return isObject() ? objectValue()->asStringObject() : 0;
}

DateObject *Value::asDateObject() const
{
    return isObject() ? objectValue()->asDateObject() : 0;
}

ArrayObject *Value::asArrayObject() const
{
    return isObject() ? objectValue()->asArrayObject() : 0;
}

ErrorObject *Value::asErrorObject() const
{
    return isObject() ? objectValue()->asErrorObject() : 0;
}

ActivationObject *Value::asArgumentsObject() const
{
    return isObject() ? objectValue()->asActivationObject() : 0;
}

Value Value::property(Context *ctx, String *name) const
{
    return isObject() ? objectValue()->getProperty(ctx, name) : undefinedValue();
}

Value *Value::getPropertyDescriptor(Context *ctx, String *name) const
{
    return isObject() ? objectValue()->getPropertyDescriptor(ctx, name) : 0;
}

void Context::init(ExecutionEngine *eng)
{
    engine = eng;
    parent = 0;
    arguments = 0;
    argumentCount = 0;
    locals = 0;
    activation = Value::nullValue();
    thisObject = Value::nullValue();
    result = Value::undefinedValue();
    formals = 0;
    formalCount = 0;
    vars = 0;
    varCount = 0;
    calledAsConstructor = false;
    hasUncaughtException = false;
}

Value *Context::lookupPropertyDescriptor(String *name)
{
    for (Context *ctx = this; ctx; ctx = ctx->parent) {
        if (ctx->activation.is(Value::Object_Type)) {
            if (Value *prop = ctx->activation.objectValue()->getPropertyDescriptor(this, name)) {
                return prop;
            }
        }
    }
    return 0;
}

void Context::throwError(const Value &value)
{
    result = value;
    hasUncaughtException = true;
}

void Context::throwError(const QString &message)
{
    Value v = Value::fromString(this, message);
    throwError(Value::fromObject(engine->newErrorObject(v)));
}

void Context::throwTypeError()
{
    Value v = Value::fromString(this, QStringLiteral("Type error"));
    throwError(Value::fromObject(engine->newErrorObject(v)));
}

void Context::throwUnimplemented(const QString &message)
{
    Value v = Value::fromString(this, QStringLiteral("Unimplemented ") + message);
    throwError(Value::fromObject(engine->newErrorObject(v)));
}

void Context::throwReferenceError(const Value &value)
{
    String *s = value.toString(this);
    QString msg = s->toQString() + QStringLiteral(" is not defined");
    throwError(Value::fromObject(engine->newErrorObject(Value::fromString(this, msg))));
}

void Context::initCallContext(ExecutionEngine *e, const Value *object, FunctionObject *f, Value *args, unsigned argc)
{
    engine = e;
    parent = f->scope;
    result = Value::undefinedValue();

    if (f->needsActivation)
        activation = __qmljs_init_object(engine->newActivationObject(this));
    else
        activation = Value::nullValue();

    if (object)
        thisObject = *object;
    else
        thisObject = Value::nullValue();

    formals = f->formalParameterList;
    formalCount = f->formalParameterCount;
    arguments = args;
    argumentCount = argc;
    if (f->needsActivation || argc < formalCount){
        arguments = new Value[qMax(argc, formalCount)];
        if (argc)
            std::copy(args, args + argc, arguments);
        if (argc < formalCount)
            std::fill(arguments + argc, arguments + formalCount, Value::undefinedValue());
    }
    vars = f->varList;
    varCount = f->varCount;
    locals = varCount ? new Value[varCount] : 0;
    hasUncaughtException = false;
    calledAsConstructor = false;
    if (varCount)
        std::fill(locals, locals + varCount, Value::undefinedValue());
}

void Context::leaveCallContext(FunctionObject *f)
{
    if (! f->needsActivation) {
        delete[] locals;
        locals = 0;
    }
}

void Context::initConstructorContext(ExecutionEngine *e, const Value *object, FunctionObject *f, Value *args, unsigned argc)
{
    initCallContext(e, object, f, args, argc);
    calledAsConstructor = true;
}

void Context::leaveConstructorContext(FunctionObject *f)
{
    assert(thisObject.is(Value::Object_Type));
    result = thisObject;

    Value proto = f->getProperty(this, engine->id_prototype);
    thisObject.objectValue()->prototype = proto.objectValue();
    if (! thisObject.isObject())
        thisObject.objectValue()->prototype = engine->objectPrototype;

    leaveCallContext(f);
}

extern "C" {

Value __qmljs_init_closure(IR::Function *clos, Context *ctx)
{
    return __qmljs_init_object(ctx->engine->newScriptFunction(ctx, clos));
}

Value __qmljs_init_native_function(void (*code)(Context *), Context *ctx)
{
    return __qmljs_init_object(ctx->engine->newNativeFunction(ctx, code));
}

Value __qmljs_string_literal_undefined(Context *ctx)
{
    return Value::fromString(ctx->engine->identifier(QStringLiteral("undefined")));
}

Value __qmljs_string_literal_null(Context *ctx)
{
    return Value::fromString(ctx->engine->identifier(QStringLiteral("null")));
}

Value __qmljs_string_literal_true(Context *ctx)
{
    return Value::fromString(ctx->engine->identifier(QStringLiteral("true")));
}

Value __qmljs_string_literal_false(Context *ctx)
{
    return Value::fromString(ctx->engine->identifier(QStringLiteral("false")));
}

Value __qmljs_string_literal_object(Context *ctx)
{
    return Value::fromString(ctx->engine->identifier(QStringLiteral("object")));
}

Value __qmljs_string_literal_boolean(Context *ctx)
{
    return Value::fromString(ctx->engine->identifier(QStringLiteral("boolean")));
}

Value __qmljs_string_literal_number(Context *ctx)
{
    return Value::fromString(ctx->engine->identifier(QStringLiteral("number")));
}

Value __qmljs_string_literal_string(Context *ctx)
{
    return Value::fromString(ctx->engine->identifier(QStringLiteral("string")));
}

Value __qmljs_string_literal_function(Context *ctx)
{
    return Value::fromString(ctx->engine->identifier(QStringLiteral("function")));
}

Value __qmljs_delete_subscript(Context *ctx, Value base, Value index)
{
    if (ArrayObject *a = base.asArrayObject()) {
        int n = -1;
        if (index.isInteger())
            n = index.integerValue();
        else if (index.isDouble())
            n = index.doubleValue();
        if (n >= 0) {
            if (n < a->value.size()) {
                a->value.assign(n, Value::undefinedValue());
                return Value::fromBoolean(true);
            }
        }
    }

    String *name = index.toString(ctx);
    return __qmljs_delete_member(ctx, base, name);
}

Value __qmljs_delete_member(Context *ctx, Value base, String *name)
{
    Value obj = base.toObject(ctx);
    return Value::fromBoolean(obj.objectValue()->deleteProperty(ctx, name, true));
}

Value __qmljs_delete_property(Context *ctx, String *name)
{
    Value obj = ctx->activation;
    if (! obj.isObject())
        obj = ctx->engine->globalObject;
    return Value::fromBoolean(obj.objectValue()->deleteProperty(ctx, name, true));
}

Value __qmljs_delete_value(Context *ctx, Value value)
{
    Q_UNUSED(value);
    return __qmljs_throw_type_error(ctx); // ### throw syntax error
}

Value __qmljs_add_helper(const Value left, const Value right, Context *ctx)
{
    Value pleft = __qmljs_to_primitive(left, ctx, PREFERREDTYPE_HINT);
    Value pright = __qmljs_to_primitive(right, ctx, PREFERREDTYPE_HINT);
    if (pleft.isString() || pright.isString()) {
        if (!pleft.isString())
            pleft = __qmljs_to_string(pleft, ctx);
        if (!pright.isString())
            pright = __qmljs_to_string(pright, ctx);
        String *string = __qmljs_string_concat(ctx, pleft.stringValue(), pright.stringValue());
        return Value::fromString(string);
    }
    double x = __qmljs_to_number(pleft, ctx);
    double y = __qmljs_to_number(pright, ctx);
    return Value::fromDouble(x + y);
}

Value __qmljs_instanceof(const Value left, const Value right, Context *ctx)
{
    if (FunctionObject *function = right.asFunctionObject()) {
        bool r = function->hasInstance(ctx, left);
        return Value::fromBoolean(r);
    }

    return __qmljs_throw_type_error(ctx);
}

Value __qmljs_in(const Value left, const Value right, Context *ctx)
{
    if (right.isObject()) {
        Value s = __qmljs_to_string(left, ctx);
        bool r = right.objectValue()->hasProperty(ctx, s.stringValue());
        return Value::fromBoolean(r);
    } else {
        return __qmljs_throw_type_error(ctx);
    }
}

void __qmljs_inplace_bit_and_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_bit_and(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_bit_or_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_bit_or(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_bit_xor_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_bit_xor(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_add_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_add(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_sub_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_sub(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_mul_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_mul(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_div_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_div(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_mod_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_mod(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_shl_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_shl(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_shr_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_shr(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_ushr_name(const Value value, String *name, Context *ctx)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = __qmljs_ushr(*prop, value, ctx);
    else
        ctx->throwReferenceError(Value::fromString(name));
}

void __qmljs_inplace_bit_and_element(Context *ctx, Value *base, Value *index, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_bit_or_element(Context *ctx, Value *base, Value *index, Value *value)
{
    Object *obj = base->toObject(ctx).objectValue();
    if (ArrayObject *a = obj->asArrayObject()) {
        if (index->isNumber()) {
            const quint32 idx = index->toUInt32(ctx);
            Value v = a->value.at(idx);
            v = __qmljs_bit_or(v, *value, ctx);
            a->value.assign(idx, v);
            return;
        }
    }
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_bit_xor_element(Context *ctx, Value *base, Value *index, Value *value)
{
    Object *obj = base->toObject(ctx).objectValue();
    if (ArrayObject *a = obj->asArrayObject()) {
        if (index->isNumber()) {
            const quint32 idx = index->toUInt32(ctx);
            Value v = a->value.at(idx);
            v = __qmljs_bit_xor(v, *value, ctx);
            a->value.assign(idx, v);
            return;
        }
    }
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_add_element(Context *ctx, Value *base, Value *index, Value *value)
{
    Object *obj = base->toObject(ctx).objectValue();
    if (ArrayObject *a = obj->asArrayObject()) {
        if (index->isNumber()) {
            const quint32 idx = index->toUInt32(ctx);
            Value v = a->value.at(idx);
            v = __qmljs_add(v, *value, ctx);
            a->value.assign(idx, v);
            return;
        }
    }
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_sub_element(Context *ctx, Value *base, Value *index, Value *value)
{
    Object *obj = base->toObject(ctx).objectValue();
    if (ArrayObject *a = obj->asArrayObject()) {
        if (index->isNumber()) {
            const quint32 idx = index->toUInt32(ctx);
            Value v = a->value.at(idx);
            v = __qmljs_sub(v, *value, ctx);
            a->value.assign(idx, v);
            return;
        }
    }
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_mul_element(Context *ctx, Value *base, Value *index, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_div_element(Context *ctx, Value *base, Value *index, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_mod_element(Context *ctx, Value *base, Value *index, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_shl_element(Context *ctx, Value *base, Value *index, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_shr_element(Context *ctx, Value *base, Value *index, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_ushr_element(Context *ctx, Value *base, Value *index, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_bit_and_member(Context *ctx, Value *base, String *name, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_bit_or_member(Context *ctx, Value *base, String *name, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_bit_xor_member(Context *ctx, Value *base, String *name, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_add_member(Context *ctx, Value *base, String *name, Value *value)
{
    Value prop = base->objectValue()->getProperty(ctx, name);
    prop = __qmljs_add(prop, *value, ctx);
    base->objectValue()->setProperty(ctx, name, prop);
}

void __qmljs_inplace_sub_member(Context *ctx, Value *base, String *name, Value *value)
{
    Value prop = base->objectValue()->getProperty(ctx, name);
    prop = __qmljs_sub(prop, *value, ctx);
    base->objectValue()->setProperty(ctx, name, prop);
}

void __qmljs_inplace_mul_member(Context *ctx, Value *base, String *name, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_div_member(Context *ctx, Value *base, String *name, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_mod_member(Context *ctx, Value *base, String *name, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_shl_member(Context *ctx, Value *base, String *name, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_shr_member(Context *ctx, Value *base, String *name, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

void __qmljs_inplace_ushr_member(Context *ctx, Value *base, String *name, Value *value)
{
    ctx->throwUnimplemented(QLatin1String(Q_FUNC_INFO));
}

String *__qmljs_string_from_utf8(Context *ctx, const char *s)
{
    return ctx->engine->newString(QString::fromUtf8(s));
}

String *__qmljs_identifier_from_utf8(Context *ctx, const char *s)
{
    return ctx->engine->identifier(QString::fromUtf8(s));
}

int __qmljs_string_length(Context *, String *string)
{
    return string->toQString().length();
}

double __qmljs_string_to_number(Context *, String *string)
{
    const QString s = string->toQString();
    if (s.startsWith(QLatin1String("0x")) || s.startsWith(QLatin1String("0X")))
        return s.toLong(0, 16);
    bool ok = false;
    return string->toQString().toDouble(&ok); // ### TODO
}

Value __qmljs_string_from_number(Context *ctx, double number)
{
    String *string = ctx->engine->newString(numberToString(number, 10));
    return Value::fromString(string);
}

Bool __qmljs_string_compare(Context *, String *left, String *right)
{
    return left->toQString() < right->toQString();
}

Bool __qmljs_string_equal(Context *, String *left, String *right)
{
    return left == right || left->isEqualTo(right);
}

String *__qmljs_string_concat(Context *ctx, String *first, String *second)
{
    return ctx->engine->newString(first->toQString() + second->toQString());
}

Bool __qmljs_is_function(const Value value)
{
    return value.objectValue()->asFunctionObject() != 0;
}

Value __qmljs_object_default_value(Context *ctx, const Value object, int typeHint)
{
    if (typeHint == PREFERREDTYPE_HINT) {
        if (object.isDateObject())
            typeHint = STRING_HINT;
        else
            typeHint = NUMBER_HINT;
    }

    String *meth1 = ctx->engine->identifier("toString");
    String *meth2 = ctx->engine->identifier("valueOf");

    if (typeHint == NUMBER_HINT)
        qSwap(meth1, meth2);

    Object *oo = object.asObject();
    assert(oo != 0);

    Value conv = oo->getProperty(ctx, meth1);
    if (!conv.isUndefined() && conv.isFunctionObject()) {
        Value r = __qmljs_call_value(ctx, object, conv, 0, 0);
        if (r.isPrimitive())
            return r;
    }

    conv = oo->getProperty(ctx, meth2);
    if (!conv.isUndefined() && conv.isFunctionObject()) {
        Value r = __qmljs_call_value(ctx, object, conv, 0, 0);
        if (r.isPrimitive())
            return r;
    }

    return Value::undefinedValue();
}

Value __qmljs_throw_type_error(Context *ctx)
{
    ctx->throwTypeError();
    return ctx->result;
}

Value __qmljs_new_object(Context *ctx)
{
    return __qmljs_init_object(ctx->engine->newObject());
}

Value __qmljs_new_boolean_object(Context *ctx, bool boolean)
{
    Value value = Value::fromBoolean(boolean);
    return __qmljs_init_object(ctx->engine->newBooleanObject(value));
}

Value __qmljs_new_number_object(Context *ctx, double number)
{
    Value value = Value::fromDouble(number);
    return __qmljs_init_object(ctx->engine->newNumberObject(value));
}

Value __qmljs_new_string_object(Context *ctx, String *string)
{
    Value value = Value::fromString(string);
    return __qmljs_init_object(ctx->engine->newStringObject(value));
}

void __qmljs_set_property(Context *ctx, Value object, String *name, Value value)
{
    object.objectValue()->setProperty(ctx, name, value, /*flags*/ 0);
}

void __qmljs_set_property_boolean(Context *ctx, Value *object, String *name, bool b)
{
    Value value = Value::fromBoolean(b);
    object->objectValue()->setProperty(ctx, name, value, /*flag*/ 0);
}

void __qmljs_set_property_number(Context *ctx, Value *object, String *name, double number)
{
    Q_UNUSED(ctx);
    Value value = Value::fromDouble(number);
    object->objectValue()->setProperty(ctx, name, value, /*flag*/ 0);
}

void __qmljs_set_property_string(Context *ctx, Value *object, String *name, String *s)
{
    Q_UNUSED(ctx);
    Value value = Value::fromString(s);
    object->objectValue()->setProperty(ctx, name, value, /*flag*/ 0);
}

void __qmljs_set_property_closure(Context *ctx, Value *object, String *name, IR::Function *function)
{
    Value value = __qmljs_init_closure(function, ctx);
    object->objectValue()->setProperty(ctx, name, value, /*flag*/ 0);
}

Value __qmljs_get_element(Context *ctx, Value object, Value index)
{
    if (object.isString() && index.isNumber()) {
        const QString s = object.stringValue()->toQString().mid(index.toUInt32(ctx), 1);
        if (s.isNull())
            return Value::undefinedValue();
        else
            return Value::fromString(ctx, s);
    } else if (object.isArrayObject() && index.isNumber()) {
        return object.asArrayObject()->value.at(index.toUInt32(ctx));
    } else {
        String *name = index.toString(ctx);

        if (! object.isObject())
            object = __qmljs_to_object(object, ctx);

        return object.property(ctx, name);
    }
}

void __qmljs_set_element(Context *ctx, Value object, Value index, Value value)
{
    if (object.isArrayObject() && index.isNumber()) {
        object.asArrayObject()->value.assign(index.toUInt32(ctx), value);
    } else {
        String *name = index.toString(ctx);

        if (! object.isObject())
            object = __qmljs_to_object(object, ctx);

        object.objectValue()->setProperty(ctx, name, value, /*flags*/ 0);
    }
}

void __qmljs_set_element_number(Context *ctx, Value *object, Value *index, double number)
{
    Value v = Value::fromDouble(number);
    __qmljs_set_element(ctx, *object, *index, v);
}

void __qmljs_set_activation_property(Context *ctx, String *name, Value value)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        *prop = value;
    else
        ctx->engine->globalObject.objectValue()->setProperty(ctx, name, value);
}

void __qmljs_set_activation_property_boolean(Context *ctx, String *name, bool b)
{
    Value value = Value::fromBoolean(b);
    __qmljs_set_activation_property(ctx, name, value);
}

void __qmljs_set_activation_property_number(Context *ctx, String *name, double number)
{
    Value value = Value::fromDouble(number);
    __qmljs_set_activation_property(ctx, name, value);
}

void __qmljs_set_activation_property_string(Context *ctx, String *name, String *string)
{
    Value value = Value::fromString(string);
    __qmljs_set_activation_property(ctx, name, value);
}

void __qmljs_set_activation_property_closure(Context *ctx, String *name, IR::Function *clos)
{
    Value value = __qmljs_init_closure(clos, ctx);
    __qmljs_set_activation_property(ctx, name, value);
}

Value __qmljs_get_property(Context *ctx, Value object, String *name)
{
    if (object.isObject()) {
        return object.property(ctx, name);
    } else if (object.isString() && name->isEqualTo(ctx->engine->id_length)) {
        return Value::fromDouble(object.stringValue()->toQString().length());
    } else {
        object = __qmljs_to_object(object, ctx);

        if (object.isObject()) {
            return __qmljs_get_property(ctx, object, name);
        } else {
            ctx->throwTypeError(); // ### not necessary.
            return Value::undefinedValue();
        }
    }
}

Value __qmljs_get_activation_property(Context *ctx, String *name)
{
    if (Value *prop = ctx->lookupPropertyDescriptor(name))
        return *prop;
    ctx->throwReferenceError(Value::fromString(name));
    return Value::undefinedValue();
}

Value __qmljs_get_thisObject(Context *ctx)
{
    if (ctx->thisObject.isObject())
        return ctx->thisObject;
    return ctx->engine->globalObject;
}

Value __qmljs_compare(const Value x, const Value y, Context *ctx, bool leftFirst)
{
    Value px, py;

    if (leftFirst) {
        px = __qmljs_to_primitive(x, ctx, NUMBER_HINT);
        py = __qmljs_to_primitive(y, ctx, NUMBER_HINT);
    } else {
        px = __qmljs_to_primitive(x, ctx, NUMBER_HINT);
        py = __qmljs_to_primitive(y, ctx, NUMBER_HINT);
    }

    if (px.isString() && py.isString()) {
        bool r = __qmljs_string_compare(ctx, px.stringValue(), py.stringValue());
        return Value::fromBoolean(r);
    } else {
        double nx = __qmljs_to_number(px, ctx);
        double ny = __qmljs_to_number(py, ctx);
        if (std::isnan(nx) || std::isnan(ny)) {
            return Value::undefinedValue();
        } else {
            return Value::fromBoolean(nx < ny);
        }
    }
}

uint __qmljs_equal(const Value x, const Value y, Context *ctx)
{
    if (x.type() == y.type()) {
        switch (x.type()) {
        case Value::Undefined_Type:
            return true;
        case Value::Null_Type:
            return true;
        case Value::Boolean_Type:
            return x.booleanValue() == y.booleanValue();
            break;
        case Value::Integer_Type:
            return x.integerValue() == y.integerValue();
        case Value::String_Type:
            return __qmljs_string_equal(ctx, x.stringValue(), y.stringValue());
        case Value::Object_Type:
            return x.objectValue() == y.objectValue();
        default: // double
            return x.doubleValue() == y.doubleValue();
        }
        // unreachable
    } else {
        if (x.isNumber() && y.isNumber())
            return x.asDouble() == y.asDouble();
        if (x.isNull() && y.isUndefined()) {
            return true;
        } else if (x.isUndefined() && y.isNull()) {
            return true;
        } else if (x.isNumber() && y.isString()) {
            Value ny = Value::fromDouble(__qmljs_to_number(y, ctx));
            return __qmljs_equal(x, ny, ctx);
        } else if (x.isString() && y.isNumber()) {
            Value nx = Value::fromDouble(__qmljs_to_number(x, ctx));
            return __qmljs_equal(nx, y, ctx);
        } else if (x.isBoolean()) {
            Value nx = Value::fromDouble((double) x.booleanValue());
            return __qmljs_equal(nx, y, ctx);
        } else if (y.isBoolean()) {
            Value ny = Value::fromDouble((double) y.booleanValue());
            return __qmljs_equal(x, ny, ctx);
        } else if ((x.isNumber() || x.isString()) && y.isObject()) {
            Value py = __qmljs_to_primitive(y, ctx, PREFERREDTYPE_HINT);
            return __qmljs_equal(x, py, ctx);
        } else if (x.isObject() && (y.isNumber() || y.isString())) {
            Value px = __qmljs_to_primitive(x, ctx, PREFERREDTYPE_HINT);
            return __qmljs_equal(px, y, ctx);
        }
    }

    return false;
}

Value __qmljs_call_activation_property(Context *context, String *name, Value *args, int argc)
{
    Value *func = context->lookupPropertyDescriptor(name);
    if (! func) {
        context->throwReferenceError(Value::fromString(name));
        return Value::undefinedValue();
    }
    Value result = __qmljs_call_value(context, Value::undefinedValue(), *func, args, argc);
    return result;
}

Value __qmljs_call_property(Context *context, const Value base, String *name, Value *args, int argc)
{
    Value baseObject;
    Value thisObject;
    if (!base.isUndefined()) {
        baseObject = base;
        if (!baseObject.isObject())
            baseObject = __qmljs_to_object(baseObject, context);
        assert(baseObject.isObject());
        thisObject = baseObject;
    } else {
        baseObject = context->activation;
        thisObject = Value::nullValue();
    }
    Value func = baseObject.property(context, name);
    Value result;
    if (FunctionObject *f = func.asFunctionObject()) {
        Context k;
        Context *ctx = f->needsActivation ? context->engine->newContext() : &k;
        ctx->initCallContext(context->engine, &thisObject, f, args, argc);
        f->call(ctx);
        if (ctx->hasUncaughtException) {
            context->hasUncaughtException = ctx->hasUncaughtException; // propagate the exception
            context->result = ctx->result;
        }
        ctx->leaveCallContext(f);
        result = ctx->result;
    } else {
        context->throwTypeError();
    }
    return result;
}

Value __qmljs_call_value(Context *context, const Value thisObject, const Value func, Value *args, int argc)
{
    Value result;
    if (FunctionObject *f = func.asFunctionObject()) {
        Context k;
        Context *ctx = f->needsActivation ? context->engine->newContext() : &k;
        const Value *that = thisObject.isUndefined() ? 0 : &thisObject;
        ctx->initCallContext(context->engine, that, f, args, argc);
        f->call(ctx);
        if (ctx->hasUncaughtException) {
            context->hasUncaughtException = ctx->hasUncaughtException; // propagate the exception
            context->result = ctx->result;
        }
        ctx->leaveCallContext(f);
        result = ctx->result;
    } else {
        context->throwTypeError();
    }
    return result;
}

Value __qmljs_construct_activation_property(Context *context, String *name, Value *args, int argc)
{
    Value *func = context->lookupPropertyDescriptor(name);
    if (! func) {
        context->throwReferenceError(Value::fromString(name));
        return Value::undefinedValue();
    }
    return __qmljs_construct_value(context, *func, args, argc);
}

Value __qmljs_construct_value(Context *context, const Value func, Value *args, int argc)
{
    if (FunctionObject *f = func.asFunctionObject()) {
        Context k;
        Context *ctx = f->needsActivation ? context->engine->newContext() : &k;
        ctx->initConstructorContext(context->engine, 0, f, args, argc);
        f->construct(ctx);
        if (ctx->hasUncaughtException) {
            context->hasUncaughtException = ctx->hasUncaughtException; // propagate the exception
            context->result = ctx->result;
        }
        ctx->leaveConstructorContext(f);
        return ctx->result;
    }
    context->throwTypeError();
    return Value::undefinedValue();
}

Value __qmljs_construct_property(Context *context, const Value base, String *name, Value *args, int argc)
{
    Value thisObject = base;
    if (!thisObject.isObject())
        thisObject = __qmljs_to_object(base, context);

    assert(thisObject.isObject());
    Value func = thisObject.property(context, name);
    if (FunctionObject *f = func.asFunctionObject()) {
        Context k;
        Context *ctx = f->needsActivation ? context->engine->newContext() : &k;
        ctx->initConstructorContext(context->engine, 0, f, args, argc);
        ctx->calledAsConstructor = true;
        f->construct(ctx);
        if (ctx->hasUncaughtException) {
            context->hasUncaughtException = ctx->hasUncaughtException; // propagate the exception
            context->result = ctx->result;
        }
        ctx->leaveConstructorContext(f);
        return ctx->result;
    }
    context->throwTypeError();
    return Value::undefinedValue();
}

void __qmljs_throw(Value value, Context *context)
{
    context->hasUncaughtException = true;
    context->result = value;
}

Value __qmljs_rethrow(Context *context)
{
    return context->result;
}

Value __qmljs_builtin_typeof(Context *context, Value *args, int argc)
{
    Q_UNUSED(argc);
    return __qmljs_typeof(args[0], context);
}

Value __qmljs_builtin_throw(Context *context, Value *args, int argc)
{
    Q_UNUSED(argc);
    __qmljs_throw(args[0], context);
    // ### change to void return value
    return Value::undefinedValue();
}

Value __qmljs_builtin_rethrow(Context *context, Value *, int)
{
    return context->result;
}

} // extern "C"


} // namespace VM
} // namespace QQmlJS