rusty_v8/
platform.rs

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
53/// Returns a new instance of the default v8::Platform implementation.
54///
55/// |thread_pool_size| is the number of worker threads to allocate for
56/// background jobs. If a value of zero is passed, a suitable default
57/// based on the current number of processors online will be chosen.
58/// If |idle_task_support| is enabled then the platform will accept idle
59/// tasks (IdleTasksEnabled will return true) and will rely on the embedder
60/// calling v8::platform::RunIdleTasks to process the idle tasks.
61pub 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
68/// The same as new_default_platform() but disables the worker thread pool.
69/// It must be used with the --single-threaded V8 flag.
70///
71/// If |idle_task_support| is enabled then the platform will accept idle
72/// tasks (IdleTasksEnabled will return true) and will rely on the embedder
73/// calling v8::platform::RunIdleTasks to process the idle tasks.
74pub 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  /// Returns a new instance of the default v8::Platform implementation.
82  ///
83  /// |thread_pool_size| is the number of worker threads to allocate for
84  /// background jobs. If a value of zero is passed, a suitable default
85  /// based on the current number of processors online will be chosen.
86  /// If |idle_task_support| is enabled then the platform will accept idle
87  /// tasks (IdleTasksEnabled will return true) and will rely on the embedder
88  /// calling v8::platform::RunIdleTasks to process the idle tasks.
89  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  /// The same as new() but disables the worker thread pool.
102  /// It must be used with the --single-threaded V8 flag.
103  ///
104  /// If |idle_task_support| is enabled then the platform will accept idle
105  /// tasks (IdleTasksEnabled will return true) and will rely on the embedder
106  /// calling v8::platform::RunIdleTasks to process the idle tasks.
107  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  /// Pumps the message loop for the given isolate.
118  ///
119  /// The caller has to make sure that this is called from the right thread.
120  /// Returns true if a task was executed, and false otherwise. If the call to
121  /// PumpMessageLoop is nested within another call to PumpMessageLoop, only
122  /// nestable tasks may run. Otherwise, any task may run. Unless requested through
123  /// the |wait_for_work| parameter, this call does not block if no task is pending.
124  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  /// Runs pending idle tasks for at most |idle_time_in_seconds| seconds.
139  ///
140  /// The caller has to make sure that this is called from the right thread.
141  /// This call does not block if no task is pending.
142  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}