summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-07-11 13:28:52 -0400
committerLinus Groh <mail@linusgroh.de>2021-07-14 20:44:42 +0100
commitd2f6255b915d8d33b00ba8a00755139eee87373e (patch)
tree1a1508b28d4b4ad9b779d8c32f2f2371657e0c47
parentf9d8e234b2cbc23c6854c041262744ef5cdfd6f4 (diff)
downloadserenity-d2f6255b915d8d33b00ba8a00755139eee87373e.zip
LibJS: Implement Atomics.sub
-rw-r--r--Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp19
-rw-r--r--Userland/Libraries/LibJS/Runtime/AtomicsObject.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.sub.js74
3 files changed, 94 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp
index e1dfca91bb..74ba487528 100644
--- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp
@@ -121,6 +121,7 @@ void AtomicsObject::initialize(GlobalObject& global_object)
define_native_function(vm.names.and_, and_, 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.sub, sub, 3, attr);
// 25.4.15 Atomics [ @@toStringTag ], https://tc39.es/ecma262/#sec-atomics-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), "Atomics"), Attribute::Configurable);
@@ -203,4 +204,22 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::or_)
VERIFY_NOT_REACHED();
}
+// 25.4.11 Atomics.sub ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.sub
+JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::sub)
+{
+ auto* typed_array = typed_array_from(global_object, vm.argument(0));
+ if (!typed_array)
+ return {};
+
+ auto atomic_sub = [](auto* storage, auto value) { return AK::atomic_fetch_sub(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_sub));
+ JS_ENUMERATE_TYPED_ARRAYS
+#undef __JS_ENUMERATE
+
+ VERIFY_NOT_REACHED();
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.h b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h
index 52a989cc1c..8cd0b5baf6 100644
--- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.h
+++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h
@@ -23,6 +23,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(and_);
JS_DECLARE_NATIVE_FUNCTION(load);
JS_DECLARE_NATIVE_FUNCTION(or_);
+ JS_DECLARE_NATIVE_FUNCTION(sub);
};
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.sub.js b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.sub.js
new file mode 100644
index 0000000000..9fc75c5cda
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.sub.js
@@ -0,0 +1,74 @@
+test("invariants", () => {
+ expect(Atomics.sub).toHaveLength(3);
+});
+
+test("error cases", () => {
+ expect(() => {
+ Atomics.sub("not an array", 0, 1);
+ }).toThrow(TypeError);
+
+ expect(() => {
+ const bad_array_type = new Float32Array(4);
+ Atomics.sub(bad_array_type, 0, 1);
+ }).toThrow(TypeError);
+
+ expect(() => {
+ const bad_array_type = new Uint8ClampedArray(4);
+ Atomics.sub(bad_array_type, 0, 1);
+ }).toThrow(TypeError);
+
+ expect(() => {
+ const array = new Int32Array(4);
+ Atomics.sub(array, 100, 1);
+ }).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.sub(array, 0, 1)).toBe(1);
+ expect(array).toEqual([0, 2, 3, 4]);
+
+ expect(Atomics.sub(array, 1, 1)).toBe(2);
+ expect(array).toEqual([0, 1, 3, 4]);
+
+ expect(Atomics.sub(array, 1, 1)).toBe(1);
+ expect(array).toEqual([0, 0, 3, 4]);
+
+ expect(Atomics.sub(array, 2, 3.14)).toBe(3);
+ expect(array).toEqual([0, 0, 0, 4]);
+
+ expect(Atomics.sub(array, 3, "1")).toBe(4);
+ expect(array).toEqual([0, 0, 0, 3]);
+ });
+});
+
+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.sub(array, 0, 1n)).toBe(1n);
+ expect(array).toEqual([0n, 2n, 3n, 4n]);
+
+ expect(Atomics.sub(array, 1, 1n)).toBe(2n);
+ expect(array).toEqual([0n, 1n, 3n, 4n]);
+
+ expect(Atomics.sub(array, 1, 1n)).toBe(1n);
+ expect(array).toEqual([0n, 0n, 3n, 4n]);
+
+ expect(Atomics.sub(array, 2, 3n)).toBe(3n);
+ expect(array).toEqual([0n, 0n, 0n, 4n]);
+
+ expect(Atomics.sub(array, 3, 1n)).toBe(4n);
+ expect(array).toEqual([0n, 0n, 0n, 3n]);
+ });
+});