Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Fix length/index in 32bit architectures #2196

Closed
wants to merge 1 commit into from
Closed
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 boa_engine/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use boa_profiler::Profiler;
#[derive(Debug, Clone, Finalize, Trace)]
pub struct ArrayIterator {
array: JsObject,
next_index: usize,
next_index: u64,
kind: PropertyNameKind,
done: bool,
}
Expand Down
56 changes: 27 additions & 29 deletions boa_engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Array {
debug_assert!(number_of_args >= 2);

// b. Let array be ? ArrayCreate(numberOfArgs, proto).
let array = Self::array_create(number_of_args, Some(prototype), context)?;
let array = Self::array_create(number_of_args as u64, Some(prototype), context)?;
// c. Let k be 0.
// d. Repeat, while k < numberOfArgs,
for (i, item) in args.iter().cloned().enumerate() {
Expand All @@ -217,12 +217,12 @@ impl Array {
///
/// [spec]: https://tc39.es/ecma262/#sec-arraycreate
pub(crate) fn array_create(
length: usize,
length: u64,
prototype: Option<JsObject>,
context: &mut Context,
) -> JsResult<JsObject> {
// 1. If length > 2^32 - 1, throw a RangeError exception.
if length > 2usize.pow(32) - 1 {
if length > 2u64.pow(32) - 1 {
return context.throw_range_error("array exceeded max size");
}
// 7. Return A.
Expand Down Expand Up @@ -334,7 +334,7 @@ impl Array {
/// see: <https://tc39.es/ecma262/#sec-arrayspeciescreate>
pub(crate) fn array_species_create(
original_array: &JsObject,
length: usize,
length: u64,
context: &mut Context,
) -> JsResult<JsObject> {
// 1. Let isArray be ? IsArray(originalArray).
Expand Down Expand Up @@ -573,7 +573,7 @@ impl Array {
// a. Let A be ? ArrayCreate(len).
let a = match this.as_constructor() {
Some(constructor) => constructor.construct(&[len.into()], None, context)?,
_ => Self::array_create(len, None, context)?,
_ => Self::array_create(len as u64, None, context)?,
};

// 6. Let k be 0.
Expand Down Expand Up @@ -667,7 +667,7 @@ impl Array {
// ii. Let len be ? LengthOfArrayLike(E).
let len = item.length_of_array_like(context)?;
// iii. If n + len > 2^53 - 1, throw a TypeError exception.
if n + len > Number::MAX_SAFE_INTEGER as usize {
if n + len > Number::MAX_SAFE_INTEGER as u64 {
return context.throw_type_error(
"length + number of arguments exceeds the max safe integer limit",
);
Expand All @@ -693,7 +693,7 @@ impl Array {
else {
// i. NOTE: E is added as a single item rather than spread.
// ii. If n ≥ 2^53 - 1, throw a TypeError exception.
if n >= Number::MAX_SAFE_INTEGER as usize {
if n >= Number::MAX_SAFE_INTEGER as u64 {
return context.throw_type_error("length exceeds the max safe integer limit");
}
// iii. Perform ? CreateDataPropertyOrThrow(A, ! ToString(𝔽(n)), E).
Expand Down Expand Up @@ -729,7 +729,7 @@ impl Array {
// 1. Let O be ? ToObject(this value).
let o = this.to_object(context)?;
// 2. Let len be ? LengthOfArrayLike(O).
let mut len = o.length_of_array_like(context)? as u64;
let mut len = o.length_of_array_like(context)?;
// 3. Let argCount be the number of elements in items.
let arg_count = args.len() as u64;
// 4. If len + argCount > 2^53 - 1, throw a TypeError exception.
Expand Down Expand Up @@ -1078,7 +1078,7 @@ impl Array {
// 1. Let O be ? ToObject(this value).
let o = this.to_object(context)?;
// 2. Let len be ? LengthOfArrayLike(O).
let len = o.length_of_array_like(context)? as u64;
let len = o.length_of_array_like(context)?;
// 3. Let argCount be the number of elements in items.
let arg_count = args.len() as u64;
// 4. If argCount > 0, then
Expand Down Expand Up @@ -1640,7 +1640,7 @@ impl Array {
Self::flatten_into_array(
&a,
&o,
source_len as u64,
source_len,
0,
depth_num,
None,
Expand Down Expand Up @@ -1687,7 +1687,7 @@ impl Array {
Self::flatten_into_array(
&a,
&o,
source_len as u64,
source_len,
0,
1,
Some(mapper_function),
Expand Down Expand Up @@ -1781,7 +1781,7 @@ impl Array {
target_index = Self::flatten_into_array(
target,
element,
element_len as u64,
element_len,
target_index,
new_depth,
None,
Expand Down Expand Up @@ -2037,7 +2037,7 @@ impl Array {
// 9. Else,
} else {
// 9a. Let insertCount be the number of elements in items.
items.len()
items.len() as u64
};
let actual_delete_count = if start.is_none() {
// 7b. Let actualDeleteCount be 0.
Expand All @@ -2056,16 +2056,14 @@ impl Array {
// c. Let actualDeleteCount be the result of clamping dc between 0 and len - actualStart.
let max = len - actual_start;
match dc {
IntegerOrInfinity::Integer(i) => {
usize::try_from(i).unwrap_or_default().clamp(0, max)
}
IntegerOrInfinity::Integer(i) => u64::try_from(i).unwrap_or_default().clamp(0, max),
IntegerOrInfinity::PositiveInfinity => max,
IntegerOrInfinity::NegativeInfinity => 0,
}
};

// 10. If len + insertCount - actualDeleteCount > 2^53 - 1, throw a TypeError exception.
if len + insert_count - actual_delete_count > Number::MAX_SAFE_INTEGER as usize {
if len + insert_count - actual_delete_count > Number::MAX_SAFE_INTEGER as u64 {
return context.throw_type_error("Target splice exceeded max safe integer value");
}

Expand All @@ -2091,7 +2089,7 @@ impl Array {
arr.set("length", actual_delete_count, true, context)?;

// 15. Let itemCount be the number of elements in items.
let item_count = items.len();
let item_count = items.len() as u64;

match item_count.cmp(&actual_delete_count) {
// 16. If itemCount < actualDeleteCount, then
Expand Down Expand Up @@ -2164,7 +2162,7 @@ impl Array {
for (k, item) in items
.iter()
.enumerate()
.map(|(i, val)| (i + actual_start, val))
.map(|(i, val)| (i as u64 + actual_start, val))
{
// a. Perform ? Set(O, ! ToString(𝔽(k)), E, true).
o.set(k, item, true, context)?;
Expand Down Expand Up @@ -2381,7 +2379,7 @@ impl Array {
let length = obj.length_of_array_like(context)?;

// 4. Let items be a new empty List.
let mut items = Vec::with_capacity(length);
let mut items = Vec::with_capacity(length as usize);

// 5. Let k be 0.
// 6. Repeat, while k < len,
Expand All @@ -2399,7 +2397,7 @@ impl Array {
}

// 7. Let itemCount be the number of elements in items.
let item_count = items.len();
let item_count = items.len() as u64;

// 8. Sort items using an implementation-defined sequence of calls to SortCompare.
// If any such call returns an abrupt completion, stop before performing any further
Expand Down Expand Up @@ -2792,8 +2790,8 @@ impl Array {
pub(super) fn get_relative_start(
context: &mut Context,
arg: Option<&JsValue>,
len: usize,
) -> JsResult<usize> {
len: u64,
) -> JsResult<u64> {
// 1. Let relativeStart be ? ToIntegerOrInfinity(start).
let relative_start = arg
.cloned()
Expand All @@ -2803,10 +2801,10 @@ impl Array {
// 2. If relativeStart is -∞, let k be 0.
IntegerOrInfinity::NegativeInfinity => Ok(0),
// 3. Else if relativeStart < 0, let k be max(len + relativeStart, 0).
IntegerOrInfinity::Integer(i) if i < 0 => Ok(max(len as i64 + i, 0) as usize),
IntegerOrInfinity::Integer(i) if i < 0 => Ok(max(len as i64 + i, 0) as u64),
// Both `as` casts are safe as both variables are non-negative
// 4. Else, let k be min(relativeStart, len).
IntegerOrInfinity::Integer(i) => Ok(min(i, len as i64) as usize),
IntegerOrInfinity::Integer(i) => Ok(min(i, len as i64) as u64),

// Special case - positive infinity. `len` is always smaller than +inf, thus from (4)
IntegerOrInfinity::PositiveInfinity => Ok(len),
Expand All @@ -2817,8 +2815,8 @@ impl Array {
pub(super) fn get_relative_end(
context: &mut Context,
arg: Option<&JsValue>,
len: usize,
) -> JsResult<usize> {
len: u64,
) -> JsResult<u64> {
let default_value = JsValue::undefined();
let value = arg.unwrap_or(&default_value);
// 1. If end is undefined, let relativeEnd be len [and return it]
Expand All @@ -2831,10 +2829,10 @@ impl Array {
// 2. If relativeEnd is -∞, let final be 0.
IntegerOrInfinity::NegativeInfinity => Ok(0),
// 3. Else if relativeEnd < 0, let final be max(len + relativeEnd, 0).
IntegerOrInfinity::Integer(i) if i < 0 => Ok(max(len as i64 + i, 0) as usize),
IntegerOrInfinity::Integer(i) if i < 0 => Ok(max(len as i64 + i, 0) as u64),
// 4. Else, let final be min(relativeEnd, len).
// Both `as` casts are safe as both variables are non-negative
IntegerOrInfinity::Integer(i) => Ok(min(i, len as i64) as usize),
IntegerOrInfinity::Integer(i) => Ok(min(i, len as i64) as u64),

// Special case - positive infinity. `len` is always smaller than +inf, thus from (4)
IntegerOrInfinity::PositiveInfinity => Ok(len),
Expand Down
33 changes: 17 additions & 16 deletions boa_engine/src/builtins/array_buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use tap::{Conv, Pipe};
#[derive(Debug, Clone, Trace, Finalize)]
pub struct ArrayBuffer {
pub array_buffer_data: Option<Vec<u8>>,
pub array_buffer_byte_length: usize,
pub array_buffer_byte_length: u64,
pub array_buffer_detach_key: JsValue,
}

impl ArrayBuffer {
pub(crate) fn array_buffer_byte_length(&self) -> usize {
pub(crate) fn array_buffer_byte_length(&self) -> u64 {
self.array_buffer_byte_length
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ impl ArrayBuffer {
};

// 14. Let newLen be max(final - first, 0).
let new_len = std::cmp::max(r#final - first, 0) as usize;
let new_len = std::cmp::max(r#final - first, 0) as u64;

// 15. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
let ctor = obj.species_constructor(StandardConstructors::array_buffer, context)?;
Expand Down Expand Up @@ -299,7 +299,7 @@ impl ArrayBuffer {
.expect("ArrayBuffer cannot be detached here");

// 26. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
copy_data_block_bytes(to_buf, 0, from_buf, first as usize, new_len);
copy_data_block_bytes(to_buf, 0, from_buf, first as usize, new_len as usize);
}

// 27. Return new.
Expand All @@ -314,7 +314,7 @@ impl ArrayBuffer {
/// [spec]: https://tc39.es/ecma262/#sec-allocatearraybuffer
pub(crate) fn allocate(
constructor: &JsValue,
byte_length: usize,
byte_length: u64,
context: &mut Context,
) -> JsResult<JsObject> {
// 1. Let obj be ? OrdinaryCreateFromConstructor(constructor, "%ArrayBuffer.prototype%", « [[ArrayBufferData]], [[ArrayBufferByteLength]], [[ArrayBufferDetachKey]] »).
Expand Down Expand Up @@ -361,8 +361,8 @@ impl ArrayBuffer {
/// [spec]: https://tc39.es/ecma262/#sec-clonearraybuffer
pub(crate) fn clone_array_buffer(
&self,
src_byte_offset: usize,
src_length: usize,
src_byte_offset: u64,
src_length: u64,
clone_constructor: &JsValue,
context: &mut Context,
) -> JsResult<JsObject> {
Expand Down Expand Up @@ -392,8 +392,8 @@ impl ArrayBuffer {
.expect("ArrayBuffer cannot me detached here"),
0,
src_block,
src_byte_offset,
src_length,
src_byte_offset as usize,
src_length as usize,
);
}

Expand Down Expand Up @@ -568,7 +568,7 @@ impl ArrayBuffer {
/// [spec]: https://tc39.es/ecma262/#sec-getvaluefrombuffer
pub(crate) fn get_value_from_buffer(
&self,
byte_index: usize,
byte_index: u64,
t: TypedArrayKind,
_is_typed_array: bool,
_order: SharedMemoryOrder,
Expand All @@ -583,13 +583,14 @@ impl ArrayBuffer {
.expect("ArrayBuffer cannot be detached here");

// 4. Let elementSize be the Element Size value specified in Table 73 for Element Type type.
let element_size = t.element_size();
let element_size = t.element_size() as usize;

// TODO: Shared Array Buffer
// 5. If IsSharedArrayBuffer(arrayBuffer) is true, then

// 6. Else, let rawValue be a List whose elements are bytes from block at indices byteIndex (inclusive) through byteIndex + elementSize (exclusive).
// 7. Assert: The number of elements in rawValue is elementSize.
let byte_index = byte_index as usize;
let raw_value = &block[byte_index..byte_index + element_size];

// TODO: Agent Record [[LittleEndian]] filed
Expand Down Expand Up @@ -700,7 +701,7 @@ impl ArrayBuffer {
/// [spec]: https://tc39.es/ecma262/#sec-setvalueinbuffer
pub(crate) fn set_value_in_buffer(
&mut self,
byte_index: usize,
byte_index: u64,
t: TypedArrayKind,
value: &JsValue,
_order: SharedMemoryOrder,
Expand Down Expand Up @@ -730,7 +731,7 @@ impl ArrayBuffer {

// 9. Else, store the individual bytes of rawBytes into block, starting at block[byteIndex].
for (i, raw_byte) in raw_bytes.iter().enumerate() {
block[byte_index + i] = *raw_byte;
block[byte_index as usize + i] = *raw_byte;
}

// 10. Return NormalCompletion(undefined).
Expand All @@ -744,16 +745,16 @@ impl ArrayBuffer {
/// integer). For more information, check the [spec][spec].
///
/// [spec]: https://tc39.es/ecma262/#sec-createbytedatablock
pub fn create_byte_data_block(size: usize, context: &mut Context) -> JsResult<Vec<u8>> {
pub fn create_byte_data_block(size: u64, context: &mut Context) -> JsResult<Vec<u8>> {
// 1. Let db be a new Data Block value consisting of size bytes. If it is impossible to
// create such a Data Block, throw a RangeError exception.
let mut data_block = Vec::new();
data_block.try_reserve(size).map_err(|e| {
data_block.try_reserve(size as usize).map_err(|e| {
context.construct_range_error(format!("couldn't allocate the data block: {e}"))
})?;

// 2. Set all of the bytes of db to 0.
data_block.resize(size, 0);
data_block.resize(size as usize, 0);

// 3. Return db.
Ok(data_block)
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/array_buffer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ fn ut_sunny_day_create_byte_data_block() {
fn ut_rainy_day_create_byte_data_block() {
let mut context = Context::default();

assert!(create_byte_data_block(usize::MAX, &mut context).is_err());
assert!(create_byte_data_block(u64::MAX, &mut context).is_err());
}
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/dataview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use tap::{Conv, Pipe};
#[derive(Debug, Clone, Trace, Finalize)]
pub struct DataView {
viewed_array_buffer: JsObject,
byte_length: usize,
byte_offset: usize,
byte_length: u64,
byte_offset: u64,
}

impl BuiltIn for DataView {
Expand Down
Loading