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] - Refactorings for Rust 1.59 #1867

Closed
wants to merge 6 commits 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
95 changes: 45 additions & 50 deletions boa_engine/src/builtins/array_buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod tests;

use crate::{
builtins::{typed_array::TypedArrayName, BuiltIn, JsArgs},
builtins::{typed_array::TypedArrayKind, BuiltIn, JsArgs},
context::StandardObjects,
object::{
internal_methods::get_prototype_from_constructor, ConstructorBuilder, FunctionBuilder,
Expand Down Expand Up @@ -397,17 +397,17 @@ impl ArrayBuffer {
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-isunclampedintegerelementtype
fn is_unclamped_integer_element_type(t: TypedArrayName) -> bool {
fn is_unclamped_integer_element_type(t: TypedArrayKind) -> bool {
// 1. If type is Int8, Uint8, Int16, Uint16, Int32, or Uint32, return true.
// 2. Return false.
matches!(
t,
TypedArrayName::Int8Array
| TypedArrayName::Uint8Array
| TypedArrayName::Int16Array
| TypedArrayName::Uint16Array
| TypedArrayName::Int32Array
| TypedArrayName::Uint32Array
TypedArrayKind::Int8
| TypedArrayKind::Uint8
| TypedArrayKind::Int16
| TypedArrayKind::Uint16
| TypedArrayKind::Int32
| TypedArrayKind::Uint32
)
}

Expand All @@ -417,13 +417,10 @@ impl ArrayBuffer {
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-isbigintelementtype
fn is_big_int_element_type(t: TypedArrayName) -> bool {
fn is_big_int_element_type(t: TypedArrayKind) -> bool {
// 1. If type is BigUint64 or BigInt64, return true.
// 2. Return false.
matches!(
t,
TypedArrayName::BigUint64Array | TypedArrayName::BigInt64Array
)
matches!(t, TypedArrayKind::BigUint64 | TypedArrayKind::BigInt64)
}

/// `25.1.2.8 IsNoTearConfiguration ( type, order )`
Expand All @@ -434,7 +431,7 @@ impl ArrayBuffer {
/// [spec]: https://tc39.es/ecma262/#sec-isnotearconfiguration
// TODO: Allow unused function until shared array buffers are implemented.
#[allow(dead_code)]
fn is_no_tear_configuration(t: TypedArrayName, order: SharedMemoryOrder) -> bool {
fn is_no_tear_configuration(t: TypedArrayKind, order: SharedMemoryOrder) -> bool {
// 1. If ! IsUnclampedIntegerElementType(type) is true, return true.
if Self::is_unclamped_integer_element_type(t) {
return true;
Expand All @@ -460,23 +457,23 @@ impl ArrayBuffer {
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-rawbytestonumeric
fn raw_bytes_to_numeric(t: TypedArrayName, bytes: &[u8], is_little_endian: bool) -> JsValue {
fn raw_bytes_to_numeric(t: TypedArrayKind, bytes: &[u8], is_little_endian: bool) -> JsValue {
let n: Numeric = match t {
TypedArrayName::Int8Array => {
TypedArrayKind::Int8 => {
if is_little_endian {
i8::from_le_bytes(bytes.try_into().expect("slice with incorrect length")).into()
} else {
i8::from_be_bytes(bytes.try_into().expect("slice with incorrect length")).into()
}
}
TypedArrayName::Uint8Array | TypedArrayName::Uint8ClampedArray => {
TypedArrayKind::Uint8 | TypedArrayKind::Uint8Clamped => {
if is_little_endian {
u8::from_le_bytes(bytes.try_into().expect("slice with incorrect length")).into()
} else {
u8::from_be_bytes(bytes.try_into().expect("slice with incorrect length")).into()
}
}
TypedArrayName::Int16Array => {
TypedArrayKind::Int16 => {
if is_little_endian {
i16::from_le_bytes(bytes.try_into().expect("slice with incorrect length"))
.into()
Expand All @@ -485,7 +482,7 @@ impl ArrayBuffer {
.into()
}
}
TypedArrayName::Uint16Array => {
TypedArrayKind::Uint16 => {
if is_little_endian {
u16::from_le_bytes(bytes.try_into().expect("slice with incorrect length"))
.into()
Expand All @@ -494,7 +491,7 @@ impl ArrayBuffer {
.into()
}
}
TypedArrayName::Int32Array => {
TypedArrayKind::Int32 => {
if is_little_endian {
i32::from_le_bytes(bytes.try_into().expect("slice with incorrect length"))
.into()
Expand All @@ -503,7 +500,7 @@ impl ArrayBuffer {
.into()
}
}
TypedArrayName::Uint32Array => {
TypedArrayKind::Uint32 => {
if is_little_endian {
u32::from_le_bytes(bytes.try_into().expect("slice with incorrect length"))
.into()
Expand All @@ -512,7 +509,7 @@ impl ArrayBuffer {
.into()
}
}
TypedArrayName::BigInt64Array => {
TypedArrayKind::BigInt64 => {
if is_little_endian {
i64::from_le_bytes(bytes.try_into().expect("slice with incorrect length"))
.into()
Expand All @@ -521,7 +518,7 @@ impl ArrayBuffer {
.into()
}
}
TypedArrayName::BigUint64Array => {
TypedArrayKind::BigUint64 => {
if is_little_endian {
u64::from_le_bytes(bytes.try_into().expect("slice with incorrect length"))
.into()
Expand All @@ -530,7 +527,7 @@ impl ArrayBuffer {
.into()
}
}
TypedArrayName::Float32Array => {
TypedArrayKind::Float32 => {
if is_little_endian {
f32::from_le_bytes(bytes.try_into().expect("slice with incorrect length"))
.into()
Expand All @@ -539,7 +536,7 @@ impl ArrayBuffer {
.into()
}
}
TypedArrayName::Float64Array => {
TypedArrayKind::Float64 => {
if is_little_endian {
f64::from_le_bytes(bytes.try_into().expect("slice with incorrect length"))
.into()
Expand All @@ -562,7 +559,7 @@ impl ArrayBuffer {
pub(crate) fn get_value_from_buffer(
&self,
byte_index: usize,
t: TypedArrayName,
t: TypedArrayKind,
_is_typed_array: bool,
_order: SharedMemoryOrder,
is_little_endian: Option<bool>,
Expand Down Expand Up @@ -600,43 +597,41 @@ impl ArrayBuffer {
///
/// [spec]: https://tc39.es/ecma262/#sec-numerictorawbytes
fn numeric_to_raw_bytes(
t: TypedArrayName,
t: TypedArrayKind,
value: &JsValue,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<Vec<u8>> {
Ok(match t {
TypedArrayName::Int8Array if is_little_endian => {
TypedArrayKind::Int8 if is_little_endian => {
value.to_int8(context)?.to_le_bytes().to_vec()
}
TypedArrayName::Int8Array => value.to_int8(context)?.to_be_bytes().to_vec(),
TypedArrayName::Uint8Array if is_little_endian => {
TypedArrayKind::Int8 => value.to_int8(context)?.to_be_bytes().to_vec(),
TypedArrayKind::Uint8 if is_little_endian => {
value.to_uint8(context)?.to_le_bytes().to_vec()
}
TypedArrayName::Uint8Array => value.to_uint8(context)?.to_be_bytes().to_vec(),
TypedArrayName::Uint8ClampedArray if is_little_endian => {
TypedArrayKind::Uint8 => value.to_uint8(context)?.to_be_bytes().to_vec(),
TypedArrayKind::Uint8Clamped if is_little_endian => {
value.to_uint8_clamp(context)?.to_le_bytes().to_vec()
}
TypedArrayName::Uint8ClampedArray => {
value.to_uint8_clamp(context)?.to_be_bytes().to_vec()
}
TypedArrayName::Int16Array if is_little_endian => {
TypedArrayKind::Uint8Clamped => value.to_uint8_clamp(context)?.to_be_bytes().to_vec(),
TypedArrayKind::Int16 if is_little_endian => {
value.to_int16(context)?.to_le_bytes().to_vec()
}
TypedArrayName::Int16Array => value.to_int16(context)?.to_be_bytes().to_vec(),
TypedArrayName::Uint16Array if is_little_endian => {
TypedArrayKind::Int16 => value.to_int16(context)?.to_be_bytes().to_vec(),
TypedArrayKind::Uint16 if is_little_endian => {
value.to_uint16(context)?.to_le_bytes().to_vec()
}
TypedArrayName::Uint16Array => value.to_uint16(context)?.to_be_bytes().to_vec(),
TypedArrayName::Int32Array if is_little_endian => {
TypedArrayKind::Uint16 => value.to_uint16(context)?.to_be_bytes().to_vec(),
TypedArrayKind::Int32 if is_little_endian => {
value.to_i32(context)?.to_le_bytes().to_vec()
}
TypedArrayName::Int32Array => value.to_i32(context)?.to_be_bytes().to_vec(),
TypedArrayName::Uint32Array if is_little_endian => {
TypedArrayKind::Int32 => value.to_i32(context)?.to_be_bytes().to_vec(),
TypedArrayKind::Uint32 if is_little_endian => {
value.to_u32(context)?.to_le_bytes().to_vec()
}
TypedArrayName::Uint32Array => value.to_u32(context)?.to_be_bytes().to_vec(),
TypedArrayName::BigInt64Array if is_little_endian => {
TypedArrayKind::Uint32 => value.to_u32(context)?.to_be_bytes().to_vec(),
TypedArrayKind::BigInt64 if is_little_endian => {
let big_int = value.to_big_int64(context)?;
big_int
.to_i64()
Expand All @@ -650,7 +645,7 @@ impl ArrayBuffer {
.to_le_bytes()
.to_vec()
}
TypedArrayName::BigInt64Array => {
TypedArrayKind::BigInt64 => {
let big_int = value.to_big_int64(context)?;
big_int
.to_i64()
Expand All @@ -664,23 +659,23 @@ impl ArrayBuffer {
.to_be_bytes()
.to_vec()
}
TypedArrayName::BigUint64Array if is_little_endian => value
TypedArrayKind::BigUint64 if is_little_endian => value
.to_big_uint64(context)?
.to_u64()
.unwrap_or(u64::MAX)
.to_le_bytes()
.to_vec(),
TypedArrayName::BigUint64Array => value
TypedArrayKind::BigUint64 => value
.to_big_uint64(context)?
.to_u64()
.unwrap_or(u64::MAX)
.to_be_bytes()
.to_vec(),
TypedArrayName::Float32Array => match value.to_number(context)? {
TypedArrayKind::Float32 => match value.to_number(context)? {
f if is_little_endian => (f as f32).to_le_bytes().to_vec(),
f => (f as f32).to_be_bytes().to_vec(),
},
TypedArrayName::Float64Array => match value.to_number(context)? {
TypedArrayKind::Float64 => match value.to_number(context)? {
f if is_little_endian => f.to_le_bytes().to_vec(),
f => f.to_be_bytes().to_vec(),
},
Expand All @@ -696,7 +691,7 @@ impl ArrayBuffer {
pub(crate) fn set_value_in_buffer(
&mut self,
byte_index: usize,
t: TypedArrayName,
t: TypedArrayKind,
value: &JsValue,
_order: SharedMemoryOrder,
is_little_endian: Option<bool>,
Expand Down
Loading