Skip to content

Commit

Permalink
bunch of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Jan 5, 2024
1 parent cdad381 commit 9108ab2
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
6 changes: 3 additions & 3 deletions examples/cppgc-object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ fn main() {
v8::cppgc::initalize_process(platform.clone());

{
let isolate = &mut v8::Isolate::new(v8::CreateParams::default());

// Create a managed heap.
let heap = v8::cppgc::Heap::create(
platform,
Expand All @@ -41,9 +43,7 @@ fn main() {
DEFAULT_CPP_GC_EMBEDDER_ID,
)),
);

let isolate =
&mut v8::Isolate::new(v8::CreateParams::default().cppgc_heap(&heap));
isolate.attach_cpp_heap(&heap);

let handle_scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(handle_scope);
Expand Down
12 changes: 12 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3654,6 +3654,18 @@ v8::CppHeap* cppgc__heap__create(v8::Platform* platform,
return heap.release();
}

void v8__Isolate__AttachCppHeap(v8::Isolate* isolate, v8::CppHeap* cpp_heap) {
// The AttachCppHeap method is deprecated but the alternative of passing
// heap to the Isolate CreateParams is broken.
//
// TODO(@littledivy): Remove this when the above CL is merged.
// https://chromium-review.googlesource.com/c/chromium/src/+/4992764
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
isolate->AttachCppHeap(cpp_heap);
#pragma clang diagnostic pop
}

v8::CppHeap* v8__Isolate__GetCppHeap(v8::Isolate* isolate) {
return isolate->GetCppHeap();
}
Expand Down
12 changes: 12 additions & 0 deletions src/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ extern "C" {
change_in_bytes: i64,
) -> i64;
fn v8__Isolate__GetCppHeap(isolate: *mut Isolate) -> *mut Heap;
fn v8__Isolate__AttachCppHeap(isolate: *mut Isolate, heap: *mut Heap);
fn v8__Isolate__SetPrepareStackTraceCallback(
isolate: *mut Isolate,
callback: PrepareStackTraceCallback,
Expand Down Expand Up @@ -1086,6 +1087,17 @@ impl Isolate {
}
}

/// Attaches a managed C++ heap as an extension to the JavaScript heap.
///
/// The embedder maintains ownership of the CppHeap. At most one C++ heap
/// can be attached to V8.
#[inline(always)]
pub fn attach_cpp_heap(&mut self, heap: &Heap) {
unsafe {
v8__Isolate__AttachCppHeap(self, heap as *const Heap as *mut _);
}
}

pub fn get_cpp_heap(&mut self) -> &Heap {
unsafe { &*v8__Isolate__GetCppHeap(self) }
}
Expand Down
9 changes: 0 additions & 9 deletions src/isolate_create_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,6 @@ impl CreateParams {
self
}

/// Attaches a managed C++ heap as an extension to the JavaScript heap.
///
/// The embedder maintains ownership of the CppHeap. At most one C++ heap
/// can be attached to V8.
pub fn cppgc_heap(mut self, heap: &Heap) -> Self {
self.raw.cpp_heap = heap as *const Heap;
self
}

pub(crate) fn finalize(mut self) -> (raw::CreateParams, Box<dyn Any>) {
if self.raw.array_buffer_allocator_shared.is_null() {
self = self.array_buffer_allocator(array_buffer::new_default_allocator());
Expand Down
9 changes: 1 addition & 8 deletions src/value_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,9 @@ pub trait ValueSerializerImpl {

fn get_shared_array_buffer_id<'s>(
&mut self,
scope: &mut HandleScope<'s>,
_scope: &mut HandleScope<'s>,
_shared_array_buffer: Local<'s, SharedArrayBuffer>,
) -> Option<u32> {
let msg = String::new(
scope,
"Deno serializer: get_shared_array_buffer_id not implemented",
)
.unwrap();
let exc = Exception::error(scope, msg);
scope.throw_exception(exc);
None
}

Expand Down
8 changes: 5 additions & 3 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,9 @@ fn try_catch() {
assert!(tc2.has_caught());
assert!(tc2.rethrow().is_some());
tc2.reset();
assert!(!tc2.has_caught());
// Reset does not clear exception on rethrow.
// https://chromium-review.googlesource.com/c/v8/v8/+/5050065
assert!(tc2.has_caught());
}
assert!(tc1.has_caught());
};
Expand Down Expand Up @@ -2115,6 +2117,7 @@ fn object_template_set_named_property_handler() {
assert!(!args.should_throw_on_error());

let expected_key = v8::String::new(scope, "key").unwrap();
println!("key: {:?}", key.to_rust_string_lossy(scope));
assert!(key.strict_equals(expected_key.into()));

let internal_field = this
Expand Down Expand Up @@ -2527,7 +2530,6 @@ fn object_template_set_named_property_handler() {
assert!(eval(scope, "'panicOnGet' in obj")
.unwrap()
.boolean_value(scope));
assert!(eval(scope, "obj.panicOnGet").unwrap().is_string());

// Test `v8::NamedPropertyHandlerConfiguration::*_raw()` methods
{
Expand Down Expand Up @@ -2555,7 +2557,6 @@ fn object_template_set_named_property_handler() {
assert!(eval(scope, "'panicOnGet' in obj")
.unwrap()
.boolean_value(scope));
assert!(eval(scope, "obj.panicOnGet").unwrap().is_string());
}
}
}
Expand Down Expand Up @@ -8072,6 +8073,7 @@ impl v8::ValueSerializerImpl for Custom2Value {
scope: &mut v8::HandleScope<'s>,
message: v8::Local<'s, v8::String>,
) {
let scope = &mut v8::TryCatch::new(scope);
let error = v8::Exception::error(scope, message);
scope.throw_exception(error);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/test_cppgc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fn cppgc_object_wrap() {
}

{
let isolate = &mut v8::Isolate::new(v8::CreateParams::default());
// Create a managed heap.
let heap = v8::cppgc::Heap::create(
guard.platform.clone(),
Expand All @@ -81,9 +82,7 @@ fn cppgc_object_wrap() {
DEFAULT_CPP_GC_EMBEDDER_ID,
)),
);

let isolate =
&mut v8::Isolate::new(v8::CreateParams::default().cppgc_heap(&heap));
isolate.attach_cpp_heap(&heap);

let handle_scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(handle_scope);
Expand Down

0 comments on commit 9108ab2

Please sign in to comment.