diff --git a/src/isolate.rs b/src/isolate.rs index 3b7de262..33d8db78 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -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 } diff --git a/tests/test_api.rs b/tests/test_api.rs index f6382e32..a1e83d06 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -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();