0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-25 00:29:14 -05:00

Fix get_slot and set_slot (#281)

This commit is contained in:
Ryan Dahl 2020-02-13 15:03:25 -05:00 committed by GitHub
parent 3dacbd396e
commit 554f06f6bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 4 deletions

View file

@ -166,6 +166,14 @@ impl Isolate {
IsolateHandle::new(self) IsolateHandle::new(self)
} }
unsafe fn set_annex(&mut self, ptr: *mut IsolateAnnex) {
v8__Isolate__SetData(self, 0, ptr as *mut c_void)
}
fn get_annex(&self) -> *mut IsolateAnnex {
unsafe { v8__Isolate__GetData(self, 0) as *mut _ }
}
/// 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 GetNumberOfDataSlots() - 1. /// between 0 and GetNumberOfDataSlots() - 1.
pub 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) {
@ -318,12 +326,12 @@ impl IsolateHandle {
} }
pub(crate) fn new(isolate: &mut Isolate) -> Self { pub(crate) fn new(isolate: &mut Isolate) -> Self {
let annex_ptr = isolate.get_data(0) as *mut IsolateAnnex; let annex_ptr = isolate.get_annex();
if annex_ptr.is_null() { if annex_ptr.is_null() {
let annex_arc = Arc::new(IsolateAnnex::new(isolate)); let annex_arc = Arc::new(IsolateAnnex::new(isolate));
let annex_ptr = Arc::into_raw(annex_arc.clone()); let annex_ptr = Arc::into_raw(annex_arc.clone());
unsafe { unsafe {
isolate.set_data(0, annex_ptr as *mut c_void); isolate.set_annex(annex_ptr as *mut IsolateAnnex);
} }
IsolateHandle(annex_arc) IsolateHandle(annex_arc)
} else { } else {
@ -334,7 +342,7 @@ impl IsolateHandle {
} }
fn dispose_isolate(isolate: &mut Isolate) { fn dispose_isolate(isolate: &mut Isolate) {
let annex_ptr = isolate.get_data(0) as *mut IsolateAnnex; let annex_ptr = isolate.get_annex();
if !annex_ptr.is_null() { if !annex_ptr.is_null() {
unsafe { unsafe {
{ {

View file

@ -406,7 +406,7 @@ fn v8_str<'sc>(
fn eval<'sc>( fn eval<'sc>(
scope: &mut impl v8::ToLocal<'sc>, scope: &mut impl v8::ToLocal<'sc>,
context: v8::Local<v8::Context>, context: v8::Local<v8::Context>,
code: &'static str, code: &str,
) -> Option<v8::Local<'sc, v8::Value>> { ) -> Option<v8::Local<'sc, v8::Value>> {
let mut hs = v8::EscapableHandleScope::new(scope); let mut hs = v8::EscapableHandleScope::new(scope);
let scope = hs.enter(); let scope = hs.enter();
@ -2542,3 +2542,21 @@ fn context_from_object_template() {
assert!(expected.strict_equals(actual)); assert!(expected.strict_equals(actual));
} }
} }
#[test]
fn get_and_set_data() {
let _setup_guard = setup();
let mut params = v8::Isolate::create_params();
params.set_array_buffer_allocator(v8::new_default_allocator());
let mut isolate = v8::Isolate::new(params);
let nslots = isolate.get_number_of_data_slots();
assert!(nslots > 0);
for slot in 0..nslots {
let b = Box::new(123 as i32);
let ptr = Box::into_raw(b);
unsafe { isolate.set_data(slot, ptr as *mut std::ffi::c_void) };
let ptr = isolate.get_data(slot) as *mut i32;
let b = unsafe { Box::from_raw(ptr) };
assert_eq!(*b, 123);
}
}