1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
//! Boa's implementation of ECMAScript's global `ArrayBuffer` and `SharedArrayBuffer` objects
//!
//! More information:
//!  - [ECMAScript reference][spec]
//!  - [MDN documentation][mdn]
//!
//! [spec]: https://tc39.es/ecma262/#sec-arraybuffer-objects
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer

#![deny(unsafe_op_in_unsafe_fn)]
#![deny(clippy::undocumented_unsafe_blocks)]

pub(crate) mod shared;
pub(crate) mod utils;

#[cfg(test)]
mod tests;

use std::ops::{Deref, DerefMut};

pub use shared::SharedArrayBuffer;
use std::sync::atomic::Ordering;

use crate::{
    builtins::BuiltInObject,
    context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
    error::JsNativeError,
    js_string,
    object::{internal_methods::get_prototype_from_constructor, JsObject, Object},
    property::Attribute,
    realm::Realm,
    string::common::StaticJsStrings,
    symbol::JsSymbol,
    Context, JsArgs, JsData, JsResult, JsString, JsValue,
};
use boa_gc::{Finalize, GcRef, GcRefMut, Trace};
use boa_profiler::Profiler;

use self::utils::{SliceRef, SliceRefMut};

use super::{
    typed_array::TypedArray, Array, BuiltInBuilder, BuiltInConstructor, DataView, IntrinsicObject,
};

#[derive(Debug, Clone, Copy)]
pub(crate) enum BufferRef<B, S> {
    Buffer(B),
    SharedBuffer(S),
}

impl<B, S> BufferRef<B, S>
where
    B: Deref<Target = ArrayBuffer>,
    S: Deref<Target = SharedArrayBuffer>,
{
    /// Gets the inner data of the buffer.
    pub(crate) fn bytes(&self, ordering: Ordering) -> Option<SliceRef<'_>> {
        match self {
            Self::Buffer(buf) => buf.deref().bytes().map(SliceRef::Slice),
            Self::SharedBuffer(buf) => Some(SliceRef::AtomicSlice(buf.deref().bytes(ordering))),
        }
    }

    /// Gets the inner data of the buffer without accessing the current atomic length.
    ///
    /// Returns `None` if the buffer is detached or if the provided `len` is bigger than
    /// the allocated buffer.
    #[track_caller]
    pub(crate) fn bytes_with_len(&self, len: usize) -> Option<SliceRef<'_>> {
        match self {
            Self::Buffer(buf) => buf.deref().bytes_with_len(len).map(SliceRef::Slice),
            Self::SharedBuffer(buf) => Some(SliceRef::AtomicSlice(buf.deref().bytes_with_len(len))),
        }
    }

    pub(crate) fn is_fixed_len(&self) -> bool {
        match self {
            Self::Buffer(buf) => buf.is_fixed_len(),
            Self::SharedBuffer(buf) => buf.is_fixed_len(),
        }
    }
}

#[derive(Debug)]
pub(crate) enum BufferRefMut<B, S> {
    Buffer(B),
    SharedBuffer(S),
}

impl<B, S> BufferRefMut<B, S>
where
    B: DerefMut<Target = ArrayBuffer>,
    S: DerefMut<Target = SharedArrayBuffer>,
{
    pub(crate) fn bytes(&mut self, ordering: Ordering) -> Option<SliceRefMut<'_>> {
        match self {
            Self::Buffer(buf) => buf.deref_mut().bytes_mut().map(SliceRefMut::Slice),
            Self::SharedBuffer(buf) => {
                Some(SliceRefMut::AtomicSlice(buf.deref_mut().bytes(ordering)))
            }
        }
    }

    /// Gets the mutable inner data of the buffer without accessing the current atomic length.
    ///
    /// Returns `None` if the buffer is detached or if the provided `len` is bigger than
    /// the allocated buffer.
    pub(crate) fn bytes_with_len(&mut self, len: usize) -> Option<SliceRefMut<'_>> {
        match self {
            Self::Buffer(buf) => buf
                .deref_mut()
                .bytes_with_len_mut(len)
                .map(SliceRefMut::Slice),
            Self::SharedBuffer(buf) => Some(SliceRefMut::AtomicSlice(
                buf.deref_mut().bytes_with_len(len),
            )),
        }
    }
}

/// A `JsObject` containing a bytes buffer as its inner data.
#[derive(Debug, Clone, Trace, Finalize)]
#[boa_gc(unsafe_no_drop)]
pub(crate) enum BufferObject {
    Buffer(JsObject<ArrayBuffer>),
    SharedBuffer(JsObject<SharedArrayBuffer>),
}

impl From<BufferObject> for JsObject {
    fn from(value: BufferObject) -> Self {
        match value {
            BufferObject::Buffer(buf) => buf.upcast(),
            BufferObject::SharedBuffer(buf) => buf.upcast(),
        }
    }
}

impl From<BufferObject> for JsValue {
    fn from(value: BufferObject) -> Self {
        JsValue::from(JsObject::from(value))
    }
}

impl BufferObject {
    /// Gets the buffer data of the object.
    #[inline]
    #[must_use]
    pub(crate) fn as_buffer(
        &self,
    ) -> BufferRef<GcRef<'_, ArrayBuffer>, GcRef<'_, SharedArrayBuffer>> {
        match self {
            Self::Buffer(buf) => BufferRef::Buffer(GcRef::map(buf.borrow(), |o| &o.data)),
            Self::SharedBuffer(buf) => {
                BufferRef::SharedBuffer(GcRef::map(buf.borrow(), |o| &o.data))
            }
        }
    }

    /// Gets the mutable buffer data of the object
    #[inline]
    pub(crate) fn as_buffer_mut(
        &self,
    ) -> BufferRefMut<
        GcRefMut<'_, Object<ArrayBuffer>, ArrayBuffer>,
        GcRefMut<'_, Object<SharedArrayBuffer>, SharedArrayBuffer>,
    > {
        match self {
            Self::Buffer(buf) => {
                BufferRefMut::Buffer(GcRefMut::map(buf.borrow_mut(), |o| &mut o.data))
            }
            Self::SharedBuffer(buf) => {
                BufferRefMut::SharedBuffer(GcRefMut::map(buf.borrow_mut(), |o| &mut o.data))
            }
        }
    }

    /// Returns `true` if the buffer objects point to the same buffer.
    #[inline]
    pub(crate) fn equals(lhs: &Self, rhs: &Self) -> bool {
        match (lhs, rhs) {
            (BufferObject::Buffer(lhs), BufferObject::Buffer(rhs)) => JsObject::equals(lhs, rhs),
            (BufferObject::SharedBuffer(lhs), BufferObject::SharedBuffer(rhs)) => {
                if JsObject::equals(lhs, rhs) {
                    return true;
                }

                let lhs = lhs.borrow();
                let rhs = rhs.borrow();

                std::ptr::eq(lhs.data.as_ptr(), rhs.data.as_ptr())
            }
            _ => false,
        }
    }
}

/// The internal representation of an `ArrayBuffer` object.
#[derive(Debug, Clone, Trace, Finalize, JsData)]
pub struct ArrayBuffer {
    /// The `[[ArrayBufferData]]` internal slot.
    data: Option<Vec<u8>>,

    /// The `[[ArrayBufferMaxByteLength]]` internal slot.
    max_byte_len: Option<u64>,

    /// The `[[ArrayBufferDetachKey]]` internal slot.
    detach_key: JsValue,
}

impl ArrayBuffer {
    pub(crate) fn from_data(data: Vec<u8>, detach_key: JsValue) -> Self {
        Self {
            data: Some(data),
            max_byte_len: None,
            detach_key,
        }
    }

    pub(crate) fn len(&self) -> usize {
        self.data.as_ref().map_or(0, Vec::len)
    }

    pub(crate) fn bytes(&self) -> Option<&[u8]> {
        self.data.as_deref()
    }

    pub(crate) fn bytes_mut(&mut self) -> Option<&mut [u8]> {
        self.data.as_deref_mut()
    }

    pub(crate) fn vec_mut(&mut self) -> Option<&mut Vec<u8>> {
        self.data.as_mut()
    }

    /// Gets the inner bytes of the buffer without accessing the current atomic length.
    #[track_caller]
    pub(crate) fn bytes_with_len(&self, len: usize) -> Option<&[u8]> {
        if let Some(s) = self.data.as_deref() {
            Some(&s[..len])
        } else {
            None
        }
    }

    /// Gets the mutable inner bytes of the buffer without accessing the current atomic length.
    #[track_caller]
    pub(crate) fn bytes_with_len_mut(&mut self, len: usize) -> Option<&mut [u8]> {
        if let Some(s) = self.data.as_deref_mut() {
            Some(&mut s[..len])
        } else {
            None
        }
    }

    /// Detaches the inner data of this `ArrayBuffer`, returning the original buffer if still
    /// present.
    ///
    /// # Errors
    ///
    /// Throws an error if the provided detach key is invalid.
    pub fn detach(&mut self, key: &JsValue) -> JsResult<Option<Vec<u8>>> {
        if !JsValue::same_value(&self.detach_key, key) {
            return Err(JsNativeError::typ()
                .with_message("Cannot detach array buffer with different key")
                .into());
        }

        Ok(self.data.take())
    }

    /// `IsDetachedBuffer ( arrayBuffer )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-isdetachedbuffer
    pub(crate) const fn is_detached(&self) -> bool {
        // 1. If arrayBuffer.[[ArrayBufferData]] is null, return true.
        // 2. Return false.
        self.data.is_none()
    }

    pub(crate) fn is_fixed_len(&self) -> bool {
        self.max_byte_len.is_none()
    }
}

impl IntrinsicObject for ArrayBuffer {
    fn init(realm: &Realm) {
        let _timer = Profiler::global().start_event(std::any::type_name::<Self>(), "init");

        let flag_attributes = Attribute::CONFIGURABLE | Attribute::NON_ENUMERABLE;

        let get_species = BuiltInBuilder::callable(realm, Self::get_species)
            .name(js_string!("get [Symbol.species]"))
            .build();

        let get_byte_length = BuiltInBuilder::callable(realm, Self::get_byte_length)
            .name(js_string!("get byteLength"))
            .build();

        let get_resizable = BuiltInBuilder::callable(realm, Self::get_resizable)
            .name(js_string!("get resizable"))
            .build();

        let get_max_byte_length = BuiltInBuilder::callable(realm, Self::get_max_byte_length)
            .name(js_string!("get maxByteLength"))
            .build();

        #[cfg(feature = "experimental")]
        let get_detached = BuiltInBuilder::callable(realm, Self::get_detached)
            .name(js_string!("get detached"))
            .build();

        let builder = BuiltInBuilder::from_standard_constructor::<Self>(realm)
            .static_accessor(
                JsSymbol::species(),
                Some(get_species),
                None,
                Attribute::CONFIGURABLE,
            )
            .static_method(Self::is_view, js_string!("isView"), 1)
            .accessor(
                js_string!("byteLength"),
                Some(get_byte_length),
                None,
                flag_attributes,
            )
            .accessor(
                js_string!("resizable"),
                Some(get_resizable),
                None,
                flag_attributes,
            )
            .accessor(
                js_string!("maxByteLength"),
                Some(get_max_byte_length),
                None,
                flag_attributes,
            )
            .method(Self::resize, js_string!("resize"), 1)
            .method(Self::slice, js_string!("slice"), 2)
            .property(
                JsSymbol::to_string_tag(),
                Self::NAME,
                Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
            );

        #[cfg(feature = "experimental")]
        let builder = builder
            .accessor(
                js_string!("detached"),
                Some(get_detached),
                None,
                flag_attributes,
            )
            .method(Self::transfer::<false>, js_string!("transfer"), 0)
            .method(
                Self::transfer::<true>,
                js_string!("transferToFixedLength"),
                0,
            );

        builder.build();
    }

    fn get(intrinsics: &Intrinsics) -> JsObject {
        Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
    }
}

impl BuiltInObject for ArrayBuffer {
    const NAME: JsString = StaticJsStrings::ARRAY_BUFFER;
}

impl BuiltInConstructor for ArrayBuffer {
    const LENGTH: usize = 1;

    const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
        StandardConstructors::array_buffer;

    /// `ArrayBuffer ( length )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-arraybuffer-length
    fn constructor(
        new_target: &JsValue,
        args: &[JsValue],
        context: &mut Context,
    ) -> JsResult<JsValue> {
        // 1. If NewTarget is undefined, throw a TypeError exception.
        if new_target.is_undefined() {
            return Err(JsNativeError::typ()
                .with_message("ArrayBuffer.constructor called with undefined new target")
                .into());
        }

        // 2. Let byteLength be ? ToIndex(length).
        let byte_len = args.get_or_undefined(0).to_index(context)?;

        // 3. Let requestedMaxByteLength be ? GetArrayBufferMaxByteLengthOption(options).
        let max_byte_len = get_max_byte_len(args.get_or_undefined(1), context)?;

        // 4. Return ? AllocateArrayBuffer(NewTarget, byteLength, requestedMaxByteLength).
        Ok(Self::allocate(new_target, byte_len, max_byte_len, context)?
            .upcast()
            .into())
    }
}

impl ArrayBuffer {
    /// `ArrayBuffer.isView ( arg )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-arraybuffer.isview
    #[allow(clippy::unnecessary_wraps)]
    fn is_view(_: &JsValue, args: &[JsValue], _context: &mut Context) -> JsResult<JsValue> {
        // 1. If Type(arg) is not Object, return false.
        // 2. If arg has a [[ViewedArrayBuffer]] internal slot, return true.
        // 3. Return false.
        Ok(args
            .get_or_undefined(0)
            .as_object()
            .map(|obj| obj.is::<TypedArray>() || obj.is::<DataView>())
            .unwrap_or_default()
            .into())
    }

    /// `get ArrayBuffer [ @@species ]`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-get-arraybuffer-@@species
    #[allow(clippy::unnecessary_wraps)]
    fn get_species(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
        // 1. Return the this value.
        Ok(this.clone())
    }

    /// `get ArrayBuffer.prototype.byteLength`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.bytelength
    pub(crate) fn get_byte_length(
        this: &JsValue,
        _args: &[JsValue],
        _: &mut Context,
    ) -> JsResult<JsValue> {
        // 1. Let O be the this value.
        // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
        // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
        let buf = this
            .as_object()
            .and_then(JsObject::downcast_ref::<Self>)
            .ok_or_else(|| {
                JsNativeError::typ()
                    .with_message("get ArrayBuffer.prototype.byteLength called with invalid `this`")
            })?;

        // 4. If IsDetachedBuffer(O) is true, return +0𝔽.
        // 5. Let length be O.[[ArrayBufferByteLength]].
        // 6. Return 𝔽(length).
        Ok(buf.len().into())
    }

    /// [`get ArrayBuffer.prototype.maxByteLength`][spec].
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.maxbytelength
    pub(crate) fn get_max_byte_length(
        this: &JsValue,
        _args: &[JsValue],
        _context: &mut Context,
    ) -> JsResult<JsValue> {
        // 1. Let O be the this value.
        // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
        // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
        let buf = this
            .as_object()
            .and_then(JsObject::downcast_ref::<Self>)
            .ok_or_else(|| {
                JsNativeError::typ().with_message(
                    "get ArrayBuffer.prototype.maxByteLength called with invalid `this`",
                )
            })?;

        // 4. If IsDetachedBuffer(O) is true, return +0𝔽.
        let Some(data) = buf.bytes() else {
            return Ok(JsValue::from(0));
        };

        // 5. If IsFixedLengthArrayBuffer(O) is true, then
        //     a. Let length be O.[[ArrayBufferByteLength]].
        // 6. Else,
        //     a. Let length be O.[[ArrayBufferMaxByteLength]].
        // 7. Return 𝔽(length).
        Ok(buf.max_byte_len.unwrap_or(data.len() as u64).into())
    }

    /// [`get ArrayBuffer.prototype.resizable`][spec].
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-get-arraybuffer.prototype.resizable
    pub(crate) fn get_resizable(
        this: &JsValue,
        _args: &[JsValue],
        _context: &mut Context,
    ) -> JsResult<JsValue> {
        // 1. Let O be the this value.
        // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
        // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
        let buf = this
            .as_object()
            .and_then(JsObject::downcast_ref::<Self>)
            .ok_or_else(|| {
                JsNativeError::typ()
                    .with_message("get ArrayBuffer.prototype.resizable called with invalid `this`")
            })?;

        // 4. If IsFixedLengthArrayBuffer(O) is false, return true; otherwise return false.
        Ok(JsValue::from(!buf.is_fixed_len()))
    }

    /// [`get ArrayBuffer.prototype.detached`][spec].
    ///
    /// [spec]: https://tc39.es/proposal-arraybuffer-transfer/#sec-get-arraybuffer.prototype.detached
    #[cfg(feature = "experimental")]
    fn get_detached(
        this: &JsValue,
        _args: &[JsValue],
        _context: &mut Context,
    ) -> JsResult<JsValue> {
        // 1. Let O be the this value.
        // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
        // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
        let buf = this
            .as_object()
            .and_then(JsObject::downcast_ref::<Self>)
            .ok_or_else(|| {
                JsNativeError::typ()
                    .with_message("get ArrayBuffer.prototype.detached called with invalid `this`")
            })?;

        // 4. Return IsDetachedBuffer(O).
        Ok(buf.is_detached().into())
    }

    /// [`ArrayBuffer.prototype.resize ( newLength )`][spec].
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-arraybuffer.prototype.resize
    pub(crate) fn resize(
        this: &JsValue,
        args: &[JsValue],
        context: &mut Context,
    ) -> JsResult<JsValue> {
        // 1. Let O be the this value.
        // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]).
        // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
        let buf = this
            .as_object()
            .and_then(|o| o.clone().downcast::<Self>().ok())
            .ok_or_else(|| {
                JsNativeError::typ()
                    .with_message("ArrayBuffer.prototype.resize called with invalid `this`")
            })?;

        let Some(max_byte_len) = buf.borrow().data.max_byte_len else {
            return Err(JsNativeError::typ()
                .with_message("ArrayBuffer.resize: cannot resize a fixed-length buffer")
                .into());
        };

        // 4. Let newByteLength be ? ToIndex(newLength).
        let new_byte_length = args.get_or_undefined(0).to_index(context)?;

        let mut buf = buf.borrow_mut();
        // 5. If IsDetachedBuffer(O) is true, throw a TypeError exception.
        let Some(buf) = buf.data.vec_mut() else {
            return Err(JsNativeError::typ()
                .with_message("ArrayBuffer.resize: cannot resize a detached buffer")
                .into());
        };

        // 6. If newByteLength > O.[[ArrayBufferMaxByteLength]], throw a RangeError exception.
        if new_byte_length > max_byte_len {
            return Err(JsNativeError::range()
                .with_message(
                    "ArrayBuffer.resize: new byte length exceeds buffer's maximum byte length",
                )
                .into());
        }

        // TODO: 7. Let hostHandled be ? HostResizeArrayBuffer(O, newByteLength).
        // 8. If hostHandled is handled, return undefined.
        // Used in engines to handle WASM buffers in a special way, but we don't
        // have a WASM interpreter in place yet.

        // 9. Let oldBlock be O.[[ArrayBufferData]].
        // 10. Let newBlock be ? CreateByteDataBlock(newByteLength).
        // 11. Let copyLength be min(newByteLength, O.[[ArrayBufferByteLength]]).
        // 12. Perform CopyDataBlockBytes(newBlock, 0, oldBlock, 0, copyLength).
        // 13. NOTE: Neither creation of the new Data Block nor copying from the old Data Block are observable.
        //     Implementations may implement this method as in-place growth or shrinkage.
        // 14. Set O.[[ArrayBufferData]] to newBlock.
        // 15. Set O.[[ArrayBufferByteLength]] to newByteLength.
        buf.resize(new_byte_length as usize, 0);

        // 16. Return undefined.
        Ok(JsValue::undefined())
    }

    /// `ArrayBuffer.prototype.slice ( start, end )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-arraybuffer.prototype.slice
    fn slice(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        // 1. Let O be the this value.
        // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
        // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
        let buf = this
            .as_object()
            .and_then(|o| o.clone().downcast::<Self>().ok())
            .ok_or_else(|| {
                JsNativeError::typ()
                    .with_message("ArrayBuffer.slice called with invalid `this` value")
            })?;

        let len = {
            let buf = buf.borrow();
            // 4. If IsDetachedBuffer(O) is true, throw a TypeError exception.
            if buf.data.is_detached() {
                return Err(JsNativeError::typ()
                    .with_message("ArrayBuffer.slice called with detached buffer")
                    .into());
            }
            // 5. Let len be O.[[ArrayBufferByteLength]].
            buf.data.len() as u64
        };

        // 6. Let relativeStart be ? ToIntegerOrInfinity(start).
        // 7. If relativeStart = -∞, let first be 0.
        // 8. Else if relativeStart < 0, let first be max(len + relativeStart, 0).
        // 9. Else, let first be min(relativeStart, len).
        let first = Array::get_relative_start(context, args.get_or_undefined(0), len)?;

        // 10. If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToIntegerOrInfinity(end).
        // 11. If relativeEnd = -∞, let final be 0.
        // 12. Else if relativeEnd < 0, let final be max(len + relativeEnd, 0).
        // 13. Else, let final be min(relativeEnd, len).
        let final_ = Array::get_relative_end(context, args.get_or_undefined(1), len)?;

        // 14. Let newLen be max(final - first, 0).
        let new_len = final_.saturating_sub(first);

        // 15. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
        let ctor = buf
            .clone()
            .upcast()
            .species_constructor(StandardConstructors::array_buffer, context)?;

        // 16. Let new be ? Construct(ctor, « 𝔽(newLen) »).
        let new = ctor.construct(&[new_len.into()], Some(&ctor), context)?;

        // 17. Perform ? RequireInternalSlot(new, [[ArrayBufferData]]).
        // 18. If IsSharedArrayBuffer(new) is true, throw a TypeError exception.
        let Ok(new) = new.downcast::<Self>() else {
            return Err(JsNativeError::typ()
                .with_message("ArrayBuffer constructor returned invalid object")
                .into());
        };

        // 20. If SameValue(new, O) is true, throw a TypeError exception.
        if JsObject::equals(&buf, &new) {
            return Err(JsNativeError::typ()
                .with_message("new ArrayBuffer is the same as this ArrayBuffer")
                .into());
        }

        {
            // 19. If IsDetachedBuffer(new) is true, throw a TypeError exception.
            // 25. Let toBuf be new.[[ArrayBufferData]].
            let mut new = new.borrow_mut();
            let Some(to_buf) = new.data.bytes_mut() else {
                return Err(JsNativeError::typ()
                    .with_message("ArrayBuffer constructor returned detached ArrayBuffer")
                    .into());
            };

            // 21. If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception.
            if (to_buf.len() as u64) < new_len {
                return Err(JsNativeError::typ()
                    .with_message("new ArrayBuffer length too small")
                    .into());
            }

            // 22. NOTE: Side-effects of the above steps may have detached O.
            // 23. If IsDetachedBuffer(O) is true, throw a TypeError exception.
            // 24. Let fromBuf be O.[[ArrayBufferData]].
            let buf = buf.borrow();
            let Some(from_buf) = buf.data.bytes() else {
                return Err(JsNativeError::typ()
                    .with_message("ArrayBuffer detached while ArrayBuffer.slice was running")
                    .into());
            };

            // 26. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
            let first = first as usize;
            let new_len = new_len as usize;
            to_buf[..new_len].copy_from_slice(&from_buf[first..first + new_len]);
        }

        // 27. Return new.
        Ok(new.upcast().into())
    }

    /// [`ArrayBuffer.prototype.transfer ( [ newLength ] )`][transfer] and
    /// [`ArrayBuffer.prototype.transferToFixedLength ( [ newLength ] )`][transferFL]
    ///
    /// [transfer]: https://tc39.es/proposal-arraybuffer-transfer/#sec-arraybuffer.prototype.transfer
    /// [transferFL]: https://tc39.es/proposal-arraybuffer-transfer/#sec-arraybuffer.prototype.transfertofixedlength
    #[cfg(feature = "experimental")]
    fn transfer<const TO_FIXED_LENGTH: bool>(
        this: &JsValue,
        args: &[JsValue],
        context: &mut Context,
    ) -> JsResult<JsValue> {
        // 1. Let O be the this value.
        // 2. Return ? ArrayBufferCopyAndDetach(O, newLength, preserve-resizability).

        // Abstract operation `ArrayBufferCopyAndDetach ( arrayBuffer, newLength, preserveResizability )`
        // https://tc39.es/proposal-arraybuffer-transfer/#sec-arraybuffercopyanddetach

        let new_length = args.get_or_undefined(0);

        // 1. Perform ? RequireInternalSlot(arrayBuffer, [[ArrayBufferData]]).
        // 2. If IsSharedArrayBuffer(arrayBuffer) is true, throw a TypeError exception.
        let buf = this
            .as_object()
            .and_then(|o| o.clone().downcast::<Self>().ok())
            .ok_or_else(|| {
                JsNativeError::typ().with_message(if TO_FIXED_LENGTH {
                    "ArrayBuffer.prototype.transferToFixedLength called with invalid `this`"
                } else {
                    "ArrayBuffer.prototype.transfer called with invalid `this`"
                })
            })?;

        // 3. If newLength is undefined, then
        let new_len = if new_length.is_undefined() {
            // a. Let newByteLength be arrayBuffer.[[ArrayBufferByteLength]].
            buf.borrow().data.len() as u64
        } else {
            // 4. Else,
            //     a. Let newByteLength be ? ToIndex(newLength).
            new_length.to_index(context)?
        };

        // 5. If IsDetachedBuffer(arrayBuffer) is true, throw a TypeError exception.
        let Some(mut bytes) = buf.borrow_mut().data.data.take() else {
            return Err(JsNativeError::typ()
                .with_message("cannot transfer a detached buffer")
                .into());
        };

        // 6. If preserveResizability is preserve-resizability and IsResizableArrayBuffer(arrayBuffer)
        //    is true, then
        //     a. Let newMaxByteLength be arrayBuffer.[[ArrayBufferMaxByteLength]].
        // 7. Else,
        //     a. Let newMaxByteLength be empty.
        let new_max_len = buf.borrow().data.max_byte_len.filter(|_| !TO_FIXED_LENGTH);

        // 8. If arrayBuffer.[[ArrayBufferDetachKey]] is not undefined, throw a TypeError exception.
        if !buf.borrow().data.detach_key.is_undefined() {
            buf.borrow_mut().data.data = Some(bytes);
            return Err(JsNativeError::typ()
                .with_message("cannot transfer a buffer with a detach key")
                .into());
        }

        // Effectively, the next steps only create a new object for the same vec, so we can skip all
        // those steps and just make a single check + trigger the realloc.

        // 9. Let newBuffer be ? AllocateArrayBuffer(%ArrayBuffer%, newByteLength, newMaxByteLength).
        // 10. Let copyLength be min(newByteLength, arrayBuffer.[[ArrayBufferByteLength]]).
        // 11. Let fromBlock be arrayBuffer.[[ArrayBufferData]].
        // 12. Let toBlock be newBuffer.[[ArrayBufferData]].
        // 13. Perform CopyDataBlockBytes(toBlock, 0, fromBlock, 0, copyLength).
        // 14. NOTE: Neither creation of the new Data Block nor copying from the old Data Block are
        //     observable. Implementations may implement this method as a zero-copy move or a realloc.
        // 15. Perform ! DetachArrayBuffer(arrayBuffer).
        // 16. Return newBuffer.
        if let Some(new_max_len) = new_max_len {
            if new_len > new_max_len {
                buf.borrow_mut().data.data = Some(bytes);
                return Err(JsNativeError::range()
                    .with_message("`length` cannot be bigger than `maxByteLength`")
                    .into());
            }
            // Should only truncate without reallocating.
            bytes.resize(new_len as usize, 0);
        } else {
            bytes.resize(new_len as usize, 0);

            // Realloc the vec to fit onto the new exact length.
            bytes.shrink_to_fit();
        }

        let prototype = context
            .intrinsics()
            .constructors()
            .array_buffer()
            .prototype();

        Ok(JsObject::from_proto_and_data_with_shared_shape(
            context.root_shape(),
            prototype,
            ArrayBuffer {
                data: Some(bytes),
                max_byte_len: new_max_len,
                detach_key: JsValue::undefined(),
            },
        )
        .into())
    }

    /// `AllocateArrayBuffer ( constructor, byteLength )`
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-allocatearraybuffer
    pub(crate) fn allocate(
        constructor: &JsValue,
        byte_len: u64,
        max_byte_len: Option<u64>,
        context: &mut Context,
    ) -> JsResult<JsObject<ArrayBuffer>> {
        // 1. Let slots be « [[ArrayBufferData]], [[ArrayBufferByteLength]], [[ArrayBufferDetachKey]] ».
        // 2. If maxByteLength is present and maxByteLength is not empty, let allocatingResizableBuffer be true; otherwise let allocatingResizableBuffer be false.
        // 3. If allocatingResizableBuffer is true, then
        //     a. If byteLength > maxByteLength, throw a RangeError exception.
        //     b. Append [[ArrayBufferMaxByteLength]] to slots.
        if let Some(max_byte_len) = max_byte_len {
            if byte_len > max_byte_len {
                return Err(JsNativeError::range()
                    .with_message("`length` cannot be bigger than `maxByteLength`")
                    .into());
            }
        }

        // 4. Let obj be ? OrdinaryCreateFromConstructor(constructor, "%ArrayBuffer.prototype%", slots).
        let prototype = get_prototype_from_constructor(
            constructor,
            StandardConstructors::array_buffer,
            context,
        )?;

        // 5. Let block be ? CreateByteDataBlock(byteLength).
        // Preemptively allocate for `max_byte_len` if possible.
        //     a. If it is not possible to create a Data Block block consisting of maxByteLength bytes, throw a RangeError exception.
        //     b. NOTE: Resizable ArrayBuffers are designed to be implementable with in-place growth. Implementations may
        //        throw if, for example, virtual memory cannot be reserved up front.
        let block = create_byte_data_block(byte_len, max_byte_len, context)?;

        let obj = JsObject::new(
            context.root_shape(),
            prototype,
            Self {
                // 6. Set obj.[[ArrayBufferData]] to block.
                // 7. Set obj.[[ArrayBufferByteLength]] to byteLength.
                data: Some(block),
                // 8. If allocatingResizableBuffer is true, then
                //    c. Set obj.[[ArrayBufferMaxByteLength]] to maxByteLength.
                max_byte_len,
                detach_key: JsValue::Undefined,
            },
        );

        // 9. Return obj.
        Ok(obj)
    }
}

/// Abstract operation [`GetArrayBufferMaxByteLengthOption ( options )`][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-getarraybuffermaxbytelengthoption
fn get_max_byte_len(options: &JsValue, context: &mut Context) -> JsResult<Option<u64>> {
    // 1. If options is not an Object, return empty.
    let Some(options) = options.as_object() else {
        return Ok(None);
    };

    // 2. Let maxByteLength be ? Get(options, "maxByteLength").
    let max_byte_len = options.get(js_string!("maxByteLength"), context)?;

    // 3. If maxByteLength is undefined, return empty.
    if max_byte_len.is_undefined() {
        return Ok(None);
    }

    // 4. Return ? ToIndex(maxByteLength).
    max_byte_len.to_index(context).map(Some)
}

/// `CreateByteDataBlock ( size )` abstract operation.
///
/// The abstract operation `CreateByteDataBlock` takes argument `size` (a non-negative
/// integer). For more information, check the [spec][spec].
///
/// [spec]: https://tc39.es/ecma262/#sec-createbytedatablock
pub(crate) fn create_byte_data_block(
    size: u64,
    max_buffer_size: Option<u64>,
    context: &mut Context,
) -> JsResult<Vec<u8>> {
    let alloc_size = max_buffer_size.unwrap_or(size);

    assert!(size <= alloc_size);

    if alloc_size > context.host_hooks().max_buffer_size(context) {
        return Err(JsNativeError::range()
            .with_message("cannot allocate a buffer that exceeds the maximum buffer size")
            .into());
    }

    // 1. Let db be a new Data Block value consisting of size bytes. If it is impossible to
    //    create such a Data Block, throw a RangeError exception.
    let alloc_size = alloc_size.try_into().map_err(|e| {
        JsNativeError::range().with_message(format!("couldn't allocate the data block: {e}"))
    })?;

    let mut data_block = Vec::new();
    data_block.try_reserve_exact(alloc_size).map_err(|e| {
        JsNativeError::range().with_message(format!("couldn't allocate the data block: {e}"))
    })?;

    // since size <= alloc_size, then `size` must also fit inside a `usize`.
    let size = size as usize;

    // 2. Set all of the bytes of db to 0.
    data_block.resize(size, 0);

    // 3. Return db.
    Ok(data_block)
}