Skip to content

Commit

Permalink
Add small documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Oct 14, 2023
1 parent b06b2f2 commit c5a9398
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion boa_engine/src/builtins/array_buffer/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ pub(crate) fn create_shared_byte_data_block(
// a. Append WriteSharedMemory { [[Order]]: init, [[NoTear]]: true, [[Block]]: db,
// [[ByteIndex]]: i, [[ElementSize]]: 1, [[Payload]]: zero } to eventsRecord.[[EventList]].
// 6. Return db.

// Initializing a boxed slice of atomics is almost impossible using safe code.
// This replaces that with a simple `alloc` and some casts to convert the allocation
// to `Box<[AtomicU8]>`.

let layout = alloc::Layout::array::<AtomicU8>(size).map_err(|e| {
JsNativeError::range().with_message(format!("couldn't allocate the data block: {e}"))
})?;
Expand All @@ -358,7 +363,11 @@ pub(crate) fn create_shared_byte_data_block(
// - `buffer` is a valid pointer by the null check above.
let buffer = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(ptr, size)) };

// Just for good measure.
// Just for good measure, since our implementation depends on having a pointer aligned
// to the alignment of `u64`.
// This could be replaced with a custom `Box` implementation, but most architectures
// already align pointers to 8 bytes, so it's a lot of work for such a small
// compatibility improvement.
assert_eq!(buffer.as_ptr().addr() % std::mem::align_of::<u64>(), 0);

// 3. Return db.
Expand Down

0 comments on commit c5a9398

Please sign in to comment.