@@ -232,6 +232,11 @@ impl ArrayBuffer {
232232 self . data . as_mut ( )
233233 }
234234
235+ /// Sets the maximum byte length of the buffer, returning the previous value if present.
236+ pub ( crate ) fn set_max_byte_length ( & mut self , max_byte_len : u64 ) -> Option < u64 > {
237+ self . max_byte_len . replace ( max_byte_len)
238+ }
239+
235240 /// Gets the inner bytes of the buffer without accessing the current atomic length.
236241 #[ track_caller]
237242 pub ( crate ) fn bytes_with_len ( & self , len : usize ) -> Option < & [ u8 ] > {
@@ -252,6 +257,32 @@ impl ArrayBuffer {
252257 }
253258 }
254259
260+ /// Resizes the buffer to the new size, clamped to the maximum byte length if present.
261+ pub fn resize ( & mut self , new_byte_length : u64 ) -> JsResult < ( ) > {
262+ let Some ( max_byte_len) = self . max_byte_len else {
263+ return Err ( JsNativeError :: typ ( )
264+ . with_message ( "ArrayBuffer.resize: cannot resize a fixed-length buffer" )
265+ . into ( ) ) ;
266+ } ;
267+
268+ let Some ( buf) = self . vec_mut ( ) else {
269+ return Err ( JsNativeError :: typ ( )
270+ . with_message ( "ArrayBuffer.resize: cannot resize a detached buffer" )
271+ . into ( ) ) ;
272+ } ;
273+
274+ if new_byte_length > max_byte_len {
275+ return Err ( JsNativeError :: range ( )
276+ . with_message (
277+ "ArrayBuffer.resize: new byte length exceeds buffer's maximum byte length" ,
278+ )
279+ . into ( ) ) ;
280+ }
281+
282+ buf. resize ( new_byte_length as usize , 0 ) ;
283+ Ok ( ( ) )
284+ }
285+
255286 /// Detaches the inner data of this `ArrayBuffer`, returning the original buffer if still
256287 /// present.
257288 ///
@@ -338,7 +369,7 @@ impl IntrinsicObject for ArrayBuffer {
338369 None ,
339370 flag_attributes,
340371 )
341- . method ( Self :: resize , js_string ! ( "resize" ) , 1 )
372+ . method ( Self :: js_resize , js_string ! ( "resize" ) , 1 )
342373 . method ( Self :: slice, js_string ! ( "slice" ) , 2 )
343374 . property (
344375 JsSymbol :: to_string_tag ( ) ,
@@ -554,7 +585,7 @@ impl ArrayBuffer {
554585 /// [`ArrayBuffer.prototype.resize ( newLength )`][spec].
555586 ///
556587 /// [spec]: https://tc39.es/ecma262/#sec-arraybuffer.prototype.resize
557- pub ( crate ) fn resize (
588+ pub ( crate ) fn js_resize (
558589 this : & JsValue ,
559590 args : & [ JsValue ] ,
560591 context : & mut Context ,
@@ -570,31 +601,12 @@ impl ArrayBuffer {
570601 . with_message ( "ArrayBuffer.prototype.resize called with invalid `this`" )
571602 } ) ?;
572603
573- let Some ( max_byte_len) = buf. borrow ( ) . data . max_byte_len else {
574- return Err ( JsNativeError :: typ ( )
575- . with_message ( "ArrayBuffer.resize: cannot resize a fixed-length buffer" )
576- . into ( ) ) ;
577- } ;
578-
579604 // 4. Let newByteLength be ? ToIndex(newLength).
580605 let new_byte_length = args. get_or_undefined ( 0 ) . to_index ( context) ?;
581606
582- let mut buf = buf . borrow_mut ( ) ;
607+ // These steps are performed in the `Self::resize` method.
583608 // 5. If IsDetachedBuffer(O) is true, throw a TypeError exception.
584- let Some ( buf) = buf. data . vec_mut ( ) else {
585- return Err ( JsNativeError :: typ ( )
586- . with_message ( "ArrayBuffer.resize: cannot resize a detached buffer" )
587- . into ( ) ) ;
588- } ;
589-
590609 // 6. If newByteLength > O.[[ArrayBufferMaxByteLength]], throw a RangeError exception.
591- if new_byte_length > max_byte_len {
592- return Err ( JsNativeError :: range ( )
593- . with_message (
594- "ArrayBuffer.resize: new byte length exceeds buffer's maximum byte length" ,
595- )
596- . into ( ) ) ;
597- }
598610
599611 // TODO: 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength).
600612 // 8. If hostHandled is handled, return undefined.
@@ -609,7 +621,7 @@ impl ArrayBuffer {
609621 // Implementations may implement this method as in-place growth or shrinkage.
610622 // 14. Set O.[[ArrayBufferData]] to newBlock.
611623 // 15. Set O.[[ArrayBufferByteLength]] to newByteLength.
612- buf. resize ( new_byte_length as usize , 0 ) ;
624+ buf. borrow_mut ( ) . data_mut ( ) . resize ( new_byte_length) ? ;
613625
614626 // 16. Return undefined.
615627 Ok ( JsValue :: undefined ( ) )
0 commit comments