LibJS: Add Float{32,64}Array

This commit is contained in:
Linus Groh 2020-12-05 22:28:10 +00:00 committed by Andreas Kling
parent 4dcd23c2be
commit a70aacd7c3
Notes: sideshowbarker 2024-07-19 01:02:39 +09:00
6 changed files with 48 additions and 15 deletions

View file

@ -73,13 +73,15 @@
__JS_ENUMERATE(TypeError, type_error, TypeErrorPrototype, TypeErrorConstructor, void) \
__JS_ENUMERATE(URIError, uri_error, URIErrorPrototype, URIErrorConstructor, void)
#define JS_ENUMERATE_TYPED_ARRAYS \
__JS_ENUMERATE(Uint8Array, uint8_array, Uint8ArrayPrototype, Uint8ArrayConstructor, u8) \
__JS_ENUMERATE(Uint16Array, uint16_array, Uint16ArrayPrototype, Uint16ArrayConstructor, u16) \
__JS_ENUMERATE(Uint32Array, uint32_array, Uint32ArrayPrototype, Uint32ArrayConstructor, u32) \
__JS_ENUMERATE(Int8Array, int8_array, Int8ArrayPrototype, Int8ArrayConstructor, i8) \
__JS_ENUMERATE(Int16Array, int16_array, Int16ArrayPrototype, Int16ArrayConstructor, i16) \
__JS_ENUMERATE(Int32Array, int32_array, Int32ArrayPrototype, Int32ArrayConstructor, i32)
#define JS_ENUMERATE_TYPED_ARRAYS \
__JS_ENUMERATE(Uint8Array, uint8_array, Uint8ArrayPrototype, Uint8ArrayConstructor, u8) \
__JS_ENUMERATE(Uint16Array, uint16_array, Uint16ArrayPrototype, Uint16ArrayConstructor, u16) \
__JS_ENUMERATE(Uint32Array, uint32_array, Uint32ArrayPrototype, Uint32ArrayConstructor, u32) \
__JS_ENUMERATE(Int8Array, int8_array, Int8ArrayPrototype, Int8ArrayConstructor, i8) \
__JS_ENUMERATE(Int16Array, int16_array, Int16ArrayPrototype, Int16ArrayConstructor, i16) \
__JS_ENUMERATE(Int32Array, int32_array, Int32ArrayPrototype, Int32ArrayConstructor, i32) \
__JS_ENUMERATE(Float32Array, float32_array, Float32ArrayPrototype, Float32ArrayConstructor, float) \
__JS_ENUMERATE(Float64Array, float64_array, Float64ArrayPrototype, Float64ArrayConstructor, double)
#define JS_ENUMERATE_ITERATOR_PROTOTYPES \
__JS_ENUMERATE(Iterator, iterator) \

View file

@ -80,7 +80,7 @@ public:
if (vm().exception())
return {};
data()[property_index] = number;
} else if constexpr (sizeof(T) == 4) {
} else if constexpr (sizeof(T) == 4 || sizeof(T) == 8) {
auto number = value.to_double(global_object());
if (vm().exception())
return {};
@ -99,9 +99,11 @@ public:
if constexpr (sizeof(T) < 4) {
return Value((i32)data()[property_index]);
} else if constexpr (sizeof(T) == 4) {
} else if constexpr (sizeof(T) == 4 || sizeof(T) == 8) {
auto value = data()[property_index];
if constexpr (NumericLimits<T>::is_signed()) {
if constexpr (IsFloatingPoint<T>::value) {
return Value((double)value);
} else if constexpr (NumericLimits<T>::is_signed()) {
if (value > NumericLimits<i32>::max() || value < NumericLimits<i32>::min())
return Value((double)value);
} else {

View file

@ -1,5 +1,14 @@
// Update when more typed arrays get added
const TYPED_ARRAYS = [Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array];
const TYPED_ARRAYS = [
Uint8Array,
Uint16Array,
Uint32Array,
Int8Array,
Int16Array,
Int32Array,
Float32Array,
Float64Array,
];
test("basic functionality", () => {
expect(ArrayBuffer.isView).toHaveLength(1);

View file

@ -5,4 +5,6 @@ test("basic functionality", () => {
expect(Int8Array.BYTES_PER_ELEMENT).toBe(1);
expect(Int16Array.BYTES_PER_ELEMENT).toBe(2);
expect(Int32Array.BYTES_PER_ELEMENT).toBe(4);
expect(Float32Array.BYTES_PER_ELEMENT).toBe(4);
expect(Float64Array.BYTES_PER_ELEMENT).toBe(8);
});

View file

@ -1,5 +1,14 @@
// Update when more typed arrays get added
const TYPED_ARRAYS = [Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array];
const TYPED_ARRAYS = [
Uint8Array,
Uint16Array,
Uint32Array,
Int8Array,
Int16Array,
Int32Array,
Float32Array,
Float64Array,
];
const getTypedArrayConstructor = () => Object.getPrototypeOf(TYPED_ARRAYS[0]);
@ -65,9 +74,9 @@ test("typed array from ArrayBuffer with custom length and offset", () => {
const uint8ArrayAll = new Uint8Array(arrayBuffer);
const uint16ArrayPartial = new Uint16Array(arrayBuffer, 2, 4);
// Affects two bytes of the buffer, beginning at offset
uint16ArrayPartial[0] = 52651
uint16ArrayPartial[0] = 52651;
// Out of relative bounds, doesn't affect buffer
uint16ArrayPartial[4] = 123
uint16ArrayPartial[4] = 123;
expect(uint8ArrayAll[0]).toBe(0);
expect(uint8ArrayAll[1]).toBe(0);
expect(uint8ArrayAll[2]).toBe(0xab);

View file

@ -1,5 +1,14 @@
// Update when more typed arrays get added
const TYPED_ARRAYS = [Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array];
const TYPED_ARRAYS = [
Uint8Array,
Uint16Array,
Uint32Array,
Int8Array,
Int16Array,
Int32Array,
Float32Array,
Float64Array,
];
test("basic functionality", () => {
TYPED_ARRAYS.forEach(T => {