@@ -232,6 +232,11 @@ impl ArrayBuffer {
232
232
self . data . as_mut ( )
233
233
}
234
234
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
+
235
240
/// Gets the inner bytes of the buffer without accessing the current atomic length.
236
241
#[ track_caller]
237
242
pub ( crate ) fn bytes_with_len ( & self , len : usize ) -> Option < & [ u8 ] > {
@@ -252,6 +257,32 @@ impl ArrayBuffer {
252
257
}
253
258
}
254
259
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
+
255
286
/// Detaches the inner data of this `ArrayBuffer`, returning the original buffer if still
256
287
/// present.
257
288
///
@@ -338,7 +369,7 @@ impl IntrinsicObject for ArrayBuffer {
338
369
None ,
339
370
flag_attributes,
340
371
)
341
- . method ( Self :: resize , js_string ! ( "resize" ) , 1 )
372
+ . method ( Self :: js_resize , js_string ! ( "resize" ) , 1 )
342
373
. method ( Self :: slice, js_string ! ( "slice" ) , 2 )
343
374
. property (
344
375
JsSymbol :: to_string_tag ( ) ,
@@ -554,7 +585,7 @@ impl ArrayBuffer {
554
585
/// [`ArrayBuffer.prototype.resize ( newLength )`][spec].
555
586
///
556
587
/// [spec]: https://tc39.es/ecma262/#sec-arraybuffer.prototype.resize
557
- pub ( crate ) fn resize (
588
+ pub ( crate ) fn js_resize (
558
589
this : & JsValue ,
559
590
args : & [ JsValue ] ,
560
591
context : & mut Context ,
@@ -570,31 +601,12 @@ impl ArrayBuffer {
570
601
. with_message ( "ArrayBuffer.prototype.resize called with invalid `this`" )
571
602
} ) ?;
572
603
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
-
579
604
// 4. Let newByteLength be ? ToIndex(newLength).
580
605
let new_byte_length = args. get_or_undefined ( 0 ) . to_index ( context) ?;
581
606
582
- let mut buf = buf . borrow_mut ( ) ;
607
+ // These steps are performed in the `Self::resize` method.
583
608
// 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
-
590
609
// 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
- }
598
610
599
611
// TODO: 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength).
600
612
// 8. If hostHandled is handled, return undefined.
@@ -609,7 +621,7 @@ impl ArrayBuffer {
609
621
// Implementations may implement this method as in-place growth or shrinkage.
610
622
// 14. Set O.[[ArrayBufferData]] to newBlock.
611
623
// 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) ? ;
613
625
614
626
// 16. Return undefined.
615
627
Ok ( JsValue :: undefined ( ) )
0 commit comments