Skip to content

Commit

Permalink
Implement getter for ArrayBuffer data
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jan 24, 2024
1 parent ced2904 commit 49989fd
Showing 1 changed file with 76 additions and 2 deletions.
78 changes: 76 additions & 2 deletions core/engine/src/object/builtins/jsarraybuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use crate::{
builtins::array_buffer::ArrayBuffer,
context::intrinsics::StandardConstructors,
error::JsNativeError,
object::{internal_methods::get_prototype_from_constructor, JsObject, JsObjectType},
object::{
internal_methods::get_prototype_from_constructor, ErasedObject, JsObject, JsObjectType,
},
value::TryFromJs,
Context, JsResult, JsValue,
};
use boa_gc::{Finalize, Trace};
use boa_gc::{Finalize, GcRef, GcRefMut, Trace};
use std::ops::Deref;

/// `JsArrayBuffer` provides a wrapper for Boa's implementation of the ECMAScript `ArrayBuffer` object
Expand Down Expand Up @@ -195,6 +197,78 @@ impl JsArrayBuffer {
.into()
})
}

/// Get an immutable reference to the [`JsArrayObject`]'s data.
///
/// Returns `None` if detached.
///
/// ```
/// # use boa_engine::{
/// # object::builtins::JsArrayBuffer,
/// # Context, JsResult, JsValue
/// # };
/// # fn main() -> JsResult<()> {
/// # // Initialize context
/// # let context = &mut Context::default();
/// // Create a buffer from a chunk of data
/// let data_block: Vec<u8> = (0..5).collect();
/// let array_buffer = JsArrayBuffer::from_byte_block(data_block, context)?;
///
/// // Get a reference to the data.
/// let internal_buffer = array_buffer.data();
///
/// assert_eq!(internal_buffer.as_deref(), Some((0..5).collect::<Vec<u8>>().as_slice()));
/// # Ok(())
/// # }
/// ```
#[inline]
#[must_use]
pub fn data(&self) -> Option<GcRef<'_, [u8]>> {
GcRef::try_map(
self.inner
.downcast_ref::<ArrayBuffer>()
.expect("inner must be an ArrayBuffer"),
ArrayBuffer::data,
)
}

/// Get a mutable reference to the [`JsArrayObject`]'s data.
///
/// Returns `None` if detached.
///
/// ```
/// # use boa_engine::{
/// # object::builtins::JsArrayBuffer,
/// # Context, JsResult, JsValue
/// # };
/// # fn main() -> JsResult<()> {
/// # // Initialize context
/// # let context = &mut Context::default();
/// // Create a buffer from a chunk of data
/// let data_block: Vec<u8> = (0..5).collect();
/// let array_buffer = JsArrayBuffer::from_byte_block(data_block, context)?;
///
/// // Get a reference to the data.
/// let mut internal_buffer = array_buffer
/// .data_mut()
/// .expect("the buffer should not be detached");
///
/// internal_buffer.fill(10);
///
/// assert_eq!(&*internal_buffer, vec![10u8; 5].as_slice());
/// # Ok(())
/// # }
/// ```
#[inline]
#[must_use]
pub fn data_mut(&self) -> Option<GcRefMut<'_, ErasedObject, [u8]>> {
GcRefMut::try_map(
self.inner
.downcast_mut::<ArrayBuffer>()
.expect("inner must be an ArrayBuffer"),
ArrayBuffer::data_mut,
)
}
}

impl From<JsArrayBuffer> for JsObject {
Expand Down

0 comments on commit 49989fd

Please sign in to comment.