mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:27:45 +00:00
LibJS: Implement Atomics.exchange
This commit is contained in:
parent
7236584132
commit
655ffce64d
4 changed files with 95 additions and 0 deletions
|
@ -119,6 +119,7 @@ void AtomicsObject::initialize(GlobalObject& global_object)
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_native_function(vm.names.add, add, 3, attr);
|
define_native_function(vm.names.add, add, 3, attr);
|
||||||
define_native_function(vm.names.and_, and_, 3, attr);
|
define_native_function(vm.names.and_, and_, 3, attr);
|
||||||
|
define_native_function(vm.names.exchange, exchange, 3, attr);
|
||||||
define_native_function(vm.names.load, load, 2, attr);
|
define_native_function(vm.names.load, load, 2, attr);
|
||||||
define_native_function(vm.names.or_, or_, 3, attr);
|
define_native_function(vm.names.or_, or_, 3, attr);
|
||||||
define_native_function(vm.names.store, store, 3, attr);
|
define_native_function(vm.names.store, store, 3, attr);
|
||||||
|
@ -165,6 +166,24 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::and_)
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 25.4.6 Atomics.exchange ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.exchange
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::exchange)
|
||||||
|
{
|
||||||
|
auto* typed_array = typed_array_from(global_object, vm.argument(0));
|
||||||
|
if (!typed_array)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto atomic_exchange = [](auto* storage, auto value) { return AK::atomic_exchange(storage, value); };
|
||||||
|
|
||||||
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
|
||||||
|
if (is<ClassName>(typed_array)) \
|
||||||
|
return perform_atomic_operation<Type>(global_object, *typed_array, move(atomic_exchange));
|
||||||
|
JS_ENUMERATE_TYPED_ARRAYS
|
||||||
|
#undef __JS_ENUMERATE
|
||||||
|
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
// 25.4.8 Atomics.load ( typedArray, index ), https://tc39.es/ecma262/#sec-atomics.load
|
// 25.4.8 Atomics.load ( typedArray, index ), https://tc39.es/ecma262/#sec-atomics.load
|
||||||
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::load)
|
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::load)
|
||||||
{
|
{
|
||||||
|
|
|
@ -21,6 +21,7 @@ public:
|
||||||
private:
|
private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(add);
|
JS_DECLARE_NATIVE_FUNCTION(add);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(and_);
|
JS_DECLARE_NATIVE_FUNCTION(and_);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(exchange);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(load);
|
JS_DECLARE_NATIVE_FUNCTION(load);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(or_);
|
JS_DECLARE_NATIVE_FUNCTION(or_);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(store);
|
JS_DECLARE_NATIVE_FUNCTION(store);
|
||||||
|
|
|
@ -125,6 +125,7 @@ namespace JS {
|
||||||
P(escape) \
|
P(escape) \
|
||||||
P(eval) \
|
P(eval) \
|
||||||
P(every) \
|
P(every) \
|
||||||
|
P(exchange) \
|
||||||
P(exec) \
|
P(exec) \
|
||||||
P(exp) \
|
P(exp) \
|
||||||
P(expm1) \
|
P(expm1) \
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
test("invariants", () => {
|
||||||
|
expect(Atomics.exchange).toHaveLength(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("error cases", () => {
|
||||||
|
expect(() => {
|
||||||
|
Atomics.exchange("not an array", 0, 0);
|
||||||
|
}).toThrow(TypeError);
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
const bad_array_type = new Float32Array(4);
|
||||||
|
Atomics.exchange(bad_array_type, 0, 0);
|
||||||
|
}).toThrow(TypeError);
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
const bad_array_type = new Uint8ClampedArray(4);
|
||||||
|
Atomics.exchange(bad_array_type, 0, 0);
|
||||||
|
}).toThrow(TypeError);
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
const array = new Int32Array(4);
|
||||||
|
Atomics.exchange(array, 100, 0);
|
||||||
|
}).toThrow(RangeError);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality (non-BigInt)", () => {
|
||||||
|
[Int8Array, Int16Array, Int32Array, Uint8Array, Uint16Array, Uint32Array].forEach(ArrayType => {
|
||||||
|
const array = new ArrayType(4);
|
||||||
|
array[0] = 1;
|
||||||
|
array[1] = 2;
|
||||||
|
array[2] = 3;
|
||||||
|
array[3] = 4;
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, 0, 5)).toBe(1);
|
||||||
|
expect(array).toEqual([5, 2, 3, 4]);
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, 0, 6)).toBe(5);
|
||||||
|
expect(array).toEqual([6, 2, 3, 4]);
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, "1", 7)).toBe(2);
|
||||||
|
expect(array).toEqual([6, 7, 3, 4]);
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, 2, "8")).toBe(3);
|
||||||
|
expect(array).toEqual([6, 7, 8, 4]);
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, 3.14, 9)).toBe(4);
|
||||||
|
expect(array).toEqual([6, 7, 8, 9]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("basic functionality (BigInt)", () => {
|
||||||
|
[BigInt64Array, BigUint64Array].forEach(ArrayType => {
|
||||||
|
const array = new ArrayType(4);
|
||||||
|
array[0] = 1n;
|
||||||
|
array[1] = 2n;
|
||||||
|
array[2] = 3n;
|
||||||
|
array[3] = 4n;
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, 0, 5n)).toBe(1n);
|
||||||
|
expect(array).toEqual([5n, 2n, 3n, 4n]);
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, 0, 6n)).toBe(5n);
|
||||||
|
expect(array).toEqual([6n, 2n, 3n, 4n]);
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, 1, 7n)).toBe(2n);
|
||||||
|
expect(array).toEqual([6n, 7n, 3n, 4n]);
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, 2, 8n)).toBe(3n);
|
||||||
|
expect(array).toEqual([6n, 7n, 8n, 4n]);
|
||||||
|
|
||||||
|
expect(Atomics.exchange(array, 3, 9n)).toBe(4n);
|
||||||
|
expect(array).toEqual([6n, 7n, 8n, 9n]);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue