diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-07-11 13:36:40 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-14 20:44:42 +0100 |
commit | b6364ec8992ecf524767a0d4fc55c9cd2659e014 (patch) | |
tree | 424a931c5dfb8daa71773335c4376bacd25a3538 /Userland/Libraries | |
parent | d2f6255b915d8d33b00ba8a00755139eee87373e (diff) | |
download | serenity-b6364ec8992ecf524767a0d4fc55c9cd2659e014.zip |
LibJS: Implement Atomics.xor
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 74ba487528..68b3f84943 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp @@ -122,6 +122,7 @@ void AtomicsObject::initialize(GlobalObject& global_object) 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); + define_native_function(vm.names.xor_, xor_, 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); @@ -222,4 +223,22 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::sub) VERIFY_NOT_REACHED(); } +// 25.4.14 Atomics.xor ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.xor +JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::xor_) +{ + auto* typed_array = typed_array_from(global_object, vm.argument(0)); + if (!typed_array) + return {}; + + auto atomic_xor = [](auto* storage, auto value) { return AK::atomic_fetch_xor(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_xor)); + 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 8cd0b5baf6..96d66bd6c1 100644 --- a/Userland/Libraries/LibJS/Runtime/AtomicsObject.h +++ b/Userland/Libraries/LibJS/Runtime/AtomicsObject.h @@ -24,6 +24,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(load); JS_DECLARE_NATIVE_FUNCTION(or_); JS_DECLARE_NATIVE_FUNCTION(sub); + JS_DECLARE_NATIVE_FUNCTION(xor_); }; } diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 640298b1f8..0675720124 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -359,6 +359,7 @@ struct CommonPropertyNames { PropertyName register_ { "register", PropertyName::StringMayBeNumber::No }; PropertyName return_ { "return", PropertyName::StringMayBeNumber::No }; PropertyName throw_ { "throw", PropertyName::StringMayBeNumber::No }; + PropertyName xor_ { "xor", PropertyName::StringMayBeNumber::No }; #define __ENUMERATE(x) PropertyName x { #x, PropertyName::StringMayBeNumber::No }; ENUMERATE_STANDARD_PROPERTY_NAMES(__ENUMERATE) #undef __ENUMERATE diff --git a/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.xor.js b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.xor.js new file mode 100644 index 0000000000..5a10ee706e --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Atomics/Atomics.xor.js @@ -0,0 +1,70 @@ +test("invariants", () => { + expect(Atomics.xor).toHaveLength(3); +}); + +test("error cases", () => { + expect(() => { + Atomics.xor("not an array", 0, 1); + }).toThrow(TypeError); + + expect(() => { + const bad_array_type = new Float32Array(4); + Atomics.xor(bad_array_type, 0, 1); + }).toThrow(TypeError); + + expect(() => { + const bad_array_type = new Uint8ClampedArray(4); + Atomics.xor(bad_array_type, 0, 1); + }).toThrow(TypeError); + + expect(() => { + const array = new Int32Array(4); + Atomics.xor(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.xor(array, 0, 0b0000)).toBe(0b0000); + expect(array).toEqual([0b0000, 0b0101]); + + expect(Atomics.xor(array, 0, 0b1111)).toBe(0b0000); + expect(array).toEqual([0b1111, 0b0101]); + + expect(Atomics.xor(array, 1, 0b0000)).toBe(0b0101); + expect(array).toEqual([0b1111, 0b0101]); + + expect(Atomics.xor(array, 1, 0b0100)).toBe(0b0101); + expect(array).toEqual([0b1111, 0b0001]); + + expect(Atomics.xor(array, 1, 0b0001)).toBe(0b0001); + expect(array).toEqual([0b1111, 0b0000]); + }); +}); + +test("basic functionality (BigInt)", () => { + [BigInt64Array, BigUint64Array].forEach(ArrayType => { + const array = new ArrayType(2); + array[0] = 0b0000n; + array[1] = 0b0101n; + + expect(Atomics.xor(array, 0, 0b0000n)).toBe(0b0000n); + expect(array).toEqual([0b0000n, 0b0101n]); + + expect(Atomics.xor(array, 0, 0b1111n)).toBe(0b0000n); + expect(array).toEqual([0b1111n, 0b0101n]); + + expect(Atomics.xor(array, 1, 0b0000n)).toBe(0b0101n); + expect(array).toEqual([0b1111n, 0b0101n]); + + expect(Atomics.xor(array, 1, 0b0100n)).toBe(0b0101n); + expect(array).toEqual([0b1111n, 0b0001n]); + + expect(Atomics.xor(array, 1, 0b0001n)).toBe(0b0001n); + expect(array).toEqual([0b1111n, 0b0000n]); + }); +}); |