aboutsummaryrefslogtreecommitdiffstats
path: root/qv4array_p.h
blob: 22c29f91b586c6f44758520be0a5c6985ae7cf26 (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
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the V4VM module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QV4ARRAY_P_H
#define QV4ARRAY_P_H

#include "qmljs_runtime.h"
#include <QtCore/QVector>
#include <deque>
#include <algorithm>

namespace QQmlJS {
namespace VM {

class Array
{
public:
    inline Array();
    inline Array(const Array &other);
    inline ~Array();

    inline Array &operator = (const Array &other);

    inline bool isEmpty() const;
    inline uint size() const;
    inline uint count() const;
    inline Value at(uint index) const;
    inline void assign(uint index, const Value &v);
    inline void clear();
    inline void resize(uint size);
    inline void concat(const Array &other);
    inline Value pop();
    inline Value takeFirst();
    inline void sort(ExecutionContext *context, const Value &comparefn);
    inline void splice(double start, double deleteCount,
                       const QVector<Value> &items,
                       Array &other);
    inline void push(const Value &value);

    void getCollectables(QVector<Object *> &objects);

private:
    typedef std::deque<Value> ToVectorType;
    ToVectorType *to_vector;
};

class ArrayElementLessThan
{
public:
    inline ArrayElementLessThan(ExecutionContext *context, const Value &comparefn)
        : m_context(context), m_comparefn(comparefn) {}

    bool operator()(const Value &v1, const Value &v2) const;

private:
    ExecutionContext *m_context;
    Value m_comparefn;
};

inline Array::Array()
{
    to_vector = new std::deque<Value>();
}

inline Array::Array(const Array &other)
{
    to_vector = new std::deque<Value>(*other.to_vector);
}

inline Array::~Array()
{
    delete to_vector;
}

inline Array &Array::operator = (const Array &other)
{
    *to_vector = *other.to_vector;
    return *this;
}

inline bool Array::isEmpty() const
{
    return to_vector->empty();
}

inline uint Array::size() const
{
    return to_vector->size();
}

inline uint Array::count() const
{
    return to_vector->size();
}

inline Value Array::at(uint index) const
{
    return index < to_vector->size() ? to_vector->at(index) : Value::undefinedValue();
}

inline void Array::assign(uint index, const Value &v)
{
    if (index == to_vector->size())
        to_vector->push_back(v);
    else {
        if (index > to_vector->size())
            resize(index + 1);

        (*to_vector)[index] = v;
    }
}

inline void Array::clear()
{
    to_vector->clear();
}

inline void Array::resize(uint s)
{
    to_vector->resize(s, Value::undefinedValue());
}

inline void Array::concat(const Array &other)
{
    for (std::deque<Value>::iterator it = other.to_vector->begin(); it != other.to_vector->end(); ++it) {
        const Value &v = *it;
        if (! v.isUndefined())
            to_vector->push_back(v);
    }
}

inline Value Array::pop()
{
    if (isEmpty())
        return Value::undefinedValue();

    Value v = to_vector->back();
    to_vector->pop_back();
    return v;
}

inline Value Array::takeFirst()
{
    if (isEmpty())
        return Value::undefinedValue();

    Value v = to_vector->front();
    to_vector->pop_front();
    return v;
}

inline void Array::sort(ExecutionContext *context, const Value &comparefn)
{
    ArrayElementLessThan lessThan(context, comparefn);
    std::sort(to_vector->begin(), to_vector->end(), lessThan);
}

inline void Array::push(const Value &value)
{
    to_vector->push_back(value);
}

inline void Array::getCollectables(QVector<Object *> &objects)
{
    for (ToVectorType::const_iterator it = to_vector->begin(), eit = to_vector->end(); it != eit; ++it) {
        if (Object *o = it->asObject())
            objects.append(o);
    }
}

inline void Array::splice(double start, double deleteCount,
                          const QVector<Value> &items,
                          Array &other)
{
    const double len = size();
    if (start < 0)
        start = qMax(len + start, double(0));
    else if (start > len)
        start = len;
    deleteCount = qMax(qMin(deleteCount, len - start), double(0));

    const uint st = uint(start);
    const uint dc = uint(deleteCount);
    other.resize(dc);

    const uint itemsSize = uint(items.size());

    for (uint i = 0; i < dc; ++i)
        other.assign(i, to_vector->at(st + i));
    if (itemsSize > dc)
        to_vector->insert(to_vector->begin() + st, itemsSize - dc, Value::undefinedValue());
    else if (itemsSize < dc)
        to_vector->erase(to_vector->begin() + st, to_vector->begin() + (dc - itemsSize));
    for (uint i = 0; i < itemsSize; ++i)
        (*to_vector)[st + i] = items.at(i);
}

} // end of namespace VM
} // end of namespace QQmlJS

#endif // QV4ARRAY_P_H