mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-12-24 08:09:16 -05:00
feat(isolate): expose get/set_data (#911)
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
parent
cfdb8b0ccd
commit
de7a1acbde
2 changed files with 62 additions and 5 deletions
|
@ -451,22 +451,25 @@ impl Isolate {
|
||||||
|
|
||||||
/// Associate embedder-specific data with the isolate. `slot` has to be
|
/// Associate embedder-specific data with the isolate. `slot` has to be
|
||||||
/// between 0 and `Isolate::get_number_of_data_slots()`.
|
/// 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)]
|
#[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)
|
v8__Isolate__SetData(self, slot + Self::INTERNAL_SLOT_COUNT, ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve embedder-specific data from the isolate.
|
/// Retrieve embedder-specific data from the isolate.
|
||||||
/// Returns NULL if SetData has never been called for the given `slot`.
|
/// Returns NULL if SetData has never been called for the given `slot`.
|
||||||
#[allow(dead_code)]
|
pub fn get_data(&self, slot: u32) -> *mut c_void {
|
||||||
fn get_data(&self, slot: u32) -> *mut c_void {
|
assert!(slot < 4);
|
||||||
unsafe { v8__Isolate__GetData(self, slot + Self::INTERNAL_SLOT_COUNT) }
|
unsafe { v8__Isolate__GetData(self, slot + Self::INTERNAL_SLOT_COUNT) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the maximum number of available embedder data slots. Valid slots
|
/// Returns the maximum number of available embedder data slots. Valid slots
|
||||||
/// are in the range of 0 - `Isolate::get_number_of_data_slots() - 1`.
|
/// are in the range of 0 - `Isolate::get_number_of_data_slots() - 1`.
|
||||||
#[allow(dead_code)]
|
pub fn get_number_of_data_slots(&self) -> u32 {
|
||||||
fn get_number_of_data_slots(&self) -> u32 {
|
|
||||||
unsafe {
|
unsafe {
|
||||||
v8__Isolate__GetNumberOfDataSlots(self) - Self::INTERNAL_SLOT_COUNT
|
v8__Isolate__GetNumberOfDataSlots(self) - Self::INTERNAL_SLOT_COUNT
|
||||||
}
|
}
|
||||||
|
|
|
@ -7487,6 +7487,60 @@ fn finalizer_on_kept_global() {
|
||||||
drop(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]
|
#[test]
|
||||||
fn host_create_shadow_realm_context_callback() {
|
fn host_create_shadow_realm_context_callback() {
|
||||||
let _setup_guard = setup();
|
let _setup_guard = setup();
|
||||||
|
|
Loading…
Reference in a new issue