1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
//! This module implements anything required to run a Janet client.
use core::{
fmt::{self, Display},
sync::atomic::{AtomicBool, Ordering},
};
#[cfg(feature = "std")]
use std::{error::Error as StdError, thread_local};
use crate::{
env::{CFunOptions, DefOptions, JanetEnvironment, VarOptions},
Janet, JanetTable,
};
// There are platforms where AtomicBool doesn't exist
// At some point it would be awesome to find what targets doesn't have support for atomics
// and for those add something else to substitute the AtomicBool.
#[cfg(feature = "std")]
thread_local! {
static INIT: AtomicBool = const { AtomicBool::new(false) };
}
#[cfg(not(feature = "std"))]
static INIT: AtomicBool = AtomicBool::new(false);
/// The possible errors for the [`JanetClient`].
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[non_exhaustive]
pub enum Error {
/// May happen when trying to initialize two or more [`JanetClient`].
AlreadyInit,
/// May happen when trying to run code that does not compile.
CompileError,
/// May happen when trying to run a Janet code without a environment table.
EnvNotInit,
/// May happen when trying to run code that failed to be parsed.
ParseError,
/// May happen when the VM errors while running code.
RunError,
}
impl Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AlreadyInit => f.pad("Janet client already initialized"),
Self::CompileError => f.pad("Failed to compile code"),
Self::EnvNotInit => f.pad("The environment table was not initialized"),
Self::ParseError => f.pad("Failed to parse code"),
Self::RunError => f.pad("Runtime VM error"),
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(_doc, doc(cfg(feature = "std")))]
impl StdError for Error {}
/// Janet client that initialize the Janet runtime.
///
/// If in a `no_std` environment you can only initialize the runtime through the safe
/// interface only once, since the static atomic global cannot be thread local in a
/// `no_std` environment, if you're on a multi-thread + `no_std` environment refer to use
/// [`init_unchecked`].
///
/// [`init_unchecked`]: ./struct.JanetClient.html#method.init_unchecked.html
#[derive(Debug)]
pub struct JanetClient {
env_table: Option<JanetEnvironment>,
}
impl JanetClient {
/// Initialize Janet global state.
///
/// This must be called only once per thread if using Janet in a multithreaded
/// environment, as all Janet global state is thread local by default.
///
/// If tried to initialize the client more than once it returns a `Err` variant.
#[inline]
pub fn init() -> Result<Self, Error> {
#[cfg(feature = "std")]
let init_state = INIT.with(|i| i.swap(true, Ordering::SeqCst));
#[cfg(not(feature = "std"))]
let init_state = INIT.swap(true, Ordering::SeqCst);
if init_state {
return Err(Error::AlreadyInit);
}
// SAFETY: We use a static AtomicBool to make sure that it is started only once (per
// thread if "std" feature activated)
unsafe { evil_janet::janet_init() };
Ok(Self { env_table: None })
}
/// Initialize Janet global state without checking.
///
/// This must be called only once per thread if using Janet in a multithreaded
/// environment, as all Janet global state is thread local by default.
///
/// # Safety
/// If initialized more than once per thread, and more than one drop, you can have a
/// double free, if one drop and another continue to execute, it will crash with
/// segmentation fault.
#[inline]
#[must_use = "function is a constructor associated function"]
pub unsafe fn init_unchecked() -> Self {
evil_janet::janet_init();
Self { env_table: None }
}
/// Initialize Janet global state and load the default environment of Janet.
///
/// This must be called only once per thread if using Janet in a multithreaded
/// environment, as all Janet global state is thread local by default.
///
/// If tried to initialize the client more than once it returns a `Err` variant.
///
/// The default environment of Janet contains all the Janet C code as well as the
/// code in [`boot.janet`](https://github.com/janet-lang/janet/blob/master/src/boot/boot.janet).
#[inline]
pub fn init_with_default_env() -> Result<Self, Error> {
let mut client = Self::init()?;
client.env_table = Some(JanetEnvironment::default());
Ok(client)
}
/// Initialize Janet global state.
///
/// This must be called only once per thread if using Janet in a multithreaded
/// environment, as all Janet global state is thread local by default.
///
/// If tried to initialize the client more than once it returns a `Err` variant.
///
/// If an item in the `replacements` table has the same name as a item in the default
/// environment table, the item is replaced by the newer.
#[inline]
pub fn init_with_replacements(replacements: JanetTable<'static>) -> Result<Self, Error> {
let mut client = Self::init()?;
client.env_table = Some(JanetEnvironment::with_replacements(replacements));
Ok(client)
}
/// Load the default environment of Janet.
///
/// The default environment of Janet contains all the Janet C code as well as the
/// code in [`boot.janet`](https://github.com/janet-lang/janet/blob/master/src/boot/boot.janet).
#[inline]
#[must_use]
pub fn load_env_default(mut self) -> Self {
self.env_table = Some(JanetEnvironment::default());
self
}
/// Load the default environment of Janet with the given `replacements` table.
///
/// If an item in the `replacements` table has the same name as a item in the default
/// environment table, the item is replaced by the newer.
#[inline]
#[must_use]
pub fn load_env_replacements(mut self, replacements: JanetTable<'static>) -> Self {
self.env_table = Some(JanetEnvironment::with_replacements(replacements));
self
}
/// Add a Janet immutable variable to the client environment if it has one, otherwise
/// creates it with the default one.
///
/// # Examples
/// ```
/// use janetrs::{client::JanetClient, env::DefOptions, Janet};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = JanetClient::init()?;
/// assert!(client.env().is_none());
///
/// client.add_def(DefOptions::new("const", 10));
/// assert!(client.env().is_some());
///
/// let c = client.run("const")?;
/// assert!(!c.is_nil());
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn add_def(&mut self, def_opt: DefOptions) {
if self.env().is_none() {
self.env_table = Some(JanetEnvironment::default());
}
if let Some(ref mut env) = self.env_table {
env.add_def(def_opt)
}
}
/// Add a Janet mutable variable to the client environment if it has one, otherwise
/// creates it with the default one.
///
/// # Examples
/// ```
/// use janetrs::{client::JanetClient, env::VarOptions, Janet};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = JanetClient::init()?;
/// assert!(client.env().is_none());
///
/// client.add_var(VarOptions::new("variable", 10));
/// assert!(client.env().is_some());
///
/// let c = client.run("variable")?;
/// assert!(!c.is_nil());
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn add_var(&mut self, var_opt: VarOptions) {
if self.env().is_none() {
self.env_table = Some(JanetEnvironment::default());
}
if let Some(ref mut env) = self.env_table {
env.add_var(var_opt);
}
}
/// Add a Janet C function to the client environment if it has one and register that
/// function in the Janet VM, otherwise creates it with default one.
///
/// # Examples
/// ```
/// use janetrs::{client::JanetClient, env::CFunOptions, janet_fn, Janet, JanetType};
///
/// #[janet_fn]
/// fn test(_args: &mut [Janet]) -> Janet {
/// Janet::nil()
/// }
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut client = JanetClient::init()?;
/// assert!(client.env().is_none());
///
/// client.add_c_fn(CFunOptions::new(c"test", test));
/// assert!(client.env().is_some());
///
/// let c = client.run("test")?;
/// assert_eq!(c.kind(), JanetType::CFunction);
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn add_c_fn(&mut self, cfun_opt: CFunOptions<'static>) {
if self.env().is_none() {
self.env_table = Some(JanetEnvironment::default());
}
if let Some(ref mut env) = self.env_table {
env.add_c_fn(cfun_opt);
}
}
/// Run given Janet `code` bytes and if no errors occurs, returns the output of the
/// given `code`.
///
/// **This function may trigger a GC collection**
///
/// # Examples
/// ```
/// use janetrs::{client::JanetClient, Janet};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = JanetClient::init_with_default_env()?;
///
/// let out = client.run_bytes(b"(def x 10) (+ x x)")?;
///
/// assert_eq!(Janet::number(20.0), out);
///
/// # Ok(())
/// # }
/// ```
///
///
/// ## TODO:
/// Right now the sourcePath value are hardcoded to `c"main"`.
/// Change that the Client struct holds sourcePath.
#[inline]
pub fn run_bytes(&self, code: impl AsRef<[u8]>) -> Result<Janet, Error> {
let code = code.as_ref();
let env = match self.env_table.as_ref() {
Some(e) => e.table(),
None => return Err(Error::EnvNotInit),
};
let mut out = Janet::nil();
let res = unsafe {
evil_janet::janet_dobytes(
env.raw,
code.as_ptr(),
code.len() as i32,
c"main".as_ptr(),
&mut out.inner,
)
};
match res {
0x01 => Err(Error::RunError),
0x02 => Err(Error::CompileError),
0x04 => Err(Error::ParseError),
_ => Ok(out),
}
}
/// Run given Janet `code` string and if no errors occurs, returns the output of the
/// given `code`.
///
/// **This function may trigger a GC collection**
///
/// # Examples
/// ```
/// use janetrs::{client::JanetClient, Janet};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = JanetClient::init_with_default_env()?;
///
/// let out = client.run("(def x 10) (+ x x)")?;
///
/// assert_eq!(Janet::number(20.0), out);
///
/// # Ok(())
/// # }
/// ```
///
/// ## TODO:
/// Right now the sourcePath value are hardcoded to `b"main"`,
/// respectively.
/// Change that the Client struct hold sourcePath.
#[inline]
pub fn run(&self, code: impl AsRef<str>) -> Result<Janet, Error> {
let code = code.as_ref();
self.run_bytes(code.as_bytes())
}
/// Return a reference of the environment table of the runtime if it exist.
#[inline]
#[must_use]
pub const fn env(&self) -> Option<&JanetEnvironment> {
self.env_table.as_ref()
}
/// Return a unique reference of the environment table of the runtime if it exist.
#[inline]
pub fn env_mut(&mut self) -> Option<&mut JanetEnvironment> {
self.env_table.as_mut()
}
}
impl Drop for JanetClient {
#[inline]
fn drop(&mut self) {
// Reset the INIT to false
#[cfg(feature = "std")]
INIT.with(|i| i.swap(false, Ordering::SeqCst));
#[cfg(not(feature = "std"))]
INIT.swap(false, Ordering::SeqCst);
unsafe { evil_janet::janet_deinit() }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn double_init() {
let c1 = JanetClient::init();
let c2 = JanetClient::init();
let c3 = JanetClient::init();
assert!(c1.is_ok());
assert_eq!(Error::AlreadyInit, c2.unwrap_err());
assert_eq!(Error::AlreadyInit, c3.unwrap_err());
}
#[test]
fn env_not_init() -> Result<(), Error> {
let client = JanetClient::init()?;
let a = client.run("()");
assert_eq!(Err(Error::EnvNotInit), a);
Ok(())
}
}