Skip to content

Commit de3b5bf

Browse files
committed
chore(ecmaascript): refactor
1 parent ad348e7 commit de3b5bf

File tree

2 files changed

+36
-105
lines changed

2 files changed

+36
-105
lines changed

nova_vm/src/ecmascript/builtins/indexed_collections/typed_array_objects/typed_array_intrinsic_object.rs

Lines changed: 32 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ use crate::{
5353
};
5454

5555
use super::abstract_operations::{
56-
TypedArrayWithBufferWitnessRecords, is_typed_array_out_of_bounds,
57-
is_valid_integer_index_generic, make_typed_array_with_buffer_witness_record,
58-
typed_array_byte_length, typed_array_create_from_constructor_with_length,
59-
typed_array_create_same_type, typed_array_length, typed_array_species_create_with_length,
60-
validate_typed_array,
56+
TypedArrayWithBufferWitnessRecords, is_typed_array_out_of_bounds, is_valid_integer_index,
57+
make_typed_array_with_buffer_witness_record, typed_array_byte_length,
58+
typed_array_create_from_constructor_with_length, typed_array_create_same_type,
59+
typed_array_length, typed_array_species_create_with_length, validate_typed_array,
6160
};
6261

6362
pub struct TypedArrayIntrinsicObject;
@@ -2162,94 +2161,16 @@ impl TypedArrayPrototype {
21622161
let ta_record = validate_typed_array(agent, o, Ordering::SeqCst, gc.nogc())
21632162
.unbind()?
21642163
.bind(gc.nogc());
2165-
let o = match ta_record.object {
2166-
TypedArray::Int8Array(_) => with_typed_array::<i8>(
2167-
agent,
2168-
ta_record.unbind(),
2169-
index.unbind(),
2170-
value.unbind(),
2171-
gc,
2172-
),
2173-
TypedArray::Uint8Array(_) => with_typed_array::<u8>(
2174-
agent,
2175-
ta_record.unbind(),
2176-
index.unbind(),
2177-
value.unbind(),
2178-
gc,
2179-
),
2180-
TypedArray::Uint8ClampedArray(_) => with_typed_array::<U8Clamped>(
2181-
agent,
2182-
ta_record.unbind(),
2183-
index.unbind(),
2184-
value.unbind(),
2185-
gc,
2186-
),
2187-
TypedArray::Int16Array(_) => with_typed_array::<i16>(
2188-
agent,
2189-
ta_record.unbind(),
2190-
index.unbind(),
2191-
value.unbind(),
2192-
gc,
2193-
),
2194-
TypedArray::Uint16Array(_) => with_typed_array::<u16>(
2195-
agent,
2196-
ta_record.unbind(),
2197-
index.unbind(),
2198-
value.unbind(),
2199-
gc,
2200-
),
2201-
TypedArray::Int32Array(_) => with_typed_array::<i32>(
2202-
agent,
2203-
ta_record.unbind(),
2204-
index.unbind(),
2205-
value.unbind(),
2206-
gc,
2207-
),
2208-
TypedArray::Uint32Array(_) => with_typed_array::<u32>(
2209-
agent,
2210-
ta_record.unbind(),
2211-
index.unbind(),
2212-
value.unbind(),
2213-
gc,
2214-
),
2215-
TypedArray::BigInt64Array(_) => with_typed_array::<i64>(
2216-
agent,
2217-
ta_record.unbind(),
2218-
index.unbind(),
2219-
value.unbind(),
2220-
gc,
2221-
),
2222-
TypedArray::BigUint64Array(_) => with_typed_array::<u64>(
2223-
agent,
2224-
ta_record.unbind(),
2225-
index.unbind(),
2226-
value.unbind(),
2227-
gc,
2228-
),
2229-
#[cfg(feature = "proposal-float16array")]
2230-
TypedArray::Float16Array(_) => with_typed_array::<f16>(
2231-
agent,
2232-
ta_record.unbind(),
2233-
index.unbind(),
2234-
value.unbind(),
2235-
gc,
2236-
),
2237-
TypedArray::Float32Array(_) => with_typed_array::<f32>(
2238-
agent,
2239-
ta_record.unbind(),
2240-
index.unbind(),
2241-
value.unbind(),
2242-
gc,
2243-
),
2244-
TypedArray::Float64Array(_) => with_typed_array::<f64>(
2164+
let o = with_typed_array_viewable!(
2165+
ta_record.object,
2166+
with_typed_array::<T>(
22452167
agent,
22462168
ta_record.unbind(),
22472169
index.unbind(),
22482170
value.unbind(),
22492171
gc,
2250-
),
2251-
};
2252-
2172+
)
2173+
);
22532174
o.map(|o| o.into_value())
22542175
}
22552176

@@ -2947,6 +2868,7 @@ fn split_typed_array_views<'a, T: Viewable + std::fmt::Debug>(
29472868
let a_ptr = a_slice.as_mut_ptr();
29482869
let a_len = a_slice.len();
29492870
let o_aligned = viewable_slice::<T>(agent, o, gc).unwrap();
2871+
// SAFETY: Confirmed beforehand that the two ArrayBuffers are in separate memory regions.
29502872
let a_aligned = unsafe { std::slice::from_raw_parts_mut(a_ptr, a_len) };
29512873
Ok((a_aligned, o_aligned))
29522874
}
@@ -2958,10 +2880,14 @@ fn with_typed_array<'a, T: Viewable + std::fmt::Debug>(
29582880
value: Value,
29592881
mut gc: GcScope<'a, '_>,
29602882
) -> JsResult<'a, TypedArray<'a>> {
2883+
let ta_record = ta_record.bind(gc.nogc());
29612884
let index = index.bind(gc.nogc());
29622885
let value = value.bind(gc.nogc());
29632886
let o = ta_record.object;
2887+
let scoped_o = o.scope(agent, gc.nogc());
29642888
let scoped_value = value.scope(agent, gc.nogc());
2889+
// 3. Let len be TypedArrayLength(taRecord).
2890+
let len = typed_array_length::<T>(agent, &ta_record, gc.nogc()) as i64;
29652891
// 4. Let relativeIndex be ? ToIntegerOrInfinity(index).
29662892
// 5. If relativeIndex ≥ 0, let actualIndex be relativeIndex.
29672893
let relative_index = if let Value::Integer(index) = index {
@@ -2972,16 +2898,20 @@ fn with_typed_array<'a, T: Viewable + std::fmt::Debug>(
29722898
.bind(gc.nogc())
29732899
.into_i64()
29742900
};
2975-
let numeric_value = scoped_value
2976-
.get(agent)
2977-
.into_value()
2978-
.to_numeric(agent, gc.reborrow())
2979-
.unbind()?
2980-
.bind(gc.nogc());
2981-
// 7. If O.[[ContentType]] is bigint, let numericValue be ? ToBigInt(value).
2982-
let numeric_value = T::from_le_value(agent, numeric_value);
2983-
// 3. Let len be TypedArrayLength(taRecord).
2984-
let len = typed_array_length::<T>(agent, &ta_record, gc.nogc()) as i64;
2901+
// 7. If O.[[ContentType]] is BIGINT, let numericValue be ? ToBigInt(value).
2902+
let numeric_value = if T::IS_BIGINT {
2903+
to_big_int(agent, scoped_value.get(agent), gc.reborrow())
2904+
.unbind()?
2905+
.bind(gc.nogc())
2906+
.into_numeric()
2907+
} else {
2908+
// 8. Else, let numericValue be ? ToNumber(value).
2909+
to_number(agent, scoped_value.get(agent), gc.reborrow())
2910+
.unbind()?
2911+
.bind(gc.nogc())
2912+
.into_numeric()
2913+
};
2914+
let numeric_value = T::from_ne_value(agent, numeric_value);
29852915
// 5. If relativeIndex ≥ 0, let actualIndex be relativeIndex.
29862916
let actual_index = if relative_index >= 0 {
29872917
relative_index
@@ -2990,15 +2920,15 @@ fn with_typed_array<'a, T: Viewable + std::fmt::Debug>(
29902920
len + relative_index
29912921
};
29922922
// 9. If IsValidIntegerIndex(O, 𝔽(actualIndex)) is false, throw a RangeError exception.
2993-
if is_valid_integer_index_generic(agent, o, actual_index, gc.nogc()).is_none() {
2923+
if is_valid_integer_index::<T>(agent, scoped_o.get(agent), actual_index, gc.nogc()).is_none() {
29942924
return Err(agent.throw_exception_with_static_message(
29952925
ExceptionType::RangeError,
29962926
"Index out of bounds",
29972927
gc.into_nogc(),
29982928
));
29992929
}
30002930
// 10. Let A be ? TypedArrayCreateSameType(O, « 𝔽(len) »).
3001-
let a = typed_array_create_same_type(agent, o, len, gc.reborrow())
2931+
let a = typed_array_create_same_type(agent, scoped_o.get(agent), len, gc.reborrow())
30022932
.unbind()?
30032933
.bind(gc.nogc());
30042934
// 11. Let k be 0.
@@ -3008,7 +2938,8 @@ fn with_typed_array<'a, T: Viewable + std::fmt::Debug>(
30082938
// c. Else, let fromValue be ! Get(O, Pk).
30092939
// d. Perform ! Set(A, Pk, fromValue, true).
30102940
// e. Set k to k + 1.
3011-
let (a_slice, o_slice) = split_typed_array_views::<T>(agent, a, o, gc.nogc()).unwrap();
2941+
let (a_slice, o_slice) =
2942+
split_typed_array_views::<T>(agent, a, scoped_o.get(agent), gc.nogc()).unwrap();
30122943
let len = len as usize;
30132944
let a_slice = &mut a_slice[..len];
30142945
let o_slice = &o_slice[..len];

tests/metrics.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"results": {
3-
"crash": 7250,
4-
"fail": 11300,
5-
"pass": 28222,
3+
"crash": 7252,
4+
"fail": 11285,
5+
"pass": 28235,
66
"skip": 29,
77
"timeout": 0,
88
"unresolved": 0
99
},
1010
"total": 46801
11-
}
11+
}

0 commit comments

Comments
 (0)