Skip to main content

sk_runtime/
lib.rs

1//! The JS host seam of Skathi: one JSC context (via redjs) on gpui's main thread, pumped once per
2//! frame, with a phase-gated bridge as the only way a JS host function may reach `&mut Window` / `&mut App`.
3//!
4//! Invariants:
5//! - One VM, one thread: VM thread == gpui main thread == Win32 UI thread.
6//! - The context is created once and never released (teardown fastfails in
7//!   this JSC prebuilt).
8//! - Every `extern "sysv64"` host function is written with [`host_fn!`] and is
9//!   therefore `catch_unwind`-shielded: a Rust panic becomes a JS exception
10//!   and poisons the runtime instead of aborting the process.
11//! - The uncaught-JS handler is a pure sink; errors are drained after each
12//!   pump, never acted on re-entrantly.
13
14pub mod app_devtools;
15pub mod bridge;
16pub mod frames;
17mod history;
18pub mod host;
19pub mod runtime;
20pub mod standins;
21pub mod testing;
22mod unsupported;
23
24pub use bridge::{with_app, with_commit, with_published, with_published_app, Phase};
25pub use runtime::{JsError, JsRuntime};
26
27pub use redjs;
28#[cfg(feature = "webgpu")]
29pub use redjs_webgpu;
30
31pub mod built {
32    pub const WEBGPU: bool = cfg!(feature = "webgpu");
33    pub const AUDIO: bool = cfg!(feature = "audio");
34    pub const STORAGES: bool = cfg!(feature = "storages");
35    pub const CLIPBOARD: bool = cfg!(feature = "clipboard");
36    pub const INDEXEDDB: bool = cfg!(feature = "indexeddb");
37}
38
39/// Defines a panic-shielded JSC host function, in redjs's host ABI: a panic becomes a thrown JS `Error`
40/// and poisons the runtime.
41#[macro_export]
42macro_rules! host_fn {
43    ($vis:vis fn $name:ident($g:ident, $args:ident) $body:block) => {
44        $crate::raw_host_fn! {
45        $vis fn $name(g, cf) {
46            match ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| {
47                let $g = g;
48                let _profile = $crate::redjs::profiling::host_span_at(g as usize, ::core::stringify!($name), ::core::file!(), ::core::line!(), ::core::column!());
49                let $args = unsafe { $crate::redjs::api::HostArgs::new(cf) };
50                let _ = &$args;
51                $body
52            })) {
53                Ok(v) => v,
54                Err(payload) => $crate::bridge::panic_to_js(g, payload),
55            }
56        }
57        }
58    };
59}
60
61/// A bare host function in redjs's ABI (`extern "sysv64"` on Windows x64, `extern "C"` elsewhere); no panic shield.
62#[macro_export]
63macro_rules! raw_host_fn {
64    ($vis:vis fn $name:ident($g:ident, $cf:ident) $body:block) => {
65        #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
66        $vis unsafe extern "sysv64" fn $name($g: *mut ::core::ffi::c_void, $cf: *mut ::core::ffi::c_void) -> u64 $body
67        #[cfg(not(all(target_os = "windows", target_arch = "x86_64")))]
68        $vis unsafe extern "C" fn $name($g: *mut ::core::ffi::c_void, $cf: *mut ::core::ffi::c_void) -> u64 $body
69    };
70}