2023-10-30 11:10:15 -04:00
|
|
|
// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
use v8::cppgc::{GarbageCollected, Visitor};
|
|
|
|
|
|
|
|
struct CppGCGuard {
|
|
|
|
pub platform: v8::SharedRef<v8::Platform>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn initalize_test() -> CppGCGuard {
|
|
|
|
v8::V8::set_flags_from_string("--no_freeze_flags_after_init --expose-gc");
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for CppGCGuard {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
v8::cppgc::shutdown_process();
|
|
|
|
v8::V8::dispose();
|
|
|
|
}
|
|
|
|
v8::V8::dispose_platform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 11:29:46 -04:00
|
|
|
const TAG: u16 = 1;
|
2023-10-30 11:10:15 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cppgc_object_wrap() {
|
|
|
|
let guard = initalize_test();
|
|
|
|
|
|
|
|
static TRACE_COUNT: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
static DROP_COUNT: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
|
2024-06-27 11:29:46 -04:00
|
|
|
struct Wrap {
|
|
|
|
value: v8::TracedReference<v8::Value>,
|
|
|
|
}
|
2023-10-30 11:10:15 -04:00
|
|
|
|
|
|
|
impl GarbageCollected for Wrap {
|
2024-06-27 11:29:46 -04:00
|
|
|
fn trace(&self, visitor: &Visitor) {
|
2023-10-30 11:10:15 -04:00
|
|
|
TRACE_COUNT.fetch_add(1, Ordering::SeqCst);
|
2024-06-27 11:29:46 -04:00
|
|
|
visitor.trace(&self.value);
|
2023-10-30 11:10:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Wrap {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
DROP_COUNT.fetch_add(1, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 11:29:46 -04:00
|
|
|
fn op_wrap(
|
2023-10-30 11:10:15 -04:00
|
|
|
scope: &mut v8::HandleScope,
|
2024-06-27 11:29:46 -04:00
|
|
|
args: v8::FunctionCallbackArguments,
|
2024-07-23 15:43:32 -04:00
|
|
|
mut rv: v8::ReturnValue<v8::Value>,
|
2023-10-30 11:10:15 -04:00
|
|
|
) {
|
2024-06-27 11:29:46 -04:00
|
|
|
fn empty(
|
|
|
|
_scope: &mut v8::HandleScope,
|
|
|
|
_args: v8::FunctionCallbackArguments,
|
2024-07-23 15:43:32 -04:00
|
|
|
_rv: v8::ReturnValue<v8::Value>,
|
2024-06-27 11:29:46 -04:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
let templ = v8::FunctionTemplate::new(scope, empty);
|
|
|
|
let func = templ.get_function(scope).unwrap();
|
|
|
|
let obj = func.new_instance(scope, &[]).unwrap();
|
|
|
|
assert!(obj.is_api_wrapper());
|
|
|
|
|
|
|
|
let wrap = Wrap {
|
|
|
|
value: v8::TracedReference::new(scope, args.get(0)),
|
|
|
|
};
|
|
|
|
let member = unsafe {
|
|
|
|
v8::cppgc::make_garbage_collected(scope.get_cpp_heap().unwrap(), wrap)
|
|
|
|
};
|
2023-10-30 11:10:15 -04:00
|
|
|
|
2024-06-27 11:29:46 -04:00
|
|
|
unsafe {
|
|
|
|
v8::Object::wrap::<TAG, Wrap>(scope, obj, &member);
|
|
|
|
}
|
2023-10-30 11:10:15 -04:00
|
|
|
|
|
|
|
rv.set(obj.into());
|
|
|
|
}
|
|
|
|
|
2024-06-27 11:29:46 -04:00
|
|
|
fn op_unwrap(
|
|
|
|
scope: &mut v8::HandleScope,
|
|
|
|
args: v8::FunctionCallbackArguments,
|
|
|
|
mut rv: v8::ReturnValue,
|
|
|
|
) {
|
|
|
|
let obj = args.get(0).try_into().unwrap();
|
|
|
|
let member = unsafe { v8::Object::unwrap::<TAG, Wrap>(scope, obj) };
|
2024-07-11 01:03:49 -04:00
|
|
|
rv.set(member.unwrap().value.get(scope).unwrap());
|
2024-06-27 11:29:46 -04:00
|
|
|
}
|
|
|
|
|
2023-10-30 11:10:15 -04:00
|
|
|
{
|
|
|
|
// Create a managed heap.
|
|
|
|
let heap = v8::cppgc::Heap::create(
|
|
|
|
guard.platform.clone(),
|
2024-06-27 11:29:46 -04:00
|
|
|
v8::cppgc::HeapCreateParams::default(),
|
2023-10-30 11:10:15 -04:00
|
|
|
);
|
2024-06-27 11:29:46 -04:00
|
|
|
let isolate =
|
|
|
|
&mut v8::Isolate::new(v8::CreateParams::default().cpp_heap(heap));
|
2023-10-30 11:10:15 -04:00
|
|
|
|
|
|
|
let handle_scope = &mut v8::HandleScope::new(isolate);
|
2024-08-05 05:15:56 -04:00
|
|
|
let context = v8::Context::new(handle_scope, Default::default());
|
2023-10-30 11:10:15 -04:00
|
|
|
let scope = &mut v8::ContextScope::new(handle_scope, context);
|
|
|
|
let global = context.global(scope);
|
|
|
|
{
|
2024-06-27 11:29:46 -04:00
|
|
|
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();
|
2023-10-30 11:10:15 -04:00
|
|
|
global.set(scope, name.into(), func.into()).unwrap();
|
|
|
|
}
|
|
|
|
|
2024-06-27 11:29:46 -04:00
|
|
|
execute_script(
|
2023-10-30 11:10:15 -04:00
|
|
|
scope,
|
|
|
|
r#"
|
2024-06-27 11:29:46 -04:00
|
|
|
{
|
|
|
|
const x = {};
|
|
|
|
const y = unwrap(wrap(x)); // collected
|
|
|
|
if (x !== y) {
|
|
|
|
throw new Error('mismatch');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
globalThis.wrapped = wrap(wrap({})); // not collected
|
2023-10-30 11:10:15 -04:00
|
|
|
"#,
|
2024-06-27 11:29:46 -04:00
|
|
|
);
|
2023-10-30 11:10:15 -04:00
|
|
|
|
|
|
|
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 0);
|
|
|
|
|
|
|
|
scope
|
|
|
|
.request_garbage_collection_for_testing(v8::GarbageCollectionType::Full);
|
|
|
|
|
|
|
|
assert!(TRACE_COUNT.load(Ordering::SeqCst) > 0);
|
|
|
|
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 1);
|
2024-06-27 11:29:46 -04:00
|
|
|
|
|
|
|
execute_script(
|
|
|
|
scope,
|
|
|
|
r#"
|
|
|
|
globalThis.wrapped = undefined;
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
scope
|
|
|
|
.request_garbage_collection_for_testing(v8::GarbageCollectionType::Full);
|
|
|
|
|
|
|
|
assert_eq!(DROP_COUNT.load(Ordering::SeqCst), 3);
|
2023-10-30 11:10:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execute_script(
|
|
|
|
context_scope: &mut v8::ContextScope<v8::HandleScope>,
|
2024-06-27 11:29:46 -04:00
|
|
|
source: &str,
|
2023-10-30 11:10:15 -04:00
|
|
|
) {
|
|
|
|
let scope = &mut v8::HandleScope::new(context_scope);
|
2024-06-27 11:29:46 -04:00
|
|
|
let scope = &mut v8::TryCatch::new(scope);
|
|
|
|
|
|
|
|
let source = v8::String::new(scope, source).unwrap();
|
2023-10-30 11:10:15 -04:00
|
|
|
|
2024-06-27 11:29:46 -04:00
|
|
|
let script =
|
|
|
|
v8::Script::compile(scope, source, None).expect("failed to compile script");
|
2023-10-30 11:10:15 -04:00
|
|
|
|
2024-06-27 11:29:46 -04:00
|
|
|
if script.run(scope).is_none() {
|
|
|
|
let exception_string = scope
|
2023-10-30 11:10:15 -04:00
|
|
|
.stack_trace()
|
2024-06-27 11:29:46 -04:00
|
|
|
.or_else(|| scope.exception())
|
|
|
|
.map(|value| value.to_rust_string_lossy(scope))
|
2023-10-30 11:10:15 -04:00
|
|
|
.unwrap_or_else(|| "no stack trace".into());
|
|
|
|
|
|
|
|
panic!("{}", exception_string);
|
|
|
|
}
|
|
|
|
}
|