diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-07-11 14:10:43 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-14 20:44:42 +0100 |
commit | 2d3af5c1b4835258f119aa0bb3b3470f5554322c (patch) | |
tree | 8630b303cc5355630f7c5eec8efe86a32bde1456 /Userland/Libraries | |
parent | 940875c9fd704ce9d7e74d3499bbdd220841d0c5 (diff) | |
download | serenity-2d3af5c1b4835258f119aa0bb3b3470f5554322c.zip |
LibJS: Implement Atomics.and
Diffstat (limited to 'Userland/Libraries')
4 files changed, 91 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp index ce6e0dd770..c149ee7fe4 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp @@ -118,6 +118,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.load, load, 2, attr); // 25.4.15 Atomics [ @@toStringTag ], https://tc39.es/ecma262/#sec-atomics-@@tostringtag @@ -142,6 +143,24 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::add) VERIFY_NOT_REACHED(); } +// 25.4.4 Atomics.and ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.and +JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::and_) +{ + auto* typed_array = typed_array_from(global_object, vm.argument(0)); + if (!typed_array) + return {}; + + auto atomic_and = [](auto* storage, auto value) { return AK::atomic_fetch_and(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_and)); + 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 8a4e842cd9..bf15d19594 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.h +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h @@ -20,6 +20,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(add); + JS_DECLARE_NATIVE_FUNCTION(and_); JS_DECLARE_NATIVE_FUNCTION(load); }; diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index ec05f5167c..1b5fb699cd 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -351,6 +351,7 @@ namespace JS { P(writable) struct CommonPropertyNames { + PropertyName and_ { "and", PropertyName::StringMayBeNumber::No }; PropertyName catch_ { "catch", PropertyName::StringMayBeNumber::No }; PropertyName delete_ { "delete", PropertyName::StringMayBeNumber::No }; PropertyName for_ { "for", PropertyName::StringMayBeNumber::No }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.and.js b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.and.js new file mode 100644 index 0000000000..0cd25a7d5c --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.and.js @@ -0,0 +1,70 @@ +test("invariants", () => { + expect(Atomics.and).toHaveLength(3); +}); + +test("error cases", () => { + expect(() => { + Atomics.and("not an array", 0, 1); + }).toThrow(TypeError); + + expect(() => { + const bad_array_type = new Float32Array(4); + Atomics.and(bad_array_type, 0, 1); + }).toThrow(TypeError); + + expect(() => { + const bad_array_type = new Uint8ClampedArray(4); + Atomics.and(bad_array_type, 0, 1); + }).toThrow(TypeError); + + expect(() => { + const array = new Int32Array(4); + Atomics.and(array, 100, 1); + }).toThrow(RangeError); +}); + +test("basic functionality (non-BigInt)", () => { + [Int8Array, Int16Array, Int32Array, Uint8Array, Uint16Array, Uint32Array].forEach(ArrayType => { + const array = new ArrayType(2); + array[0] = 0b0000; + array[1] = 0b0101; + + expect(Atomics.and(array, 0, 0b0000)).toBe(0b0000); + expect(array).toEqual([0b0000, 0b0101]); + + expect(Atomics.and(array, 0, 0b1111)).toBe(0b0000); + expect(array).toEqual([0b0000, 0b0101]); + + expect(Atomics.and(array, 1, 0b0101)).toBe(0b0101); + expect(array).toEqual([0b0000, 0b0101]); + + expect(Atomics.and(array, 1, 0b0100)).toBe(0b0101); + expect(array).toEqual([0b0000, 0b0100]); + + expect(Atomics.and(array, 1, 0b0000)).toBe(0b0100); + expect(array).toEqual([0b0000, 0b0000]); + }); +}); + +test("basic functionality (BigInt)", () => { + [BigInt64Array, BigUint64Array].forEach(ArrayType => { + const array = new ArrayType(2); + array[0] = 0b0000n; + array[1] = 0b0101n; + + expect(Atomics.and(array, 0, 0b0000n)).toBe(0b0000n); + expect(array).toEqual([0b0000n, 0b0101n]); + + expect(Atomics.and(array, 0, 0b1111n)).toBe(0b0000n); + expect(array).toEqual([0b0000n, 0b0101n]); + + expect(Atomics.and(array, 1, 0b0101n)).toBe(0b0101n); + expect(array).toEqual([0b0000n, 0b0101n]); + + expect(Atomics.and(array, 1, 0b0100n)).toBe(0b0101n); + expect(array).toEqual([0b0000n, 0b0100n]); + + expect(Atomics.and(array, 1, 0b0000n)).toBe(0b0100n); + expect(array).toEqual([0b0000n, 0b0000n]); + }); +}); |