summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
blob: d731afc322f7abc4f6869b68ffb68475698ccd59 (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
/*
 * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteBuffer.h>
#include <AK/Variant.h>
#include <LibJS/Runtime/Object.h>

namespace JS {

class ArrayBuffer : public Object {
    JS_OBJECT(ArrayBuffer, Object);

public:
    static ArrayBuffer* create(GlobalObject&, size_t);
    static ArrayBuffer* create(GlobalObject&, ByteBuffer&);
    static ArrayBuffer* create(GlobalObject&, ByteBuffer*);

    ArrayBuffer(size_t, Object& prototype);
    ArrayBuffer(ByteBuffer& buffer, Object& prototype);
    ArrayBuffer(ByteBuffer* buffer, Object& prototype);
    virtual ~ArrayBuffer() override;

    size_t byte_length() const { return buffer_impl().size(); }
    ByteBuffer& buffer() { return buffer_impl(); }
    const ByteBuffer& buffer() const { return buffer_impl(); }

private:
    ByteBuffer& buffer_impl()
    {
        ByteBuffer* ptr { nullptr };
        m_buffer.visit([&](auto* pointer) { ptr = pointer; }, [&](auto& value) { ptr = &value; });
        return *ptr;
    }

    const ByteBuffer& buffer_impl() const { return const_cast<ArrayBuffer*>(this)->buffer_impl(); }

    Variant<ByteBuffer, ByteBuffer*> m_buffer;
};

}