diff --git a/Libraries/LibJS/Forward.h b/Libraries/LibJS/Forward.h index 34ec9d2952a..42756076043 100644 --- a/Libraries/LibJS/Forward.h +++ b/Libraries/LibJS/Forward.h @@ -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) \ diff --git a/Libraries/LibJS/Runtime/TypedArray.h b/Libraries/LibJS/Runtime/TypedArray.h index fccf72b4c23..1a10a1a93f3 100644 --- a/Libraries/LibJS/Runtime/TypedArray.h +++ b/Libraries/LibJS/Runtime/TypedArray.h @@ -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::is_signed()) { + if constexpr (IsFloatingPoint::value) { + return Value((double)value); + } else if constexpr (NumericLimits::is_signed()) { if (value > NumericLimits::max() || value < NumericLimits::min()) return Value((double)value); } else { diff --git a/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.isView.js b/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.isView.js index c7056b88b28..db1be84067e 100644 --- a/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.isView.js +++ b/Libraries/LibJS/Tests/builtins/ArrayBuffer/ArrayBuffer.isView.js @@ -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); diff --git a/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.BYTES_PER_ELEMENT.js b/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.BYTES_PER_ELEMENT.js index 82c207e5dc3..c53213ac070 100644 --- a/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.BYTES_PER_ELEMENT.js +++ b/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.BYTES_PER_ELEMENT.js @@ -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); }); diff --git a/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js b/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js index 09884a6f81a..d443d13cd66 100644 --- a/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js +++ b/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js @@ -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); diff --git a/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.length.js b/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.length.js index bbe85a2bf1c..ce02c72391c 100644 --- a/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.length.js +++ b/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.length.js @@ -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 => {