Skip to content

Commit 30ea2b1

Browse files
committed
Use UnsafeCell for allocated_bytes_ptr
1 parent b82f905 commit 30ea2b1

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,14 @@ fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr>
4141
where
4242
F: FnOnce(u32) -> u32,
4343
{
44+
use std::cell::UnsafeCell;
45+
4446
// Allocate AllocatedBytesPtr to get return value from the host
45-
let mut allocated_bytes_ptr = [0u32, 0u32];
46-
let serialized_allocated_bytes_raw_ptr = allocated_bytes_ptr.as_mut_ptr();
47+
//
48+
// I'm not sure if `.as_mut_ptr()` creates an alias for array,
49+
// using `UnsafeCell` here makes the semantics clearer.
50+
let mut allocated_bytes_ptr: UnsafeCell<[u32; 2]> = UnsafeCell::new([0; 2]);
51+
let serialized_allocated_bytes_raw_ptr = allocated_bytes_ptr.get();
4752

4853
assert_eq!(std::mem::size_of::<*mut u32>(), std::mem::size_of::<u32>());
4954

@@ -57,6 +62,8 @@ where
5762
return None;
5863
}
5964

65+
let allocated_bytes_ptr = allocated_bytes_ptr.into_inner();
66+
6067
// Return reconstructted AllocatedBytesPtr to reveal ptr to the allocated bytes
6168
Some(AllocatedBytesPtr(
6269
allocated_bytes_ptr[0],

0 commit comments

Comments
 (0)