From 554f06f6bca81a357fb91cab2fb683bfcf6421ca Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 13 Feb 2020 15:03:25 -0500 Subject: [PATCH] Fix get_slot and set_slot (#281) --- src/isolate.rs | 14 +++++++++++--- tests/test_api.rs | 20 +++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/isolate.rs b/src/isolate.rs index d4215574..6c5a9893 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -166,6 +166,14 @@ impl Isolate { 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 /// between 0 and GetNumberOfDataSlots() - 1. 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 { - let annex_ptr = isolate.get_data(0) as *mut IsolateAnnex; + let annex_ptr = isolate.get_annex(); if annex_ptr.is_null() { let annex_arc = Arc::new(IsolateAnnex::new(isolate)); let annex_ptr = Arc::into_raw(annex_arc.clone()); unsafe { - isolate.set_data(0, annex_ptr as *mut c_void); + isolate.set_annex(annex_ptr as *mut IsolateAnnex); } IsolateHandle(annex_arc) } else { @@ -334,7 +342,7 @@ impl IsolateHandle { } 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() { unsafe { { diff --git a/tests/test_api.rs b/tests/test_api.rs index 900c64ec..ba498b7c 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -406,7 +406,7 @@ fn v8_str<'sc>( fn eval<'sc>( scope: &mut impl v8::ToLocal<'sc>, context: v8::Local, - code: &'static str, + code: &str, ) -> Option> { let mut hs = v8::EscapableHandleScope::new(scope); let scope = hs.enter(); @@ -2542,3 +2542,21 @@ fn context_from_object_template() { 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); + } +}