1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:57:35 +00:00

LibJS: Allow ArrayBuffer to not own its backing data buffer as well

This is implemented as a ByteBuffer* in a variant, so its size should
only be increased by an index.
This commit is contained in:
Ali Mohammad Pur 2021-05-17 21:39:31 +04:30 committed by Ali Mohammad Pur
parent 3926eab3b7
commit 4fd43a8f96
2 changed files with 27 additions and 4 deletions

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Variant.h>
#include <LibJS/Runtime/Object.h>
namespace JS {
@ -17,17 +18,28 @@ class ArrayBuffer : public 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 m_buffer.size(); }
ByteBuffer& buffer() { return m_buffer; }
const ByteBuffer& buffer() const { return m_buffer; }
size_t byte_length() const { return buffer_impl().size(); }
ByteBuffer& buffer() { return buffer_impl(); }
const ByteBuffer& buffer() const { return buffer_impl(); }
private:
ByteBuffer m_buffer;
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;
};
}