#![deny(unsafe_op_in_unsafe_fn)]
#![warn(missing_docs)]
pub use js_compile;
pub use js_gc::{CollectStats, Gc, Heap, Trace, Tracer, WeakGc};
pub use js_parse;
mod bignum;
pub mod builtins;
pub mod interp;
pub mod object;
pub mod value;
pub use interp::{Interp, Signal};
pub use object::{Callable, JsObject, ObjectKind, Property};
pub use value::{Cell, JsString, Value};
pub const COMPAT_VERSION: &str = "2026-06-04";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
Syntax(String),
Exception(String),
NotImplemented(&'static str),
OutOfMemory,
StackOverflow,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::NotImplemented(what) => write!(f, "not implemented yet: {what}"),
Error::Syntax(what) => write!(f, "{what}"),
Error::Exception(what) => write!(f, "{what}"),
Error::OutOfMemory => write!(f, "out of memory"),
Error::StackOverflow => write!(f, "stack overflow"),
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;
pub struct Runtime {
heap: Heap,
memory_limit: Option<usize>,
stack_size: Option<usize>,
}
impl Default for Runtime {
fn default() -> Self {
Self::new()
}
}
impl Runtime {
pub fn new() -> Runtime {
Runtime {
heap: Heap::new(),
memory_limit: None,
stack_size: None,
}
}
pub fn heap(&self) -> &Heap {
&self.heap
}
pub fn set_memory_limit(&mut self, limit: Option<usize>) {
self.memory_limit = limit;
}
pub fn memory_limit(&self) -> Option<usize> {
self.memory_limit
}
pub fn set_stack_size(&mut self, limit: Option<usize>) {
self.stack_size = limit;
}
pub fn stack_size(&self) -> Option<usize> {
self.stack_size
}
pub fn run_gc(&self) -> CollectStats {
self.heap.collect()
}
}
pub struct Context<'rt> {
runtime: &'rt Runtime,
interp: Option<Interp<'rt>>,
last_value: Value,
script_name: Option<String>,
}
impl<'rt> Context<'rt> {
pub fn new(runtime: &'rt Runtime) -> Context<'rt> {
Context {
runtime,
interp: None,
last_value: Value::Undefined,
script_name: None,
}
}
pub fn set_script_name(&mut self, name: impl Into<String>) {
self.script_name = Some(name.into());
}
pub fn runtime(&self) -> &'rt Runtime {
self.runtime
}
pub fn output(&self) -> &str {
self.interp.as_ref().map_or("", |interp| &interp.output)
}
fn interp(&mut self) -> &mut Interp<'rt> {
let heap: &'rt Heap = &self.runtime.heap;
self.interp.get_or_insert_with(|| Interp::new(heap))
}
pub fn eval(&mut self, source: &str) -> Result<Value> {
let program =
js_parse::parse_script(source).map_err(|error| Error::Syntax(error.to_string()))?;
let mut code = js_compile::compile_script(&program)
.map_err(|error| Error::Syntax(error.message.clone()))?;
code.set_filename(self.script_name.as_deref().unwrap_or("<input>"));
let interp = self.interp();
match interp.run_script(code) {
Ok(value) => {
self.last_value = value.clone();
Ok(value)
}
Err(Signal::Throw(value)) => {
let text = interp.describe_exception(&value);
Err(Error::Exception(text))
}
Err(Signal::OutOfMemory) => Err(Error::OutOfMemory),
}
}
pub fn last_value(&self) -> &Value {
&self.last_value
}
}