feat(webidl): write BufferSource rows without changing padding

Extracted from wpt-misc-fix.

Source-commit: 4fb35a340e
This commit is contained in:
ldm0
2026-10-12 02:14:09 +08:00
committed by Donough Liu
parent 417946cc5f
commit 53111bf02f
2 changed files with 48 additions and 2 deletions
+15 -2
View File
@@ -115,7 +115,20 @@ impl AllowSharedBufferSource<'_> {
/// Copy into the native view's byte range, preserving surrounding bytes.
pub fn write_bytes(&self, scope: &mut v8::PinScope<'_, '_>, bytes: &[u8]) -> bool {
if bytes.len() > self.byte_length() {
self.write_bytes_at(scope, 0, bytes)
}
/// Copy a row without touching plane padding or other shared-memory bytes.
pub fn write_bytes_at(
&self,
scope: &mut v8::PinScope<'_, '_>,
start: usize,
bytes: &[u8],
) -> bool {
if start
.checked_add(bytes.len())
.is_none_or(|end| end > self.byte_length())
{
return false;
}
if bytes.is_empty() {
@@ -124,7 +137,7 @@ impl AllowSharedBufferSource<'_> {
let backing = self
.backing_store(scope)
.expect("BufferSource has native backing");
let offset = self.byte_offset();
let offset = self.byte_offset() + start;
if backing.is_shared() {
// SAFETY: fixed shared storage is retained and V8 validated the
// view range; byte stores are unordered as required for BufferSource.
+33
View File
@@ -0,0 +1,33 @@
use moli_webidl::Context;
#[test]
fn buffer_source_row_writes_preserve_padding_and_reject_out_of_bounds() {
moli_v8_test_util::ensure_v8();
let mut isolate = v8::Isolate::new(v8::CreateParams::default());
v8::scope!(let scope, &mut isolate);
let context = v8::Context::new(scope, Default::default());
let scope = &mut v8::ContextScope::new(scope, context);
for source in [
"new DataView(new ArrayBuffer(8),2,4)",
"new DataView(new SharedArrayBuffer(8),2,4)",
] {
let source = v8::String::new(scope, source).unwrap();
let value = v8::Script::compile(scope, source, None)
.unwrap()
.run(scope)
.unwrap();
let buffer = moli_webidl::convert::<moli_webidl::AllowSharedBufferSource>(
scope,
value,
Context::argument("Test", 1),
)
.unwrap();
assert!(buffer.write_bytes(scope, &[7, 7, 7, 7]));
assert!(buffer.write_bytes_at(scope, 1, &[1, 2]));
assert_eq!(buffer.to_vec(scope), vec![7, 1, 2, 7]);
assert!(!buffer.write_bytes_at(scope, 3, &[1, 2]));
assert!(!buffer.write_bytes_at(scope, usize::MAX, &[1]));
assert!(buffer.write_bytes_at(scope, 4, &[]));
assert_eq!(buffer.to_vec(scope), vec![7, 1, 2, 7]);
}
}