1use crate::support::int;
2use crate::Isolate;
3
4use crate::support::long;
5use crate::support::Opaque;
6use crate::support::Shared;
7use crate::support::SharedPtrBase;
8use crate::support::SharedRef;
9use crate::support::UniquePtr;
10use crate::support::UniqueRef;
11
12extern "C" {
13 fn v8__Platform__NewDefaultPlatform(
14 thread_pool_size: int,
15 idle_task_support: bool,
16 ) -> *mut Platform;
17 fn v8__Platform__NewSingleThreadedDefaultPlatform(
18 idle_task_support: bool,
19 ) -> *mut Platform;
20 fn v8__Platform__DELETE(this: *mut Platform);
21
22 fn v8__Platform__PumpMessageLoop(
23 platform: *mut Platform,
24 isolate: *mut Isolate,
25 wait_for_work: bool,
26 ) -> bool;
27
28 fn v8__Platform__RunIdleTasks(
29 platform: *mut Platform,
30 isolate: *mut Isolate,
31 idle_time_in_seconds: f64,
32 );
33
34 fn std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr(
35 unique_ptr: UniquePtr<Platform>,
36 ) -> SharedPtrBase<Platform>;
37 fn std__shared_ptr__v8__Platform__get(
38 ptr: *const SharedPtrBase<Platform>,
39 ) -> *mut Platform;
40 fn std__shared_ptr__v8__Platform__COPY(
41 ptr: *const SharedPtrBase<Platform>,
42 ) -> SharedPtrBase<Platform>;
43 fn std__shared_ptr__v8__Platform__reset(ptr: *mut SharedPtrBase<Platform>);
44 fn std__shared_ptr__v8__Platform__use_count(
45 ptr: *const SharedPtrBase<Platform>,
46 ) -> long;
47}
48
49#[repr(C)]
50#[derive(Debug)]
51pub struct Platform(Opaque);
52
53pub fn new_default_platform(
62 thread_pool_size: u32,
63 idle_task_support: bool,
64) -> UniqueRef<Platform> {
65 Platform::new(thread_pool_size, idle_task_support)
66}
67
68pub fn new_single_threaded_default_platform(
75 idle_task_support: bool,
76) -> UniqueRef<Platform> {
77 Platform::new_single_threaded(idle_task_support)
78}
79
80impl Platform {
81 pub fn new(
90 thread_pool_size: u32,
91 idle_task_support: bool,
92 ) -> UniqueRef<Self> {
93 unsafe {
94 UniqueRef::from_raw(v8__Platform__NewDefaultPlatform(
95 thread_pool_size.min(16) as i32,
96 idle_task_support,
97 ))
98 }
99 }
100
101 pub fn new_single_threaded(idle_task_support: bool) -> UniqueRef<Self> {
108 unsafe {
109 UniqueRef::from_raw(v8__Platform__NewSingleThreadedDefaultPlatform(
110 idle_task_support,
111 ))
112 }
113 }
114}
115
116impl Platform {
117 pub fn pump_message_loop(
125 platform: &SharedRef<Self>,
126 isolate: &mut Isolate,
127 wait_for_work: bool,
128 ) -> bool {
129 unsafe {
130 v8__Platform__PumpMessageLoop(
131 &**platform as *const Self as *mut _,
132 isolate,
133 wait_for_work,
134 )
135 }
136 }
137
138 pub fn run_idle_tasks(
143 platform: &SharedRef<Self>,
144 isolate: &mut Isolate,
145 idle_time_in_seconds: f64,
146 ) {
147 unsafe {
148 v8__Platform__RunIdleTasks(
149 &**platform as *const Self as *mut _,
150 isolate,
151 idle_time_in_seconds,
152 )
153 }
154 }
155}
156
157impl Shared for Platform {
158 fn from_unique_ptr(unique_ptr: UniquePtr<Self>) -> SharedPtrBase<Self> {
159 unsafe {
160 std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr(unique_ptr)
161 }
162 }
163 fn get(ptr: &SharedPtrBase<Self>) -> *const Self {
164 unsafe { std__shared_ptr__v8__Platform__get(ptr) }
165 }
166 fn clone(ptr: &SharedPtrBase<Self>) -> SharedPtrBase<Self> {
167 unsafe { std__shared_ptr__v8__Platform__COPY(ptr) }
168 }
169 fn reset(ptr: &mut SharedPtrBase<Self>) {
170 unsafe { std__shared_ptr__v8__Platform__reset(ptr) }
171 }
172 fn use_count(ptr: &SharedPtrBase<Self>) -> long {
173 unsafe { std__shared_ptr__v8__Platform__use_count(ptr) }
174 }
175}
176
177impl Drop for Platform {
178 fn drop(&mut self) {
179 unsafe { v8__Platform__DELETE(self) };
180 }
181}