use std::sync::OnceLock;
use super::V8Options;
static RELIABLE_TERMINATION: OnceLock<bool> = OnceLock::new();
pub(super) fn initialize(options: &V8Options) -> crate::Result<()> {
choose(&RELIABLE_TERMINATION, options.reliable_termination, || {
deno_core::v8_set_flags(vec![String::new(), "--no-concurrent-osr".into()]);
})
}
fn choose(chosen: &OnceLock<bool>, requested: bool, set: impl FnOnce()) -> crate::Result<()> {
let first = *chosen.get_or_init(|| {
if requested {
set();
}
requested
});
if requested && !first {
return Err(crate::Error::Configuration(
"V8Options::reliable_termination must be set on the first V8 instance of the process"
.into(),
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_first_instance_decides_the_flag() {
let set = OnceLock::new();
let mut calls = 0;
choose(&set, true, || calls += 1).unwrap();
choose(&set, false, || calls += 1).unwrap();
choose(&set, true, || calls += 1).unwrap();
assert_eq!(calls, 1);
let unset = OnceLock::new();
choose(&unset, false, || unreachable!()).unwrap();
assert!(matches!(
choose(&unset, true, || unreachable!()),
Err(crate::Error::Configuration(_))
));
}
}