mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
LibJS: Implement Atomics.load
This commit is contained in:
parent
cc3b96743a
commit
940875c9fd
Notes:
sideshowbarker
2024-07-18 09:02:25 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/940875c9fd7 Pull-request: https://github.com/SerenityOS/serenity/pull/8656 Reviewed-by: https://github.com/linusg ✅
4 changed files with 80 additions and 0 deletions
|
@ -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.load, load, 2, 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);
|
||||
|
@ -141,4 +142,27 @@ JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::add)
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
// 25.4.8 Atomics.load ( typedArray, index ), https://tc39.es/ecma262/#sec-atomics.load
|
||||
JS_DEFINE_NATIVE_FUNCTION(AtomicsObject::load)
|
||||
{
|
||||
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 {};
|
||||
|
||||
if (typed_array->viewed_array_buffer()->is_detached()) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
|
||||
return {};
|
||||
}
|
||||
|
||||
return typed_array->get_value_from_buffer(*indexed_position, ArrayBuffer::Order::SeqCst, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ public:
|
|||
|
||||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(add);
|
||||
JS_DECLARE_NATIVE_FUNCTION(load);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -224,6 +224,7 @@ namespace JS {
|
|||
P(lastIndexOf) \
|
||||
P(length) \
|
||||
P(link) \
|
||||
P(load) \
|
||||
P(log) \
|
||||
P(log1p) \
|
||||
P(log2) \
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
test("invariants", () => {
|
||||
expect(Atomics.load).toHaveLength(2);
|
||||
});
|
||||
|
||||
test("error cases", () => {
|
||||
expect(() => {
|
||||
Atomics.load("not an array", 0);
|
||||
}).toThrow(TypeError);
|
||||
|
||||
expect(() => {
|
||||
const bad_array_type = new Float32Array(4);
|
||||
Atomics.load(bad_array_type, 0);
|
||||
}).toThrow(TypeError);
|
||||
|
||||
expect(() => {
|
||||
const bad_array_type = new Uint8ClampedArray(4);
|
||||
Atomics.load(bad_array_type, 0);
|
||||
}).toThrow(TypeError);
|
||||
|
||||
expect(() => {
|
||||
const array = new Int32Array(4);
|
||||
Atomics.load(array, 100);
|
||||
}).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.load(array, 0)).toBe(1);
|
||||
expect(Atomics.load(array, 1)).toBe(2);
|
||||
expect(Atomics.load(array, 2)).toBe(3);
|
||||
expect(Atomics.load(array, 3)).toBe(4);
|
||||
});
|
||||
});
|
||||
|
||||
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.load(array, 0)).toBe(1n);
|
||||
expect(Atomics.load(array, 1)).toBe(2n);
|
||||
expect(Atomics.load(array, 2)).toBe(3n);
|
||||
expect(Atomics.load(array, 3)).toBe(4n);
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue