From a6a3161d2a8650ffd473d8c90843651f3b5a2fc6 Mon Sep 17 00:00:00 2001 From: RageKnify Date: Thu, 24 Feb 2022 18:53:31 +0000 Subject: [PATCH 1/6] Refactor: Respect repeated names lint Remove Array suffix from TypedArrayName variants --- boa_engine/src/builtins/array_buffer/mod.rs | 79 +++++++------- boa_engine/src/builtins/dataview/mod.rs | 40 +++---- boa_engine/src/builtins/mod.rs | 26 ++--- boa_engine/src/builtins/typed_array/mod.rs | 112 ++++++++++---------- 4 files changed, 126 insertions(+), 131 deletions(-) diff --git a/boa_engine/src/builtins/array_buffer/mod.rs b/boa_engine/src/builtins/array_buffer/mod.rs index 9bc118f6b9c..3ee3add1eea 100644 --- a/boa_engine/src/builtins/array_buffer/mod.rs +++ b/boa_engine/src/builtins/array_buffer/mod.rs @@ -402,12 +402,12 @@ impl ArrayBuffer { // 2. Return false. matches!( t, - TypedArrayName::Int8Array - | TypedArrayName::Uint8Array - | TypedArrayName::Int16Array - | TypedArrayName::Uint16Array - | TypedArrayName::Int32Array - | TypedArrayName::Uint32Array + TypedArrayName::Int8 + | TypedArrayName::Uint8 + | TypedArrayName::Int16 + | TypedArrayName::Uint16 + | TypedArrayName::Int32 + | TypedArrayName::Uint32 ) } @@ -420,10 +420,7 @@ impl ArrayBuffer { fn is_big_int_element_type(t: TypedArrayName) -> bool { // 1. If type is BigUint64 or BigInt64, return true. // 2. Return false. - matches!( - t, - TypedArrayName::BigUint64Array | TypedArrayName::BigInt64Array - ) + matches!(t, TypedArrayName::BigUint64 | TypedArrayName::BigInt64) } /// `25.1.2.8 IsNoTearConfiguration ( type, order )` @@ -462,21 +459,21 @@ impl ArrayBuffer { /// [spec]: https://tc39.es/ecma262/#sec-rawbytestonumeric fn raw_bytes_to_numeric(t: TypedArrayName, bytes: &[u8], is_little_endian: bool) -> JsValue { let n: Numeric = match t { - TypedArrayName::Int8Array => { + TypedArrayName::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 => { + TypedArrayName::Uint8 | TypedArrayName::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 => { + TypedArrayName::Int16 => { if is_little_endian { i16::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -485,7 +482,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Uint16Array => { + TypedArrayName::Uint16 => { if is_little_endian { u16::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -494,7 +491,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Int32Array => { + TypedArrayName::Int32 => { if is_little_endian { i32::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -503,7 +500,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Uint32Array => { + TypedArrayName::Uint32 => { if is_little_endian { u32::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -512,7 +509,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::BigInt64Array => { + TypedArrayName::BigInt64 => { if is_little_endian { i64::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -521,7 +518,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::BigUint64Array => { + TypedArrayName::BigUint64 => { if is_little_endian { u64::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -530,7 +527,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Float32Array => { + TypedArrayName::Float32 => { if is_little_endian { f32::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -539,7 +536,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Float64Array => { + TypedArrayName::Float64 => { if is_little_endian { f64::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -606,37 +603,35 @@ impl ArrayBuffer { context: &mut Context, ) -> JsResult> { Ok(match t { - TypedArrayName::Int8Array if is_little_endian => { + TypedArrayName::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 => { + TypedArrayName::Int8 => value.to_int8(context)?.to_be_bytes().to_vec(), + TypedArrayName::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 => { + TypedArrayName::Uint8 => value.to_uint8(context)?.to_be_bytes().to_vec(), + TypedArrayName::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 => { + TypedArrayName::Uint8Clamped => value.to_uint8_clamp(context)?.to_be_bytes().to_vec(), + TypedArrayName::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 => { + TypedArrayName::Int16 => value.to_int16(context)?.to_be_bytes().to_vec(), + TypedArrayName::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 => { + TypedArrayName::Uint16 => value.to_uint16(context)?.to_be_bytes().to_vec(), + TypedArrayName::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 => { + TypedArrayName::Int32 => value.to_i32(context)?.to_be_bytes().to_vec(), + TypedArrayName::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 => { + TypedArrayName::Uint32 => value.to_u32(context)?.to_be_bytes().to_vec(), + TypedArrayName::BigInt64 if is_little_endian => { let big_int = value.to_big_int64(context)?; big_int .to_i64() @@ -650,7 +645,7 @@ impl ArrayBuffer { .to_le_bytes() .to_vec() } - TypedArrayName::BigInt64Array => { + TypedArrayName::BigInt64 => { let big_int = value.to_big_int64(context)?; big_int .to_i64() @@ -664,23 +659,23 @@ impl ArrayBuffer { .to_be_bytes() .to_vec() } - TypedArrayName::BigUint64Array if is_little_endian => value + TypedArrayName::BigUint64 if is_little_endian => value .to_big_uint64(context)? .to_u64() .unwrap_or(u64::MAX) .to_le_bytes() .to_vec(), - TypedArrayName::BigUint64Array => value + TypedArrayName::BigUint64 => value .to_big_uint64(context)? .to_u64() .unwrap_or(u64::MAX) .to_be_bytes() .to_vec(), - TypedArrayName::Float32Array => match value.to_number(context)? { + TypedArrayName::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)? { + TypedArrayName::Float64 => match value.to_number(context)? { f if is_little_endian => f.to_le_bytes().to_vec(), f => f.to_be_bytes().to_vec(), }, diff --git a/boa_engine/src/builtins/dataview/mod.rs b/boa_engine/src/builtins/dataview/mod.rs index 4b8c256c8e3..c6eaeb9257c 100644 --- a/boa_engine/src/builtins/dataview/mod.rs +++ b/boa_engine/src/builtins/dataview/mod.rs @@ -378,7 +378,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::BigInt64Array, + TypedArrayName::BigInt64, context, ) } @@ -407,7 +407,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::BigUint64Array, + TypedArrayName::BigUint64, context, ) } @@ -436,7 +436,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Float32Array, + TypedArrayName::Float32, context, ) } @@ -465,7 +465,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Float64Array, + TypedArrayName::Float64, context, ) } @@ -494,7 +494,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int8Array, + TypedArrayName::Int8, context, ) } @@ -523,7 +523,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int16Array, + TypedArrayName::Int16, context, ) } @@ -552,7 +552,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int32Array, + TypedArrayName::Int32, context, ) } @@ -581,7 +581,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint8Array, + TypedArrayName::Uint8, context, ) } @@ -610,7 +610,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint16Array, + TypedArrayName::Uint16, context, ) } @@ -639,7 +639,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint32Array, + TypedArrayName::Uint32, context, ) } @@ -747,7 +747,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::BigInt64Array, + TypedArrayName::BigInt64, value, context, ) @@ -778,7 +778,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::BigUint64Array, + TypedArrayName::BigUint64, value, context, ) @@ -809,7 +809,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Float32Array, + TypedArrayName::Float32, value, context, ) @@ -840,7 +840,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Float64Array, + TypedArrayName::Float64, value, context, ) @@ -871,7 +871,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int8Array, + TypedArrayName::Int8, value, context, ) @@ -902,7 +902,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int16Array, + TypedArrayName::Int16, value, context, ) @@ -933,7 +933,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int32Array, + TypedArrayName::Int32, value, context, ) @@ -964,7 +964,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint8Array, + TypedArrayName::Uint8, value, context, ) @@ -995,7 +995,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint16Array, + TypedArrayName::Uint16, value, context, ) @@ -1026,7 +1026,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint32Array, + TypedArrayName::Uint32, value, context, ) diff --git a/boa_engine/src/builtins/mod.rs b/boa_engine/src/builtins/mod.rs index de272641a21..e3863182d6d 100644 --- a/boa_engine/src/builtins/mod.rs +++ b/boa_engine/src/builtins/mod.rs @@ -57,8 +57,8 @@ pub(crate) use self::{ string::String, symbol::Symbol, typed_array::{ - BigInt64Array, BigUint64Array, Float32Array, Float64Array, Int16Array, Int32Array, - Int8Array, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, + BigInt64, BigUint64, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, + Uint8Clamped, }, undefined::Undefined, }; @@ -140,17 +140,17 @@ pub fn init(context: &mut Context) { Set, String, RegExp, - Int8Array, - Uint8Array, - Uint8ClampedArray, - Int16Array, - Uint16Array, - Int32Array, - Uint32Array, - BigInt64Array, - BigUint64Array, - Float32Array, - Float64Array, + Int8, + Uint8, + Uint8Clamped, + Int16, + Uint16, + Int32, + Uint32, + BigInt64, + BigUint64, + Float32, + Float64, Symbol, Error, RangeError, diff --git a/boa_engine/src/builtins/typed_array/mod.rs b/boa_engine/src/builtins/typed_array/mod.rs index 8fab604ffbc..9155fc9d692 100644 --- a/boa_engine/src/builtins/typed_array/mod.rs +++ b/boa_engine/src/builtins/typed_array/mod.rs @@ -797,7 +797,7 @@ impl TypedArray { // i. Let value be GetValueFromBuffer(buffer, fromByteIndex, Uint8, true, Unordered). let value = buffer.get_value_from_buffer( from_byte_index as usize, - TypedArrayName::Uint8Array, + TypedArrayName::Uint8, true, SharedMemoryOrder::Unordered, None, @@ -806,7 +806,7 @@ impl TypedArray { // ii. Perform SetValueInBuffer(buffer, toByteIndex, Uint8, value, true, Unordered). buffer.set_value_in_buffer( to_byte_index as usize, - TypedArrayName::Uint8Array, + TypedArrayName::Uint8, &value, SharedMemoryOrder::Unordered, None, @@ -2054,7 +2054,7 @@ impl TypedArray { // i. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, Uint8, true, Unordered). let value = src_buffer.get_value_from_buffer( src_byte_index, - TypedArrayName::Uint8Array, + TypedArrayName::Uint8, true, SharedMemoryOrder::Unordered, None, @@ -2067,7 +2067,7 @@ impl TypedArray { .expect("Must be an array buffer") .set_value_in_buffer( target_byte_index, - TypedArrayName::Uint8Array, + TypedArrayName::Uint8, &value, SharedMemoryOrder::Unordered, None, @@ -2367,7 +2367,7 @@ impl TypedArray { // 1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, Uint8, true, Unordered). let value = src_buffer.get_value_from_buffer( src_byte_index, - TypedArrayName::Uint8Array, + TypedArrayName::Uint8, true, SharedMemoryOrder::Unordered, None, @@ -2376,7 +2376,7 @@ impl TypedArray { // 2. Perform SetValueInBuffer(targetBuffer, targetByteIndex, Uint8, value, true, Unordered). target_buffer.set_value_in_buffer( target_byte_index, - TypedArrayName::Uint8Array, + TypedArrayName::Uint8, &value, SharedMemoryOrder::Unordered, None, @@ -2815,17 +2815,17 @@ impl TypedArray { ) -> JsResult { // 1. Let defaultConstructor be the intrinsic object listed in column one of Table 73 for exemplar.[[TypedArrayName]]. let default_constructor = match typed_array_name { - TypedArrayName::Int8Array => StandardObjects::typed_int8_array_object, - TypedArrayName::Uint8Array => StandardObjects::typed_uint8_array_object, - TypedArrayName::Uint8ClampedArray => StandardObjects::typed_uint8clamped_array_object, - TypedArrayName::Int16Array => StandardObjects::typed_int16_array_object, - TypedArrayName::Uint16Array => StandardObjects::typed_uint16_array_object, - TypedArrayName::Int32Array => StandardObjects::typed_int32_array_object, - TypedArrayName::Uint32Array => StandardObjects::typed_uint32_array_object, - TypedArrayName::BigInt64Array => StandardObjects::typed_bigint64_array_object, - TypedArrayName::BigUint64Array => StandardObjects::typed_biguint64_array_object, - TypedArrayName::Float32Array => StandardObjects::typed_float32_array_object, - TypedArrayName::Float64Array => StandardObjects::typed_float64_array_object, + TypedArrayName::Int8 => StandardObjects::typed_int8_array_object, + TypedArrayName::Uint8 => StandardObjects::typed_uint8_array_object, + TypedArrayName::Uint8Clamped => StandardObjects::typed_uint8clamped_array_object, + TypedArrayName::Int16 => StandardObjects::typed_int16_array_object, + TypedArrayName::Uint16 => StandardObjects::typed_uint16_array_object, + TypedArrayName::Int32 => StandardObjects::typed_int32_array_object, + TypedArrayName::Uint32 => StandardObjects::typed_uint32_array_object, + TypedArrayName::BigInt64 => StandardObjects::typed_bigint64_array_object, + TypedArrayName::BigUint64 => StandardObjects::typed_biguint64_array_object, + TypedArrayName::Float32 => StandardObjects::typed_float32_array_object, + TypedArrayName::Float64 => StandardObjects::typed_float64_array_object, }; // 2. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor). @@ -3288,17 +3288,17 @@ impl TypedArray { /// Names of all the typed arrays. #[derive(Debug, Clone, Copy, Finalize, PartialEq)] pub(crate) enum TypedArrayName { - Int8Array, - Uint8Array, - Uint8ClampedArray, - Int16Array, - Uint16Array, - Int32Array, - Uint32Array, - BigInt64Array, - BigUint64Array, - Float32Array, - Float64Array, + Int8, + Uint8, + Uint8Clamped, + Int16, + Uint16, + Int32, + Uint32, + BigInt64, + BigUint64, + Float32, + Float64, } unsafe impl Trace for TypedArrayName { @@ -3313,10 +3313,10 @@ impl TypedArrayName { #[inline] pub(crate) const fn element_size(self) -> usize { match self { - Self::Int8Array | Self::Uint8Array | Self::Uint8ClampedArray => 1, - Self::Int16Array | Self::Uint16Array => 2, - Self::Int32Array | Self::Uint32Array | Self::Float32Array => 4, - Self::BigInt64Array | Self::BigUint64Array | Self::Float64Array => 8, + Self::Int8 | Self::Uint8 | Self::Uint8Clamped => 1, + Self::Int16 | Self::Uint16 => 2, + Self::Int32 | Self::Uint32 | Self::Float32 => 4, + Self::BigInt64 | Self::BigUint64 | Self::Float64 => 8, } } @@ -3324,7 +3324,7 @@ impl TypedArrayName { #[inline] pub(crate) const fn content_type(self) -> ContentType { match self { - Self::BigInt64Array | Self::BigUint64Array => ContentType::BigInt, + Self::BigInt64 | Self::BigUint64 => ContentType::BigInt, _ => ContentType::Number, } } @@ -3333,44 +3333,44 @@ impl TypedArrayName { #[inline] pub(crate) const fn name(&self) -> &str { match self { - TypedArrayName::Int8Array => "Int8Array", - TypedArrayName::Uint8Array => "Uint8Array", - TypedArrayName::Uint8ClampedArray => "Uint8ClampedArray", - TypedArrayName::Int16Array => "Int16Array", - TypedArrayName::Uint16Array => "Uint16Array", - TypedArrayName::Int32Array => "Int32Array", - TypedArrayName::Uint32Array => "Uint32Array", - TypedArrayName::BigInt64Array => "BigInt64Array", - TypedArrayName::BigUint64Array => "BigUint64Array", - TypedArrayName::Float32Array => "Float32Array", - TypedArrayName::Float64Array => "Float64Array", + TypedArrayName::Int8 => "Int8Array", + TypedArrayName::Uint8 => "Uint8Array", + TypedArrayName::Uint8Clamped => "Uint8ClampedArray", + TypedArrayName::Int16 => "Int16Array", + TypedArrayName::Uint16 => "Uint16Array", + TypedArrayName::Int32 => "Int32Array", + TypedArrayName::Uint32 => "Uint32Array", + TypedArrayName::BigInt64 => "BigInt64Array", + TypedArrayName::BigUint64 => "BigUint64Array", + TypedArrayName::Float32 => "Float32Array", + TypedArrayName::Float64 => "Float64Array", } } pub(crate) fn is_big_int_element_type(self) -> bool { matches!( self, - TypedArrayName::BigUint64Array | TypedArrayName::BigInt64Array + TypedArrayName::BigUint64 | TypedArrayName::BigInt64 ) } } -typed_array!(Int8Array, "Int8Array", typed_int8_array_object); -typed_array!(Uint8Array, "Uint8Array", typed_uint8_array_object); +typed_array!(Int8, "Int8Array", typed_int8_array_object); +typed_array!(Uint8, "Uint8Array", typed_uint8_array_object); typed_array!( - Uint8ClampedArray, + Uint8Clamped, "Uint8ClampedArray", typed_uint8clamped_array_object ); -typed_array!(Int16Array, "Int16Array", typed_int16_array_object); -typed_array!(Uint16Array, "Uint16Array", typed_uint16_array_object); -typed_array!(Int32Array, "Int32Array", typed_int32_array_object); -typed_array!(Uint32Array, "Uint32Array", typed_uint32_array_object); -typed_array!(BigInt64Array, "BigInt64Array", typed_bigint64_array_object); +typed_array!(Int16, "Int16Array", typed_int16_array_object); +typed_array!(Uint16, "Uint16Array", typed_uint16_array_object); +typed_array!(Int32, "Int32Array", typed_int32_array_object); +typed_array!(Uint32, "Uint32Array", typed_uint32_array_object); +typed_array!(BigInt64, "BigInt64Array", typed_bigint64_array_object); typed_array!( - BigUint64Array, + BigUint64, "BigUint64Array", typed_biguint64_array_object ); -typed_array!(Float32Array, "Float32Array", typed_float32_array_object); -typed_array!(Float64Array, "Float64Array", typed_float64_array_object); +typed_array!(Float32, "Float32Array", typed_float32_array_object); +typed_array!(Float64, "Float64Array", typed_float64_array_object); From 6697c27e89ba89ca22c75419272a85b9d2c797a5 Mon Sep 17 00:00:00 2001 From: RageKnify Date: Thu, 24 Feb 2022 19:01:50 +0000 Subject: [PATCH 2/6] Refactor: Remove unnecessary self --- boa_engine/src/bytecompiler.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/boa_engine/src/bytecompiler.rs b/boa_engine/src/bytecompiler.rs index 57e0d7f6599..2aca2a1c292 100644 --- a/boa_engine/src/bytecompiler.rs +++ b/boa_engine/src/bytecompiler.rs @@ -456,7 +456,7 @@ impl<'b> ByteCompiler<'b> { } #[inline] - fn compile_access<'a>(&mut self, node: &'a Node) -> Access<'a> { + fn compile_access(node: &'_ Node) -> Access<'_> { match node { Node::Identifier(name) => Access::Variable { name: name.sym() }, Node::GetConstField(node) => Access::ByName { node }, @@ -571,7 +571,7 @@ impl<'b> ByteCompiler<'b> { self.compile_expr(unary.target(), true)?; self.emit(Opcode::Inc, &[]); - let access = self.compile_access(unary.target()); + let access = Self::compile_access(unary.target()); self.access_set(access, None, true)?; None } @@ -579,7 +579,7 @@ impl<'b> ByteCompiler<'b> { self.compile_expr(unary.target(), true)?; self.emit(Opcode::Dec, &[]); - let access = self.compile_access(unary.target()); + let access = Self::compile_access(unary.target()); self.access_set(access, None, true)?; None } @@ -587,7 +587,7 @@ impl<'b> ByteCompiler<'b> { self.compile_expr(unary.target(), true)?; self.emit(Opcode::Dup, &[]); self.emit(Opcode::Inc, &[]); - let access = self.compile_access(unary.target()); + let access = Self::compile_access(unary.target()); self.access_set(access, None, false)?; None @@ -596,7 +596,7 @@ impl<'b> ByteCompiler<'b> { self.compile_expr(unary.target(), true)?; self.emit(Opcode::Dup, &[]); self.emit(Opcode::Dec, &[]); - let access = self.compile_access(unary.target()); + let access = Self::compile_access(unary.target()); self.access_set(access, None, false)?; None @@ -744,7 +744,7 @@ impl<'b> ByteCompiler<'b> { AssignOp::BoolAnd => { let exit = self.jump_with_custom_opcode(Opcode::LogicalAnd); self.compile_expr(binary.rhs(), true)?; - let access = self.compile_access(binary.lhs()); + let access = Self::compile_access(binary.lhs()); self.access_set(access, None, use_expr)?; self.patch_jump(exit); None @@ -752,7 +752,7 @@ impl<'b> ByteCompiler<'b> { AssignOp::BoolOr => { let exit = self.jump_with_custom_opcode(Opcode::LogicalOr); self.compile_expr(binary.rhs(), true)?; - let access = self.compile_access(binary.lhs()); + let access = Self::compile_access(binary.lhs()); self.access_set(access, None, use_expr)?; self.patch_jump(exit); None @@ -760,7 +760,7 @@ impl<'b> ByteCompiler<'b> { AssignOp::Coalesce => { let exit = self.jump_with_custom_opcode(Opcode::Coalesce); self.compile_expr(binary.rhs(), true)?; - let access = self.compile_access(binary.lhs()); + let access = Self::compile_access(binary.lhs()); self.access_set(access, None, use_expr)?; self.patch_jump(exit); None @@ -770,7 +770,7 @@ impl<'b> ByteCompiler<'b> { if let Some(opcode) = opcode { self.compile_expr(binary.rhs(), true)?; self.emit(opcode, &[]); - let access = self.compile_access(binary.lhs()); + let access = Self::compile_access(binary.lhs()); self.access_set(access, None, use_expr)?; } } @@ -891,7 +891,7 @@ impl<'b> ByteCompiler<'b> { if let Node::Object(_) = assign.lhs() { self.emit_opcode(Opcode::PushUndefined); } else { - let access = self.compile_access(assign.lhs()); + let access = Self::compile_access(assign.lhs()); self.access_set(access, Some(assign.rhs()), use_expr)?; } } From e3b3309c6ac23ee6f4223a7c791e14cccbae6134 Mon Sep 17 00:00:00 2001 From: RageKnify Date: Thu, 24 Feb 2022 19:04:28 +0000 Subject: [PATCH 3/6] Refactor: simple refactors to respect lints --- boa_engine/src/object/jsobject.rs | 2 +- boa_engine/src/value/equality.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/boa_engine/src/object/jsobject.rs b/boa_engine/src/object/jsobject.rs index c4f66e62b88..8a6a995bbe8 100644 --- a/boa_engine/src/object/jsobject.rs +++ b/boa_engine/src/object/jsobject.rs @@ -511,7 +511,7 @@ impl JsObject { // 15. If desc.[[Get]] is present or desc.[[Set]] is present, then ... // a. If desc.[[Value]] is present or desc.[[Writable]] is present, throw a TypeError exception. - if get.as_ref().or_else(|| set.as_ref()).is_some() && desc.inner().is_data_descriptor() { + if get.as_ref().or(set.as_ref()).is_some() && desc.inner().is_data_descriptor() { return context.throw_type_error( "Invalid property descriptor.\ Cannot both specify accessors and a value or writable attribute", diff --git a/boa_engine/src/value/equality.rs b/boa_engine/src/value/equality.rs index 1039d61d90d..d5812fa6091 100644 --- a/boa_engine/src/value/equality.rs +++ b/boa_engine/src/value/equality.rs @@ -76,10 +76,10 @@ impl JsValue { }, // 8. If Type(x) is Boolean, return the result of the comparison ! ToNumber(x) == y. - (Self::Boolean(x), _) => return other.equals(&Self::new(*x as i32), context), + (Self::Boolean(x), _) => return other.equals(&Self::new(i32::from(*x)), context), // 9. If Type(y) is Boolean, return the result of the comparison x == ! ToNumber(y). - (_, Self::Boolean(y)) => return self.equals(&Self::new(*y as i32), context), + (_, Self::Boolean(y)) => return self.equals(&Self::new(i32::from(*y)), context), // 10. If Type(x) is either String, Number, BigInt, or Symbol and Type(y) is Object, return the result // of the comparison x == ? ToPrimitive(y). From 98a354dffdfbe65fca851759662d2e54c4caaaa0 Mon Sep 17 00:00:00 2001 From: RageKnify Date: Thu, 24 Feb 2022 19:08:13 +0000 Subject: [PATCH 4/6] Refactor: add `#[must_use]` to `-> Self` methods --- boa_engine/src/object/mod.rs | 3 +++ boa_engine/src/property/mod.rs | 17 +++++++++++++++++ boa_engine/src/value/display.rs | 1 + 3 files changed, 21 insertions(+) diff --git a/boa_engine/src/object/mod.rs b/boa_engine/src/object/mod.rs index 7a100101ea5..ff3ef040cbc 100644 --- a/boa_engine/src/object/mod.rs +++ b/boa_engine/src/object/mod.rs @@ -1304,6 +1304,7 @@ impl<'context> FunctionBuilder<'context> { /// /// The default is `""` (empty string). #[inline] + #[must_use] pub fn name(mut self, name: N) -> Self where N: AsRef, @@ -1318,6 +1319,7 @@ impl<'context> FunctionBuilder<'context> { /// /// The default is `0`. #[inline] + #[must_use] pub fn length(mut self, length: usize) -> Self { self.length = length; self @@ -1327,6 +1329,7 @@ impl<'context> FunctionBuilder<'context> { /// /// The default is `false`. #[inline] + #[must_use] pub fn constructor(mut self, yes: bool) -> Self { match self.function { Function::Native { diff --git a/boa_engine/src/property/mod.rs b/boa_engine/src/property/mod.rs index 079f985d786..bad7e732688 100644 --- a/boa_engine/src/property/mod.rs +++ b/boa_engine/src/property/mod.rs @@ -216,6 +216,7 @@ impl PropertyDescriptor { } #[inline] + #[must_use] pub fn into_accessor_defaulted(mut self) -> Self { self.kind = DescriptorKind::Accessor { get: self.get().cloned(), @@ -226,6 +227,7 @@ impl PropertyDescriptor { .build() } + #[must_use] pub fn into_data_defaulted(mut self) -> Self { self.kind = DescriptorKind::Data { value: self.value().cloned(), @@ -237,6 +239,7 @@ impl PropertyDescriptor { } #[inline] + #[must_use] pub fn complete_property_descriptor(self) -> Self { PropertyDescriptorBuilder { inner: self } .complete_with_defaults() @@ -297,6 +300,7 @@ impl PropertyDescriptorBuilder { Self::default() } + #[must_use] pub fn value>(mut self, value: V) -> Self { match self.inner.kind { DescriptorKind::Data { @@ -313,6 +317,7 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn writable(mut self, writable: bool) -> Self { match self.inner.kind { DescriptorKind::Data { @@ -330,6 +335,7 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn get>(mut self, get: V) -> Self { match self.inner.kind { DescriptorKind::Accessor { get: ref mut g, .. } => *g = Some(get.into()), @@ -344,6 +350,7 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn set>(mut self, set: V) -> Self { match self.inner.kind { DescriptorKind::Accessor { set: ref mut s, .. } => *s = Some(set.into()), @@ -358,6 +365,7 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn maybe_enumerable(mut self, enumerable: Option) -> Self { if let Some(enumerable) = enumerable { self = self.enumerable(enumerable); @@ -365,6 +373,7 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn maybe_configurable(mut self, configurable: Option) -> Self { if let Some(configurable) = configurable { self = self.configurable(configurable); @@ -372,6 +381,7 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn maybe_value>(mut self, value: Option) -> Self { if let Some(value) = value { self = self.value(value); @@ -379,6 +389,7 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn maybe_writable(mut self, writable: Option) -> Self { if let Some(writable) = writable { self = self.writable(writable); @@ -386,6 +397,7 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn maybe_get>(mut self, get: Option) -> Self { if let Some(get) = get { self = self.get(get); @@ -393,6 +405,7 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn maybe_set>(mut self, set: Option) -> Self { if let Some(set) = set { self = self.set(set); @@ -400,15 +413,19 @@ impl PropertyDescriptorBuilder { self } + #[must_use] pub fn enumerable(mut self, enumerable: bool) -> Self { self.inner.enumerable = Some(enumerable); self } + + #[must_use] pub fn configurable(mut self, configurable: bool) -> Self { self.inner.configurable = Some(configurable); self } + #[must_use] pub fn complete_with_defaults(mut self) -> Self { match self.inner.kind { DescriptorKind::Generic => { diff --git a/boa_engine/src/value/display.rs b/boa_engine/src/value/display.rs index 28299882554..97f2754bbc9 100644 --- a/boa_engine/src/value/display.rs +++ b/boa_engine/src/value/display.rs @@ -14,6 +14,7 @@ impl ValueDisplay<'_> { /// /// By default this is `false`. #[inline] + #[must_use] pub fn internals(mut self, yes: bool) -> Self { self.internals = yes; self From 997fac24ccf0df53c53845a6d48b7982679876eb Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Thu, 24 Feb 2022 23:30:47 +0100 Subject: [PATCH 5/6] run cargo fmt --- boa_engine/src/builtins/typed_array/mod.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/boa_engine/src/builtins/typed_array/mod.rs b/boa_engine/src/builtins/typed_array/mod.rs index 9155fc9d692..15ed57bca08 100644 --- a/boa_engine/src/builtins/typed_array/mod.rs +++ b/boa_engine/src/builtins/typed_array/mod.rs @@ -3348,10 +3348,7 @@ impl TypedArrayName { } pub(crate) fn is_big_int_element_type(self) -> bool { - matches!( - self, - TypedArrayName::BigUint64 | TypedArrayName::BigInt64 - ) + matches!(self, TypedArrayName::BigUint64 | TypedArrayName::BigInt64) } } @@ -3367,10 +3364,6 @@ typed_array!(Uint16, "Uint16Array", typed_uint16_array_object); typed_array!(Int32, "Int32Array", typed_int32_array_object); typed_array!(Uint32, "Uint32Array", typed_uint32_array_object); typed_array!(BigInt64, "BigInt64Array", typed_bigint64_array_object); -typed_array!( - BigUint64, - "BigUint64Array", - typed_biguint64_array_object -); +typed_array!(BigUint64, "BigUint64Array", typed_biguint64_array_object); typed_array!(Float32, "Float32Array", typed_float32_array_object); typed_array!(Float64, "Float64Array", typed_float64_array_object); From 4a2dc526e6b30b163676c4611b68d696ff08727f Mon Sep 17 00:00:00 2001 From: RageKnify Date: Sat, 26 Feb 2022 13:51:08 +0000 Subject: [PATCH 6/6] Refactor: change enum name, keep built-ins names Discussed in PR and Discord --- boa_engine/src/builtins/array_buffer/mod.rs | 90 ++++++------- boa_engine/src/builtins/dataview/mod.rs | 46 +++---- boa_engine/src/builtins/mod.rs | 26 ++-- .../typed_array/integer_indexed_object.rs | 8 +- boa_engine/src/builtins/typed_array/mod.rs | 127 +++++++++++------- 5 files changed, 164 insertions(+), 133 deletions(-) diff --git a/boa_engine/src/builtins/array_buffer/mod.rs b/boa_engine/src/builtins/array_buffer/mod.rs index 3ee3add1eea..b485fd78f6f 100644 --- a/boa_engine/src/builtins/array_buffer/mod.rs +++ b/boa_engine/src/builtins/array_buffer/mod.rs @@ -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, @@ -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::Int8 - | TypedArrayName::Uint8 - | TypedArrayName::Int16 - | TypedArrayName::Uint16 - | TypedArrayName::Int32 - | TypedArrayName::Uint32 + TypedArrayKind::Int8 + | TypedArrayKind::Uint8 + | TypedArrayKind::Int16 + | TypedArrayKind::Uint16 + | TypedArrayKind::Int32 + | TypedArrayKind::Uint32 ) } @@ -417,10 +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::BigUint64 | TypedArrayName::BigInt64) + matches!(t, TypedArrayKind::BigUint64 | TypedArrayKind::BigInt64) } /// `25.1.2.8 IsNoTearConfiguration ( type, order )` @@ -431,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; @@ -457,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::Int8 => { + 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::Uint8 | TypedArrayName::Uint8Clamped => { + 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::Int16 => { + TypedArrayKind::Int16 => { if is_little_endian { i16::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -482,7 +482,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Uint16 => { + TypedArrayKind::Uint16 => { if is_little_endian { u16::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -491,7 +491,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Int32 => { + TypedArrayKind::Int32 => { if is_little_endian { i32::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -500,7 +500,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Uint32 => { + TypedArrayKind::Uint32 => { if is_little_endian { u32::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -509,7 +509,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::BigInt64 => { + TypedArrayKind::BigInt64 => { if is_little_endian { i64::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -518,7 +518,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::BigUint64 => { + TypedArrayKind::BigUint64 => { if is_little_endian { u64::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -527,7 +527,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Float32 => { + TypedArrayKind::Float32 => { if is_little_endian { f32::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -536,7 +536,7 @@ impl ArrayBuffer { .into() } } - TypedArrayName::Float64 => { + TypedArrayKind::Float64 => { if is_little_endian { f64::from_le_bytes(bytes.try_into().expect("slice with incorrect length")) .into() @@ -559,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, @@ -597,41 +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> { Ok(match t { - TypedArrayName::Int8 if is_little_endian => { + TypedArrayKind::Int8 if is_little_endian => { value.to_int8(context)?.to_le_bytes().to_vec() } - TypedArrayName::Int8 => value.to_int8(context)?.to_be_bytes().to_vec(), - TypedArrayName::Uint8 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::Uint8 => value.to_uint8(context)?.to_be_bytes().to_vec(), - TypedArrayName::Uint8Clamped 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::Uint8Clamped => value.to_uint8_clamp(context)?.to_be_bytes().to_vec(), - TypedArrayName::Int16 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::Int16 => value.to_int16(context)?.to_be_bytes().to_vec(), - TypedArrayName::Uint16 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::Uint16 => value.to_uint16(context)?.to_be_bytes().to_vec(), - TypedArrayName::Int32 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::Int32 => value.to_i32(context)?.to_be_bytes().to_vec(), - TypedArrayName::Uint32 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::Uint32 => value.to_u32(context)?.to_be_bytes().to_vec(), - TypedArrayName::BigInt64 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() @@ -645,7 +645,7 @@ impl ArrayBuffer { .to_le_bytes() .to_vec() } - TypedArrayName::BigInt64 => { + TypedArrayKind::BigInt64 => { let big_int = value.to_big_int64(context)?; big_int .to_i64() @@ -659,23 +659,23 @@ impl ArrayBuffer { .to_be_bytes() .to_vec() } - TypedArrayName::BigUint64 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::BigUint64 => value + TypedArrayKind::BigUint64 => value .to_big_uint64(context)? .to_u64() .unwrap_or(u64::MAX) .to_be_bytes() .to_vec(), - TypedArrayName::Float32 => 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::Float64 => 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(), }, @@ -691,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, diff --git a/boa_engine/src/builtins/dataview/mod.rs b/boa_engine/src/builtins/dataview/mod.rs index c6eaeb9257c..6e587feb287 100644 --- a/boa_engine/src/builtins/dataview/mod.rs +++ b/boa_engine/src/builtins/dataview/mod.rs @@ -1,5 +1,5 @@ use crate::{ - builtins::{array_buffer::SharedMemoryOrder, typed_array::TypedArrayName, BuiltIn, JsArgs}, + builtins::{array_buffer::SharedMemoryOrder, typed_array::TypedArrayKind, BuiltIn, JsArgs}, context::StandardObjects, object::{ internal_methods::get_prototype_from_constructor, ConstructorBuilder, FunctionBuilder, @@ -300,7 +300,7 @@ impl DataView { view: &JsValue, request_index: &JsValue, is_little_endian: &JsValue, - t: TypedArrayName, + t: TypedArrayKind, context: &mut Context, ) -> JsResult { // 1. Perform ? RequireInternalSlot(view, [[DataView]]). @@ -378,7 +378,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::BigInt64, + TypedArrayKind::BigInt64, context, ) } @@ -407,7 +407,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::BigUint64, + TypedArrayKind::BigUint64, context, ) } @@ -436,7 +436,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Float32, + TypedArrayKind::Float32, context, ) } @@ -465,7 +465,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Float64, + TypedArrayKind::Float64, context, ) } @@ -494,7 +494,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int8, + TypedArrayKind::Int8, context, ) } @@ -523,7 +523,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int16, + TypedArrayKind::Int16, context, ) } @@ -552,7 +552,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int32, + TypedArrayKind::Int32, context, ) } @@ -581,7 +581,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint8, + TypedArrayKind::Uint8, context, ) } @@ -610,7 +610,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint16, + TypedArrayKind::Uint16, context, ) } @@ -639,7 +639,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint32, + TypedArrayKind::Uint32, context, ) } @@ -658,7 +658,7 @@ impl DataView { view: &JsValue, request_index: &JsValue, is_little_endian: &JsValue, - t: TypedArrayName, + t: TypedArrayKind, value: &JsValue, context: &mut Context, ) -> JsResult { @@ -747,7 +747,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::BigInt64, + TypedArrayKind::BigInt64, value, context, ) @@ -778,7 +778,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::BigUint64, + TypedArrayKind::BigUint64, value, context, ) @@ -809,7 +809,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Float32, + TypedArrayKind::Float32, value, context, ) @@ -840,7 +840,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Float64, + TypedArrayKind::Float64, value, context, ) @@ -871,7 +871,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int8, + TypedArrayKind::Int8, value, context, ) @@ -902,7 +902,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int16, + TypedArrayKind::Int16, value, context, ) @@ -933,7 +933,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Int32, + TypedArrayKind::Int32, value, context, ) @@ -964,7 +964,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint8, + TypedArrayKind::Uint8, value, context, ) @@ -995,7 +995,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint16, + TypedArrayKind::Uint16, value, context, ) @@ -1026,7 +1026,7 @@ impl DataView { this, byte_offset, is_little_endian, - TypedArrayName::Uint32, + TypedArrayKind::Uint32, value, context, ) diff --git a/boa_engine/src/builtins/mod.rs b/boa_engine/src/builtins/mod.rs index e3863182d6d..de272641a21 100644 --- a/boa_engine/src/builtins/mod.rs +++ b/boa_engine/src/builtins/mod.rs @@ -57,8 +57,8 @@ pub(crate) use self::{ string::String, symbol::Symbol, typed_array::{ - BigInt64, BigUint64, Float32, Float64, Int16, Int32, Int8, Uint16, Uint32, Uint8, - Uint8Clamped, + BigInt64Array, BigUint64Array, Float32Array, Float64Array, Int16Array, Int32Array, + Int8Array, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, }, undefined::Undefined, }; @@ -140,17 +140,17 @@ pub fn init(context: &mut Context) { Set, String, RegExp, - Int8, - Uint8, - Uint8Clamped, - Int16, - Uint16, - Int32, - Uint32, - BigInt64, - BigUint64, - Float32, - Float64, + Int8Array, + Uint8Array, + Uint8ClampedArray, + Int16Array, + Uint16Array, + Int32Array, + Uint32Array, + BigInt64Array, + BigUint64Array, + Float32Array, + Float64Array, Symbol, Error, RangeError, diff --git a/boa_engine/src/builtins/typed_array/integer_indexed_object.rs b/boa_engine/src/builtins/typed_array/integer_indexed_object.rs index f9297eb0cff..f36982126f4 100644 --- a/boa_engine/src/builtins/typed_array/integer_indexed_object.rs +++ b/boa_engine/src/builtins/typed_array/integer_indexed_object.rs @@ -9,7 +9,7 @@ //! [spec]: https://tc39.es/ecma262/#sec-integer-indexed-exotic-objects use crate::{ - builtins::typed_array::TypedArrayName, + builtins::typed_array::TypedArrayKind, object::{JsObject, ObjectData}, Context, }; @@ -31,7 +31,7 @@ unsafe impl Trace for ContentType { #[derive(Debug, Clone, Trace, Finalize)] pub struct IntegerIndexed { viewed_array_buffer: Option, - typed_array_name: TypedArrayName, + typed_array_name: TypedArrayKind, byte_offset: usize, byte_length: usize, array_length: usize, @@ -40,7 +40,7 @@ pub struct IntegerIndexed { impl IntegerIndexed { pub(crate) fn new( viewed_array_buffer: Option, - typed_array_name: TypedArrayName, + typed_array_name: TypedArrayKind, byte_offset: usize, byte_length: usize, array_length: usize, @@ -115,7 +115,7 @@ impl IntegerIndexed { } /// Get the integer indexed object's typed array name. - pub(crate) fn typed_array_name(&self) -> TypedArrayName { + pub(crate) fn typed_array_name(&self) -> TypedArrayKind { self.typed_array_name } diff --git a/boa_engine/src/builtins/typed_array/mod.rs b/boa_engine/src/builtins/typed_array/mod.rs index 15ed57bca08..e6d783f9d05 100644 --- a/boa_engine/src/builtins/typed_array/mod.rs +++ b/boa_engine/src/builtins/typed_array/mod.rs @@ -37,7 +37,7 @@ use std::cmp::Ordering; pub mod integer_indexed_object; macro_rules! typed_array { - ($ty:ident, $name:literal, $global_object_name:ident) => { + ($ty:ident, $variant:ident, $name:literal, $global_object_name:ident) => { #[doc = concat!("JavaScript `", $name, "` built-in implementation.")] #[derive(Debug, Clone, Copy)] pub struct $ty; @@ -75,12 +75,12 @@ macro_rules! typed_array { ) .property( "BYTES_PER_ELEMENT", - TypedArrayName::$ty.element_size(), + TypedArrayKind::$variant.element_size(), Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT, ) .static_property( "BYTES_PER_ELEMENT", - TypedArrayName::$ty.element_size(), + TypedArrayKind::$variant.element_size(), Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::PERMANENT, ) .custom_prototype(typed_array_constructor) @@ -113,7 +113,7 @@ macro_rules! typed_array { } // 2. Let constructorName be the String value of the Constructor Name value specified in Table 72 for this TypedArray constructor. - let constructor_name = TypedArrayName::$ty; + let constructor_name = TypedArrayKind::$variant; // 3. Let proto be "%TypedArray.prototype%". let proto = StandardObjects::$global_object_name; @@ -797,7 +797,7 @@ impl TypedArray { // i. Let value be GetValueFromBuffer(buffer, fromByteIndex, Uint8, true, Unordered). let value = buffer.get_value_from_buffer( from_byte_index as usize, - TypedArrayName::Uint8, + TypedArrayKind::Uint8, true, SharedMemoryOrder::Unordered, None, @@ -806,7 +806,7 @@ impl TypedArray { // ii. Perform SetValueInBuffer(buffer, toByteIndex, Uint8, value, true, Unordered). buffer.set_value_in_buffer( to_byte_index as usize, - TypedArrayName::Uint8, + TypedArrayKind::Uint8, &value, SharedMemoryOrder::Unordered, None, @@ -2054,7 +2054,7 @@ impl TypedArray { // i. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, Uint8, true, Unordered). let value = src_buffer.get_value_from_buffer( src_byte_index, - TypedArrayName::Uint8, + TypedArrayKind::Uint8, true, SharedMemoryOrder::Unordered, None, @@ -2067,7 +2067,7 @@ impl TypedArray { .expect("Must be an array buffer") .set_value_in_buffer( target_byte_index, - TypedArrayName::Uint8, + TypedArrayKind::Uint8, &value, SharedMemoryOrder::Unordered, None, @@ -2367,7 +2367,7 @@ impl TypedArray { // 1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, Uint8, true, Unordered). let value = src_buffer.get_value_from_buffer( src_byte_index, - TypedArrayName::Uint8, + TypedArrayKind::Uint8, true, SharedMemoryOrder::Unordered, None, @@ -2376,7 +2376,7 @@ impl TypedArray { // 2. Perform SetValueInBuffer(targetBuffer, targetByteIndex, Uint8, value, true, Unordered). target_buffer.set_value_in_buffer( target_byte_index, - TypedArrayName::Uint8, + TypedArrayKind::Uint8, &value, SharedMemoryOrder::Unordered, None, @@ -2809,23 +2809,23 @@ impl TypedArray { /// [spec]: https://tc39.es/ecma262/#typedarray-species-create fn species_create( exemplar: &JsObject, - typed_array_name: TypedArrayName, + typed_array_name: TypedArrayKind, args: &[JsValue], context: &mut Context, ) -> JsResult { // 1. Let defaultConstructor be the intrinsic object listed in column one of Table 73 for exemplar.[[TypedArrayName]]. let default_constructor = match typed_array_name { - TypedArrayName::Int8 => StandardObjects::typed_int8_array_object, - TypedArrayName::Uint8 => StandardObjects::typed_uint8_array_object, - TypedArrayName::Uint8Clamped => StandardObjects::typed_uint8clamped_array_object, - TypedArrayName::Int16 => StandardObjects::typed_int16_array_object, - TypedArrayName::Uint16 => StandardObjects::typed_uint16_array_object, - TypedArrayName::Int32 => StandardObjects::typed_int32_array_object, - TypedArrayName::Uint32 => StandardObjects::typed_uint32_array_object, - TypedArrayName::BigInt64 => StandardObjects::typed_bigint64_array_object, - TypedArrayName::BigUint64 => StandardObjects::typed_biguint64_array_object, - TypedArrayName::Float32 => StandardObjects::typed_float32_array_object, - TypedArrayName::Float64 => StandardObjects::typed_float64_array_object, + TypedArrayKind::Int8 => StandardObjects::typed_int8_array_object, + TypedArrayKind::Uint8 => StandardObjects::typed_uint8_array_object, + TypedArrayKind::Uint8Clamped => StandardObjects::typed_uint8clamped_array_object, + TypedArrayKind::Int16 => StandardObjects::typed_int16_array_object, + TypedArrayKind::Uint16 => StandardObjects::typed_uint16_array_object, + TypedArrayKind::Int32 => StandardObjects::typed_int32_array_object, + TypedArrayKind::Uint32 => StandardObjects::typed_uint32_array_object, + TypedArrayKind::BigInt64 => StandardObjects::typed_bigint64_array_object, + TypedArrayKind::BigUint64 => StandardObjects::typed_biguint64_array_object, + TypedArrayKind::Float32 => StandardObjects::typed_float32_array_object, + TypedArrayKind::Float64 => StandardObjects::typed_float64_array_object, }; // 2. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor). @@ -2975,7 +2975,7 @@ impl TypedArray { /// /// [spec]: https://tc39.es/ecma262/#sec-allocatetypedarray fn allocate

( - constructor_name: TypedArrayName, + constructor_name: TypedArrayKind, new_target: &JsValue, default_proto: P, length: Option, @@ -3287,7 +3287,7 @@ impl TypedArray { /// Names of all the typed arrays. #[derive(Debug, Clone, Copy, Finalize, PartialEq)] -pub(crate) enum TypedArrayName { +pub(crate) enum TypedArrayKind { Int8, Uint8, Uint8Clamped, @@ -3301,12 +3301,12 @@ pub(crate) enum TypedArrayName { Float64, } -unsafe impl Trace for TypedArrayName { +unsafe impl Trace for TypedArrayKind { // Safe because `TypedArrayName` is `Copy` unsafe_empty_trace!(); } -impl TypedArrayName { +impl TypedArrayKind { /// Gets the element size of the given typed array name, as per the [spec]. /// /// [spec]: https://tc39.es/ecma262/#table-the-typedarray-constructors @@ -3333,37 +3333,68 @@ impl TypedArrayName { #[inline] pub(crate) const fn name(&self) -> &str { match self { - TypedArrayName::Int8 => "Int8Array", - TypedArrayName::Uint8 => "Uint8Array", - TypedArrayName::Uint8Clamped => "Uint8ClampedArray", - TypedArrayName::Int16 => "Int16Array", - TypedArrayName::Uint16 => "Uint16Array", - TypedArrayName::Int32 => "Int32Array", - TypedArrayName::Uint32 => "Uint32Array", - TypedArrayName::BigInt64 => "BigInt64Array", - TypedArrayName::BigUint64 => "BigUint64Array", - TypedArrayName::Float32 => "Float32Array", - TypedArrayName::Float64 => "Float64Array", + TypedArrayKind::Int8 => "Int8Array", + TypedArrayKind::Uint8 => "Uint8Array", + TypedArrayKind::Uint8Clamped => "Uint8ClampedArray", + TypedArrayKind::Int16 => "Int16Array", + TypedArrayKind::Uint16 => "Uint16Array", + TypedArrayKind::Int32 => "Int32Array", + TypedArrayKind::Uint32 => "Uint32Array", + TypedArrayKind::BigInt64 => "BigInt64Array", + TypedArrayKind::BigUint64 => "BigUint64Array", + TypedArrayKind::Float32 => "Float32Array", + TypedArrayKind::Float64 => "Float64Array", } } pub(crate) fn is_big_int_element_type(self) -> bool { - matches!(self, TypedArrayName::BigUint64 | TypedArrayName::BigInt64) + matches!(self, TypedArrayKind::BigUint64 | TypedArrayKind::BigInt64) } } -typed_array!(Int8, "Int8Array", typed_int8_array_object); -typed_array!(Uint8, "Uint8Array", typed_uint8_array_object); +typed_array!(Int8Array, Int8, "Int8Array", typed_int8_array_object); +typed_array!(Uint8Array, Uint8, "Uint8Array", typed_uint8_array_object); typed_array!( + Uint8ClampedArray, Uint8Clamped, "Uint8ClampedArray", typed_uint8clamped_array_object ); -typed_array!(Int16, "Int16Array", typed_int16_array_object); -typed_array!(Uint16, "Uint16Array", typed_uint16_array_object); -typed_array!(Int32, "Int32Array", typed_int32_array_object); -typed_array!(Uint32, "Uint32Array", typed_uint32_array_object); -typed_array!(BigInt64, "BigInt64Array", typed_bigint64_array_object); -typed_array!(BigUint64, "BigUint64Array", typed_biguint64_array_object); -typed_array!(Float32, "Float32Array", typed_float32_array_object); -typed_array!(Float64, "Float64Array", typed_float64_array_object); +typed_array!(Int16Array, Int16, "Int16Array", typed_int16_array_object); +typed_array!( + Uint16Array, + Uint16, + "Uint16Array", + typed_uint16_array_object +); +typed_array!(Int32Array, Int32, "Int32Array", typed_int32_array_object); +typed_array!( + Uint32Array, + Uint32, + "Uint32Array", + typed_uint32_array_object +); +typed_array!( + BigInt64Array, + BigInt64, + "BigInt64Array", + typed_bigint64_array_object +); +typed_array!( + BigUint64Array, + BigUint64, + "BigUint64Array", + typed_biguint64_array_object +); +typed_array!( + Float32Array, + Float32, + "Float32Array", + typed_float32_array_object +); +typed_array!( + Float64Array, + Float64, + "Float64Array", + typed_float64_array_object +);