mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +00:00
LibJS: Add Float{32,64}Array
This commit is contained in:
parent
4dcd23c2be
commit
a70aacd7c3
6 changed files with 48 additions and 15 deletions
|
@ -73,13 +73,15 @@
|
||||||
__JS_ENUMERATE(TypeError, type_error, TypeErrorPrototype, TypeErrorConstructor, void) \
|
__JS_ENUMERATE(TypeError, type_error, TypeErrorPrototype, TypeErrorConstructor, void) \
|
||||||
__JS_ENUMERATE(URIError, uri_error, URIErrorPrototype, URIErrorConstructor, void)
|
__JS_ENUMERATE(URIError, uri_error, URIErrorPrototype, URIErrorConstructor, void)
|
||||||
|
|
||||||
#define JS_ENUMERATE_TYPED_ARRAYS \
|
#define JS_ENUMERATE_TYPED_ARRAYS \
|
||||||
__JS_ENUMERATE(Uint8Array, uint8_array, Uint8ArrayPrototype, Uint8ArrayConstructor, u8) \
|
__JS_ENUMERATE(Uint8Array, uint8_array, Uint8ArrayPrototype, Uint8ArrayConstructor, u8) \
|
||||||
__JS_ENUMERATE(Uint16Array, uint16_array, Uint16ArrayPrototype, Uint16ArrayConstructor, u16) \
|
__JS_ENUMERATE(Uint16Array, uint16_array, Uint16ArrayPrototype, Uint16ArrayConstructor, u16) \
|
||||||
__JS_ENUMERATE(Uint32Array, uint32_array, Uint32ArrayPrototype, Uint32ArrayConstructor, u32) \
|
__JS_ENUMERATE(Uint32Array, uint32_array, Uint32ArrayPrototype, Uint32ArrayConstructor, u32) \
|
||||||
__JS_ENUMERATE(Int8Array, int8_array, Int8ArrayPrototype, Int8ArrayConstructor, i8) \
|
__JS_ENUMERATE(Int8Array, int8_array, Int8ArrayPrototype, Int8ArrayConstructor, i8) \
|
||||||
__JS_ENUMERATE(Int16Array, int16_array, Int16ArrayPrototype, Int16ArrayConstructor, i16) \
|
__JS_ENUMERATE(Int16Array, int16_array, Int16ArrayPrototype, Int16ArrayConstructor, i16) \
|
||||||
__JS_ENUMERATE(Int32Array, int32_array, Int32ArrayPrototype, Int32ArrayConstructor, i32)
|
__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 \
|
#define JS_ENUMERATE_ITERATOR_PROTOTYPES \
|
||||||
__JS_ENUMERATE(Iterator, iterator) \
|
__JS_ENUMERATE(Iterator, iterator) \
|
||||||
|
|
|
@ -80,7 +80,7 @@ public:
|
||||||
if (vm().exception())
|
if (vm().exception())
|
||||||
return {};
|
return {};
|
||||||
data()[property_index] = number;
|
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());
|
auto number = value.to_double(global_object());
|
||||||
if (vm().exception())
|
if (vm().exception())
|
||||||
return {};
|
return {};
|
||||||
|
@ -99,9 +99,11 @@ public:
|
||||||
|
|
||||||
if constexpr (sizeof(T) < 4) {
|
if constexpr (sizeof(T) < 4) {
|
||||||
return Value((i32)data()[property_index]);
|
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];
|
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())
|
if (value > NumericLimits<i32>::max() || value < NumericLimits<i32>::min())
|
||||||
return Value((double)value);
|
return Value((double)value);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,5 +1,14 @@
|
||||||
// Update when more typed arrays get added
|
// 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", () => {
|
test("basic functionality", () => {
|
||||||
expect(ArrayBuffer.isView).toHaveLength(1);
|
expect(ArrayBuffer.isView).toHaveLength(1);
|
||||||
|
|
|
@ -5,4 +5,6 @@ test("basic functionality", () => {
|
||||||
expect(Int8Array.BYTES_PER_ELEMENT).toBe(1);
|
expect(Int8Array.BYTES_PER_ELEMENT).toBe(1);
|
||||||
expect(Int16Array.BYTES_PER_ELEMENT).toBe(2);
|
expect(Int16Array.BYTES_PER_ELEMENT).toBe(2);
|
||||||
expect(Int32Array.BYTES_PER_ELEMENT).toBe(4);
|
expect(Int32Array.BYTES_PER_ELEMENT).toBe(4);
|
||||||
|
expect(Float32Array.BYTES_PER_ELEMENT).toBe(4);
|
||||||
|
expect(Float64Array.BYTES_PER_ELEMENT).toBe(8);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,14 @@
|
||||||
// Update when more typed arrays get added
|
// 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]);
|
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 uint8ArrayAll = new Uint8Array(arrayBuffer);
|
||||||
const uint16ArrayPartial = new Uint16Array(arrayBuffer, 2, 4);
|
const uint16ArrayPartial = new Uint16Array(arrayBuffer, 2, 4);
|
||||||
// Affects two bytes of the buffer, beginning at offset
|
// Affects two bytes of the buffer, beginning at offset
|
||||||
uint16ArrayPartial[0] = 52651
|
uint16ArrayPartial[0] = 52651;
|
||||||
// Out of relative bounds, doesn't affect buffer
|
// Out of relative bounds, doesn't affect buffer
|
||||||
uint16ArrayPartial[4] = 123
|
uint16ArrayPartial[4] = 123;
|
||||||
expect(uint8ArrayAll[0]).toBe(0);
|
expect(uint8ArrayAll[0]).toBe(0);
|
||||||
expect(uint8ArrayAll[1]).toBe(0);
|
expect(uint8ArrayAll[1]).toBe(0);
|
||||||
expect(uint8ArrayAll[2]).toBe(0xab);
|
expect(uint8ArrayAll[2]).toBe(0xab);
|
||||||
|
|
|
@ -1,5 +1,14 @@
|
||||||
// Update when more typed arrays get added
|
// 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", () => {
|
test("basic functionality", () => {
|
||||||
TYPED_ARRAYS.forEach(T => {
|
TYPED_ARRAYS.forEach(T => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue