use std::collections::{HashMap, VecDeque};
use std::ffi::c_void;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex, OnceLock};
use std::time::Duration;
use sk_runtime::redjs::api::Value;
use sk_runtime::{host_fn, JsRuntime};
mod host_context;
pub use host_context::{HostContext, HostCookie, SameSite};
type Setup = Box<dyn FnOnce(&HostContext) -> Result<(), String> + Send + 'static>;
type PrepareDataDir = Box<dyn FnOnce(&Path) -> Result<(), String> + Send + 'static>;
#[derive(Default)]
struct SetupHook(Mutex<Option<Setup>>);
impl SetupHook {
fn run(&self, context: &HostContext) -> Result<(), String> {
let setup = self
.0
.lock()
.map_err(|_| "host setup lock was poisoned".to_string())?
.take();
let Some(setup) = setup else { return Ok(()) };
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| setup(context)))
.unwrap_or_else(|_| Err("host setup panicked".into()))
}
}
#[cfg(not(target_os = "macos"))]
const STACK: usize = 8 * 1024 * 1024;
const DRAIN_CAP: usize = 16;
pub type Handler = Arc<dyn Fn(Call) + Send + Sync + 'static>;
struct Config {
data_dir: Option<PathBuf>,
#[cfg(feature = "storages")]
storage_encrypted: Option<bool>,
visible: bool,
desktop_launch: bool,
setup: SetupHook,
functions: HashMap<String, Handler>,
activate: bool,
#[cfg_attr(not(sk_devtools), allow(dead_code))]
inspector_exe: Option<PathBuf>,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
desktop_id: Option<String>,
}
static CONFIG: OnceLock<Config> = OnceLock::new();
static STARTED: AtomicBool = AtomicBool::new(false);
pub(crate) fn active() -> bool {
CONFIG.get().is_some()
}
pub(crate) fn desktop_launch_prepared() -> bool {
CONFIG.get().is_some_and(|config| config.desktop_launch)
}
pub(crate) fn activates() -> bool {
CONFIG
.get()
.is_none_or(|c| requests_activation(c.activate, c.visible))
}
pub(crate) fn visible() -> bool {
CONFIG.get().is_none_or(|c| c.visible)
}
pub fn encrypt_local_storage_at(directory: &Path) -> Result<bool, String> {
#[cfg(feature = "storages")]
{
sk_storage::encrypt_local_storage_at(directory)
}
#[cfg(not(feature = "storages"))]
{
let _ = directory;
Err("enable the storages feature to encrypt localStorage".into())
}
}
#[cfg(feature = "storages")]
pub(crate) fn storage_encrypted() -> Option<bool> {
CONFIG.get()?.storage_encrypted
}
pub(crate) fn data_dir() -> Option<&'static Path> {
CONFIG.get()?.data_dir.as_deref()
}
fn resolve_data_dir(
configured: Option<PathBuf>,
override_dir: Option<std::ffi::OsString>,
) -> Result<Option<PathBuf>, Error> {
let directory = override_dir
.filter(|path| !path.is_empty())
.map(PathBuf::from)
.or(configured);
if let Some(path) = &directory {
if !path.is_absolute() {
return Err(Error::DataDirectory(format!(
"data directory must be absolute: {}",
path.display()
)));
}
}
Ok(directory)
}
fn prepare_directory(
directory: Option<&Path>,
prepare: Option<PrepareDataDir>,
) -> Result<(), Error> {
let Some(directory) = directory else {
return if prepare.is_some() {
Err(Error::DataDirectory(
"prepare_data_dir requires a data directory".into(),
))
} else {
Ok(())
};
};
if let Some(prepare) = prepare {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| prepare(directory)))
.unwrap_or_else(|_| Err("data directory preparation panicked".into()))
.map_err(Error::DataDirectory)?;
}
std::fs::create_dir_all(directory).map_err(|error| {
Error::DataDirectory(format!("could not create {}: {error}", directory.display()))
})
}
fn requests_activation(activate: bool, visible: bool) -> bool {
activate && visible
}
#[cfg_attr(not(sk_devtools), allow(dead_code))]
pub(crate) fn inspector_exe() -> Option<PathBuf> {
CONFIG.get()?.inspector_exe.clone()
}
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub(crate) fn desktop_id() -> Option<String> {
CONFIG.get()?.desktop_id.clone()
}
#[cfg_attr(not(feature = "prod"), allow(dead_code))]
enum PayloadSource {
File(PathBuf),
Bytes(Vec<u8>),
Static(&'static [u8]),
}
#[cfg(any(feature = "prod", test))]
impl PayloadSource {
fn load(self) -> Result<(crate::payload::PayloadBytes, String, Option<PathBuf>), Error> {
use crate::payload::PayloadBytes;
match self {
Self::File(path) => {
let file = crate::mapped_file::MappedFile::open(&path)
.map_err(|e| Error::Payload(format!("{}: {e}", path.display())))?;
let dir = path.parent().map(Path::to_path_buf);
Ok((PayloadBytes::File(file), path.display().to_string(), dir))
}
Self::Bytes(bytes) => Ok((PayloadBytes::Owned(bytes), "bytes".into(), None)),
Self::Static(bytes) => Ok((PayloadBytes::Embedded(bytes), "static bytes".into(), None)),
}
}
}
#[derive(Debug)]
pub enum Error {
AlreadyStarted,
NoPayload,
PayloadNeedsProd,
Payload(String),
ArgsAlreadyRead,
LaunchExit(i32),
DataDirectory(String),
Spawn(std::io::Error),
MainThreadRequired,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::AlreadyStarted => write!(f, "Skathi already ran in this process; it runs once"),
Error::NoPayload => write!(f, "no app: give the builder a payload (sk build writes dist/.sk/app.skpk)"),
Error::PayloadNeedsProd => {
write!(f, "a payload needs sk-app's `prod` feature; a dev build boots the dev dist instead")
}
Error::Payload(e) => write!(f, "{e}"),
Error::ArgsAlreadyRead => write!(f, "Skathi's arguments were read before spawn"),
Error::LaunchExit(code) => write!(f, "desktop launch finished with exit code {code}"),
Error::DataDirectory(e) => write!(f, "data directory: {e}"),
Error::Spawn(e) => write!(f, "the Skathi thread did not start: {e}"),
Error::MainThreadRequired => write!(f, "AppKit requires the process main thread; call Builder::run_on_main_thread from main"),
}
}
}
impl std::error::Error for Error {}
pub struct Builder {
data_dir: Option<PathBuf>,
storage_encrypted: Option<bool>,
prepare_data_dir: Option<PrepareDataDir>,
visible: bool,
desktop_launch: bool,
setup: Option<Setup>,
payload: Option<PayloadSource>,
args: Vec<String>,
functions: HashMap<String, Handler>,
activate: bool,
inspector_exe: Option<PathBuf>,
desktop_id: Option<String>,
}
impl Default for Builder {
fn default() -> Self {
Self::new()
}
}
impl Builder {
pub fn new() -> Self {
Builder {
data_dir: None,
storage_encrypted: None,
prepare_data_dir: None,
visible: true,
desktop_launch: false,
setup: None,
payload: None,
args: Vec::new(),
functions: HashMap::new(),
activate: true,
inspector_exe: None,
desktop_id: None,
}
}
pub fn data_dir(mut self, directory: impl Into<PathBuf>) -> Self {
self.data_dir = Some(directory.into());
self
}
pub fn storage_encrypted(mut self, encrypted: bool) -> Self {
self.storage_encrypted = Some(encrypted);
self
}
pub fn prepare_data_dir<F>(mut self, prepare: F) -> Self
where
F: FnOnce(&Path) -> Result<(), String> + Send + 'static,
{
self.prepare_data_dir = Some(Box::new(prepare));
self
}
pub fn desktop_launch(mut self, enabled: bool) -> Self {
self.desktop_launch = enabled;
self
}
pub fn setup<F>(mut self, setup: F) -> Self
where
F: FnOnce(&HostContext) -> Result<(), String> + Send + 'static,
{
self.setup = Some(Box::new(setup));
self
}
pub fn payload_file(mut self, path: impl Into<PathBuf>) -> Self {
self.payload = Some(PayloadSource::File(path.into()));
self
}
pub fn payload_bytes(mut self, bytes: impl Into<Vec<u8>>) -> Self {
self.payload = Some(PayloadSource::Bytes(bytes.into()));
self
}
pub fn payload_static(mut self, bytes: &'static [u8]) -> Self {
self.payload = Some(PayloadSource::Static(bytes));
self
}
pub fn args<I, S>(mut self, args: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.args = args.into_iter().map(Into::into).collect();
self
}
pub fn function<F>(mut self, name: impl Into<String>, handler: F) -> Self
where
F: Fn(Call) + Send + Sync + 'static,
{
self.functions.insert(name.into(), Arc::new(handler));
self
}
pub fn visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
pub fn activate(mut self, on: bool) -> Self {
self.activate = on;
self
}
pub fn inspector_exe(mut self, path: impl Into<PathBuf>) -> Self {
self.inspector_exe = Some(path.into());
self
}
pub fn desktop_id(mut self, id: impl Into<String>) -> Self {
self.desktop_id = Some(id.into());
self
}
pub fn spawn(self) -> Result<Handle, Error> {
#[cfg(target_os = "macos")]
{
Err(Error::MainThreadRequired)
}
#[cfg(not(target_os = "macos"))]
{
let handle = self.prepare()?;
std::thread::Builder::new()
.name("skathi".into())
.stack_size(STACK)
.spawn(run_thread)
.map_err(Error::Spawn)?;
Ok(handle)
}
}
#[cfg(target_os = "macos")]
pub fn run_on_main_thread<F>(self, on_exit: F) -> Result<(), Error>
where
F: FnOnce(Exit) -> i32 + Send + 'static,
{
require_main_thread()?;
let handle = self.prepare()?;
std::thread::Builder::new()
.name("skathi-lifecycle".into())
.spawn(move || {
let exit = handle.wait();
let code = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| on_exit(exit)))
.unwrap_or_else(|_| {
eprintln!("[embed] the application shutdown callback panicked");
1
});
std::process::exit(code);
})
.map_err(Error::Spawn)?;
run_thread();
Ok(())
}
fn prepare(mut self) -> Result<Handle, Error> {
let data_dir = resolve_data_dir(self.data_dir.take(), std::env::var_os("SK_DATA_DIR"))?;
#[cfg(not(feature = "storages"))]
if self.storage_encrypted.is_some() {
return Err(Error::DataDirectory(
"storage_encrypted requires the storages feature".into(),
));
}
if STARTED.swap(true, Ordering::SeqCst) {
return Err(Error::AlreadyStarted);
}
if let Err(e) = self.load_payload() {
STARTED.store(false, Ordering::SeqCst);
return Err(e);
}
if self.desktop_launch {
let args = sk_desktop::args::process();
let launch = crate::boot_mode::launch_config();
if let sk_desktop::launch::Outcome::Exit(code) =
sk_desktop::launch::start_with_activation(
args,
launch.as_deref(),
crate::boot_mode::app_name(),
None,
requests_activation(self.activate, self.visible),
)
{
return Err(Error::LaunchExit(code));
}
sk_desktop::launch::register_declared();
} else {
let exe = std::env::current_exe()
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default();
if !sk_desktop::args::set_embedded(exe, self.args) {
return Err(Error::ArgsAlreadyRead);
}
}
prepare_directory(data_dir.as_deref(), self.prepare_data_dir.take())?;
let config = Config {
data_dir,
#[cfg(feature = "storages")]
storage_encrypted: self.storage_encrypted,
visible: self.visible,
desktop_launch: self.desktop_launch,
setup: SetupHook(Mutex::new(self.setup)),
functions: self.functions,
activate: self.activate,
inspector_exe: self.inspector_exe,
desktop_id: self.desktop_id,
};
if CONFIG.set(config).is_err() {
return Err(Error::AlreadyStarted);
}
Ok(Handle { _private: () })
}
#[cfg(feature = "prod")]
fn load_payload(&mut self) -> Result<(), Error> {
let Some(source) = self.payload.take() else {
return if crate::payload::get().is_some() {
Ok(())
} else {
Err(Error::NoPayload)
};
};
let (bytes, label, dir) = source.load()?;
crate::payload::set_external(bytes, label, dir.as_deref()).map_err(Error::Payload)
}
#[cfg(not(feature = "prod"))]
fn load_payload(&self) -> Result<(), Error> {
match self.payload {
Some(_) => Err(Error::PayloadNeedsProd),
None => Ok(()),
}
}
}
#[cfg(target_os = "macos")]
fn require_main_thread() -> Result<(), Error> {
unsafe extern "C" {
fn pthread_main_np() -> std::ffi::c_int;
}
if unsafe { pthread_main_np() } == 0 {
Err(Error::MainThreadRequired)
} else {
Ok(())
}
}
fn run_thread() {
if let Err(p) = std::panic::catch_unwind(crate::run_embedded) {
let why = p
.downcast_ref::<&str>()
.map(|s| s.to_string())
.or_else(|| p.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "a panic".into());
eprintln!("[embed] the Skathi thread panicked: {why}");
finished(false, format!("panicked: {why}"));
}
loop {
std::thread::park();
}
}
#[derive(Debug, Clone)]
pub struct Exit {
pub clean: bool,
pub summary: String,
}
static EXIT: Mutex<Option<Exit>> = Mutex::new(None);
static EXIT_CV: Condvar = Condvar::new();
static CLOSE: AtomicBool = AtomicBool::new(false);
static SHOW: AtomicBool = AtomicBool::new(false);
pub(crate) fn finished(clean: bool, summary: String) {
let Ok(mut exit) = EXIT.lock() else { return };
if exit.is_none() {
*exit = Some(Exit { clean, summary });
EXIT_CV.notify_all();
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum EmitError {
NotJson,
Closed,
}
impl std::fmt::Display for EmitError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EmitError::NotJson => write!(f, "the event's data is not JSON"),
EmitError::Closed => write!(f, "the app has closed"),
}
}
}
impl std::error::Error for EmitError {}
#[derive(Clone)]
pub struct Handle {
_private: (),
}
impl Handle {
pub fn emit(&self, event: &str, data: &str) -> Result<(), EmitError> {
if self.is_closed() {
return Err(EmitError::Closed);
}
if crate::json::parse(data).is_none() {
return Err(EmitError::NotJson);
}
let json = format!(
"{{\"event\":{},\"data\":{data}}}",
crate::json::quote(event)
);
push(Item::Event { json });
Ok(())
}
pub fn show(&self) -> bool {
if self.is_closed() || crate::shutdown::is_closing() || CLOSE.load(Ordering::Acquire) {
return false;
}
SHOW.store(true, Ordering::Release);
wake();
true
}
pub fn close(&self) {
CLOSE.store(true, Ordering::Release);
wake();
}
pub fn is_closed(&self) -> bool {
EXIT.lock().is_ok_and(|e| e.is_some())
}
pub fn wait(&self) -> Exit {
let mut exit = EXIT.lock().unwrap_or_else(|p| p.into_inner());
loop {
if let Some(e) = exit.as_ref() {
return e.clone();
}
exit = EXIT_CV.wait(exit).unwrap_or_else(|p| p.into_inner());
}
}
pub fn wait_timeout(&self, timeout: Duration) -> Option<Exit> {
let exit = EXIT.lock().unwrap_or_else(|p| p.into_inner());
let (exit, _) = EXIT_CV
.wait_timeout_while(exit, timeout, |e| e.is_none())
.unwrap_or_else(|p| p.into_inner());
exit.clone()
}
}
pub struct Call {
name: String,
args: String,
reply: Reply,
}
impl Call {
pub fn name(&self) -> &str {
&self.name
}
pub fn args(&self) -> &str {
&self.args
}
pub fn resolve(self, json: impl Into<String>) {
self.reply.resolve(json)
}
pub fn reject(self, message: impl Into<String>) {
self.reply.reject(message)
}
pub fn into_reply(self) -> Reply {
self.reply
}
}
pub struct Reply {
token: u32,
settled: bool,
}
impl Reply {
pub fn resolve(mut self, json: impl Into<String>) {
self.settled = true;
let json = json.into();
if crate::json::parse(&json).is_some() {
push(Item::Settle {
token: self.token,
ok: true,
text: json,
});
} else {
push(Item::Settle {
token: self.token,
ok: false,
text: "the host answered with text that is not JSON".into(),
});
}
}
pub fn reject(mut self, message: impl Into<String>) {
self.settled = true;
push(Item::Settle {
token: self.token,
ok: false,
text: message.into(),
});
}
}
impl Drop for Reply {
fn drop(&mut self) {
if self.settled {
return;
}
let why = if std::thread::panicking() {
"the host function panicked"
} else {
"the host dropped this call without answering"
};
push(Item::Settle {
token: self.token,
ok: false,
text: why.into(),
});
}
}
enum Item {
Settle { token: u32, ok: bool, text: String },
Event { json: String },
}
static QUEUE: Mutex<VecDeque<Item>> = Mutex::new(VecDeque::new());
static QUEUED: AtomicUsize = AtomicUsize::new(0);
static IN_FLIGHT: AtomicUsize = AtomicUsize::new(0);
static NEXT_TOKEN: AtomicU32 = AtomicU32::new(1);
static RT_G: AtomicUsize = AtomicUsize::new(0);
static RT_ID: AtomicU64 = AtomicU64::new(0);
thread_local! {
static DISPATCHER: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
fn push(item: Item) {
if let Ok(mut q) = QUEUE.lock() {
q.push_back(item);
QUEUED.fetch_add(1, Ordering::AcqRel);
}
wake();
}
fn wake() {
let id = RT_ID.load(Ordering::Acquire);
if id != 0 {
sk_runtime::redjs::web::wake(sk_runtime::redjs::context::ContextId(id));
}
sk_runtime::frames::wake();
}
fn is_ours(g: *mut c_void) -> bool {
!g.is_null() && g as usize == RT_G.load(Ordering::Acquire)
}
unsafe fn src_inflight(g: *mut c_void) -> bool {
is_ours(g) && IN_FLIGHT.load(Ordering::Acquire) != 0
}
unsafe fn src_ready(g: *mut c_void) -> bool {
is_ours(g) && QUEUED.load(Ordering::Acquire) != 0 && DISPATCHER.with(|d| d.get()) != 0
}
unsafe fn src_drain(g: *mut c_void) -> usize {
if !unsafe { src_ready(g) } {
return 0;
}
let budget = Duration::from_micros(sk_runtime::redjs::web::pump_budget_us() as u64);
let start = std::time::Instant::now();
let mut n = 0;
while n < DRAIN_CAP {
let Some(item) = QUEUE.lock().ok().and_then(|mut q| q.pop_front()) else {
break;
};
QUEUED.fetch_sub(1, Ordering::AcqRel);
match item {
Item::Settle { token, ok, text } => {
IN_FLIGHT.fetch_sub(1, Ordering::AcqRel);
deliver(token, ok, &text);
}
Item::Event { json } => deliver(0, true, &json),
}
n += 1;
if start.elapsed() >= budget {
break;
}
}
n
}
fn deliver(token: u32, ok: bool, text: &str) {
let dispatcher = DISPATCHER.with(|d| d.get());
if dispatcher == 0 {
return;
}
JsRuntime::with(|rt| {
let a = [
Value::number(f64::from(token)),
Value::boolean(ok),
rt.ctx().string(text),
];
if let Err(sk_runtime::redjs::api::Error::Exception(e)) =
rt.ctx().call(Value(dispatcher), Value::undefined(), &a)
{
let err = rt.describe(e);
eprintln!("[appHost dispatch] {}\n{}", err.header, err.stack);
}
});
}
pub(crate) fn setup(runtime: &JsRuntime) -> Result<(), String> {
let Some(config) = CONFIG.get() else {
return Ok(());
};
config.setup.run(&HostContext::installed(Some(runtime)))
}
#[cfg(target_os = "macos")]
pub(crate) fn reopen() {
let _accepted = Handle { _private: () }.show();
}
pub(crate) fn pump() {
let show = SHOW.swap(false, Ordering::AcqRel);
if CLOSE.swap(false, Ordering::AcqRel) {
sk_tree::log::record("embed", "[embed] the embedder closed the app");
crate::window::request_close();
} else if show && !crate::shutdown::is_closing() {
crate::window::request_activate();
}
}
host_fn!(fn sk_host_set_dispatcher(g, args) {
let f = args.arg(0);
unsafe { sk_runtime::redjs::ffi::redjs_protect(g, f.0) };
let old = DISPATCHER.with(|d| d.replace(f.0));
if old != 0 {
unsafe { sk_runtime::redjs::ffi::redjs_unprotect(g, old) };
}
wake();
Value::undefined().0
});
host_fn!(fn sk_host_call(g, args) {
let name = unsafe { sk_runtime::redjs::webapi::util::read_string(g, args.arg(0).0) }.unwrap_or_default();
let call_args = unsafe { sk_runtime::redjs::webapi::util::read_string(g, args.arg(1).0) }
.filter(|a| crate::json::parse(a).is_some())
.unwrap_or_else(|| "[]".into());
let Some(handler) = CONFIG.get().and_then(|c| c.functions.get(&name)).cloned() else {
return Value::number(0.0).0;
};
let token = loop {
let t = NEXT_TOKEN.fetch_add(1, Ordering::Relaxed);
if t != 0 {
break t;
}
};
IN_FLIGHT.fetch_add(1, Ordering::AcqRel);
let call = Call { name, args: call_args, reply: Reply { token, settled: false } };
if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| handler(call))).is_err() {
eprintln!("[embed] the host function behind appHost.call panicked");
}
Value::number(f64::from(token)).0
});
host_fn!(fn sk_host_functions(_g, _args) {
let mut names: Vec<&String> = CONFIG.get().map(|c| c.functions.keys().collect()).unwrap_or_default();
names.sort();
let json = crate::json::Json::arr(names.into_iter().map(|n| crate::json::Json::s(n.as_str())).collect()).to_json();
JsRuntime::with(|rt| rt.ctx().string(&json)).0
});
pub(crate) fn install(rt: &JsRuntime) {
if !active() {
return;
}
rt.register(
"__sk_host_set_dispatcher",
1,
sk_host_set_dispatcher as *const _,
);
rt.register("__sk_host_call", 2, sk_host_call as *const _);
rt.register("__sk_host_functions", 0, sk_host_functions as *const _);
let g = rt.raw();
RT_G.store(g as usize, Ordering::Release);
if let Some(id) = sk_runtime::redjs::context::id_of(g) {
RT_ID.store(id.0, Ordering::Release);
let _ = sk_runtime::redjs::web::waker(id);
}
sk_runtime::redjs::web::register_pump_source(sk_runtime::redjs::web::PumpSource {
inflight: src_inflight,
ready: src_ready,
drain: src_drain,
});
}
#[cfg(test)]
mod tests {
use super::*;
fn drain_queue() -> Vec<(u32, bool, String)> {
let mut q = QUEUE.lock().unwrap();
let out = q
.drain(..)
.map(|i| match i {
Item::Settle { token, ok, text } => (token, ok, text),
Item::Event { json } => (0, true, json),
})
.collect();
QUEUED.store(0, Ordering::Release);
out
}
#[test]
fn payload_source_moves_builder_bytes_and_maps_files_without_cloning() {
let bytes = vec![1, 2, 3, 4];
let pointer = bytes.as_ptr();
let mut builder = Builder::new().payload_bytes(bytes);
let (owned, _, _) = builder.payload.take().unwrap().load().unwrap();
let crate::payload::PayloadBytes::Owned(owned) = owned else {
panic!("owned bytes expected")
};
assert_eq!(owned.as_ptr(), pointer);
assert!(builder.payload.is_none());
static STATIC_PAYLOAD: &[u8] = b"static payload bytes";
let mut builder = Builder::new().payload_static(STATIC_PAYLOAD);
let (borrowed, _, _) = builder.payload.take().unwrap().load().unwrap();
let crate::payload::PayloadBytes::Embedded(borrowed) = borrowed else {
panic!("static bytes expected")
};
assert_eq!(borrowed.as_ptr(), STATIC_PAYLOAD.as_ptr());
assert_eq!(borrowed.len(), STATIC_PAYLOAD.len());
let mut files = crate::mapped_file::tests::Files::new();
let path = files.write("app.skpk", b"file bytes");
let mut builder = Builder::new().payload_file(&path);
let (mapped, _, dir) = builder.payload.take().unwrap().load().unwrap();
let crate::payload::PayloadBytes::File(mapped) = mapped else {
panic!("mapped file expected")
};
assert_eq!(mapped.as_bytes(), b"file bytes");
assert_eq!(dir.as_deref(), path.parent());
assert!(builder.payload.is_none());
#[cfg(target_os = "windows")]
assert!(std::fs::OpenOptions::new().write(true).open(&path).is_err());
drop(mapped);
std::fs::write(&path, b"released").unwrap();
}
#[test]
fn data_directory_override_and_preparation_precede_storage() {
let base = std::env::temp_dir().join(format!(
"sk-data-dir-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let configured = base.join("configured");
let overridden = base.join("override");
assert_eq!(
resolve_data_dir(
Some(configured.clone()),
Some(overridden.clone().into_os_string())
)
.unwrap(),
Some(overridden)
);
assert_eq!(
resolve_data_dir(Some(configured.clone()), Some("".into())).unwrap(),
Some(configured.clone())
);
assert_eq!(resolve_data_dir(None, None).unwrap(), None);
assert!(resolve_data_dir(Some(PathBuf::from("relative")), None).is_err());
assert!(prepare_directory(None, Some(Box::new(|_| Ok(())))).is_err());
let refused = base.join("refused");
assert!(prepare_directory(
Some(&refused),
Some(Box::new(|_| Err("migration refused".into())))
)
.is_err());
assert!(!refused.exists());
prepare_directory(
Some(&configured),
Some(Box::new(|path| {
assert!(!path.exists());
std::fs::create_dir_all(path).map_err(|e| e.to_string())?;
std::fs::write(path.join("prepared"), "profile").map_err(|e| e.to_string())
})),
)
.unwrap();
assert_eq!(
std::fs::read_to_string(configured.join("prepared")).unwrap(),
"profile"
);
std::fs::remove_file(configured.join("prepared")).unwrap();
std::fs::remove_dir(configured).unwrap();
std::fs::remove_dir(base).unwrap();
}
#[cfg(target_os = "macos")]
#[test]
fn appkit_rejects_worker_entry_before_startup_state_changes() {
std::thread::spawn(|| {
assert!(matches!(
require_main_thread(),
Err(Error::MainThreadRequired)
));
assert!(matches!(
Builder::new().spawn(),
Err(Error::MainThreadRequired)
));
assert!(matches!(
Builder::new().run_on_main_thread(|_| 0),
Err(Error::MainThreadRequired)
));
})
.join()
.unwrap();
}
#[test]
fn setup_receives_installed_context_once_and_propagates_failure() {
let called = Arc::new(AtomicUsize::new(0));
let observed = called.clone();
let hook = SetupHook(Mutex::new(Some(Box::new(move |ctx| {
assert_eq!(
ctx.storage_dir().ok(),
HostContext::installed(None).storage_dir().ok()
);
observed.fetch_add(1, Ordering::SeqCst);
Err("bootstrap refused".into())
}))));
assert_eq!(
hook.run(&HostContext::installed(None)),
Err("bootstrap refused".into())
);
assert!(hook.run(&HostContext::installed(None)).is_ok());
assert_eq!(called.load(Ordering::SeqCst), 1);
let panic = SetupHook(Mutex::new(Some(Box::new(|_| panic!("plugin panic")))));
assert!(panic.run(&HostContext::installed(None)).is_err());
}
#[test]
fn replies_settle_once_and_a_dropped_one_rejects() {
drain_queue();
Reply {
token: 7,
settled: false,
}
.resolve(r#"{"a":1}"#);
Reply {
token: 8,
settled: false,
}
.resolve("not json");
Reply {
token: 9,
settled: false,
}
.reject("nope");
drop(Reply {
token: 10,
settled: false,
});
let got = drain_queue();
assert_eq!(got[0], (7, true, r#"{"a":1}"#.to_string()));
assert_eq!(got[1].0, 8);
assert!(!got[1].1);
assert_eq!(got[2], (9, false, "nope".to_string()));
assert_eq!(got[3].0, 10);
assert!(!got[3].1);
let h = Handle { _private: () };
assert_eq!(h.emit("x", "{bad"), Err(EmitError::NotJson));
h.emit("status", r#"{"online":true}"#).unwrap();
assert_eq!(
drain_queue()[0].2,
r#"{"event":"status","data":{"online":true}}"#
);
}
}