1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 15:47:45 +00:00

LibJS: Stub out Atomics.wait and Atomics.waitAsync

We don't have the facilities to implement these methods fully (namely, a
fully realized SharedArrayBuffer). But we can implement enough to
validate the values passed in by the user.
This commit is contained in:
Timothy Flynn 2023-11-15 16:03:27 -05:00 committed by Tim Flynn
parent a7073c3f1f
commit 78edaad97d
6 changed files with 241 additions and 0 deletions

View file

@ -0,0 +1,76 @@
describe("errors", () => {
test("called on non-TypedArray", () => {
expect(() => {
Atomics.wait(Symbol.hasInstance, 0, 0, 0);
}).toThrowWithMessage(TypeError, "Not an object of type TypedArray");
});
test("detached buffer", () => {
expect(() => {
const typedArray = new Int32Array(4);
detachArrayBuffer(typedArray.buffer);
Atomics.wait(typedArray, 0, 0, 0);
}).toThrowWithMessage(TypeError, "ArrayBuffer is detached");
});
test("invalid TypedArray type", () => {
expect(() => {
const typedArray = new Float32Array(4);
Atomics.wait(typedArray, 0, 0, 0);
}).toThrowWithMessage(
TypeError,
"Typed array Float32Array element type is not Int32 or BigInt64"
);
});
test("non-shared ArrayBuffer", () => {
expect(() => {
const typedArray = new Int32Array(4);
Atomics.wait(typedArray, 0, 0, 0);
}).toThrowWithMessage(
TypeError,
"The TypedArray's underlying buffer must be a SharedArrayBuffer"
);
});
test("invalid index", () => {
expect(() => {
const buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
const typedArray = new Int32Array(buffer);
Atomics.wait(typedArray, 4, 0, 0);
}).toThrowWithMessage(RangeError, "Index 4 is out of range of array length 4");
});
test("invalid value", () => {
expect(() => {
const buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
const typedArray = new Int32Array(buffer);
Atomics.wait(typedArray, 0, Symbol.hasInstance, 0);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
expect(() => {
const buffer = new SharedArrayBuffer(4 * BigInt64Array.BYTES_PER_ELEMENT);
const typedArray = new BigInt64Array(buffer);
Atomics.wait(typedArray, 0, Symbol.hasInstance, 0);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt");
});
test("invalid timeout", () => {
expect(() => {
const buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
const typedArray = new Int32Array(buffer);
Atomics.wait(typedArray, 0, 0, Symbol.hasInstance);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
});
});
test("basic functionality", () => {
test("invariants", () => {
expect(Atomics.wait).toHaveLength(4);
});
});

View file

@ -0,0 +1,76 @@
describe("errors", () => {
test("called on non-TypedArray", () => {
expect(() => {
Atomics.waitAsync(Symbol.hasInstance, 0, 0, 0);
}).toThrowWithMessage(TypeError, "Not an object of type TypedArray");
});
test("detached buffer", () => {
expect(() => {
const typedArray = new Int32Array(4);
detachArrayBuffer(typedArray.buffer);
Atomics.waitAsync(typedArray, 0, 0, 0);
}).toThrowWithMessage(TypeError, "ArrayBuffer is detached");
});
test("invalid TypedArray type", () => {
expect(() => {
const typedArray = new Float32Array(4);
Atomics.waitAsync(typedArray, 0, 0, 0);
}).toThrowWithMessage(
TypeError,
"Typed array Float32Array element type is not Int32 or BigInt64"
);
});
test("non-shared ArrayBuffer", () => {
expect(() => {
const typedArray = new Int32Array(4);
Atomics.waitAsync(typedArray, 0, 0, 0);
}).toThrowWithMessage(
TypeError,
"The TypedArray's underlying buffer must be a SharedArrayBuffer"
);
});
test("invalid index", () => {
expect(() => {
const buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
const typedArray = new Int32Array(buffer);
Atomics.waitAsync(typedArray, 4, 0, 0);
}).toThrowWithMessage(RangeError, "Index 4 is out of range of array length 4");
});
test("invalid value", () => {
expect(() => {
const buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
const typedArray = new Int32Array(buffer);
Atomics.waitAsync(typedArray, 0, Symbol.hasInstance, 0);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
expect(() => {
const buffer = new SharedArrayBuffer(4 * BigInt64Array.BYTES_PER_ELEMENT);
const typedArray = new BigInt64Array(buffer);
Atomics.waitAsync(typedArray, 0, Symbol.hasInstance, 0);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt");
});
test("invalid timeout", () => {
expect(() => {
const buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
const typedArray = new Int32Array(buffer);
Atomics.waitAsync(typedArray, 0, 0, Symbol.hasInstance);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
});
});
test("basic functionality", () => {
test("invariants", () => {
expect(Atomics.waitAsync).toHaveLength(4);
});
});