Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/wintertc/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use std::sync::Arc;
mod from;
mod to;

#[cfg(test)]
mod tests;

/// Convenience method to avoid copy-pasting the same message.
#[inline]
fn unsupported_type() -> JsError {
Expand Down
80 changes: 80 additions & 0 deletions core/wintertc/src/store/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use super::*;
use boa_engine::context::HostHooks;
use boa_engine::object::builtins::JsArrayBuffer;
use std::rc::Rc;

struct LimitedBufferHooks(u64);

impl HostHooks for LimitedBufferHooks {
fn max_buffer_size(&self, _context: &mut Context) -> u64 {
self.0
}
}

#[test]
fn array_buffer_deserialization_respects_max_buffer_size() {
let hooks = Rc::new(LimitedBufferHooks(16));
let mut context = Context::builder().host_hooks(hooks).build().unwrap();

// Create an ArrayBuffer value store with 32 bytes (exceeds 16 byte limit)
let payload = AlignedVec::from_slice(0, &[0x42; 32]);
let store = JsValueStore::new(ValueStoreInner::ArrayBuffer(payload));

let result = store.try_into_js(&mut context);
assert!(result.is_err());
let err_str = result.unwrap_err().to_string();
assert!(
err_str.contains("cannot allocate a buffer that exceeds the maximum buffer size"),
"expected max buffer size RangeError, got: {err_str}"
);
}

#[test]
fn array_buffer_deserialization_within_max_buffer_size() {
let hooks = Rc::new(LimitedBufferHooks(16));
let mut context = Context::builder().host_hooks(hooks).build().unwrap();

let data = [1, 2, 3, 4, 5, 6, 7, 8];
let payload = AlignedVec::from_slice(0, &data);
let store = JsValueStore::new(ValueStoreInner::ArrayBuffer(payload));

let result = store.try_into_js(&mut context).unwrap();
let obj = result.as_object().unwrap();
let ab = JsArrayBuffer::from_object(obj.clone()).unwrap();
assert_eq!(ab.byte_length(), 8);
assert_eq!(ab.data().unwrap().as_ref(), &data);
}

#[test]
fn array_buffer_roundtrip_clone() {
let mut context = Context::default();

let data = [10, 20, 30, 40, 50];
let original = JsArrayBuffer::new(data.len(), &mut context).unwrap();
original.data_mut().unwrap().copy_from_slice(&data);

let js_val: JsValue = original.into();
let store = JsValueStore::try_from_js(&js_val, &mut context, vec![]).unwrap();
let cloned_val = store.try_into_js(&mut context).unwrap();

let cloned_obj = cloned_val.as_object().unwrap();
let cloned_ab = JsArrayBuffer::from_object(cloned_obj.clone()).unwrap();

assert_eq!(cloned_ab.byte_length(), 5);
assert_eq!(cloned_ab.data().unwrap().as_ref(), &data);
}

#[test]
fn empty_array_buffer_roundtrip() {
let mut context = Context::default();

let original = JsArrayBuffer::new(0, &mut context).unwrap();
let js_val: JsValue = original.into();
let store = JsValueStore::try_from_js(&js_val, &mut context, vec![]).unwrap();
let cloned_val = store.try_into_js(&mut context).unwrap();

let cloned_obj = cloned_val.as_object().unwrap();
let cloned_ab = JsArrayBuffer::from_object(cloned_obj.clone()).unwrap();

assert_eq!(cloned_ab.byte_length(), 0);
}
9 changes: 7 additions & 2 deletions core/wintertc/src/store/to.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! All methods for deserializing a [`JsValueStore`] into a [`JsValue`].
use crate::store::{JsValueStore, StringStore, ValueStoreInner, unsupported_type};
use boa_engine::builtins::array_buffer::{AlignedVec, SharedArrayBuffer};
use boa_engine::builtins::array_buffer::SharedArrayBuffer;
use boa_engine::builtins::typed_array::TypedArrayKind;
use boa_engine::object::builtins::{
JsArray, JsArrayBuffer, JsDataView, JsDate, JsMap, JsRegExp, JsSet, JsSharedArrayBuffer,
Expand Down Expand Up @@ -67,7 +67,12 @@ fn try_into_js_array_buffer(
seen: &mut ReverseSeenMap,
context: &mut Context,
) -> JsResult<JsValue> {
let buffer = JsArrayBuffer::from_byte_block(AlignedVec::from_slice(0, data), context)?;
// Allocate via `new` instead of `from_byte_block` to enforce `HostHooks::max_buffer_size`
// and use fallible memory reservation via `CreateByteDataBlock`.
let buffer = JsArrayBuffer::new(data.len(), context)?;
if let Some(mut data_mut) = buffer.data_mut() {
data_mut.copy_from_slice(data);
}
Comment on lines +72 to +75

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remark: consider adding a small concise comment/note why from_byte_block isn't used here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an explanatory note as suggested! We use JsArrayBuffer::new instead of rom_byte_block so that HostHooks::max_buffer_size is enforced and memory reservation uses fallible allocation via CreateByteDataBlock. Also rebased onto latest main. Thanks for the review!

let obj = JsObject::from(buffer);
seen.insert(store, obj.clone());
Ok(JsValue::from(obj))
Expand Down