File: kjs_arraybufferview.h

package info (click to toggle)
khtml 5.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,940 kB
  • sloc: cpp: 206,281; java: 4,060; ansic: 2,829; perl: 2,313; yacc: 1,497; python: 339; sh: 141; xml: 37; makefile: 7
file content (446 lines) | stat: -rw-r--r-- 14,517 bytes parent folder | download | duplicates (5)
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
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 2013 Bernd Buschinski <b.buschinski@googlemail.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef KJS_ARRAYBUFFERVIEW_H
#define KJS_ARRAYBUFFERVIEW_H

#include "ecma/kjs_arraybuffer.h"

#include <kjs/object.h>
#include <kjs/function.h>
#include <kjs/function_object.h>
#include <kjs/operations.h>
#include <kjs/array_instance.h>
#include <dom/dom_exception.h>

namespace KJS
{
class ArrayBuffer;

// Keep enum outside of template classes, for lut tables
namespace ArrayBufferViewBase
{
enum {
    Buffer, ByteLength, ByteOffset, Subarray, Length, Set, BytesPerElement
};
}

//type, TypedArrayClass
template <class T, class U>
class ArrayBufferViewConstructorImp : public KJS::FunctionPrototype
{
public:
    ArrayBufferViewConstructorImp(ExecState *exec, DOM::DocumentImpl *d);
    bool implementsConstruct() const override;
    using KJS::JSObject::construct;
    JSObject *construct(ExecState *exec, const List &args) override;

    JSValue *getValueProperty(ExecState *exec, int token) const;
    using KJS::JSObject::getOwnPropertySlot;
    bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
private:
    SharedPtr<DOM::DocumentImpl> doc;
};

//type, TypedArrayPrototype
template <class T, class P>
class ArrayBufferView : public JSObject
{
public:
    explicit ArrayBufferView(ExecState *exec, ArrayBuffer *buffer, size_t byteOffset, size_t byteLength);
    virtual ~ArrayBufferView();

    bool getOwnPropertySlot(ExecState *exec, unsigned i, PropertySlot &slot) override;
    bool getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot) override;
    JSValue *getValueProperty(ExecState *exec, int token) const;

    void put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr = None) override;
    void put(ExecState *exec, unsigned propertyName, JSValue *value, int attr = None) override;

    ArrayBuffer *buffer() const
    {
        return m_buffer;
    }
    size_t byteLength() const
    {
        return m_byteLength;
    }
    size_t byteOffset() const
    {
        return m_byteOffset;
    }
    size_t length() const
    {
        return m_length;
    }
    inline T *bufferStart() const
    {
        return m_bufferStart;
    }

private:
    // checks if the pos is a valid array index, returns false if not
    inline bool checkIndex(ExecState *exec, unsigned pos);

    ProtectedPtr<ArrayBuffer> m_buffer;
    size_t m_byteOffset;
    size_t m_byteLength;
    size_t m_length;
    T *m_bufferStart;
};

//unrolled KJS_DEFINE_PROTOTYPE(ArrayBufferViewProto), for template sake

//type, TypedArrayClass
template <class T, class U>
class ArrayBufferViewProto : public KJS::JSObject
{
public:
    bool getOwnPropertySlot(KJS::ExecState *, const KJS::Identifier &, KJS::PropertySlot &) override;
    using JSObject::getOwnPropertySlot;
protected:
    ArrayBufferViewProto(KJS::ExecState *exec);
};

//unrolled KJS_IMPLEMENT_PROTOFUNC(ArrayBufferViewProtoFunc), for template sake
template <class T, class U>
class ArrayBufferViewProtoFunc : public KJS::InternalFunctionImp
{
public:
    ArrayBufferViewProtoFunc<T, U>(KJS::ExecState *exec, int i, int len, const KJS::Identifier &name)
        : InternalFunctionImp(static_cast<KJS::FunctionPrototype *>(exec->lexicalInterpreter()->builtinFunctionPrototype()), name), id(i)
    {
        put(exec, exec->propertyNames().length, KJS::jsNumber(len), KJS::DontDelete | KJS::ReadOnly | KJS::DontEnum);
    }

    KJS::JSValue *callAsFunction(KJS::ExecState *exec, KJS::JSObject *thisObj, const KJS::List &args) override;
private:
    int id;
};
}

#include "kjs_arraybufferview.lut.h"

namespace KJS
{

// unrolled KJS_IMPLEMENT_PROTOTYPE("ArrayBufferView", ArrayBufferViewProto, ArrayBufferViewProtoFunc, ObjectPrototype)
// for template sake
template <class T, class U>
bool ArrayBufferViewProto<T, U>::getOwnPropertySlot(KJS::ExecState *exec, const KJS::Identifier &propertyName, KJS::PropertySlot &slot)
{
    return KJS::getStaticFunctionSlot<ArrayBufferViewProtoFunc<T, U>, KJS::JSObject>(exec, &ArrayBufferViewProtoTable, this, propertyName, slot);
}

template <class T, class U>
ArrayBufferViewProto<T, U>::ArrayBufferViewProto(KJS::ExecState *exec)
    : KJS::JSObject(ObjectPrototype::self(exec))
{
}
// unroll end

// -------------------- ArrayBufferViewProtoFunc ---------------------

template <class T, class U>
JSValue *ArrayBufferViewProtoFunc<T, U>::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
{
    if (!thisObj->inherits(&U::info)) {
        return jsUndefined();
    }
    U *view = static_cast<U *>(thisObj);

    switch (id) {
    case ArrayBufferViewBase::Subarray: {
        //Subarray takes max long/signed size_t
        // If start or end are negative, it refers to an index from the end of the array
        ssize_t begin = 0;
        ssize_t end = 0;
        double tmp;
        if (args[0]->getNumber(tmp)) {
            begin = static_cast<ssize_t>(tmp);
        }
        if (args.size() >= 2 && args[1]->getNumber(tmp)) {
            end = static_cast<ssize_t>(tmp);
        }

        // turn negative start/end into a valid positive index
        if (begin < 0 && view->length() > static_cast<size_t>(-begin)) {
            begin = view->length() + begin;
        }
        if (end < 0 && view->length() > static_cast<size_t>(-end)) {
            end = view->length() + end;
        }

        if (static_cast<size_t>(begin) > view->length()) {
            begin = view->length();
        }
        if (static_cast<size_t>(end) > view->length()) {
            end = 0;
        }

        size_t length = 0;
        if (begin < end) {
            length = (end - begin) * sizeof(T);
        }

        U *newView = new U(exec, view->buffer(), begin * sizeof(T), length);
        return newView;
    }
    case ArrayBufferViewBase::Set: {
        JSObject *obj = args[0]->getObject();
        if (!obj) {
            return jsUndefined();
        }
        if (obj->inherits(&U::info)) {
            U *other = static_cast<U *>(obj);
            double tmp;
            size_t offset = 0;
            if (args.size() >= 2 && args[1]->getNumber(tmp) && tmp > 0) {
                offset = static_cast<size_t>(tmp) * sizeof(T);
            }

            if (offset > other->byteLength() || other->byteLength() - offset > view->byteLength()) {
                setDOMException(exec, DOM::DOMException::INDEX_SIZE_ERR);
                return jsUndefined();
            }
            memcpy(view->buffer()->buffer(), other->buffer()->buffer() + offset, std::max<ssize_t>(static_cast<ssize_t>(other->byteLength()) - static_cast<ssize_t>(offset), 0));
            return jsUndefined();
        } else if (obj->inherits(&ArrayInstance::info)) {
            ArrayInstance *array = static_cast<ArrayInstance *>(obj);
            if (array->getLength() > view->length()) {
                setDOMException(exec, DOM::DOMException::INDEX_SIZE_ERR);
                return jsUndefined();
            }
            for (unsigned i = 0; i < array->getLength(); ++i) {
                view->put(exec, i, array->getItem(i));
            }
            return jsUndefined();
        } else {
            return jsUndefined();
        }
    }
    default:
        ASSERT_NOT_REACHED();
        return jsUndefined();
    }
}

// -------------------- ArrayBufferViewConstructorImp ---------------------

template <class T, class U>
ArrayBufferViewConstructorImp<T, U>::ArrayBufferViewConstructorImp(ExecState *exec, DOM::DocumentImpl *d)
    : KJS::FunctionPrototype(exec),
      doc(d)
{
}

template <class T, class U>
bool ArrayBufferViewConstructorImp<T, U>::implementsConstruct() const
{
    return true;
}

template <class T, class U>
JSObject *ArrayBufferViewConstructorImp<T, U>::construct(ExecState *exec, const List &args)
{
    JSType type = args[0]->type();

    switch (type) {
    case ObjectType: {
        JSObject *obj = args[0]->getObject();
        if (!obj) {
            return throwError(exec, TypeError);
        }
        if (obj->inherits(&ArrayBuffer::info)) {
            // new ArrayBufferView(ArrayBuffer, [byteOffset[, byteLength]])
            ArrayBuffer *buf = static_cast<ArrayBuffer *>(obj);

            size_t byteOffset = 0, byteLength = 0;
            double tmp;
            if (args.size() >= 2 && args[1]->getNumber(tmp) && tmp > 0) {
                byteOffset = static_cast<size_t>(tmp);
            }

            if (args.size() >= 3 && args[2]->getNumber(tmp) && tmp > 0) {
                byteLength = static_cast<size_t>(tmp) * sizeof(T);
            }

            return new U(exec, buf, byteOffset, byteLength);
        } else if (obj->inherits(&ArrayInstance::info)) {
            // new ArrayBufferView(Array)
            ArrayInstance *arr = dynamic_cast<ArrayInstance *>(obj);
            ArrayBuffer *buf = new ArrayBuffer(arr->getLength()*sizeof(T));
            U *view = new U(exec, buf, 0, 0);
            for (unsigned i = 0; i < arr->getLength(); ++i) {
                view->put(exec, i, arr->getItem(i));
            }
            return view;
        } else if (obj->inherits(&U::info)) {
            // new ArrayBufferView(ArrayBufferView)
            U *arr = static_cast<U *>(obj);
            ArrayBuffer *buf = new ArrayBuffer(arr->buffer()->buffer(), arr->byteLength());
            U *view = new U(exec, buf, 0, 0);
            return view;
        } else {
            break;
        }
    }
    case NumberType: {
        // new ArrayBufferView(Length)
        size_t length = 0;
        double lengthF = args[0]->getNumber();
        if (!KJS::isNaN(lengthF) && !KJS::isInf(lengthF) && lengthF > 0) {
            length = static_cast<size_t>(lengthF);
        }
        ArrayBuffer *buf = new ArrayBuffer(length * sizeof(T));
        return new U(exec, buf, 0, 0);
    }
    default:
        break;
    }
    // new ArrayBufferView(), create empty ArrayBuffer
    ArrayBuffer *buf = new ArrayBuffer(0);
    return new U(exec, buf, 0, 0);
}

template <class T, class U>
JSValue *ArrayBufferViewConstructorImp<T, U>::getValueProperty(ExecState *, int) const
{
    // switch (id)
    // {
    // case ArrayBufferViewFuncImp<T>::BytesPerElement:
    return jsNumber(sizeof(T));
    // }
}

template <class T, class U>
bool ArrayBufferViewConstructorImp<T, U>::getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot)
{
    return getStaticPropertySlot<ArrayBufferViewProtoFunc<T, U>, ArrayBufferViewConstructorImp<T, U>, KJS::FunctionPrototype>(exec, &ArrayBufferViewConstTable, this, propertyName, slot);
}

// -------------------- ArrayBufferView ---------------------

template <class T, class P>
ArrayBufferView<T, P>::ArrayBufferView(KJS::ExecState *exec, KJS::ArrayBuffer *buffer, size_t byteOffset, size_t byteLength)
    : JSObject(),
      m_buffer(buffer),
      m_byteOffset(byteOffset)
{
    if (byteLength == 0) {
        if (byteOffset < buffer->byteLength()) {
            m_byteLength = buffer->byteLength() - byteOffset;
        } else {
            m_byteLength = 0;
        }
    } else {
        m_byteLength = byteLength;
    }
    m_length = m_byteLength / sizeof(T);
    setPrototype(P::self(exec));
    m_bufferStart = reinterpret_cast<T *>(m_buffer->buffer() + m_byteOffset);
}

template <class T, class P>
ArrayBufferView<T, P>::~ArrayBufferView()
{
}

template <class T, class P>
bool ArrayBufferView<T, P>::getOwnPropertySlot(ExecState *exec, unsigned int i, PropertySlot &slot)
{
    if (!checkIndex(exec, i)) {
        return false;
    }

    slot.setValue(this, jsNumber(m_bufferStart[i]));
    return true;
}

template <class T, class P>
bool ArrayBufferView<T, P>::getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot)
{
    bool ok = false;
    unsigned i = propertyName.toArrayIndex(&ok);
    if (ok) {
        return ArrayBufferView<T, P>::getOwnPropertySlot(exec, i, slot);
    }

    return getStaticValueSlot< ArrayBufferView<T, P>, JSObject>(exec, &ArrayBufferViewTable, this, propertyName, slot);
}

template <class T, class P>
JSValue *ArrayBufferView<T, P>::getValueProperty(ExecState * /*exec*/, int token) const
{
    switch (token) {
    case ArrayBufferViewBase::Buffer:
        return m_buffer;
    case ArrayBufferViewBase::ByteLength:
        return jsNumber(m_byteLength);
    case ArrayBufferViewBase::ByteOffset:
        return jsNumber(m_byteOffset);
    case ArrayBufferViewBase::Length:
        return jsNumber(m_length);
    default:
        ASSERT_NOT_REACHED();
        qCWarning(KHTML_LOG) << "ArrayBufferView<T>::getValueProperty unhandled token " << token;
        break;
    }
    return nullptr;
}

template <class T, class P>
void ArrayBufferView<T, P>::put(ExecState *exec, const Identifier &propertyName, JSValue *value, int attr)
{
    bool ok = false;
    unsigned i = propertyName.toArrayIndex(&ok);
    if (ok) {
        ArrayBufferView<T, P>::put(exec, i, value, attr);
    } else {
        KJS::JSObject::put(exec, propertyName, value, attr);
    }
}

template <class T, class P>
void ArrayBufferView<T, P>::put(ExecState *exec, unsigned int i, JSValue *value, int /*attr*/)
{
    if (!checkIndex(exec, i)) {
        return;
    }
    if (value && value->type() != NumberType) {
        return;
    }
    m_bufferStart[i] = static_cast<T>(value->getNumber());
}

template <class T, class P>
bool ArrayBufferView<T, P>::checkIndex(KJS::ExecState * /*exec*/, unsigned int pos)
{
    if (m_byteOffset + (pos + 1)*sizeof(T) > m_buffer->byteLength()) {
        return false;
    }
    if (pos * sizeof(T) >= m_byteLength) {
        return false;
    }
    return true;
}

} // namespace KJS

#endif //KJS_ARRAYBUFFERVIEW_H