From 5b927b82af73ffec089ac1e09795a90df62055f7 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Tue, 22 Oct 2019 15:46:42 -0700 Subject: [PATCH] wip --- src/main.rs | 46 +----------------------------- src/support.rs | 2 +- src/v8/platform/task.rs | 63 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index 62e10490..75a26c8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ mod v8; mod example { use crate::support::UniquePtr; use crate::v8::inspector::channel::*; - use crate::v8::platform::task::*; use crate::v8::*; pub struct TestChannel { @@ -47,43 +46,9 @@ mod example { } } } - - pub struct TestTask { - a: i32, - base: TaskBase, - b: i32, - } - - impl TaskImpl for TestTask { - fn base(&self) -> &TaskBase { - &self.base - } - fn base_mut(&mut self) -> &mut TaskBase { - &mut self.base - } - fn Run(&mut self) -> () { - println!("TestTask::Run {} {}", self.a, self.b); - } - } - - impl TestTask { - pub fn new() -> Self { - Self { - base: TaskBase::new::(), - a: 2, - b: 3, - } - } - } - - impl Drop for TestTask { - fn drop(&mut self) { - println!("TestTask::drop()"); - } - } } -fn main1() { +fn main() { use crate::v8::inspector::channel::*; use crate::v8::*; use example::*; @@ -94,12 +59,3 @@ fn main1() { let message = StringBuffer::create(&message); chan.sendResponse(3, message); } - -fn main() { - use crate::v8::platform::task::*; - use example::*; - let mut v = TestTask::new(); - v.Run(); - let b = Box::new(v); - b.into_unique_ptr(); -} diff --git a/src/support.rs b/src/support.rs index 7d3e661b..9edc0e33 100644 --- a/src/support.rs +++ b/src/support.rs @@ -94,7 +94,7 @@ impl FieldOffset { let field_addr = field_ptr as usize; assert!(field_addr >= embedder_addr); assert!((field_addr + size_of::()) <= (embedder_addr + size_of::())); - Self(embedder_addr - field_addr, PhantomData) + Self(field_addr - embedder_addr, PhantomData) } pub unsafe fn to_embedder(self, field: &F) -> &E { diff --git a/src/v8/platform/task.rs b/src/v8/platform/task.rs index 534509ac..b0c01d9a 100644 --- a/src/v8/platform/task.rs +++ b/src/v8/platform/task.rs @@ -160,3 +160,66 @@ impl TaskBase { std::mem::transmute(Self::dispatch_mut(task)) } } + +mod tests { + use super::*; + use std::sync::atomic::AtomicUsize; + use std::sync::atomic::Ordering::SeqCst; + + static RUN_COUNT: AtomicUsize = AtomicUsize::new(0); + static DROP_COUNT: AtomicUsize = AtomicUsize::new(0); + + // Using repr(C) to preserve field ordering and test that everything works + // when the TaskBase field is not the first element of the struct. + #[repr(C)] + pub struct TestTask { + f1: i32, + base: TaskBase, + f2: f64, + } + + impl TestTask { + pub fn new() -> Self { + Self { + base: TaskBase::new::(), + f1: 42, + f2: 4.2, + } + } + } + + impl TaskImpl for TestTask { + fn base(&self) -> &TaskBase { + &self.base + } + fn base_mut(&mut self) -> &mut TaskBase { + &mut self.base + } + fn Run(&mut self) -> () { + RUN_COUNT.fetch_add(1, SeqCst); + } + } + + impl Drop for TestTask { + fn drop(&mut self) { + DROP_COUNT.fetch_add(1, SeqCst); + } + } + + #[test] + fn test_v8_platform_task() { + { + let mut v = TestTask::new(); + v.Run(); + } + assert_eq!(RUN_COUNT.swap(0, SeqCst), 1); + assert_eq!(DROP_COUNT.swap(0, SeqCst), 1); + + { + let b = Box::new(TestTask::new()); + b.into_unique_ptr(); + } + assert_eq!(RUN_COUNT.swap(0, SeqCst), 0); + assert_eq!(DROP_COUNT.swap(0, SeqCst), 1); + } +}