pub mod aot;
pub mod aot_native;
pub mod ast;
pub mod banner;
pub mod builtins;
pub mod cache;
pub mod cli;
pub mod compiler;
pub mod dap;
pub mod host;
pub mod lexer;
pub mod lsp;
pub mod module;
pub mod parser;
pub mod regexp;
pub mod repl;
pub mod rust_ffi;
pub mod stdlib;
pub mod tiers;
pub use fusevm::Value;
pub fn compile(src: &str) -> Result<compiler::Program, String> {
let stmts = parser::parse(src)?;
compiler::compile(&stmts, false)
}
pub fn compile_completion(src: &str) -> Result<compiler::Program, String> {
let stmts = parser::parse(src)?;
compiler::compile_completion(&stmts, false)
}
pub fn compile_debug(src: &str) -> Result<compiler::Program, String> {
let stmts = parser::parse(src)?;
compiler::compile(&stmts, true)
}
pub fn load_merged(mut prog: compiler::Program) -> fusevm::Chunk {
let (func_off, try_off) = host::with_host(|h| h.program_offsets());
compiler::rebase_program(&mut prog, func_off, try_off);
let compiler::Program {
main,
functions,
tries,
} = prog;
let funcs: Vec<host::FuncDef> = functions.into_iter().map(|(_, f)| f).collect();
host::with_host(|h| h.load_program(funcs, tries));
main
}
pub fn run_compiled(prog: compiler::Program) -> Result<Value, String> {
host::run_main(load_merged(prog))
}
pub fn compile_or_load(src: &str) -> Result<compiler::Program, String> {
if let Some(prog) = cache::load(src) {
if std::env::var_os("NODE_JS_TRACE").is_some() {
eprintln!(
"node-js: cache HIT ({} ops, {} functions) — skipped lex/parse/lower",
prog.main.ops.len(),
prog.functions.len()
);
}
return Ok(prog);
}
let prog = compile(src)?;
let _ = cache::store(src, &prog);
if std::env::var_os("NODE_JS_TRACE").is_some() {
eprintln!(
"node-js: cache MISS — compiled + stored ({} ops, {} functions)",
prog.main.ops.len(),
prog.functions.len()
);
}
Ok(prog)
}
pub fn eval_str(src: &str) -> Result<Value, String> {
host::reset_host();
if let Ok(cwd) = std::env::current_dir() {
module::set_entry_dir(cwd);
}
run_compiled(compile_or_load(src)?)
}
pub fn eval_file(path: &str) -> Result<Value, String> {
let src = std::fs::read_to_string(path).map_err(|e| format!("cannot read {path}: {e}"))?;
host::reset_host();
let dir = std::path::Path::new(path)
.parent()
.filter(|p| !p.as_os_str().is_empty())
.map(std::path::Path::to_path_buf)
.or_else(|| std::env::current_dir().ok())
.unwrap_or_default();
let dir = std::fs::canonicalize(&dir).unwrap_or(dir);
module::set_entry_dir(dir);
run_compiled(compile_or_load(&src)?)
}
pub fn eval_file_debug(path: &str) -> Result<Value, String> {
let src = std::fs::read_to_string(path).map_err(|e| format!("cannot read {path}: {e}"))?;
let prog = compile_debug(&src)?;
host::reset_host();
host::set_debug_mode(true);
let r = run_compiled(prog);
host::set_debug_mode(false);
r
}