Skip to content

Commit

Permalink
chore: Add Debug for ExternalReference (denoland#1272)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastrac committed Jul 11, 2023
1 parent 226c662 commit 4110d1b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/external_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::NamedGetterCallback;
use crate::NamedSetterCallback;
use crate::PropertyEnumeratorCallback;
use std::ffi::c_void;
use std::fmt::Debug;

#[derive(Clone, Copy)]
pub union ExternalReference<'s> {
Expand All @@ -26,6 +27,13 @@ pub union ExternalReference<'s> {
pub pointer: *mut c_void,
}

impl<'s> Debug for ExternalReference<'s> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// SAFETY: All union fields are the same size
unsafe { (self.pointer).fmt(f) }
}
}

#[derive(Debug, Clone)]
pub struct ExternalReferences {
null_terminated: Vec<intptr_t>,
Expand Down
7 changes: 5 additions & 2 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5309,7 +5309,7 @@ fn external_references() {
let external_ptr = Box::into_raw(vec![0_u8, 1, 2, 3, 4].into_boxed_slice())
as *mut [u8] as *mut c_void;
// Push them to the external reference table.
let refs = v8::ExternalReferences::new(&[
let refs = [
v8::ExternalReference {
function: fn_callback.map_fn_to(),
},
Expand All @@ -5319,7 +5319,10 @@ fn external_references() {
v8::ExternalReference {
pointer: external_ptr,
},
]);
];
// Exercise the Debug impl
println!("{refs:?}");
let refs = v8::ExternalReferences::new(&refs);
// TODO(piscisaureus): leaking the `ExternalReferences` collection shouldn't
// be necessary. The reference needs to remain valid for the lifetime of the
// `SnapshotCreator` or `Isolate` that uses it, which would be the case here
Expand Down

0 comments on commit 4110d1b

Please sign in to comment.