//! JavaScript engine — lexer, parser, bytecode, register VM, GC, JIT (AArch64). pub mod ast; pub mod bytecode; pub mod compiler; pub mod lexer; pub mod parser; pub mod vm; use std::fmt; /// An error produced by the JavaScript engine. #[derive(Debug)] pub enum JsError { /// The engine does not yet support this feature or syntax. NotImplemented, /// A parse/syntax error in the source. SyntaxError(String), /// A runtime error during execution. RuntimeError(String), } impl fmt::Display for JsError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { JsError::NotImplemented => write!(f, "not implemented"), JsError::SyntaxError(msg) => write!(f, "SyntaxError: {}", msg), JsError::RuntimeError(msg) => write!(f, "RuntimeError: {}", msg), } } } /// Evaluate a JavaScript source string and return the completion value as a string. /// /// Parses the source, compiles to bytecode, and executes in the VM. pub fn evaluate(source: &str) -> Result { let program = parser::Parser::parse(source).map_err(|e| JsError::SyntaxError(e.to_string()))?; let func = compiler::compile(&program)?; let mut engine = vm::Vm::new(); let result = engine .execute(&func) .map_err(|e| JsError::RuntimeError(e.to_string()))?; Ok(result.to_string()) }