use crate::Isolate;
use crate::isolate::RealIsolate;
use crate::support::int;
use crate::support::Opaque;
use crate::support::Shared;
use crate::support::SharedPtrBase;
use crate::support::SharedRef;
use crate::support::UniquePtr;
use crate::support::UniqueRef;
use crate::support::long;
unsafe extern "C" {
fn v8__Platform__NewDefaultPlatform(
thread_pool_size: int,
idle_task_support: bool,
) -> *mut Platform;
fn v8__Platform__NewUnprotectedDefaultPlatform(
thread_pool_size: int,
idle_task_support: bool,
) -> *mut Platform;
fn v8__Platform__NewSingleThreadedDefaultPlatform(
idle_task_support: bool,
) -> *mut Platform;
fn v8__Platform__NewCustomPlatform(
thread_pool_size: int,
idle_task_support: bool,
unprotected: bool,
context: *mut std::ffi::c_void,
) -> *mut Platform;
fn v8__Platform__DELETE(this: *mut Platform);
fn v8__Platform__PumpMessageLoop(
platform: *mut Platform,
isolate: *mut RealIsolate,
wait_for_work: bool,
) -> bool;
fn v8__Platform__RunIdleTasks(
platform: *mut Platform,
isolate: *mut RealIsolate,
idle_time_in_seconds: f64,
);
fn v8__Platform__NotifyIsolateShutdown(
platform: *mut Platform,
isolate: *mut RealIsolate,
);
fn std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr(
unique_ptr: UniquePtr<Platform>,
) -> SharedPtrBase<Platform>;
fn std__shared_ptr__v8__Platform__get(
ptr: *const SharedPtrBase<Platform>,
) -> *mut Platform;
fn std__shared_ptr__v8__Platform__COPY(
ptr: *const SharedPtrBase<Platform>,
) -> SharedPtrBase<Platform>;
fn std__shared_ptr__v8__Platform__reset(ptr: *mut SharedPtrBase<Platform>);
fn std__shared_ptr__v8__Platform__use_count(
ptr: *const SharedPtrBase<Platform>,
) -> long;
}
#[repr(C)]
#[derive(Debug)]
pub struct Platform(Opaque);
#[allow(unused_variables)]
pub trait PlatformImpl: Send + Sync {
fn post_task(&self, isolate_ptr: *mut std::ffi::c_void) {}
fn post_non_nestable_task(&self, isolate_ptr: *mut std::ffi::c_void) {}
fn post_delayed_task(
&self,
isolate_ptr: *mut std::ffi::c_void,
delay_in_seconds: f64,
) {
}
fn post_non_nestable_delayed_task(
&self,
isolate_ptr: *mut std::ffi::c_void,
delay_in_seconds: f64,
) {
}
fn post_idle_task(&self, isolate_ptr: *mut std::ffi::c_void) {}
}
#[unsafe(no_mangle)]
unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostTask(
context: *mut std::ffi::c_void,
isolate: *mut std::ffi::c_void,
) {
let imp = unsafe { &*(context as *const Box<dyn PlatformImpl>) };
imp.post_task(isolate);
}
#[unsafe(no_mangle)]
unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostNonNestableTask(
context: *mut std::ffi::c_void,
isolate: *mut std::ffi::c_void,
) {
let imp = unsafe { &*(context as *const Box<dyn PlatformImpl>) };
imp.post_non_nestable_task(isolate);
}
#[unsafe(no_mangle)]
unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostDelayedTask(
context: *mut std::ffi::c_void,
isolate: *mut std::ffi::c_void,
delay_in_seconds: f64,
) {
let imp = unsafe { &*(context as *const Box<dyn PlatformImpl>) };
imp.post_delayed_task(isolate, delay_in_seconds);
}
#[unsafe(no_mangle)]
unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostNonNestableDelayedTask(
context: *mut std::ffi::c_void,
isolate: *mut std::ffi::c_void,
delay_in_seconds: f64,
) {
let imp = unsafe { &*(context as *const Box<dyn PlatformImpl>) };
imp.post_non_nestable_delayed_task(isolate, delay_in_seconds);
}
#[unsafe(no_mangle)]
unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostIdleTask(
context: *mut std::ffi::c_void,
isolate: *mut std::ffi::c_void,
) {
let imp = unsafe { &*(context as *const Box<dyn PlatformImpl>) };
imp.post_idle_task(isolate);
}
#[unsafe(no_mangle)]
unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__DROP(
context: *mut std::ffi::c_void,
) {
unsafe {
let _ = Box::from_raw(context as *mut Box<dyn PlatformImpl>);
}
}
#[inline(always)]
pub fn new_default_platform(
thread_pool_size: u32,
idle_task_support: bool,
) -> UniqueRef<Platform> {
Platform::new(thread_pool_size, idle_task_support)
}
#[inline(always)]
pub fn new_unprotected_default_platform(
thread_pool_size: u32,
idle_task_support: bool,
) -> UniqueRef<Platform> {
Platform::new_unprotected(thread_pool_size, idle_task_support)
}
#[inline(always)]
pub fn new_single_threaded_default_platform(
idle_task_support: bool,
) -> UniqueRef<Platform> {
Platform::new_single_threaded(idle_task_support)
}
#[inline(always)]
pub fn new_custom_platform(
thread_pool_size: u32,
idle_task_support: bool,
unprotected: bool,
platform_impl: impl PlatformImpl + 'static,
) -> UniqueRef<Platform> {
Platform::new_custom(
thread_pool_size,
idle_task_support,
unprotected,
platform_impl,
)
}
impl Platform {
#[inline(always)]
pub fn new(
thread_pool_size: u32,
idle_task_support: bool,
) -> UniqueRef<Self> {
unsafe {
UniqueRef::from_raw(v8__Platform__NewDefaultPlatform(
thread_pool_size.min(16) as i32,
idle_task_support,
))
}
}
#[inline(always)]
pub fn new_unprotected(
thread_pool_size: u32,
idle_task_support: bool,
) -> UniqueRef<Self> {
unsafe {
UniqueRef::from_raw(v8__Platform__NewUnprotectedDefaultPlatform(
thread_pool_size.min(16) as i32,
idle_task_support,
))
}
}
#[inline(always)]
pub fn new_single_threaded(idle_task_support: bool) -> UniqueRef<Self> {
unsafe {
UniqueRef::from_raw(v8__Platform__NewSingleThreadedDefaultPlatform(
idle_task_support,
))
}
}
#[inline(always)]
pub fn new_custom(
thread_pool_size: u32,
idle_task_support: bool,
unprotected: bool,
platform_impl: impl PlatformImpl + 'static,
) -> UniqueRef<Self> {
let boxed: Box<dyn PlatformImpl> = Box::new(platform_impl);
let context = Box::into_raw(Box::new(boxed)) as *mut std::ffi::c_void;
unsafe {
UniqueRef::from_raw(v8__Platform__NewCustomPlatform(
thread_pool_size as i32,
idle_task_support,
unprotected,
context,
))
}
}
}
impl Platform {
#[inline(always)]
pub fn pump_message_loop(
platform: &SharedRef<Self>,
isolate: &Isolate,
wait_for_work: bool,
) -> bool {
unsafe {
v8__Platform__PumpMessageLoop(
&**platform as *const Self as *mut _,
isolate.as_real_ptr(),
wait_for_work,
)
}
}
#[inline(always)]
pub fn run_idle_tasks(
platform: &SharedRef<Self>,
isolate: &Isolate,
idle_time_in_seconds: f64,
) {
unsafe {
v8__Platform__RunIdleTasks(
&**platform as *const Self as *mut _,
isolate.as_real_ptr(),
idle_time_in_seconds,
);
}
}
#[inline(always)]
pub(crate) unsafe fn notify_isolate_shutdown(
platform: &SharedRef<Self>,
isolate: &Isolate,
) {
unsafe {
v8__Platform__NotifyIsolateShutdown(
&**platform as *const Self as *mut _,
isolate.as_real_ptr(),
);
}
}
}
impl Shared for Platform {
fn from_unique_ptr(unique_ptr: UniquePtr<Self>) -> SharedPtrBase<Self> {
unsafe {
std__shared_ptr__v8__Platform__CONVERT__std__unique_ptr(unique_ptr)
}
}
fn get(ptr: &SharedPtrBase<Self>) -> *const Self {
unsafe { std__shared_ptr__v8__Platform__get(ptr) }
}
fn clone(ptr: &SharedPtrBase<Self>) -> SharedPtrBase<Self> {
unsafe { std__shared_ptr__v8__Platform__COPY(ptr) }
}
fn reset(ptr: &mut SharedPtrBase<Self>) {
unsafe { std__shared_ptr__v8__Platform__reset(ptr) }
}
fn use_count(ptr: &SharedPtrBase<Self>) -> long {
unsafe { std__shared_ptr__v8__Platform__use_count(ptr) }
}
}
impl Drop for Platform {
fn drop(&mut self) {
unsafe { v8__Platform__DELETE(self) };
}
}