aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4object.cpp
blob: fb56d623fd7457b761350c41116d75745199ad47 (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
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and 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 "qv4object_p.h"
#include "qv4jsir_p.h"
#include "qv4isel_p.h"
#include "qv4objectproto_p.h"
#include "qv4stringobject_p.h"
#include "qv4argumentsobject_p.h"
#include "qv4mm_p.h"
#include "qv4lookup_p.h"
#include "qv4scopedvalue_p.h"

#include <private/qqmljsengine_p.h>
#include <private/qqmljslexer_p.h>
#include <private/qqmljsparser_p.h>
#include <private/qqmljsast_p.h>
#include <qv4jsir_p.h>
#include <qv4codegen_p.h>
#include "private/qlocale_tools_p.h"

#include <QtCore/qmath.h>
#include <QtCore/QDebug>
#include <cassert>
#include <typeinfo>
#include <iostream>
#include <stdint.h>
#include "qv4alloca_p.h"

using namespace QV4;

DEFINE_MANAGED_VTABLE(Object);

Object::Object(ExecutionEngine *engine)
    : Managed(engine->objectClass)
    , memberDataAlloc(InlinePropertySize), memberData(inlineProperties)
{
    flags = SimpleArray;
}

Object::Object(InternalClass *ic)
    : Managed(ic)
    , memberDataAlloc(InlinePropertySize), memberData(inlineProperties)
{
    Q_ASSERT(internalClass->vtable && internalClass->vtable != &Managed::static_vtbl);
    flags = SimpleArray;

    if (internalClass->size >= memberDataAlloc) {
        memberDataAlloc = internalClass->size;
        memberData = new Property[memberDataAlloc];
    }
}

Object::~Object()
{
    if (memberData != inlineProperties)
        delete [] memberData;
    delete [] (arrayData.data - (arrayData.sparse ? 0 : arrayData.offset));
    if (arrayData.attributes)
        delete [] (arrayData.attributes - (arrayData.sparse ? 0 : arrayData.offset));
    delete arrayData.sparse;
    _data = 0;
}

bool Object::setPrototype(Object *proto)
{
    Object *pp = proto;
    while (pp) {
        if (pp == this)
            return false;
        pp = pp->prototype();
    }
    internalClass = internalClass->changePrototype(proto);
    return true;
}

void Object::destroy(Managed *that)
{
    static_cast<Object *>(that)->~Object();
}

void Object::put(ExecutionContext *ctx, const QString &name, const ValueRef value)
{
    Scope scope(ctx);
    ScopedString n(scope, ctx->engine->newString(name));
    put(n, value);
}

ReturnedValue Object::getValue(const ValueRef thisObject, const Property *p, PropertyAttributes attrs)
{
    if (!attrs.isAccessor())
        return p->value.asReturnedValue();
    FunctionObject *getter = p->getter();
    if (!getter)
        return Encode::undefined();

    Scope scope(getter->engine());
    ScopedCallData callData(scope, 0);
    callData->thisObject = *thisObject;
    return getter->call(callData);
}

void Object::putValue(Property *pd, PropertyAttributes attrs, const ValueRef value)
{
    if (internalClass->engine->hasException)
        return;

    if (attrs.isAccessor()) {
        if (pd->set) {
            Scope scope(pd->set->engine());
            ScopedCallData callData(scope, 1);
            callData->args[0] = *value;
            callData->thisObject = this;
            pd->set->call(callData);
            return;
        }
        goto reject;
    }

    if (!attrs.isWritable())
        goto reject;

    pd->value = *value;
    return;

  reject:
    if (engine()->currentContext()->strictMode)
        engine()->currentContext()->throwTypeError();
}

void Object::defineDefaultProperty(const StringRef name, ValueRef value)
{
    Property *pd = insertMember(name, Attr_Data|Attr_NotEnumerable);
    pd->value = *value;
}

void Object::defineDefaultProperty(const QString &name, ValueRef value)
{
    ExecutionEngine *e = engine();
    Scope scope(e);
    ScopedString s(scope, e->newIdentifier(name));
    defineDefaultProperty(s, value);
}

void Object::defineDefaultProperty(const QString &name, ReturnedValue (*code)(CallContext *), int argumentCount)
{
    ExecutionEngine *e = engine();
    Scope scope(e);
    ScopedString s(scope, e->newIdentifier(name));
    Scoped<FunctionObject> function(scope, e->newBuiltinFunction(e->rootContext, s, code));
    function->defineReadonlyProperty(e->id_length, Primitive::fromInt32(argumentCount));
    defineDefaultProperty(s, function);
}

void Object::defineDefaultProperty(const StringRef name, ReturnedValue (*code)(CallContext *), int argumentCount)
{
    ExecutionEngine *e = engine();
    Scope scope(e);
    Scoped<FunctionObject> function(scope, e->newBuiltinFunction(e->rootContext, name, code));
    function->defineReadonlyProperty(e->id_length, Primitive::fromInt32(argumentCount));
    defineDefaultProperty(name, function);
}

void Object::defineAccessorProperty(const QString &name, ReturnedValue (*getter)(CallContext *), ReturnedValue (*setter)(CallContext *))
{
    ExecutionEngine *e = engine();
    Scope scope(e);
    Scoped<String> s(scope, e->newIdentifier(name));
    defineAccessorProperty(s, getter, setter);
}

void Object::defineAccessorProperty(const StringRef name, ReturnedValue (*getter)(CallContext *), ReturnedValue (*setter)(CallContext *))
{
    ExecutionEngine *v4 = engine();
    Property *p = insertMember(name, QV4::Attr_Accessor|QV4::Attr_NotConfigurable|QV4::Attr_NotEnumerable);

    if (getter)
        p->setGetter(v4->newBuiltinFunction(v4->rootContext, name, getter)->getPointer());
    if (setter)
        p->setSetter(v4->newBuiltinFunction(v4->rootContext, name, setter)->getPointer());
}

void Object::defineReadonlyProperty(const QString &name, ValueRef value)
{
    QV4::ExecutionEngine *e = engine();
    Scope scope(e);
    ScopedString s(scope, e->newIdentifier(name));
    defineReadonlyProperty(s, value);
}

void Object::defineReadonlyProperty(const StringRef name, ValueRef value)
{
    Property *pd = insertMember(name, Attr_ReadOnly);
    pd->value = *value;
}

void Object::markObjects(Managed *that, ExecutionEngine *e)
{
    Object *o = static_cast<Object *>(that);

    if (!o->hasAccessorProperty) {
        for (uint i = 0; i < o->internalClass->size; ++i)
            o->memberData[i].value.mark(e);
    } else {
        for (uint i = 0; i < o->internalClass->size; ++i) {
            const Property &pd = o->memberData[i];
            if (o->internalClass->propertyData[i].isAccessor()) {
                if (pd.getter())
                    pd.getter()->mark(e);
                if (pd.setter())
                    pd.setter()->mark(e);
            } else {
                pd.value.mark(e);
            }
        }
    }
    if (o->flags & SimpleArray) {
        for (uint i = 0; i < o->arrayData.length; ++i)
            o->arrayData.data[i].value.mark(e);
        return;
    } else {
        for (uint i = 0; i < o->arrayData.length; ++i) {
            const Property &pd = o->arrayData.data[i];
            if (o->arrayData.attributes && o->arrayData.attributes[i].isAccessor()) {
                if (pd.getter())
                    pd.getter()->mark(e);
                if (pd.setter())
                    pd.setter()->mark(e);
            } else {
                pd.value.mark(e);
            }
        }
    }
}

void Object::ensureMemberIndex(uint idx)
{
    if (idx >= memberDataAlloc) {
        memberDataAlloc = qMax((uint)8, 2*memberDataAlloc);
        Property *newMemberData = new Property[memberDataAlloc];
        memcpy(newMemberData, memberData, sizeof(Property)*idx);
        memset(newMemberData + idx, 0, sizeof(Property)*(memberDataAlloc - idx));
        if (memberData != inlineProperties)
            delete [] memberData;
        memberData = newMemberData;
    }
}

Property *Object::insertMember(const StringRef s, PropertyAttributes attributes)
{
    uint idx;
    internalClass = internalClass->addMember(s.getPointer(), attributes, &idx);

    if (attributes.isAccessor())
        hasAccessorProperty = 1;

    ensureMemberIndex(idx);

    return memberData + idx;
}

// Section 8.12.1
Property *Object::__getOwnProperty__(const StringRef name, PropertyAttributes *attrs)
{
    uint idx = name->asArrayIndex();
    if (idx != UINT_MAX)
        return __getOwnProperty__(idx, attrs);

    uint member = internalClass->find(name);
    if (member < UINT_MAX) {
        if (attrs)
            *attrs = internalClass->propertyData[member];
        return memberData + member;
    }

    if (attrs)
        *attrs = Attr_Invalid;
    return 0;
}

Property *Object::__getOwnProperty__(uint index, PropertyAttributes *attrs)
{
    uint pidx = propertyIndexFromArrayIndex(index);
    if (pidx < UINT_MAX) {
        Property *p = arrayData.data + pidx;
        if (!p->value.isEmpty() && !(arrayData.attributes && arrayData.attributes[pidx].isGeneric())) {
            if (attrs)
                *attrs = arrayData.attributes ? arrayData.attributes[pidx] : PropertyAttributes(Attr_Data);
            return p;
        }
    }
    if (isStringObject()) {
        if (attrs)
            *attrs = Attr_NotConfigurable|Attr_NotWritable;
        return static_cast<StringObject *>(this)->getIndex(index);
    }

    if (attrs)
        *attrs = Attr_Invalid;
    return 0;
}

// Section 8.12.2
Property *Object::__getPropertyDescriptor__(const StringRef name, PropertyAttributes *attrs) const
{
    uint idx = name->asArrayIndex();
    if (idx != UINT_MAX)
        return __getPropertyDescriptor__(idx);


    const Object *o = this;
    while (o) {
        uint idx = o->internalClass->find(name.getPointer());
        if (idx < UINT_MAX) {
            if (attrs)
                *attrs = o->internalClass->propertyData[idx];
            return o->memberData + idx;
        }

        o = o->prototype();
    }
    if (attrs)
        *attrs = Attr_Invalid;
    return 0;
}

Property *Object::__getPropertyDescriptor__(uint index, PropertyAttributes *attrs) const
{
    const Object *o = this;
    while (o) {
        uint pidx = o->propertyIndexFromArrayIndex(index);
        if (pidx < UINT_MAX) {
            Property *p = o->arrayData.data + pidx;
            if (!p->value.isEmpty()) {
                if (attrs)
                    *attrs = o->arrayData.attributes ? o->arrayData.attributes[pidx] : PropertyAttributes(Attr_Data);
                return p;
            }
        }
        if (o->isStringObject()) {
            Property *p = static_cast<const StringObject *>(o)->getIndex(index);
            if (p) {
                if (attrs)
                    *attrs = (Attr_NotWritable|Attr_NotConfigurable);
                return p;
            }
        }
        o = o->prototype();
    }
    if (attrs)
        *attrs = Attr_Invalid;
    return 0;
}

bool Object::__hasProperty__(const StringRef name) const
{
    if (__getPropertyDescriptor__(name))
        return true;

    const Object *o = this;
    while (o) {
        if (!o->query(name).isEmpty())
            return true;
        o = o->prototype();
    }

    return false;
}

bool Object::__hasProperty__(uint index) const
{
    if (__getPropertyDescriptor__(index))
        return true;

    const Object *o = this;
    while (o) {
        if (!o->queryIndexed(index).isEmpty())
            return true;
        o = o->prototype();
    }

    return false;
}

bool Object::hasOwnProperty(const StringRef name) const
{
    return const_cast<Object *>(this)->__getOwnProperty__(name) != 0;
}

bool Object::hasOwnProperty(uint index) const
{
    return const_cast<Object *>(this)->__getOwnProperty__(index) != 0;
}

ReturnedValue Object::get(Managed *m, const StringRef name, bool *hasProperty)
{
    return static_cast<Object *>(m)->internalGet(name, hasProperty);
}

ReturnedValue Object::getIndexed(Managed *m, uint index, bool *hasProperty)
{
    return static_cast<Object *>(m)->internalGetIndexed(index, hasProperty);
}

void Object::put(Managed *m, const StringRef name, const ValueRef value)
{
    static_cast<Object *>(m)->internalPut(name, value);
}

void Object::putIndexed(Managed *m, uint index, const ValueRef value)
{
    static_cast<Object *>(m)->internalPutIndexed(index, value);
}

PropertyAttributes Object::query(const Managed *m, StringRef name)
{
    uint idx = name->asArrayIndex();
    if (idx != UINT_MAX)
        return queryIndexed(m, idx);

    const Object *o = static_cast<const Object *>(m);
    idx = o->internalClass->find(name.getPointer());
    if (idx < UINT_MAX)
        return o->internalClass->propertyData[idx];

    return Attr_Invalid;
}

PropertyAttributes Object::queryIndexed(const Managed *m, uint index)
{
    const Object *o = static_cast<const Object *>(m);
    uint pidx = o->propertyIndexFromArrayIndex(index);
    if (pidx < UINT_MAX) {
        if (o->arrayData.attributes)
            return o->arrayData.attributes[pidx];
        if (!o->arrayData.data[pidx].value.isEmpty())
            return Attr_Data;
    }
    if (o->isStringObject()) {
        Property *p = static_cast<const StringObject *>(o)->getIndex(index);
        if (p)
            return Attr_Data;
    }
    return Attr_Invalid;
}

bool Object::deleteProperty(Managed *m, const StringRef name)
{
    return static_cast<Object *>(m)->internalDeleteProperty(name);
}

bool Object::deleteIndexedProperty(Managed *m, uint index)
{
    return static_cast<Object *>(m)->internalDeleteIndexedProperty(index);
}

ReturnedValue Object::getLookup(Managed *m, Lookup *l)
{
    Object *o = static_cast<Object *>(m);
    PropertyAttributes attrs;
    Property *p = l->lookup(o, &attrs);
    if (p) {
        if (attrs.isData()) {
            if (l->level == 0)
                l->getter = Lookup::getter0;
            else if (l->level == 1)
                l->getter = Lookup::getter1;
            else if (l->level == 2)
                l->getter = Lookup::getter2;
            return p->value.asReturnedValue();
        } else {
            if (l->level == 0)
                l->getter = Lookup::getterAccessor0;
            else if (l->level == 1)
                l->getter = Lookup::getterAccessor1;
            else if (l->level == 2)
                l->getter = Lookup::getterAccessor2;
            return o->getValue(p, attrs);
        }
    }
    return Encode::undefined();
}

void Object::setLookup(Managed *m, Lookup *l, const ValueRef value)
{
    Scope scope(m->engine());
    ScopedObject o(scope, static_cast<Object *>(m));

    InternalClass *c = o->internalClass;
    uint idx = c->find(l->name);
    if (!o->isArrayObject() || idx != ArrayObject::LengthPropertyIndex) {
        if (idx != UINT_MAX && o->internalClass->propertyData[idx].isData() && o->internalClass->propertyData[idx].isWritable()) {
            l->classList[0] = o->internalClass;
            l->index = idx;
            l->setter = Lookup::setter0;
            o->memberData[idx].value = *value;
            return;
        }

        if (idx != UINT_MAX) {
            o->putValue(o->memberData + idx, o->internalClass->propertyData[idx], value);
            return;
        }
    }

    ScopedString s(scope, l->name);
    o->put(s, value);

    if (o->internalClass == c)
        return;
    idx = o->internalClass->find(l->name);
    if (idx == UINT_MAX)
        return;
    l->classList[0] = c;
    l->classList[3] = o->internalClass;
    l->index = idx;
    if (!o->prototype()) {
        l->setter = Lookup::setterInsert0;
        return;
    }
    o = o->prototype();
    l->classList[1] = o->internalClass;
    if (!o->prototype()) {
        l->setter = Lookup::setterInsert1;
        return;
    }
    o = o->prototype();
    l->classList[2] = o->internalClass;
    if (!o->prototype())
        l->setter = Lookup::setterInsert2;
}

Property *Object::advanceIterator(Managed *m, ObjectIterator *it, StringRef name, uint *index, PropertyAttributes *attrs)
{
    Object *o = static_cast<Object *>(m);
    name = (String *)0;
    *index = UINT_MAX;

    if (!it->arrayIndex)
        it->arrayNode = o->sparseArrayBegin();

    // sparse arrays
    if (it->arrayNode) {
        while (it->arrayNode != o->sparseArrayEnd()) {
            int k = it->arrayNode->key();
            uint pidx = it->arrayNode->value;
            Property *p = o->arrayData.data + pidx;
            it->arrayNode = it->arrayNode->nextNode();
            PropertyAttributes a = o->arrayData.attributes ? o->arrayData.attributes[pidx] : PropertyAttributes(Attr_Data);
            if (!(it->flags & ObjectIterator::EnumerableOnly) || a.isEnumerable()) {
                it->arrayIndex = k + 1;
                *index = k;
                if (attrs)
                    *attrs = a;
                return p;
            }
        }
        it->arrayNode = 0;
        it->arrayIndex = UINT_MAX;
    }
    // dense arrays
    while (it->arrayIndex < o->arrayData.length) {
        uint pidx = o->propertyIndexFromArrayIndex(it->arrayIndex);
        Property *p = o->arrayData.data + pidx;
        PropertyAttributes a = o->arrayData.attributes ? o->arrayData.attributes[pidx] : PropertyAttributes(Attr_Data);
        ++it->arrayIndex;
        if (!p->value.isEmpty()
            && (!(it->flags & ObjectIterator::EnumerableOnly) || a.isEnumerable())) {
            *index = it->arrayIndex - 1;
            if (attrs)
                *attrs = a;
            return p;
        }
    }

    while (it->memberIndex < o->internalClass->size) {
        String *n = o->internalClass->nameMap.at(it->memberIndex);
        assert(n);

        Property *p = o->memberData + it->memberIndex;
        PropertyAttributes a = o->internalClass->propertyData[it->memberIndex];
        ++it->memberIndex;
        if (!(it->flags & ObjectIterator::EnumerableOnly) || a.isEnumerable()) {
            name = n;
            if (attrs)
                *attrs = a;
            return p;
        }
    }

    return 0;
}

// Section 8.12.3
ReturnedValue Object::internalGet(const StringRef name, bool *hasProperty)
{
    uint idx = name->asArrayIndex();
    if (idx != UINT_MAX)
        return getIndexed(idx, hasProperty);

    name->makeIdentifier();

    Object *o = this;
    while (o) {
        uint idx = o->internalClass->find(name.getPointer());
        if (idx < UINT_MAX) {
            if (hasProperty)
                *hasProperty = true;
            return getValue(o->memberData + idx, o->internalClass->propertyData.at(idx));
        }

        o = o->prototype();
    }

    if (hasProperty)
        *hasProperty = false;
    return Encode::undefined();
}

ReturnedValue Object::internalGetIndexed(uint index, bool *hasProperty)
{
    Property *pd = 0;
    PropertyAttributes attrs = Attr_Data;
    Object *o = this;
    while (o) {
        uint pidx = o->propertyIndexFromArrayIndex(index);
        if (pidx < UINT_MAX) {
            if (!o->arrayData.data[pidx].value.isEmpty()) {
                pd = o->arrayData.data + pidx;
                if (o->arrayData.attributes)
                    attrs = o->arrayData.attributes[pidx];
                break;
            }
        }
        if (o->isStringObject()) {
            pd = static_cast<StringObject *>(o)->getIndex(index);
            if (pd) {
                attrs = (Attr_NotWritable|Attr_NotConfigurable);
                break;
            }
        }
        o = o->prototype();
    }

    if (pd) {
        if (hasProperty)
            *hasProperty = true;
        return getValue(pd, attrs);
    }

    if (hasProperty)
        *hasProperty = false;
    return Encode::undefined();
}


// Section 8.12.5
void Object::internalPut(const StringRef name, const ValueRef value)
{
    if (internalClass->engine->hasException)
        return;

    uint idx = name->asArrayIndex();
    if (idx != UINT_MAX)
        return putIndexed(idx, value);

    name->makeIdentifier();

    uint member = internalClass->find(name.getPointer());
    Property *pd = 0;
    PropertyAttributes attrs;
    if (member < UINT_MAX) {
        pd = memberData + member;
        attrs = internalClass->propertyData[member];
    }

    // clause 1
    if (pd) {
        if (attrs.isAccessor()) {
            if (pd->setter())
                goto cont;
            goto reject;
        } else if (!attrs.isWritable())
            goto reject;
        else if (isArrayObject() && name->equals(engine()->id_length)) {
            bool ok;
            uint l = value->asArrayLength(&ok);
            if (!ok) {
                engine()->currentContext()->throwRangeError(value);
                return;
            }
            ok = setArrayLength(l);
            if (!ok)
                goto reject;
        } else {
            pd->value = *value;
        }
        return;
    } else if (!prototype()) {
        if (!extensible)
            goto reject;
    } else {
        // clause 4
        if ((pd = prototype()->__getPropertyDescriptor__(name, &attrs))) {
            if (attrs.isAccessor()) {
                if (!pd->setter())
                    goto reject;
            } else if (!extensible || !attrs.isWritable()) {
                goto reject;
            }
        } else if (!extensible) {
            goto reject;
        }
    }

    cont:

    // Clause 5
    if (pd && attrs.isAccessor()) {
        assert(pd->setter() != 0);

        Scope scope(engine());
        ScopedCallData callData(scope, 1);
        callData->args[0] = *value;
        callData->thisObject = this;
        pd->setter()->call(callData);
        return;
    }

    {
        Property *p = insertMember(name, Attr_Data);
        p->value = *value;
        return;
    }

  reject:
    if (engine()->currentContext()->strictMode) {
        QString message = QStringLiteral("Cannot assign to read-only property \"");
        message += name->toQString();
        message += QLatin1Char('\"');
        engine()->currentContext()->throwTypeError(message);
    }
}

void Object::internalPutIndexed(uint index, const ValueRef value)
{
    if (internalClass->engine->hasException)
        return;

    Property *pd = 0;
    PropertyAttributes attrs;

    uint pidx = propertyIndexFromArrayIndex(index);
    if (pidx < UINT_MAX && !arrayData.data[pidx].value.isEmpty()) {
        pd = arrayData.data + pidx;
        attrs = arrayData.attributes ? arrayData.attributes[pidx] : PropertyAttributes(Attr_Data);
    }

    if (!pd && isStringObject()) {
        pd = static_cast<StringObject *>(this)->getIndex(index);
        if (pd)
            // not writable
            goto reject;
    }

    // clause 1
    if (pd) {
        if (attrs.isAccessor()) {
            if (pd->setter())
                goto cont;
            goto reject;
        } else if (!attrs.isWritable())
            goto reject;
        else
            pd->value = *value;
        return;
    } else if (!prototype()) {
        if (!extensible)
            goto reject;
    } else {
        // clause 4
        if ((pd = prototype()->__getPropertyDescriptor__(index, &attrs))) {
            if (attrs.isAccessor()) {
                if (!pd->setter())
                    goto reject;
            } else if (!extensible || !attrs.isWritable()) {
                goto reject;
            }
        } else if (!extensible) {
            goto reject;
        }
    }

    cont:

    // Clause 5
    if (pd && attrs.isAccessor()) {
        assert(pd->setter() != 0);

        Scope scope(engine());
        ScopedCallData callData(scope, 1);
        callData->args[0] = *value;
        callData->thisObject = this;
        pd->setter()->call(callData);
        return;
    }

    arraySet(index, value);
    return;

  reject:
    if (engine()->currentContext()->strictMode)
        engine()->currentContext()->throwTypeError();
}

// Section 8.12.7
bool Object::internalDeleteProperty(const StringRef name)
{
    if (internalClass->engine->hasException)
        return false;

    uint idx = name->asArrayIndex();
    if (idx != UINT_MAX)
        return deleteIndexedProperty(idx);

    name->makeIdentifier();

    uint memberIdx = internalClass->find(name);
    if (memberIdx != UINT_MAX) {
        if (internalClass->propertyData[memberIdx].isConfigurable()) {
            internalClass->removeMember(this, name->identifier);
            memmove(memberData + memberIdx, memberData + memberIdx + 1, (internalClass->size - memberIdx)*sizeof(Property));
            return true;
        }
        if (engine()->currentContext()->strictMode)
            engine()->currentContext()->throwTypeError();
        return false;
    }

    return true;
}

bool Object::internalDeleteIndexedProperty(uint index)
{
    if (internalClass->engine->hasException)
        return false;

    uint pidx = propertyIndexFromArrayIndex(index);
    if (pidx == UINT_MAX)
        return true;
    if (arrayData.data[pidx].value.isEmpty())
        return true;

    if (!arrayData.attributes || arrayData.attributes[pidx].isConfigurable()) {
        arrayData.data[pidx].value = Primitive::emptyValue();
        if (arrayData.attributes)
            arrayData.attributes[pidx].clear();
        if (arrayData.sparse) {
            arrayData.data[pidx].value.int_32 = arrayData.freeList;
            arrayData.freeList = pidx;
        }
        return true;
    }

    if (engine()->currentContext()->strictMode)
        engine()->currentContext()->throwTypeError();
    return false;
}

// Section 8.12.9
bool Object::__defineOwnProperty__(ExecutionContext *ctx, const StringRef name, const Property &p, PropertyAttributes attrs)
{
    uint idx = name->asArrayIndex();
    if (idx != UINT_MAX)
        return __defineOwnProperty__(ctx, idx, p, attrs);

    name->makeIdentifier();

    Scope scope(ctx);
    Property *current;
    PropertyAttributes *cattrs;

    if (isArrayObject() && name->equals(ctx->engine->id_length)) {
        assert(ArrayObject::LengthPropertyIndex == internalClass->find(ctx->engine->id_length));
        Property *lp = memberData + ArrayObject::LengthPropertyIndex;
        cattrs = internalClass->propertyData.constData() + ArrayObject::LengthPropertyIndex;
        if (attrs.isEmpty() || p.isSubset(attrs, *lp, *cattrs))
            return true;
        if (!cattrs->isWritable() || attrs.type() == PropertyAttributes::Accessor || attrs.isConfigurable() || attrs.isEnumerable())
            goto reject;
        bool succeeded = true;
        if (attrs.type() == PropertyAttributes::Data) {
            bool ok;
            uint l = p.value.asArrayLength(&ok);
            if (!ok) {
                ScopedValue v(scope, p.value);
                ctx->throwRangeError(v);
                return false;
            }
            succeeded = setArrayLength(l);
        }
        if (attrs.hasWritable() && !attrs.isWritable())
            cattrs->setWritable(false);
        if (!succeeded)
            goto reject;
        if (attrs.isAccessor())
            hasAccessorProperty = 1;
        return true;
    }

    // Clause 1
    {
        uint member = internalClass->find(name.getPointer());
        current = (member < UINT_MAX) ? memberData + member : 0;
        cattrs = internalClass->propertyData.constData() + member;
    }

    if (!current) {
        // clause 3
        if (!extensible)
            goto reject;
        // clause 4
        Property *pd = insertMember(name, attrs);
        *pd = p;
        pd->fullyPopulated(&attrs);
        return true;
    }

    return __defineOwnProperty__(ctx, current, name, p, attrs);
reject:
  if (ctx->strictMode)
      ctx->throwTypeError();
  return false;
}

bool Object::__defineOwnProperty__(ExecutionContext *ctx, uint index, const Property &p, PropertyAttributes attrs)
{
    // 15.4.5.1, 4b
    if (isArrayObject() && index >= arrayLength() && !internalClass->propertyData[ArrayObject::LengthPropertyIndex].isWritable())
        goto reject;

    if (ArgumentsObject::isNonStrictArgumentsObject(this))
        return static_cast<ArgumentsObject *>(this)->defineOwnProperty(ctx, index, p, attrs);

    return defineOwnProperty2(ctx, index, p, attrs);
reject:
  if (ctx->strictMode)
      ctx->throwTypeError();
  return false;
}

bool Object::defineOwnProperty2(ExecutionContext *ctx, uint index, const Property &p, PropertyAttributes attrs)
{
    Property *current = 0;

    // Clause 1
    {
        uint pidx = propertyIndexFromArrayIndex(index);
        if (pidx < UINT_MAX && !arrayData.data[pidx].value.isEmpty())
            current = arrayData.data + pidx;
        if (!current && isStringObject())
            current = static_cast<StringObject *>(this)->getIndex(index);
    }

    if (!current) {
        // clause 3
        if (!extensible)
            goto reject;
        // clause 4
        Property *pd = arrayInsert(index, attrs);
        *pd = p;
        pd->fullyPopulated(&attrs);
        return true;
    }

    return __defineOwnProperty__(ctx, current, StringRef::null(), p, attrs);
reject:
  if (ctx->strictMode)
      ctx->throwTypeError();
  return false;
}

bool Object::__defineOwnProperty__(ExecutionContext *ctx, Property *current, const StringRef member, const Property &p, PropertyAttributes attrs)
{
    // clause 5
    if (attrs.isEmpty())
        return true;

    PropertyAttributes cattrs = Attr_Data;
    if (!member.isNull())
        cattrs = internalClass->propertyData[current - memberData];
    else if (arrayData.attributes)
        cattrs = arrayData.attributes[current - arrayData.data];

    // clause 6
    if (p.isSubset(attrs, *current, cattrs))
        return true;

    // clause 7
    if (!cattrs.isConfigurable()) {
        if (attrs.isConfigurable())
            goto reject;
        if (attrs.hasEnumerable() && attrs.isEnumerable() != cattrs.isEnumerable())
            goto reject;
    }

    // clause 8
    if (attrs.isGeneric() || current->value.isEmpty())
        goto accept;

    // clause 9
    if (cattrs.isData() != attrs.isData()) {
        // 9a
        if (!cattrs.isConfigurable())
            goto reject;
        if (cattrs.isData()) {
            // 9b
            cattrs.setType(PropertyAttributes::Accessor);
            cattrs.clearWritable();
            current->setGetter(0);
            current->setSetter(0);
        } else {
            // 9c
            cattrs.setType(PropertyAttributes::Data);
            cattrs.setWritable(false);
            current->value = Primitive::undefinedValue();
        }
    } else if (cattrs.isData() && attrs.isData()) { // clause 10
        if (!cattrs.isConfigurable() && !cattrs.isWritable()) {
            if (attrs.isWritable() || !current->value.sameValue(p.value))
                goto reject;
        }
    } else { // clause 10
        assert(cattrs.isAccessor() && attrs.isAccessor());
        if (!cattrs.isConfigurable()) {
            if (p.getter() && !(current->getter() == p.getter() || (!current->getter() && (quintptr)p.getter() == 0x1)))
                goto reject;
            if (p.setter() && !(current->setter() == p.setter() || (!current->setter() && (quintptr)p.setter() == 0x1)))
                goto reject;
        }
    }

  accept:

    current->merge(cattrs, p, attrs);
    if (!member.isNull()) {
        internalClass = internalClass->changeMember(member.getPointer(), cattrs);
    } else {
        if (cattrs != Attr_Data)
            ensureArrayAttributes();
        if (arrayData.attributes)
            arrayData.attributes[current - arrayData.data] = cattrs;
    }
    if (attrs.isAccessor())
        hasAccessorProperty = 1;
    return true;
  reject:
    if (ctx->strictMode)
        ctx->throwTypeError();
    return false;
}


bool Object::__defineOwnProperty__(ExecutionContext *ctx, const QString &name, const Property &p, PropertyAttributes attrs)
{
    Scope scope(ctx);
    ScopedString s(scope, ctx->engine->newString(name));
    return __defineOwnProperty__(ctx, s, p, attrs);
}


void Object::copyArrayData(Object *other)
{
    Q_ASSERT(isArrayObject());
    Scope scope(engine());

    if (other->protoHasArray() || other->hasAccessorProperty) {
        uint len = other->arrayLength();
        Q_ASSERT(len);

        ScopedValue v(scope);
        for (uint i = 0; i < len; ++i) {
            arraySet(i, (v = other->getIndexed(i)));
        }
    } else {
        arrayReserve(other->arrayData.length);
        arrayData.length = other->arrayData.length;
        memcpy(arrayData.data, other->arrayData.data, arrayData.length*sizeof(Property));
    }

    arrayData.offset = 0;

    if (other->arrayData.sparse) {
        flags &= ~SimpleArray;
        arrayData.sparse = new SparseArray(*other->arrayData.sparse);
        arrayData.freeList = other->arrayData.freeList;
    }

    setArrayLengthUnchecked(other->arrayLength());
}


ReturnedValue Object::arrayIndexOf(const ValueRef v, uint fromIndex, uint endIndex, ExecutionContext *ctx, Object *o)
{
    Q_UNUSED(ctx);

    Scope scope(engine());
    ScopedValue value(scope);

    if (!(o->flags & SimpleArray) || o->protoHasArray()) {
        // lets be safe and slow
        for (uint i = fromIndex; i < endIndex; ++i) {
            bool exists;
            value = o->getIndexed(i, &exists);
            if (scope.hasException())
                return Encode::undefined();
            if (exists && __qmljs_strict_equal(value, v))
                return Encode(i);
        }
    } else if (arrayData.sparse) {
        for (SparseArrayNode *n = arrayData.sparse->lowerBound(fromIndex); n != arrayData.sparse->end() && n->key() < endIndex; n = n->nextNode()) {
            value = o->getValue(arrayData.data + n->value, arrayData.attributes ? arrayData.attributes[n->value] : Attr_Data);
            if (scope.hasException())
                return Encode::undefined();
            if (__qmljs_strict_equal(value, v))
                return Encode(n->key());
        }
    } else {
        if (endIndex > arrayData.length)
            endIndex = arrayData.length;
        Property *pd = arrayData.data;
        Property *end = pd + endIndex;
        pd += fromIndex;
        while (pd < end) {
            if (!pd->value.isEmpty()) {
                value = o->getValue(pd, arrayData.attributes ? arrayData.attributes[pd - arrayData.data] : Attr_Data);
                if (scope.hasException())
                    return Encode::undefined();
                if (__qmljs_strict_equal(value, v))
                    return Encode((uint)(pd - arrayData.data));
            }
            ++pd;
        }
    }
    return Encode(-1);
}

void Object::arrayConcat(const ArrayObject *other)
{
    int newLen = arrayData.length + other->arrayLength();
    if (other->arrayData.sparse)
        initSparse();
    // ### copy attributes as well!
    if (arrayData.sparse) {
        if (other->arrayData.sparse) {
            for (const SparseArrayNode *it = other->arrayData.sparse->begin(); it != other->arrayData.sparse->end(); it = it->nextNode())
                arraySet(arrayData.length + it->key(), other->arrayData.data + it->value);
        } else {
            int oldSize = arrayData.length;
            arrayReserve(oldSize + other->arrayLength());
            memcpy(arrayData.data + oldSize, other->arrayData.data, other->arrayLength()*sizeof(Property));
            if (arrayData.attributes)
                std::fill(arrayData.attributes + oldSize, arrayData.attributes + oldSize + other->arrayLength(), PropertyAttributes(Attr_Data));
            for (uint i = 0; i < other->arrayLength(); ++i) {
                SparseArrayNode *n = arrayData.sparse->insert(arrayData.length + i);
                n->value = oldSize + i;
            }
        }
    } else {
        uint oldSize = arrayLength();
        arrayReserve(oldSize + other->arrayData.length);
        if (oldSize > arrayData.length) {
            for (uint i = arrayData.length; i < oldSize; ++i)
                arrayData.data[i].value = Primitive::emptyValue();
        }
        if (other->arrayData.attributes) {
            for (uint i = 0; i < other->arrayData.length; ++i) {
                bool exists;
                arrayData.data[oldSize + i].value = const_cast<ArrayObject *>(other)->getIndexed(i, &exists);
                arrayData.length = oldSize + i + 1;
                if (arrayData.attributes)
                    arrayData.attributes[oldSize + i] = Attr_Data;
                if (!exists)
                    arrayData.data[oldSize + i].value = Primitive::emptyValue();
            }
        } else {
            arrayData.length = oldSize + other->arrayData.length;
            memcpy(arrayData.data + oldSize, other->arrayData.data, other->arrayData.length*sizeof(Property));
            if (arrayData.attributes)
                std::fill(arrayData.attributes + oldSize, arrayData.attributes + oldSize + other->arrayData.length, PropertyAttributes(Attr_Data));
        }
    }
    setArrayLengthUnchecked(newLen);
}

void Object::arraySort(ExecutionContext *context, ObjectRef thisObject, const ValueRef comparefn, uint len)
{
    if (!arrayData.length)
        return;

    if (arrayData.sparse) {
        context->throwUnimplemented(QStringLiteral("Object::sort unimplemented for sparse arrays"));
        return;
    }

    if (len > arrayData.length)
        len = arrayData.length;

    // The spec says the sorting goes through a series of get,put and delete operations.
    // this implies that the attributes don't get sorted around.
    // behavior of accessor properties is implementation defined. We simply turn them all
    // into data properties and then sort. This is in line with the sentence above.
    if (arrayData.attributes) {
        for (uint i = 0; i < len; i++) {
            if ((arrayData.attributes && arrayData.attributes[i].isGeneric()) || arrayData.data[i].value.isEmpty()) {
                while (--len > i)
                    if (!((arrayData.attributes && arrayData.attributes[len].isGeneric())|| arrayData.data[len].value.isEmpty()))
                        break;
                arrayData.data[i].value = getValue(arrayData.data + len, arrayData.attributes[len]);
                arrayData.data[len].value = Primitive::emptyValue();
                if (arrayData.attributes) {
                    arrayData.attributes[i] = Attr_Data;
                    arrayData.attributes[len].clear();
                }
            } else if (arrayData.attributes[i].isAccessor()) {
                arrayData.data[i].value = getValue(arrayData.data + i, arrayData.attributes[i]);
                arrayData.attributes[i] = Attr_Data;
            }
        }
    }

    if (!(comparefn->isUndefined() || comparefn->asObject())) {
        context->throwTypeError();
        return;
    }

    ArrayElementLessThan lessThan(context, thisObject, comparefn);

    if (!len)
        return;
    Property *begin = arrayData.data;
    std::sort(begin, begin + len, lessThan);
}


void Object::initSparse()
{
    if (!arrayData.sparse) {
        flags &= ~SimpleArray;
        arrayData.sparse = new SparseArray;
        for (uint i = 0; i < arrayData.length; ++i) {
            if (!((arrayData.attributes && arrayData.attributes[i].isGeneric()) || arrayData.data[i].value.isEmpty())) {
                SparseArrayNode *n = arrayData.sparse->insert(i);
                n->value = i + arrayData.offset;
            }
        }

        uint off = arrayData.offset;
        if (!arrayData.offset) {
            arrayData.freeList = arrayData.length;
        } else {
            arrayData.freeList = 0;
            arrayData.data -= off;
            arrayData.alloc += off;
            int o = off;
            for (int i = 0; i < o - 1; ++i) {
                arrayData.data[i].value = Primitive::fromInt32(i + 1);
            }
            arrayData.data[o - 1].value = Primitive::fromInt32(arrayData.length + off);
        }
        for (uint i = arrayData.length + off; i < arrayData.alloc; ++i) {
            arrayData.data[i].value = Primitive::fromInt32(i + 1);
        }
    }
}

void Object::arrayReserve(uint n)
{
    if (n < 8)
        n = 8;
    if (n >= arrayData.alloc) {
        uint off;
        if (arrayData.sparse) {
            assert(arrayData.freeList == arrayData.alloc);
            // ### FIXME
            arrayData.length = arrayData.alloc;
            off = 0;
        } else {
            off = arrayData.offset;
        }
        arrayData.alloc = qMax(n, 2*arrayData.alloc);
        Property *newArrayData = new Property[arrayData.alloc + off];
        if (arrayData.data) {
            memcpy(newArrayData + off, arrayData.data, sizeof(Property)*arrayData.length);
            delete [] (arrayData.data - off);
        }
        arrayData.data = newArrayData + off;
        if (arrayData.sparse) {
            for (uint i = arrayData.freeList; i < arrayData.alloc; ++i) {
                arrayData.data[i].value = Primitive::emptyValue();
                arrayData.data[i].value = Primitive::fromInt32(i + 1);
            }
        }

        if (arrayData.attributes) {
            PropertyAttributes *newAttrs = new PropertyAttributes[arrayData.alloc];
            memcpy(newAttrs, arrayData.attributes, sizeof(PropertyAttributes)*arrayData.length);
            delete [] (arrayData.attributes - off);

            arrayData.attributes = newAttrs;
            if (arrayData.sparse) {
                for (uint i = arrayData.freeList; i < arrayData.alloc; ++i)
                    arrayData.attributes[i] = Attr_Invalid;
            }
        }
    }
}

void Object::ensureArrayAttributes()
{
    if (arrayData.attributes)
        return;

    flags &= ~SimpleArray;
    uint off = arrayData.sparse ? 0 : arrayData.offset;
    arrayData.attributes = new PropertyAttributes[arrayData.alloc + off];
    arrayData.attributes += off;
    for (uint i = 0; i < arrayData.length; ++i)
        arrayData.attributes[i] = Attr_Data;
    for (uint i = arrayData.length; i < arrayData.alloc; ++i)
        arrayData.attributes[i] = Attr_Invalid;
}


bool Object::setArrayLength(uint newLen) {
    assert(isArrayObject());
    const Property *lengthProperty = memberData + ArrayObject::LengthPropertyIndex;
    if (lengthProperty && !internalClass->propertyData[ArrayObject::LengthPropertyIndex].isWritable())
        return false;
    uint oldLen = arrayLength();
    bool ok = true;
    if (newLen < oldLen) {
        if (arrayData.sparse) {
            SparseArrayNode *begin = arrayData.sparse->lowerBound(newLen);
            if (begin != arrayData.sparse->end()) {
                SparseArrayNode *it = arrayData.sparse->end()->previousNode();
                while (1) {
                    Property &pd = arrayData.data[it->value];
                    if (arrayData.attributes) {
                        if (!arrayData.attributes[it->value].isConfigurable()) {
                            ok = false;
                            newLen = it->key() + 1;
                            break;
                        } else {
                            arrayData.attributes[it->value].clear();
                        }
                    }
                    pd.value.tag = Value::Empty_Type;
                    pd.value.int_32 = arrayData.freeList;
                    arrayData.freeList = it->value;
                    bool brk = (it == begin);
                    SparseArrayNode *prev = it->previousNode();
                    arrayData.sparse->erase(it);
                    if (brk)
                        break;
                    it = prev;
                }
            }
        } else {
            Property *it = arrayData.data + arrayData.length;
            const Property *begin = arrayData.data + newLen;
            while (--it >= begin) {
                if (arrayData.attributes) {
                    if (!arrayData.attributes[it - arrayData.data].isEmpty() && !arrayData.attributes[it - arrayData.data].isConfigurable()) {
                        ok = false;
                        newLen = it - arrayData.data + 1;
                        break;
                    } else {
                        arrayData.attributes[it - arrayData.data].clear();
                    }
                    it->value = Primitive::emptyValue();
                }
            }
            arrayData.length = newLen;
        }
    } else {
        if (newLen >= 0x100000)
            initSparse();
    }
    setArrayLengthUnchecked(newLen);
    return ok;
}

DEFINE_MANAGED_VTABLE(ArrayObject);

ArrayObject::ArrayObject(ExecutionEngine *engine, const QStringList &list)
    : Object(engine->arrayClass)
{
    init(engine);
    Scope scope(engine);
    ScopedValue protectThis(scope, this);

    // Converts a QStringList to JS.
    // The result is a new Array object with length equal to the length
    // of the QStringList, and the elements being the QStringList's
    // elements converted to JS Strings.
    int len = list.count();
    arrayReserve(len);
    for (int ii = 0; ii < len; ++ii) {
        arrayData.data[ii].value = Encode(engine->newString(list.at(ii)));
        arrayData.length = ii + 1;
    }
    setArrayLengthUnchecked(len);
}

void ArrayObject::init(ExecutionEngine *engine)
{
    Q_UNUSED(engine);

    memberData[LengthPropertyIndex].value = Primitive::fromInt32(0);
}

QStringList ArrayObject::toQStringList() const
{
    QStringList result;

    QV4::ExecutionEngine *engine = internalClass->engine;
    Scope scope(engine);
    ScopedValue v(scope);

    uint32_t length = arrayLength();
    for (uint32_t i = 0; i < length; ++i) {
        v = const_cast<ArrayObject *>(this)->getIndexed(i);
        result.append(v->toQStringNoThrow());
    }
    return result;
}