0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

re-add deprecated cppgc api (#1611)

This commit is contained in:
snek 2024-09-06 12:04:19 -07:00 committed by GitHub
parent bca14ac66d
commit 8a5f484c71
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 84 additions and 41 deletions

2
.gn
View file

@ -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

View file

@ -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) {

View file

@ -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<Heap> {
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.

View file

@ -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<Heap>) {
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) };

View file

@ -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);
}
}