diff options
author | Andreas Kling <kling@serenityos.org> | 2021-02-21 11:40:35 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-21 14:21:26 +0100 |
commit | df8f074cf614aeffc127c6f60317b7a6f80bbf63 (patch) | |
tree | 914742b8435df079e30ed639bbaea10b6dda7630 | |
parent | a65f178ce841a5bd82ba777b27715b3ff06be8f2 (diff) | |
download | serenity-df8f074cf614aeffc127c6f60317b7a6f80bbf63.zip |
LibJS: Make TypedArray::data() return a Span<T>
This inserts bounds checking assertions whenever we're reading/writing
a typed array from JS.
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/TypedArray.h | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index 4e29ef6fd6..a0f6709a76 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -50,7 +50,7 @@ public: virtual size_t element_size() const = 0; protected: - TypedArrayBase(Object& prototype) + explicit TypedArrayBase(Object& prototype) : Object(prototype) { } @@ -116,7 +116,14 @@ public: } } - T* data() const { return reinterpret_cast<T*>(m_viewed_array_buffer->buffer().data()); } + Span<const T> data() const + { + return { reinterpret_cast<const T*>(m_viewed_array_buffer->buffer().data()), m_array_length }; + } + Span<T> data() + { + return { reinterpret_cast<T*>(m_viewed_array_buffer->buffer().data()), m_array_length }; + } virtual size_t element_size() const override { return sizeof(T); }; @@ -127,7 +134,7 @@ protected: ASSERT(!Checked<u32>::multiplication_would_overflow(array_length, sizeof(T))); m_viewed_array_buffer = ArrayBuffer::create(global_object(), array_length * sizeof(T)); if (array_length) - ASSERT(data() != nullptr); + ASSERT(!data().is_null()); m_array_length = array_length; m_byte_length = m_viewed_array_buffer->byte_length(); } |