summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-07-12 08:08:13 -0400
committerLinus Groh <mail@linusgroh.de>2021-07-14 22:13:15 +0100
commit655ffce64df9a1823592377b2a44296571a50e7b (patch)
tree1905cd2a8787737319fab3ccba20b2dec0d2352f /Userland/Libraries
parent72365841320b9677017ba1667d7cf243f0e01b1f (diff)
downloadserenity-655ffce64df9a1823592377b2a44296571a50e7b.zip
LibJS: Implement Atomics.exchange
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp19
-rw-r--r--Userland/Libraries/LibJS/Runtime/AtomicsObject.h1
-rw-r--r--Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.exchange.js74
4 files changed, 95 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp
index 5365da1b3d..0db5093b52 100644
--- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp
@@ -119,6 +119,7 @@ void AtomicsObject::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.add, add, 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.or_, or_, 3, attr);
define_native_function(vm.names.store, store, 3, attr);
@@ -165,6 +166,24 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::and_)
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
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::load)
{
diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.h b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h
index a6ea4d7445..dca971d5f3 100644
--- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.h
+++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h
@@ -21,6 +21,7 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(add);
JS_DECLARE_NATIVE_FUNCTION(and_);
+ JS_DECLARE_NATIVE_FUNCTION(exchange);
JS_DECLARE_NATIVE_FUNCTION(load);
JS_DECLARE_NATIVE_FUNCTION(or_);
JS_DECLARE_NATIVE_FUNCTION(store);
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
index 6d6e589544..8e48de60e3 100644
--- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
+++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h
@@ -125,6 +125,7 @@ namespace JS {
P(escape) \
P(eval) \
P(every) \
+ P(exchange) \
P(exec) \
P(exp) \
P(expm1) \
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.exchange.js b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.exchange.js
new file mode 100644
index 0000000000..802524f1cc
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.exchange.js
@@ -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]);
+ });
+});