From 8a5f484c719a0a05f062145de31e31948aa4e203 Mon Sep 17 00:00:00 2001 From: snek Date: Fri, 6 Sep 2024 12:04:19 -0700 Subject: [PATCH] re-add deprecated cppgc api (#1611) --- .gn | 2 ++ src/binding.cc | 20 +++++++++---- src/cppgc.rs | 17 ++++++++--- src/isolate.rs | 14 +++++++++ tests/test_cppgc.rs | 72 +++++++++++++++++++++++++-------------------- 5 files changed, 84 insertions(+), 41 deletions(-) diff --git a/.gn b/.gn index cf30151c..1c2b2ce5 100644 --- a/.gn +++ b/.gn @@ -40,6 +40,8 @@ default_args = { v8_enable_pointer_compression = false + v8_imminent_deprecation_warnings = false + # This flag speeds up the performance of fork/execve on Linux systems for # embedders which use it (like Node.js). It works by marking the pages that # V8 allocates as MADV_DONTFORK. Without MADV_DONTFORK, the Linux kernel diff --git a/src/binding.cc b/src/binding.cc index 713724b3..de913d5d 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -3888,13 +3888,25 @@ void v8__Object__Wrap(v8::Isolate* isolate, const v8::Object& wrapper, tag); } +v8::CppHeap* v8__Isolate__GetCppHeap(v8::Isolate* isolate) { + return isolate->GetCppHeap(); +} + +void v8__Isolate__AttachCppHeap(v8::Isolate* isolate, v8::CppHeap* cpp_heap) { + isolate->AttachCppHeap(cpp_heap); +} + +void v8__Isolate__DetachCppHeap(v8::Isolate* isolate) { + isolate->DetachCppHeap(); +} + void cppgc__initialize_process(v8::Platform* platform) { cppgc::InitializeProcess(platform->GetPageAllocator()); } void cppgc__shutdown_process() { cppgc::ShutdownProcess(); } -v8::CppHeap* cppgc__heap__create(v8::Platform* platform, +v8::CppHeap* v8__CppHeap__Create(v8::Platform* platform, cppgc::Heap::MarkingType marking_support, cppgc::Heap::SweepingType sweeping_support) { v8::CppHeapCreateParams params{{}}; @@ -3904,11 +3916,9 @@ v8::CppHeap* cppgc__heap__create(v8::Platform* platform, return heap.release(); } -v8::CppHeap* v8__Isolate__GetCppHeap(v8::Isolate* isolate) { - return isolate->GetCppHeap(); -} +void v8__CppHeap__Terminate(v8::CppHeap* cpp_heap) { cpp_heap->Terminate(); } -void cppgc__heap__DELETE(v8::CppHeap* self) { delete self; } +void v8__CppHeap__DELETE(v8::CppHeap* self) { delete self; } void cppgc__heap__enable_detached_garbage_collections_for_testing( v8::CppHeap* heap) { diff --git a/src/cppgc.rs b/src/cppgc.rs index 2db8bf15..7ecd6a80 100644 --- a/src/cppgc.rs +++ b/src/cppgc.rs @@ -14,12 +14,13 @@ extern "C" { fn cppgc__initialize_process(platform: *mut Platform); fn cppgc__shutdown_process(); - fn cppgc__heap__create( + fn v8__CppHeap__Create( platform: *mut Platform, marking_support: MarkingType, sweeping_support: SweepingType, ) -> *mut Heap; - fn cppgc__heap__DELETE(heap: *mut Heap); + fn v8__CppHeap__Terminate(heap: *mut Heap); + fn v8__CppHeap__DELETE(heap: *mut Heap); fn cppgc__make_garbage_collectable( heap: *mut Heap, size: usize, @@ -226,7 +227,9 @@ pub struct Heap(Opaque); impl Drop for Heap { fn drop(&mut self) { - unsafe { cppgc__heap__DELETE(self) } + unsafe { + v8__CppHeap__DELETE(self); + } } } @@ -236,7 +239,7 @@ impl Heap { params: HeapCreateParams, ) -> UniqueRef { unsafe { - UniqueRef::from_raw(cppgc__heap__create( + UniqueRef::from_raw(v8__CppHeap__Create( &*platform as *const Platform as *mut _, params.marking_support, params.sweeping_support, @@ -263,6 +266,12 @@ impl Heap { ); } } + + pub fn terminate(&mut self) { + unsafe { + v8__CppHeap__Terminate(self); + } + } } /// Base trait for managed objects. diff --git a/src/isolate.rs b/src/isolate.rs index e77ac7fb..a2de0ba9 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -18,6 +18,7 @@ use crate::support::MapFnFrom; use crate::support::MapFnTo; use crate::support::Opaque; use crate::support::ToCFn; +use crate::support::UniqueRef; use crate::support::UnitType; use crate::wasm::trampoline; use crate::wasm::WasmStreaming; @@ -467,6 +468,8 @@ extern "C" { change_in_bytes: i64, ) -> i64; fn v8__Isolate__GetCppHeap(isolate: *mut Isolate) -> *mut Heap; + fn v8__Isolate__AttachCppHeap(isolate: *mut Isolate, cpp_heap: *mut Heap); + fn v8__Isolate__DetachCppHeap(isolate: *mut Isolate); fn v8__Isolate__SetPrepareStackTraceCallback( isolate: *mut Isolate, callback: PrepareStackTraceCallback, @@ -1219,10 +1222,21 @@ impl Isolate { } } + #[inline(always)] pub fn get_cpp_heap(&mut self) -> Option<&Heap> { unsafe { v8__Isolate__GetCppHeap(self).as_ref() } } + #[inline(always)] + pub fn attach_cpp_heap(&mut self, cpp_heap: &mut UniqueRef) { + unsafe { v8__Isolate__AttachCppHeap(self, cpp_heap.deref_mut()) } + } + + #[inline(always)] + pub fn detach_cpp_heap(&mut self) { + unsafe { v8__Isolate__DetachCppHeap(self) } + } + #[inline(always)] pub fn set_oom_error_handler(&mut self, callback: OomErrorCallback) { unsafe { v8__Isolate__SetOOMErrorHandler(self, callback) }; diff --git a/tests/test_cppgc.rs b/tests/test_cppgc.rs index 3379afd3..b44a9293 100644 --- a/tests/test_cppgc.rs +++ b/tests/test_cppgc.rs @@ -94,31 +94,32 @@ fn cppgc_object_wrap() { { // Create a managed heap. - let heap = v8::cppgc::Heap::create( + let mut heap = v8::cppgc::Heap::create( guard.platform.clone(), v8::cppgc::HeapCreateParams::default(), ); - let isolate = - &mut v8::Isolate::new(v8::CreateParams::default().cpp_heap(heap)); + let isolate = &mut v8::Isolate::new(v8::CreateParams::default()); + isolate.attach_cpp_heap(&mut heap); - let handle_scope = &mut v8::HandleScope::new(isolate); - let context = v8::Context::new(handle_scope, Default::default()); - let scope = &mut v8::ContextScope::new(handle_scope, context); - let global = context.global(scope); { - let func = v8::Function::new(scope, op_wrap).unwrap(); - let name = v8::String::new(scope, "wrap").unwrap(); - global.set(scope, name.into(), func.into()).unwrap(); - } - { - let func = v8::Function::new(scope, op_unwrap).unwrap(); - let name = v8::String::new(scope, "unwrap").unwrap(); - global.set(scope, name.into(), func.into()).unwrap(); - } + let handle_scope = &mut v8::HandleScope::new(isolate); + let context = v8::Context::new(handle_scope, Default::default()); + let scope = &mut v8::ContextScope::new(handle_scope, context); + let global = context.global(scope); + { + let func = v8::Function::new(scope, op_wrap).unwrap(); + let name = v8::String::new(scope, "wrap").unwrap(); + global.set(scope, name.into(), func.into()).unwrap(); + } + { + let func = v8::Function::new(scope, op_unwrap).unwrap(); + let name = v8::String::new(scope, "unwrap").unwrap(); + global.set(scope, name.into(), func.into()).unwrap(); + } - execute_script( - scope, - r#" + execute_script( + scope, + r#" { const x = {}; const y = unwrap(wrap(x)); // collected @@ -129,27 +130,34 @@ fn cppgc_object_wrap() { globalThis.wrapped = wrap(wrap({})); // not collected "#, - ); + ); - assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 0); + assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 0); - scope - .request_garbage_collection_for_testing(v8::GarbageCollectionType::Full); + scope.request_garbage_collection_for_testing( + v8::GarbageCollectionType::Full, + ); - assert!(TRACE_COUNT.load(Ordering::SeqCst) > 0); - assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 1); + assert!(TRACE_COUNT.load(Ordering::SeqCst) > 0); + assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 1); - execute_script( - scope, - r#" + execute_script( + scope, + r#" globalThis.wrapped = undefined; "#, - ); + ); - scope - .request_garbage_collection_for_testing(v8::GarbageCollectionType::Full); + scope.request_garbage_collection_for_testing( + v8::GarbageCollectionType::Full, + ); - assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 3); + assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 3); + } + + isolate.detach_cpp_heap(); + heap.terminate(); + drop(heap); } }