0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-23 15:50:11 -05:00

feat(isolate): expose get/set_data (#911)

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Aaron O'Mullan 2022-10-06 14:19:38 -03:00 committed by GitHub
parent cfdb8b0ccd
commit de7a1acbde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 5 deletions

View file

@ -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
}

View file

@ -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();