Skip to content

Commit

Permalink
feat(isolate): expose get/set_data (denoland#911)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartek Iwańczuk <[email protected]>
  • Loading branch information
AaronO and bartlomieju authored Oct 6, 2022
1 parent cfdb8b0 commit de7a1ac
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,22 +451,25 @@ impl Isolate {

/// Associate embedder-specific data with the isolate. `slot` has to be
/// between 0 and `Isolate::get_number_of_data_slots()`.
///
/// 0-indexed slot is used internally by rusty_v8, so users have 3 slots
/// left to use.
#[inline(always)]
unsafe fn set_data(&mut self, slot: u32, ptr: *mut c_void) {
pub unsafe fn set_data(&mut self, slot: u32, ptr: *mut c_void) {
assert!(slot < 4);
v8__Isolate__SetData(self, slot + Self::INTERNAL_SLOT_COUNT, ptr)
}

/// Retrieve embedder-specific data from the isolate.
/// Returns NULL if SetData has never been called for the given `slot`.
#[allow(dead_code)]
fn get_data(&self, slot: u32) -> *mut c_void {
pub fn get_data(&self, slot: u32) -> *mut c_void {
assert!(slot < 4);
unsafe { v8__Isolate__GetData(self, slot + Self::INTERNAL_SLOT_COUNT) }
}

/// Returns the maximum number of available embedder data slots. Valid slots
/// are in the range of 0 - `Isolate::get_number_of_data_slots() - 1`.
#[allow(dead_code)]
fn get_number_of_data_slots(&self) -> u32 {
pub fn get_number_of_data_slots(&self) -> u32 {
unsafe {
v8__Isolate__GetNumberOfDataSlots(self) - Self::INTERNAL_SLOT_COUNT
}
Expand Down
54 changes: 54 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7487,6 +7487,60 @@ fn finalizer_on_kept_global() {
drop(global);
}

#[test]
fn isolate_data_fields() {
let _setup_guard = setup();

let mut isolate = v8::Isolate::new(Default::default());

struct SomeData {
foo: &'static str,
bar: &'static str,
fizz: &'static str,
}

let some_data1 = Box::new(SomeData {
foo: "foo",
bar: "",
fizz: "",
});
let some_data2 = Box::new(SomeData {
foo: "",
bar: "bar",
fizz: "",
});
let some_data3 = Box::new(SomeData {
foo: "",
bar: "",
fizz: "fizz",
});
unsafe {
isolate.set_data(1, Box::into_raw(some_data1) as *mut _ as *mut c_void);
isolate.set_data(2, Box::into_raw(some_data2) as *mut _ as *mut c_void);
isolate.set_data(3, Box::into_raw(some_data3) as *mut _ as *mut c_void);
}

{
let data_some_data1 = isolate.get_data(1) as *mut SomeData;
let data_some_data1 = unsafe { &mut *data_some_data1 };
assert_eq!(data_some_data1.foo, "foo");
assert_eq!(data_some_data1.bar, "");
assert_eq!(data_some_data1.fizz, "");

let data_some_data2 = isolate.get_data(2) as *mut SomeData;
let data_some_data2 = unsafe { &mut *data_some_data2 };
assert_eq!(data_some_data2.foo, "");
assert_eq!(data_some_data2.bar, "bar");
assert_eq!(data_some_data2.fizz, "");

let data_some_data3 = isolate.get_data(3) as *mut SomeData;
let data_some_data3 = unsafe { &mut *data_some_data3 };
assert_eq!(data_some_data3.foo, "");
assert_eq!(data_some_data3.bar, "");
assert_eq!(data_some_data3.fizz, "fizz");
}
}

#[test]
fn host_create_shadow_realm_context_callback() {
let _setup_guard = setup();
Expand Down

0 comments on commit de7a1ac

Please sign in to comment.