LibJS: Implement Atomics.store
This commit is contained in:
parent
b6364ec899
commit
6211eb0f9a
Notes:
sideshowbarker
2024-07-18 09:02:07 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/6211eb0f9a7 Pull-request: https://github.com/SerenityOS/serenity/pull/8656 Reviewed-by: https://github.com/linusg ✅
4 changed files with 99 additions and 0 deletions
Userland/Libraries/LibJS
|
@ -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.store, store, 3, attr);
|
||||
define_native_function(vm.names.sub, sub, 3, attr);
|
||||
define_native_function(vm.names.xor_, xor_, 3, attr);
|
||||
|
||||
|
@ -205,6 +206,42 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::or_)
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
// 25.4.10 Atomics.store ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.store
|
||||
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::store)
|
||||
{
|
||||
auto* typed_array = typed_array_from(global_object, vm.argument(0));
|
||||
if (!typed_array)
|
||||
return {};
|
||||
|
||||
validate_integer_typed_array(global_object, *typed_array);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
auto indexed_position = validate_atomic_access(global_object, *typed_array, vm.argument(1));
|
||||
if (!indexed_position.has_value())
|
||||
return {};
|
||||
|
||||
auto value = vm.argument(2);
|
||||
Value value_to_set;
|
||||
if (typed_array->content_type() == TypedArrayBase::ContentType::BigInt) {
|
||||
value_to_set = value.to_bigint(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
} else {
|
||||
value_to_set = Value(value.to_integer_or_infinity(global_object));
|
||||
if (vm.exception())
|
||||
return {};
|
||||
}
|
||||
|
||||
if (typed_array->viewed_array_buffer()->is_detached()) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
|
||||
return {};
|
||||
}
|
||||
|
||||
typed_array->set_value_in_buffer(*indexed_position, value_to_set, ArrayBuffer::Order::SeqCst, true);
|
||||
return value_to_set;
|
||||
}
|
||||
|
||||
// 25.4.11 Atomics.sub ( typedArray, index, value ), https://tc39.es/ecma262/#sec-atomics.sub
|
||||
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::sub)
|
||||
{
|
||||
|
|
|
@ -23,6 +23,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(and_);
|
||||
JS_DECLARE_NATIVE_FUNCTION(load);
|
||||
JS_DECLARE_NATIVE_FUNCTION(or_);
|
||||
JS_DECLARE_NATIVE_FUNCTION(store);
|
||||
JS_DECLARE_NATIVE_FUNCTION(sub);
|
||||
JS_DECLARE_NATIVE_FUNCTION(xor_);
|
||||
};
|
||||
|
|
|
@ -306,6 +306,7 @@ namespace JS {
|
|||
P(sqrt) \
|
||||
P(startsWith) \
|
||||
P(sticky) \
|
||||
P(store) \
|
||||
P(strike) \
|
||||
P(stringify) \
|
||||
P(sub) \
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
test("invariants", () => {
|
||||
expect(Atomics.store).toHaveLength(3);
|
||||
});
|
||||
|
||||
test("error cases", () => {
|
||||
expect(() => {
|
||||
Atomics.store("not an array", 0);
|
||||
}).toThrow(TypeError);
|
||||
|
||||
expect(() => {
|
||||
const bad_array_type = new Float32Array(4);
|
||||
Atomics.store(bad_array_type, 0);
|
||||
}).toThrow(TypeError);
|
||||
|
||||
expect(() => {
|
||||
const bad_array_type = new Uint8ClampedArray(4);
|
||||
Atomics.store(bad_array_type, 0);
|
||||
}).toThrow(TypeError);
|
||||
|
||||
expect(() => {
|
||||
const array = new Int32Array(4);
|
||||
Atomics.store(array, 100);
|
||||
}).toThrow(RangeError);
|
||||
});
|
||||
|
||||
test("basic functionality (non-BigInt)", () => {
|
||||
[Int8Array, Int16Array, Int32Array, Uint8Array, Uint16Array, Uint32Array].forEach(ArrayType => {
|
||||
const array = new ArrayType(4);
|
||||
|
||||
expect(Atomics.store(array, 0, 1)).toBe(1);
|
||||
expect(array[0]).toBe(1);
|
||||
|
||||
expect(Atomics.store(array, 1, 2)).toBe(2);
|
||||
expect(array[1]).toBe(2);
|
||||
|
||||
expect(Atomics.store(array, 2, 3)).toBe(3);
|
||||
expect(array[2]).toBe(3);
|
||||
|
||||
expect(Atomics.store(array, 3, 4)).toBe(4);
|
||||
expect(array[3]).toBe(4);
|
||||
});
|
||||
});
|
||||
|
||||
test("basic functionality (BigInt)", () => {
|
||||
[BigInt64Array, BigUint64Array].forEach(ArrayType => {
|
||||
const array = new ArrayType(4);
|
||||
|
||||
expect(Atomics.store(array, 0, 1n)).toBe(1n);
|
||||
expect(array[0]).toBe(1n);
|
||||
|
||||
expect(Atomics.store(array, 1, 2n)).toBe(2n);
|
||||
expect(array[1]).toBe(2n);
|
||||
|
||||
expect(Atomics.store(array, 2, 3n)).toBe(3n);
|
||||
expect(array[2]).toBe(3n);
|
||||
|
||||
expect(Atomics.store(array, 3, 4n)).toBe(4n);
|
||||
expect(array[3]).toBe(4n);
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue