mirror of
https://github.com/denoland/rusty_v8.git
synced 2025-02-01 12:15:46 -05:00
fix: let cppgc align to 16 bytes (#1677)
This commit is contained in:
parent
25900a3c33
commit
8020991257
6 changed files with 218 additions and 157 deletions
|
@ -27,7 +27,7 @@ fn main() {
|
|||
v8::V8::initialize_platform(platform.clone());
|
||||
v8::V8::initialize();
|
||||
|
||||
v8::cppgc::initalize_process(platform.clone());
|
||||
v8::cppgc::initialize_process(platform.clone());
|
||||
|
||||
{
|
||||
let heap =
|
||||
|
|
|
@ -46,7 +46,7 @@ fn main() {
|
|||
let platform = v8::new_default_platform(0, false).make_shared();
|
||||
v8::V8::initialize_platform(platform.clone());
|
||||
v8::V8::initialize();
|
||||
v8::cppgc::initalize_process(platform.clone());
|
||||
v8::cppgc::initialize_process(platform.clone());
|
||||
|
||||
{
|
||||
// Create a managed heap.
|
||||
|
|
|
@ -3976,10 +3976,20 @@ void cppgc__heap__collect_garbage_for_testing(
|
|||
heap->CollectGarbageForTesting(stack_state);
|
||||
}
|
||||
|
||||
RustObj* cppgc__make_garbage_collectable(v8::CppHeap* heap, size_t size) {
|
||||
class alignas(16) RustObjButAlign16 : public RustObj {};
|
||||
|
||||
RustObj* cppgc__make_garbage_collectable(v8::CppHeap* heap, size_t size,
|
||||
size_t alignment) {
|
||||
if (alignment <= 8) {
|
||||
return cppgc::MakeGarbageCollected<RustObj>(heap->GetAllocationHandle(),
|
||||
cppgc::AdditionalBytes(size));
|
||||
}
|
||||
if (alignment <= 16) {
|
||||
return cppgc::MakeGarbageCollected<RustObjButAlign16>(
|
||||
heap->GetAllocationHandle(), cppgc::AdditionalBytes(size));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void cppgc__Visitor__Trace__Member(cppgc::Visitor* visitor,
|
||||
cppgc::Member<RustObj>* member) {
|
||||
|
|
13
src/cppgc.rs
13
src/cppgc.rs
|
@ -27,6 +27,7 @@ extern "C" {
|
|||
fn cppgc__make_garbage_collectable(
|
||||
heap: *mut Heap,
|
||||
size: usize,
|
||||
alignment: usize,
|
||||
) -> *mut RustObj;
|
||||
|
||||
fn cppgc__heap__enable_detached_garbage_collections_for_testing(
|
||||
|
@ -145,7 +146,7 @@ unsafe fn get_object_from_rust_obj<T: GarbageCollected>(
|
|||
/// creating a Heap.
|
||||
///
|
||||
/// Can be called multiple times when paired with `ShutdownProcess()`.
|
||||
pub fn initalize_process(platform: SharedRef<Platform>) {
|
||||
pub fn initialize_process(platform: SharedRef<Platform>) {
|
||||
unsafe {
|
||||
cppgc__initialize_process(&*platform as *const Platform as *mut _);
|
||||
}
|
||||
|
@ -155,7 +156,7 @@ pub fn initalize_process(platform: SharedRef<Platform>) {
|
|||
///
|
||||
/// Must be called after destroying the last used heap. Some process-global
|
||||
/// metadata may not be returned and reused upon a subsequent
|
||||
/// `initalize_process()` call.
|
||||
/// `initialize_process()` call.
|
||||
pub unsafe fn shutdown_process() {
|
||||
cppgc__shutdown_process();
|
||||
}
|
||||
|
@ -343,6 +344,11 @@ pub unsafe fn make_garbage_collected<T: GarbageCollected + 'static>(
|
|||
heap: &Heap,
|
||||
obj: T,
|
||||
) -> Ptr<T> {
|
||||
const {
|
||||
// max alignment in cppgc is 16
|
||||
assert!(std::mem::align_of::<T>() <= 16);
|
||||
}
|
||||
|
||||
let additional_bytes = (object_offset_for_rust_obj::<T>()
|
||||
- std::mem::size_of::<RustObj>())
|
||||
+ std::mem::size_of::<T>();
|
||||
|
@ -351,9 +357,12 @@ pub unsafe fn make_garbage_collected<T: GarbageCollected + 'static>(
|
|||
cppgc__make_garbage_collectable(
|
||||
heap as *const Heap as *mut _,
|
||||
additional_bytes,
|
||||
std::mem::align_of::<T>(),
|
||||
)
|
||||
};
|
||||
|
||||
assert!(!pointer.is_null());
|
||||
|
||||
unsafe {
|
||||
let inner = get_object_from_rust_obj::<T>(pointer);
|
||||
inner.write(obj);
|
||||
|
|
|
@ -193,7 +193,7 @@ struct three_pointers_t {
|
|||
|
||||
#endif // SUPPORT_H_
|
||||
|
||||
class RustObj final : public cppgc::GarbageCollected<RustObj>,
|
||||
class RustObj : public cppgc::GarbageCollected<RustObj>,
|
||||
public cppgc::NameProvider {
|
||||
public:
|
||||
~RustObj();
|
||||
|
|
|
@ -2,40 +2,70 @@
|
|||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use v8::cppgc::{GarbageCollected, Visitor};
|
||||
|
||||
struct CppGCGuard {
|
||||
pub platform: v8::SharedRef<v8::Platform>,
|
||||
mod setup {
|
||||
use std::sync::Once;
|
||||
use std::sync::RwLock;
|
||||
// use std::sync::RwLockReadGuard;
|
||||
use std::sync::RwLockWriteGuard;
|
||||
|
||||
static PROCESS_LOCK: RwLock<()> = RwLock::new(());
|
||||
|
||||
/*
|
||||
/// Set up global state for a test that can run in parallel with other tests.
|
||||
pub(super) fn parallel_test() -> SetupGuard<RwLockReadGuard<'static, ()>> {
|
||||
initialize_once();
|
||||
SetupGuard::new(PROCESS_LOCK.read().unwrap())
|
||||
}
|
||||
*/
|
||||
|
||||
/// Set up global state for a test that must be the only test running.
|
||||
pub(super) fn sequential_test() -> SetupGuard<RwLockWriteGuard<'static, ()>> {
|
||||
initialize_once();
|
||||
SetupGuard::new(PROCESS_LOCK.write().unwrap())
|
||||
}
|
||||
|
||||
fn initalize_test() -> CppGCGuard {
|
||||
v8::V8::set_flags_from_string("--no_freeze_flags_after_init --expose-gc");
|
||||
fn initialize_once() {
|
||||
static START: Once = Once::new();
|
||||
START.call_once(|| {
|
||||
assert!(v8::icu::set_common_data_74(align_data::include_aligned!(
|
||||
align_data::Align16,
|
||||
"../third_party/icu/common/icudtl.dat"
|
||||
))
|
||||
.is_ok());
|
||||
v8::V8::set_flags_from_string(
|
||||
"--no_freeze_flags_after_init --expose_gc --harmony-import-assertions --harmony-shadow-realm --allow_natives_syntax --turbo_fast_api_calls",
|
||||
);
|
||||
|
||||
let platform = v8::new_unprotected_default_platform(0, false).make_shared();
|
||||
v8::V8::initialize_platform(platform.clone());
|
||||
v8::V8::initialize();
|
||||
v8::cppgc::initalize_process(platform.clone());
|
||||
|
||||
CppGCGuard { platform }
|
||||
v8::cppgc::initialize_process(platform.clone());
|
||||
});
|
||||
}
|
||||
|
||||
impl Drop for CppGCGuard {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
v8::cppgc::shutdown_process();
|
||||
v8::V8::dispose();
|
||||
#[must_use]
|
||||
pub(super) struct SetupGuard<G> {
|
||||
_inner: G,
|
||||
}
|
||||
|
||||
impl<G> SetupGuard<G> {
|
||||
fn new(inner: G) -> Self {
|
||||
Self { _inner: inner }
|
||||
}
|
||||
v8::V8::dispose_platform();
|
||||
}
|
||||
}
|
||||
|
||||
const TAG: u16 = 1;
|
||||
|
||||
#[test]
|
||||
fn cppgc_object_wrap() {
|
||||
let guard = initalize_test();
|
||||
macro_rules! test {
|
||||
( $( $decln:ident : $declt:ty )?, $( $initn:ident : $inite:expr )? ) => {{
|
||||
let _guard = setup::sequential_test();
|
||||
|
||||
static TRACE_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
struct Wrap {
|
||||
$( #[allow(unused)] $decln: $declt , )?
|
||||
value: v8::TracedReference<v8::Value>,
|
||||
}
|
||||
|
||||
|
@ -73,6 +103,7 @@ fn cppgc_object_wrap() {
|
|||
assert!(obj.is_api_wrapper());
|
||||
|
||||
let wrap = Wrap {
|
||||
$( $initn: $inite , )?
|
||||
value: v8::TracedReference::new(scope, args.get(0)),
|
||||
};
|
||||
let member = unsafe {
|
||||
|
@ -99,7 +130,7 @@ fn cppgc_object_wrap() {
|
|||
{
|
||||
// Create a managed heap.
|
||||
let mut heap = v8::cppgc::Heap::create(
|
||||
guard.platform.clone(),
|
||||
v8::V8::get_current_platform(),
|
||||
v8::cppgc::HeapCreateParams::default(),
|
||||
);
|
||||
let isolate = &mut v8::Isolate::new(v8::CreateParams::default());
|
||||
|
@ -173,6 +204,17 @@ fn cppgc_object_wrap() {
|
|||
heap.terminate();
|
||||
drop(heap);
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cppgc_object_wrap8() {
|
||||
test!(,);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cppgc_object_wrap16() {
|
||||
test!(big: u128, big: 0);
|
||||
}
|
||||
|
||||
fn execute_script(
|
||||
|
|
Loading…
Add table
Reference in a new issue