1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:37:43 +00:00

LibJS: Implement missing checks for SharedArrayBuffer values

This commit is contained in:
Timothy Flynn 2023-12-28 09:27:42 -05:00 committed by Andreas Kling
parent 299c86db20
commit 526a74f2f1
6 changed files with 46 additions and 6 deletions

View file

@ -201,7 +201,7 @@ size_t array_buffer_byte_length(ArrayBuffer const& array_buffer, ArrayBuffer::Or
ThrowCompletionOr<void> detach_array_buffer(VM& vm, ArrayBuffer& array_buffer, Optional<Value> key)
{
// 1. Assert: IsSharedArrayBuffer(arrayBuffer) is false.
// FIXME: Check for shared buffer
VERIFY(!array_buffer.is_shared_array_buffer());
// 2. If key is not present, set key to undefined.
if (!key.has_value())

View file

@ -174,7 +174,8 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice)
auto array_buffer_object = TRY(typed_this_value(vm));
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
// FIXME: Check for shared buffer
if (array_buffer_object->is_shared_array_buffer())
return vm.throw_completion<TypeError>(ErrorType::SharedArrayBuffer);
// 4. If IsDetachedBuffer(O) is true, throw a TypeError exception.
if (array_buffer_object->is_detached())
@ -226,7 +227,8 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice)
auto* new_array_buffer_object = static_cast<ArrayBuffer*>(new_array_buffer.ptr());
// 18. If IsSharedArrayBuffer(new) is true, throw a TypeError exception.
// FIXME: Check for shared buffer
if (new_array_buffer_object->is_shared_array_buffer())
return vm.throw_completion<TypeError>(ErrorType::SharedArrayBuffer);
// 19. If IsDetachedBuffer(new) is true, throw a TypeError exception.
if (new_array_buffer_object->is_detached())

View file

@ -40,7 +40,8 @@ JS_DEFINE_NATIVE_FUNCTION(SharedArrayBufferPrototype::byte_length_getter)
auto array_buffer_object = TRY(typed_this_value(vm));
// 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
// FIXME: Check for shared buffer
if (!array_buffer_object->is_shared_array_buffer())
return vm.throw_completion<TypeError>(ErrorType::NotASharedArrayBuffer);
// 4. Let length be O.[[ArrayBufferByteLength]].
// 5. Return 𝔽(length).
@ -60,7 +61,8 @@ JS_DEFINE_NATIVE_FUNCTION(SharedArrayBufferPrototype::slice)
auto array_buffer_object = TRY(typed_this_value(vm));
// 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
// FIXME: Check for shared buffer
if (!array_buffer_object->is_shared_array_buffer())
return vm.throw_completion<TypeError>(ErrorType::NotASharedArrayBuffer);
// 4. Let len be O.[[ArrayBufferByteLength]].
auto length = array_buffer_object->byte_length();
@ -108,7 +110,8 @@ JS_DEFINE_NATIVE_FUNCTION(SharedArrayBufferPrototype::slice)
auto* new_array_buffer_object = static_cast<ArrayBuffer*>(new_array_buffer.ptr());
// 17. If IsSharedArrayBuffer(new) is true, throw a TypeError exception.
// FIXME: Check for shared buffer
if (!new_array_buffer_object->is_shared_array_buffer())
return vm.throw_completion<TypeError>(ErrorType::NotASharedArrayBuffer);
// 18. If new.[[ArrayBufferData]] is O.[[ArrayBufferData]], throw a TypeError exception.
if (new_array_buffer == array_buffer_object)

View file

@ -1,3 +1,11 @@
describe("errors", () => {
test("called on SharedArrayBuffer object", () => {
expect(() => {
ArrayBuffer.prototype.slice.call(new SharedArrayBuffer());
}).toThrowWithMessage(TypeError, "The array buffer object cannot be a SharedArrayBuffer");
});
});
test("single parameter", () => {
const buffer = new ArrayBuffer(16);
const fullView = new Int32Array(buffer);

View file

@ -1,3 +1,18 @@
describe("errors", () => {
test("called on non-SharedArrayBuffer object", () => {
expect(() => {
SharedArrayBuffer.prototype.byteLength;
}).toThrowWithMessage(TypeError, "Not an object of type SharedArrayBuffer");
let byteLength = Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, "byteLength");
let getter = byteLength.get;
expect(() => {
getter.call(new ArrayBuffer());
}).toThrowWithMessage(TypeError, "The array buffer object must be a SharedArrayBuffer");
});
});
test("basic functionality", () => {
expect(new SharedArrayBuffer().byteLength).toBe(0);
expect(new SharedArrayBuffer(1).byteLength).toBe(1);

View file

@ -1,3 +1,15 @@
describe("errors", () => {
test("called on non-SharedArrayBuffer object", () => {
expect(() => {
SharedArrayBuffer.prototype.slice(Symbol.hasInstance);
}).toThrowWithMessage(TypeError, "Not an object of type SharedArrayBuffer");
expect(() => {
SharedArrayBuffer.prototype.slice.call(new ArrayBuffer());
}).toThrowWithMessage(TypeError, "The array buffer object must be a SharedArrayBuffer");
});
});
test("single parameter", () => {
const buffer = new SharedArrayBuffer(16);
const fullView = new Int32Array(buffer);