@@ -29,16 +29,32 @@ pub(crate) use ordering::*;
2929
3030const MAX_TYPED_ARRAY_LENGTH : usize = 1_000_000 ;
3131
32- /// Internal slot naming the concrete TypedArray kind (e.g. `"Uint8Array"`).
33- pub ( crate ) const TYPED_ARRAY_KIND_PROPERTY : & str = "\0 TypedArrayKind" ;
34- /// Internal slot referencing the backing `ArrayBuffer` object.
35- pub ( crate ) const TYPED_ARRAY_BUFFER_PROPERTY : & str = "\0 TypedArrayBuffer" ;
36- /// Internal slot holding the byte offset of the view into its buffer.
37- pub ( crate ) const TYPED_ARRAY_BYTE_OFFSET_PROPERTY : & str = "\0 TypedArrayByteOffset" ;
38- /// Internal slot holding the element count of the view.
39- pub ( crate ) const TYPED_ARRAY_LENGTH_PROPERTY : & str = "\0 TypedArrayArrayLength" ;
40- /// Internal slot marking views whose length tracks a resizable ArrayBuffer.
41- pub ( crate ) const TYPED_ARRAY_LENGTH_TRACKING_PROPERTY : & str = "\0 TypedArrayLengthTracking" ;
32+ #[ derive( Clone ) ]
33+ pub ( crate ) struct TypedArraySlots {
34+ kind : NativeFunction ,
35+ buffer : ObjectRef ,
36+ byte_offset : usize ,
37+ fixed_length : usize ,
38+ length_tracking : bool ,
39+ }
40+
41+ impl TypedArraySlots {
42+ pub ( crate ) fn new (
43+ kind : NativeFunction ,
44+ buffer : ObjectRef ,
45+ byte_offset : usize ,
46+ fixed_length : usize ,
47+ length_tracking : bool ,
48+ ) -> Self {
49+ Self {
50+ kind,
51+ buffer,
52+ byte_offset,
53+ fixed_length,
54+ length_tracking,
55+ }
56+ }
57+ }
4258
4359/// Whether `object` carries the TypedArray brand.
4460pub ( crate ) fn is_typed_array_object ( object : & ObjectRef ) -> bool {
@@ -378,10 +394,7 @@ pub(crate) fn native_typed_array_prototype_buffer(
378394 this_value : Value ,
379395) -> Result < Value , RuntimeError > {
380396 let object = typed_array_receiver ( & this_value) ?;
381- match object. own_property ( TYPED_ARRAY_BUFFER_PROPERTY ) {
382- Some ( Property { value, .. } ) => Ok ( value) ,
383- None => Ok ( Value :: Undefined ) ,
384- }
397+ Ok ( typed_array_buffer ( & object) . map_or ( Value :: Undefined , Value :: Object ) )
385398}
386399
387400/// `get %TypedArray.prototype%[Symbol.toStringTag]`: the kind name, or
@@ -390,15 +403,11 @@ pub(crate) fn native_typed_array_prototype_to_string_tag(
390403 this_value : Value ,
391404) -> Result < Value , RuntimeError > {
392405 match this_value {
393- Value :: Object ( object) if is_typed_array_object ( & object) => {
394- match object. own_property ( TYPED_ARRAY_KIND_PROPERTY ) {
395- Some ( Property {
396- value : Value :: String ( name) ,
397- ..
398- } ) => Ok ( Value :: String ( name) ) ,
399- _ => Ok ( Value :: Undefined ) ,
400- }
401- }
406+ Value :: Object ( object) if is_typed_array_object ( & object) => Ok ( Value :: String (
407+ typed_array_name ( typed_array_kind ( & object) )
408+ . to_owned ( )
409+ . into ( ) ,
410+ ) ) ,
402411 _ => Ok ( Value :: Undefined ) ,
403412 }
404413}
@@ -605,13 +614,9 @@ fn is_object_like(value: &Value) -> bool {
605614// --- Internal-slot helpers ---------------------------------------------------
606615
607616pub ( crate ) fn typed_array_kind ( object : & ObjectRef ) -> NativeFunction {
608- match object. own_property ( TYPED_ARRAY_KIND_PROPERTY ) {
609- Some ( Property {
610- value : Value :: String ( name) ,
611- ..
612- } ) => native_for_name ( & name) ,
613- _ => NativeFunction :: Uint8Array ,
614- }
617+ object
618+ . typed_array_slots ( )
619+ . map_or ( NativeFunction :: Uint8Array , |slots| slots. kind )
615620}
616621
617622pub ( crate ) fn typed_array_length ( object : & ObjectRef ) -> usize {
@@ -628,13 +633,10 @@ pub(super) fn typed_array_length_for_buffer_byte_length(
628633 object : & ObjectRef ,
629634 buffer_byte_length : usize ,
630635) -> usize {
631- let fixed_length = match object. own_property ( TYPED_ARRAY_LENGTH_PROPERTY ) {
632- Some ( Property {
633- value : Value :: Number ( length) ,
634- ..
635- } ) => length as usize ,
636- _ => return 0 ,
636+ let Some ( slots) = object. typed_array_slots ( ) else {
637+ return 0 ;
637638 } ;
639+ let fixed_length = slots. fixed_length ;
638640 let offset = typed_array_byte_offset ( object) ;
639641 let element = bytes_per_element ( typed_array_kind ( object) ) ;
640642 if typed_array_is_length_tracking ( object) {
@@ -657,23 +659,13 @@ pub(super) fn typed_array_length_for_buffer_byte_length(
657659}
658660
659661fn typed_array_fixed_length ( object : & ObjectRef ) -> Option < usize > {
660- match object. own_property ( TYPED_ARRAY_LENGTH_PROPERTY ) {
661- Some ( Property {
662- value : Value :: Number ( length) ,
663- ..
664- } ) => Some ( length as usize ) ,
665- _ => None ,
666- }
662+ object. typed_array_slots ( ) . map ( |slots| slots. fixed_length )
667663}
668664
669665pub ( crate ) fn typed_array_is_length_tracking ( object : & ObjectRef ) -> bool {
670- matches ! (
671- object. own_property( TYPED_ARRAY_LENGTH_TRACKING_PROPERTY ) ,
672- Some ( Property {
673- value: Value :: Boolean ( true ) ,
674- ..
675- } )
676- )
666+ object
667+ . typed_array_slots ( )
668+ . is_some_and ( |slots| slots. length_tracking )
677669}
678670
679671pub ( crate ) fn typed_array_is_out_of_bounds ( object : & ObjectRef ) -> bool {
@@ -687,13 +679,7 @@ pub(crate) fn typed_array_is_out_of_bounds(object: &ObjectRef) -> bool {
687679 if typed_array_is_length_tracking ( object) {
688680 typed_array_byte_offset ( object) > buffer_byte_length
689681 } else {
690- let fixed_length = match object. own_property ( TYPED_ARRAY_LENGTH_PROPERTY ) {
691- Some ( Property {
692- value : Value :: Number ( length) ,
693- ..
694- } ) => length as usize ,
695- _ => 0 ,
696- } ;
682+ let fixed_length = typed_array_fixed_length ( object) . unwrap_or ( 0 ) ;
697683 let element = bytes_per_element ( typed_array_kind ( object) ) ;
698684 let byte_length = fixed_length. checked_mul ( element) ;
699685 byte_length. is_none_or ( |byte_length| {
@@ -705,23 +691,13 @@ pub(crate) fn typed_array_is_out_of_bounds(object: &ObjectRef) -> bool {
705691}
706692
707693pub ( crate ) fn typed_array_byte_offset ( object : & ObjectRef ) -> usize {
708- match object. own_property ( TYPED_ARRAY_BYTE_OFFSET_PROPERTY ) {
709- Some ( Property {
710- value : Value :: Number ( offset) ,
711- ..
712- } ) => offset as usize ,
713- _ => 0 ,
714- }
694+ object
695+ . typed_array_slots ( )
696+ . map_or ( 0 , |slots| slots. byte_offset )
715697}
716698
717699pub ( crate ) fn typed_array_buffer ( object : & ObjectRef ) -> Option < ObjectRef > {
718- match object. own_property ( TYPED_ARRAY_BUFFER_PROPERTY ) {
719- Some ( Property {
720- value : Value :: Object ( buffer) ,
721- ..
722- } ) => Some ( buffer) ,
723- _ => None ,
724- }
700+ object. typed_array_slots ( ) . map ( |slots| slots. buffer . clone ( ) )
725701}
726702
727703pub ( crate ) fn typed_array_buffer_detached ( object : & ObjectRef ) -> bool {
@@ -878,14 +854,6 @@ pub(crate) fn typed_array_name(native: NativeFunction) -> &'static str {
878854 }
879855}
880856
881- fn native_for_name ( name : & str ) -> NativeFunction {
882- TYPED_ARRAY_KINDS
883- . iter ( )
884- . find ( |( kind, _) | * kind == name)
885- . map ( |( _, native) | * native)
886- . unwrap_or ( NativeFunction :: Uint8Array )
887- }
888-
889857#[ cfg( test) ]
890858mod base64_tests;
891859#[ cfg( test) ]
0 commit comments