mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
LibJS: Handle possible allocation failure in ArrayBuffer(size_t)
...by replacing it with a ctor that takes the buffer instead, and handling the allocation failure in ArrayBuffer::create(size_t) by throwing a RangeError as specified by the spec.
This commit is contained in:
parent
d20fc922c5
commit
7589cc2494
5 changed files with 22 additions and 6 deletions
|
@ -11,7 +11,12 @@ namespace JS {
|
||||||
|
|
||||||
ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, size_t byte_size)
|
ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, size_t byte_size)
|
||||||
{
|
{
|
||||||
return global_object.heap().allocate<ArrayBuffer>(global_object, byte_size, *global_object.array_buffer_prototype());
|
auto buffer = ByteBuffer::create_zeroed(byte_size);
|
||||||
|
if (!buffer.has_value()) {
|
||||||
|
global_object.vm().throw_exception<RangeError>(global_object, ErrorType::NotEnoughMemoryToAllocate, byte_size);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return global_object.heap().allocate<ArrayBuffer>(global_object, buffer.release_value(), *global_object.array_buffer_prototype());
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, ByteBuffer* buffer)
|
ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, ByteBuffer* buffer)
|
||||||
|
@ -19,9 +24,9 @@ ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, ByteBuffer* buffer
|
||||||
return global_object.heap().allocate<ArrayBuffer>(global_object, buffer, *global_object.array_buffer_prototype());
|
return global_object.heap().allocate<ArrayBuffer>(global_object, buffer, *global_object.array_buffer_prototype());
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayBuffer::ArrayBuffer(size_t byte_size, Object& prototype)
|
ArrayBuffer::ArrayBuffer(ByteBuffer buffer, Object& prototype)
|
||||||
: Object(prototype)
|
: Object(prototype)
|
||||||
, m_buffer(ByteBuffer::create_zeroed(byte_size).release_value()) // FIXME: Handle this possible OOM failure.
|
, m_buffer(move(buffer))
|
||||||
, m_detach_key(js_undefined())
|
, m_detach_key(js_undefined())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ public:
|
||||||
static ArrayBuffer* create(GlobalObject&, size_t);
|
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);
|
ArrayBuffer(ByteBuffer* buffer, Object& prototype);
|
||||||
virtual ~ArrayBuffer() override;
|
virtual ~ArrayBuffer() override;
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,11 @@ Value ArrayBufferConstructor::construct(FunctionObject&)
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
return ArrayBuffer::create(global_object(), byte_length);
|
auto array_buffer = ArrayBuffer::create(global_object(), byte_length);
|
||||||
|
if (!array_buffer)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return array_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview
|
// 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview
|
||||||
|
|
|
@ -213,7 +213,8 @@
|
||||||
M(BadArgCountOne, "{}() needs one argument") \
|
M(BadArgCountOne, "{}() needs one argument") \
|
||||||
M(BadArgCountAtLeastOne, "{}() needs at least one argument") \
|
M(BadArgCountAtLeastOne, "{}() needs at least one argument") \
|
||||||
M(BadArgCountMany, "{}() needs {} arguments") \
|
M(BadArgCountMany, "{}() needs {} arguments") \
|
||||||
M(FixmeAddAnErrorString, "FIXME: Add a string for this error.")
|
M(FixmeAddAnErrorString, "FIXME: Add a string for this error.") \
|
||||||
|
M(NotEnoughMemoryToAllocate, "Not enough memory to allocate {} bytes")
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
|
|
|
@ -182,6 +182,9 @@ static void initialize_typed_array_from_array_like(GlobalObject& global_object,
|
||||||
}
|
}
|
||||||
auto byte_length = element_size * length;
|
auto byte_length = element_size * length;
|
||||||
auto array_buffer = ArrayBuffer::create(global_object, byte_length);
|
auto array_buffer = ArrayBuffer::create(global_object, byte_length);
|
||||||
|
if (!array_buffer)
|
||||||
|
return;
|
||||||
|
|
||||||
typed_array.set_viewed_array_buffer(array_buffer);
|
typed_array.set_viewed_array_buffer(array_buffer);
|
||||||
typed_array.set_byte_length(byte_length);
|
typed_array.set_byte_length(byte_length);
|
||||||
typed_array.set_byte_offset(0);
|
typed_array.set_byte_offset(0);
|
||||||
|
@ -215,6 +218,9 @@ static void initialize_typed_array_from_list(GlobalObject& global_object, TypedA
|
||||||
}
|
}
|
||||||
auto byte_length = element_size * list.size();
|
auto byte_length = element_size * list.size();
|
||||||
auto array_buffer = ArrayBuffer::create(global_object, byte_length);
|
auto array_buffer = ArrayBuffer::create(global_object, byte_length);
|
||||||
|
if (!array_buffer)
|
||||||
|
return;
|
||||||
|
|
||||||
typed_array.set_viewed_array_buffer(array_buffer);
|
typed_array.set_viewed_array_buffer(array_buffer);
|
||||||
typed_array.set_byte_length(byte_length);
|
typed_array.set_byte_length(byte_length);
|
||||||
typed_array.set_byte_offset(0);
|
typed_array.set_byte_offset(0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue