mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
LibJS: Implement Atomics.sub
This commit is contained in:
parent
f9d8e234b2
commit
d2f6255b91
3 changed files with 94 additions and 0 deletions
|
@ -121,6 +121,7 @@ void AtomicsObject::initialize(GlobalObject& global_object)
|
||||||
define_native_function(vm.names.and_, and_, 3, attr);
|
define_native_function(vm.names.and_, and_, 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.sub, sub, 3, attr);
|
||||||
|
|
||||||
// 25.4.15 Atomics [ @@toStringTag ], https://tc39.es/ecma262/#sec-atomics-@@tostringtag
|
// 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);
|
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();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(and_);
|
JS_DECLARE_NATIVE_FUNCTION(and_);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(load);
|
JS_DECLARE_NATIVE_FUNCTION(load);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(or_);
|
JS_DECLARE_NATIVE_FUNCTION(or_);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(sub);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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]);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue