Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/engine/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl ArrayIterator {
.into());
};

f.array_length(buf.len())
f.array_length(buf.len()) as u64
} else {
array_iterator.array.length_of_array_like(context)?
};
Expand Down
5 changes: 4 additions & 1 deletion core/engine/src/builtins/atomics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,9 @@ fn validate_atomic_access(

// 2. Let accessIndex be ? ToIndex(requestIndex).
let access_index = request_index.to_index(context)?;
let access_index: usize = access_index
.try_into()
.map_err(|_| JsNativeError::range().with_message("index too large"))?;

// 3. Assert: accessIndex ≥ 0.
// ensured by the type.
Expand All @@ -698,7 +701,7 @@ fn validate_atomic_access(
}

// 8. Return (accessIndex × elementSize) + offset.
let offset = ((access_index * kind.element_size()) + offset) as usize;
let offset = (access_index * kind.element_size()) + offset;
Ok(AtomicAccess {
byte_offset: offset,
kind,
Expand Down
153 changes: 94 additions & 59 deletions core/engine/src/builtins/typed_array/builtin.rs

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions core/engine/src/builtins/typed_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,19 +466,19 @@ impl TypedArrayKind {
}

/// Gets the size of the type of element of this `TypedArrayKind`.
pub(crate) const fn element_size(self) -> u64 {
pub(crate) const fn element_size(self) -> usize {
match self {
TypedArrayKind::Int8 | TypedArrayKind::Uint8 | TypedArrayKind::Uint8Clamped => {
size_of::<u8>() as u64
size_of::<u8>()
}
TypedArrayKind::Int16 | TypedArrayKind::Uint16 => size_of::<u16>() as u64,
TypedArrayKind::Int16 | TypedArrayKind::Uint16 => size_of::<u16>(),
#[cfg(feature = "float16")]
TypedArrayKind::Float16 => size_of::<u16>() as u64,
TypedArrayKind::Float16 => size_of::<u16>(),
TypedArrayKind::Int32 | TypedArrayKind::Uint32 | TypedArrayKind::Float32 => {
size_of::<u32>() as u64
size_of::<u32>()
}
TypedArrayKind::BigInt64 | TypedArrayKind::BigUint64 | TypedArrayKind::Float64 => {
size_of::<u64>() as u64
size_of::<u64>()
}
}
}
Expand Down
36 changes: 16 additions & 20 deletions core/engine/src/builtins/typed_array/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ use super::{TypedArrayKind, is_valid_integer_index};
pub struct TypedArray {
viewed_array_buffer: BufferObject,
kind: TypedArrayKind,
byte_offset: u64,
byte_length: Option<u64>,
array_length: Option<u64>,
byte_offset: usize,
byte_length: Option<usize>,
array_length: Option<usize>,
}

impl JsData for TypedArray {
Expand All @@ -59,9 +59,9 @@ impl TypedArray {
pub(crate) const fn new(
viewed_array_buffer: BufferObject,
kind: TypedArrayKind,
byte_offset: u64,
byte_length: Option<u64>,
array_length: Option<u64>,
byte_offset: usize,
byte_length: Option<usize>,
array_length: Option<usize>,
) -> Self {
Self {
viewed_array_buffer,
Expand All @@ -81,9 +81,6 @@ impl TypedArray {
///
/// [spec]: https://tc39.es/ecma262/sec-istypedarrayoutofbounds
pub(crate) fn is_out_of_bounds(&self, buf_byte_len: usize) -> bool {
// Checks when allocating the buffer ensure the length fits inside an `u64`.
let buf_byte_len = buf_byte_len as u64;

// 1. Let O be taRecord.[[Object]].
// 2. Let bufferByteLength be taRecord.[[CachedBufferByteLength]].
// 3. Assert: IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is true if and only if bufferByteLength is detached.
Expand Down Expand Up @@ -112,7 +109,7 @@ impl TypedArray {

/// Get the `TypedArray` object's byte offset.
#[must_use]
pub const fn byte_offset(&self) -> u64 {
pub const fn byte_offset(&self) -> usize {
self.byte_offset
}

Expand All @@ -134,7 +131,7 @@ impl TypedArray {
///
/// [spec]: https://tc39.es/ecma262/#sec-typedarraybytelength
#[must_use]
pub fn byte_length(&self, buf_byte_len: usize) -> u64 {
pub fn byte_length(&self, buf_byte_len: usize) -> usize {
// 1. If IsTypedArrayOutOfBounds(taRecord) is true, return 0.
if self.is_out_of_bounds(buf_byte_len) {
return 0;
Expand Down Expand Up @@ -168,10 +165,9 @@ impl TypedArray {
///
/// [spec]: https://tc39.es/ecma262/#sec-typedarraylength
#[must_use]
pub fn array_length(&self, buf_byte_len: usize) -> u64 {
pub fn array_length(&self, buf_byte_len: usize) -> usize {
// 1. Assert: IsTypedArrayOutOfBounds(taRecord) is false.
debug_assert!(!self.is_out_of_bounds(buf_byte_len));
let buf_byte_len = buf_byte_len as u64;

// 2. Let O be taRecord.[[Object]].

Expand Down Expand Up @@ -229,7 +225,7 @@ impl TypedArray {
///
/// Note: if this is only used for bounds checking, it is recommended to use
/// the `Ordering::Relaxed` ordering to get the buffer slice.
pub(crate) fn validate_index(&self, index: f64, buf_len: usize) -> Option<u64> {
pub(crate) fn validate_index(&self, index: f64, buf_len: usize) -> Option<usize> {
// 2. If IsIntegralNumber(index) is false, return false.
if index.is_nan() || index.is_infinite() || index.fract() != 0.0 {
return None;
Expand All @@ -254,15 +250,15 @@ impl TypedArray {
}

// 9. Return true.
Some(index as u64)
Some(index as usize) // index is already validated to be within usize range (buf_len is usize)
}

/// Validates a `u64` index to be in bounds for the inner buffer of this `TypedArray`.
///
/// This is an optimized variant of [`validate_index`](Self::validate_index) for cases where
/// the index is already known to be a non-negative integer (`u64`), skipping the redundant
/// checks for `NaN`, infinity, fractional values, and negative zero.
pub(crate) fn validate_index_u64(&self, index: u64, buf_len: usize) -> Option<u64> {
pub(crate) fn validate_index_usize(&self, index: u64, buf_len: usize) -> Option<usize> {
// 1. If IsTypedArrayOutOfBounds(taRecord) is true, return false.
if self.is_out_of_bounds(buf_len) {
return None;
Expand All @@ -272,12 +268,12 @@ impl TypedArray {
let length = self.array_length(buf_len);

// 3. If index ≥ length, return false.
if index >= length {
if index >= length as u64 {
return None;
}

// 4. Return true.
Some(index)
Some(index as usize)
}
}

Expand Down Expand Up @@ -682,7 +678,7 @@ fn typed_array_get_element(obj: &JsObject, index: f64) -> JsResult<Option<JsValu
let size = inner.kind.element_size();

// 4. Let byteIndexInBuffer be (ℝ(index) × elementSize) + offset.
let byte_index = ((index * size) + offset) as usize;
let byte_index = (index * size) + offset;

// 5. Let elementType be TypedArrayElementType(O).
let elem_type = inner.kind();
Expand Down Expand Up @@ -741,7 +737,7 @@ pub(crate) fn typed_array_set_element(
let size = elem_type.element_size();

// c. Let byteIndexInBuffer be (ℝ(index) × elementSize) + offset.
let byte_index = ((index * size) + offset) as usize;
let byte_index = (index * size) + offset;

// e. Perform SetValueInBuffer(O.[[ViewedArrayBuffer]], byteIndexInBuffer, elementType, numValue, true, unordered).
// SAFETY: The TypedArray object guarantees that the buffer is aligned.
Expand Down
4 changes: 2 additions & 2 deletions core/engine/src/object/builtins/jstypedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,8 +1211,8 @@ impl JsUint8Array {
.with_message("typed array is outside the bounds of its inner buffer")
.into());
}
let byte_offset = ta.byte_offset() as usize;
let byte_len = ta.byte_length(buf_byte_len) as usize;
let byte_offset = ta.byte_offset();
let byte_len = ta.byte_length(buf_byte_len);
slice.subslice(byte_offset..byte_offset + byte_len).to_vec()
};
Ok(vec)
Expand Down
6 changes: 3 additions & 3 deletions core/engine/src/value/display/typed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ pub(super) fn log_typed_array(

write!(f, "{type_name}({length}) [ ")?;

let offset = inner.byte_offset() as usize;
let elem_size = kind.element_size() as usize;
let offset = inner.byte_offset();
let elem_size = kind.element_size();

for i in 0..length as usize {
for i in 0..length {
if i > 0 {
f.write_str(", ")?;
}
Expand Down
Loading