/src/serenity/Userland/Libraries/LibWeb/WebIDL/Buffers.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org> |
3 | | * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/Variant.h> |
11 | | #include <LibJS/Forward.h> |
12 | | #include <LibJS/Heap/Cell.h> |
13 | | #include <LibJS/Heap/CellAllocator.h> |
14 | | |
15 | | namespace Web::WebIDL { |
16 | | |
17 | | using BufferableObject = Variant< |
18 | | JS::NonnullGCPtr<JS::TypedArrayBase>, |
19 | | JS::NonnullGCPtr<JS::DataView>, |
20 | | JS::NonnullGCPtr<JS::ArrayBuffer>>; |
21 | | |
22 | | class BufferableObjectBase : public JS::Cell { |
23 | | JS_CELL(BufferableObjectBase, JS::Cell); |
24 | | JS_DECLARE_ALLOCATOR(BufferableObjectBase); |
25 | | |
26 | | public: |
27 | | virtual ~BufferableObjectBase() override = default; |
28 | | |
29 | | u32 byte_length() const; |
30 | | |
31 | | JS::NonnullGCPtr<JS::Object> raw_object(); |
32 | 0 | JS::NonnullGCPtr<JS::Object const> raw_object() const { return const_cast<BufferableObjectBase&>(*this).raw_object(); } |
33 | | |
34 | | JS::GCPtr<JS::ArrayBuffer> viewed_array_buffer(); |
35 | | |
36 | 0 | BufferableObject const& bufferable_object() const { return m_bufferable_object; } |
37 | 0 | BufferableObject& bufferable_object() { return m_bufferable_object; } |
38 | | |
39 | | protected: |
40 | | BufferableObjectBase(JS::NonnullGCPtr<JS::Object>); |
41 | | |
42 | | virtual void visit_edges(Visitor&) override; |
43 | | |
44 | | bool is_data_view() const; |
45 | | bool is_typed_array_base() const; |
46 | | bool is_array_buffer() const; |
47 | | |
48 | | static BufferableObject bufferable_object_from_raw_object(JS::NonnullGCPtr<JS::Object>); |
49 | | |
50 | | BufferableObject m_bufferable_object; |
51 | | }; |
52 | | |
53 | | // https://webidl.spec.whatwg.org/#ArrayBufferView |
54 | | // |
55 | | // typedef (Int8Array or Int16Array or Int32Array or |
56 | | // Uint8Array or Uint16Array or Uint32Array or Uint8ClampedArray or |
57 | | // BigInt64Array or BigUint64Array or |
58 | | // Float32Array or Float64Array or DataView) ArrayBufferView; |
59 | | class ArrayBufferView : public BufferableObjectBase { |
60 | | JS_CELL(ArrayBufferView, BufferableObjectBase); |
61 | | JS_DECLARE_ALLOCATOR(ArrayBufferView); |
62 | | |
63 | | public: |
64 | | using BufferableObjectBase::BufferableObjectBase; |
65 | | |
66 | | virtual ~ArrayBufferView() override; |
67 | | |
68 | | using BufferableObjectBase::is_data_view; |
69 | | using BufferableObjectBase::is_typed_array_base; |
70 | | |
71 | | u32 byte_offset() const; |
72 | | }; |
73 | | |
74 | | // https://webidl.spec.whatwg.org/#BufferSource |
75 | | // |
76 | | // typedef (ArrayBufferView or ArrayBuffer) BufferSource; |
77 | | class BufferSource : public BufferableObjectBase { |
78 | | JS_CELL(BufferSource, BufferableObjectBase); |
79 | | JS_DECLARE_ALLOCATOR(BufferSource); |
80 | | |
81 | | public: |
82 | | using BufferableObjectBase::BufferableObjectBase; |
83 | | |
84 | | virtual ~BufferSource() override; |
85 | | |
86 | | using BufferableObjectBase::is_array_buffer; |
87 | | using BufferableObjectBase::is_data_view; |
88 | | using BufferableObjectBase::is_typed_array_base; |
89 | | }; |
90 | | |
91 | | } |