Skip to main content

nodejs/
lib.rs

1//! node-js — JavaScript as a fusevm frontend.
2//!
3//! Pipeline: `lexer` → `parser` builds a JS AST → `compiler` lowers it to a
4//! `fusevm::Chunk` (plus a table of function/arrow sub-chunks and try-block
5//! chunks) → fusevm executes it, calling back into the `host` (through
6//! registered builtins and the strict numeric hook) for every JS-specific
7//! operation. There is no bespoke VM or JIT here — execution and codegen live in
8//! fusevm.
9
10pub mod aot;
11pub mod aot_native;
12pub mod ast;
13pub mod banner;
14pub mod builtins;
15pub mod cache;
16pub mod capture;
17pub mod cli;
18pub mod compiler;
19pub mod dap;
20pub mod host;
21pub mod lexer;
22pub mod lsp;
23pub mod module;
24pub mod parser;
25pub mod proxy;
26pub mod regexp;
27pub mod repl;
28pub mod rust_ffi;
29pub mod slots;
30pub mod stdlib;
31pub mod tiers;
32pub mod utf16;
33
34pub use fusevm::Value;
35
36/// Stack reserved for the thread JS runs on ([`run_on_js_stack`]).
37///
38/// A JS call is a Rust recursion (`host::run_user_func_nt` → `run_chunk_on` →
39/// a fresh `fusevm::VM` on the stack), so recursion depth is bounded by the
40/// native stack rather than by a frame counter. On the OS default 8 MiB this
41/// bought only 83 frames in a debug build — measured, `node -e 'function
42/// f(n){if(n<=0)return 0;return 1+f(n-1)} f(84)'` aborted — where node v26.7.0
43/// reaches 9901. Reserving 256 MiB is virtual address space, faulted in only as
44/// deep recursion actually uses it, and `host::stack_exhausted` still turns the
45/// far end into a catchable `RangeError` rather than an abort. The reservation
46/// is capped rather than sized to match node's depth exactly so that a runaway
47/// recursion's peak RSS stays bounded; the resulting depth is documented in
48/// BUGS.md.
49pub const JS_STACK_SIZE: usize = 256 * 1024 * 1024;
50
51/// Run `f` on a thread with [`JS_STACK_SIZE`] of stack, falling back to the
52/// calling thread if the reservation is refused (a `ulimit`ed or memory-capped
53/// environment must still run programs, just at a lower recursion ceiling —
54/// `host::stack_exhausted` measures whatever stack it ends up on).
55///
56/// Takes a plain `fn` pointer, not a closure: `Builder::spawn` consumes what it
57/// is given and does not hand it back on failure, and a `fn` is `Copy`, so the
58/// fallback can still call the same entry point.
59pub fn run_on_js_stack(f: fn() -> std::process::ExitCode) -> std::process::ExitCode {
60    match std::thread::Builder::new()
61        .name("node-js".into())
62        .stack_size(JS_STACK_SIZE)
63        .spawn(f)
64    {
65        // A panic on the JS thread has already written its message to stderr;
66        // re-raising keeps the process dying exactly as it would have without
67        // the hop, rather than turning an abort into a quiet exit code.
68        Ok(h) => h.join().unwrap_or_else(|p| std::panic::resume_unwind(p)),
69        Err(_) => f(),
70    }
71}
72
73/// Compile a source string to a runnable program.
74pub fn compile(src: &str) -> Result<compiler::Program, String> {
75    let stmts = parser::parse(src)?;
76    compiler::compile(&stmts, false)
77}
78
79/// Compile leaving the final top-level expression as the program's completion
80/// value (for `vm.runInThisContext` / `eval`).
81pub fn compile_completion(src: &str) -> Result<compiler::Program, String> {
82    let stmts = parser::parse(src)?;
83    compiler::compile_completion(&stmts, false)
84}
85
86/// Compile with per-statement DAP line markers enabled (`node --dap`).
87pub fn compile_debug(src: &str) -> Result<compiler::Program, String> {
88    let stmts = parser::parse(src)?;
89    compiler::compile(&stmts, true)
90}
91
92/// Rebase a freshly compiled program's func/try ids above those already loaded
93/// on the host, install its functions/tries, and return the (rebased) main
94/// chunk to run.
95pub fn load_merged(mut prog: compiler::Program) -> fusevm::Chunk {
96    let (func_off, try_off) = host::with_host(|h| h.program_offsets());
97    compiler::rebase_program(&mut prog, func_off, try_off);
98    let compiler::Program {
99        main,
100        functions,
101        tries,
102    } = prog;
103    let funcs: Vec<host::FuncDef> = functions.into_iter().map(|(_, f)| f).collect();
104    host::with_host(|h| h.load_program(funcs, tries));
105    main
106}
107
108/// Run an already-compiled program on the current host.
109pub fn run_compiled(prog: compiler::Program) -> Result<Value, String> {
110    host::run_main(load_merged(prog))
111}
112
113/// `process.exitCode` as the program left it, or `None` if it was never set.
114///
115/// The binary reads this after a run completes to pick its own status — Node
116/// exits with `process.exitCode` when the loop drains normally, so a script
117/// that signals failure that way (rather than by throwing or calling
118/// `process.exit`) is reported as a failure rather than as success.
119pub fn exit_code() -> Option<i32> {
120    host::with_host(|h| h.exit_code)
121}
122
123/// Run the `exit` event for a program that died on an uncaught exception, and
124/// report the status to leave with.
125///
126/// Node fires `exit` on this path too, and an uncaught exception FORCES the
127/// code to 1 — overriding any `process.exitCode` the script had already set —
128/// while a code the handler itself assigns still wins. Verified on node
129/// v26.7.0: `process.exitCode = 3; process.on('exit', c => console.log(c));
130/// throw new Error('z')` prints `1` and exits 1, and
131/// `process.on('exit', () => { process.exitCode = 9 }); throw new Error('z')`
132/// exits 9.
133pub fn exit_code_after_failure() -> i32 {
134    host::with_host(|h| h.exit_code = Some(1));
135    let _ = stdlib::process::emit_exit_event(1);
136    host::with_host(|h| h.exit_code).unwrap_or(1)
137}
138
139/// Compile `src` and run it on the LIVE host — no reset, no event-loop drain —
140/// in the GLOBAL scope, returning its completion value.
141///
142/// This is the ONE runtime-source evaluator on this frontend. Every construct
143/// that turns a source string into a running program funnels through here:
144/// the CommonJS module wrapper (`module::compile_wrapper`), `vm.runInThisContext`
145/// / `vm.Script` / `vm.compileFunction`, `new Function` / `Function(...)`
146/// (`builtins::dynamic_function`), and the internal JS factories
147/// (`util.promisify`, `stream/promises`, `stream/consumers`,
148/// `performance.timerify`, `module.builtinModules`). Each of those used to carry
149/// its own `compile_completion` → `load_merged` → `run_chunk_on` triple — seven
150/// copies of the same three lines — and every one of them inherited the same
151/// bug: `run_chunk_on` executes on whatever frame is CURRENT, so nested source
152/// saw the calling function's locals. Measured against node v26.7.0,
153/// `function outer(){ let secret = 1; return require('./m.js'); }` with `m.js` =
154/// `module.exports = typeof secret` is `"undefined"` there and was `"number"`
155/// here; `vm.runInThisContext('typeof loc')` likewise. `run_chunk_in_global_scope`
156/// fixes it once, for all of them.
157pub fn eval_in_global_scope(src: &str) -> Result<Value, String> {
158    let prog = compile_completion(src)?;
159    let chunk = load_merged(prog);
160    host::run_chunk_in_global_scope(chunk)
161}
162
163/// Transparent bytecode cache: return the cached compiled `Program` for `src`
164/// (skipping lex/parse/lower entirely), else compile it, store it in the
165/// `~/.node-js/scripts.rkyv` shard, and return it. This runs on EVERY ordinary
166/// `node foo.js` / `node -e` invocation, so scripts are rkyv-cached automatically
167/// — not only under `--build`. Set `NODE_JS_TRACE=1` to log hit/miss to stderr
168/// (silent otherwise; normal runs print nothing).
169pub fn compile_or_load(src: &str) -> Result<compiler::Program, String> {
170    if let Some(prog) = cache::load(src) {
171        if std::env::var_os("NODE_JS_TRACE").is_some() {
172            eprintln!(
173                "node-js: cache HIT ({} ops, {} functions) — skipped lex/parse/lower",
174                prog.main.ops.len(),
175                prog.functions.len()
176            );
177        }
178        return Ok(prog);
179    }
180    let prog = compile(src)?;
181    let _ = cache::store(src, &prog);
182    if std::env::var_os("NODE_JS_TRACE").is_some() {
183        eprintln!(
184            "node-js: cache MISS — compiled + stored ({} ops, {} functions)",
185            prog.main.ops.len(),
186            prog.functions.len()
187        );
188    }
189    Ok(prog)
190}
191
192/// Parse/load, compile, and run a JS source string on a fresh host (rkyv-cached).
193///
194/// This is the `node -e` entry point; [`eval_str_from`] names the other
195/// source-on-the-command-line one, which reports a different `__filename`.
196pub fn eval_str(src: &str) -> Result<Value, String> {
197    eval_str_from(src, "[eval]")
198}
199
200/// [`eval_str`] with the entry-point NAME node reports for it: `[eval]` for
201/// `-e`, `[stdin]` for source piped in. The two are observably different —
202/// `__filename`, `module.id` and a stack frame's file all carry it.
203pub fn eval_str_from(src: &str, origin: &str) -> Result<Value, String> {
204    host::reset_host();
205    // `node -e` resolves top-level `require` from the current working directory.
206    if let Ok(cwd) = std::env::current_dir() {
207        module::set_entry_dir(cwd);
208    }
209    module::install_entry_globals(origin);
210    run_compiled(compile_or_load(src)?)
211}
212
213/// `node -p <src>`: evaluate as `-e` does, then write the program's COMPLETION
214/// value through the `console.log` formatter, exactly as Node's
215/// `--print` does (`node -p '[1,2]'` prints `[ 1, 2 ]`, `node -p '"s"'` prints
216/// the bare `s`). Side effects still happen, so `node -p 'console.log("x")'`
217/// prints `x` and then `undefined`.
218///
219/// Deliberately compiled with [`compile_completion`] rather than through the
220/// source-keyed rkyv cache: the cache is keyed by source TEXT alone, so a
221/// `-p`-shaped chunk and an `-e`-shaped chunk for the same string would alias.
222pub fn eval_str_print(src: &str, origin: &str) -> Result<(), String> {
223    host::reset_host();
224    if let Ok(cwd) = std::env::current_dir() {
225        module::set_entry_dir(cwd);
226    }
227    module::install_entry_globals(origin);
228    let value = run_compiled(compile_completion(src)?)?;
229    let line = stdlib::util::format(std::slice::from_ref(&value));
230    host::with_host(|h| h.write_out(&format!("{line}\n"), false));
231    Ok(())
232}
233
234/// Run a JS source string on a fresh host with `globals` bound and the
235/// program's output captured in-process, returning the program's outcome
236/// alongside everything it wrote.
237///
238/// This is the entry point for an embedder rather than for the `node` binary,
239/// and it exists because [`eval_str`] cannot serve one: it resets the host
240/// first, which wipes any global installed beforehand, and it lets
241/// `console.log` reach the real stdout, which corrupts a host that owns the
242/// terminal. Both are fixed here — the globals are seeded *after* the reset,
243/// and every write the program makes lands in the returned string.
244///
245/// The outcome and the output are returned separately (rather than the output
246/// only on success) because a program that prints and *then* throws produced
247/// both, and an embedder generally wants to show both.
248///
249/// Globals are given as text and interned as real JS strings here. They are
250/// deliberately *not* `Value`: strings live on this host's heap as
251/// `JsObj::Str`, so a `Value::Str` a caller builds is at best coerced and at
252/// worst method-less. Handing the host text and letting it intern removes that
253/// trap, and matches the sibling runtimes' embedder entry points.
254///
255/// ```no_run
256/// let (result, out) = nodejs::eval_str_captured("console.log(stdin.toUpperCase())", &[("stdin", "hi")]);
257/// assert!(result.is_ok());
258/// assert_eq!(out, "HI\n");
259/// ```
260pub fn eval_str_captured(src: &str, globals: &[(&str, &str)]) -> (Result<Value, String>, String) {
261    host::reset_host();
262    if let Ok(cwd) = std::env::current_dir() {
263        module::set_entry_dir(cwd);
264    }
265    host::with_host(|h| {
266        for (name, text) in globals {
267            let value = h.new_str(*text);
268            h.set_global(name, value);
269        }
270        h.begin_capture();
271    });
272    let result = compile_or_load(src).and_then(run_compiled);
273    let output = host::with_host(|h| h.end_capture());
274    (result, output)
275}
276
277/// Read and run a `.js` file (transparently rkyv-cached — see `compile_or_load`).
278pub fn eval_file(path: &str) -> Result<Value, String> {
279    let src = std::fs::read_to_string(path).map_err(|e| format!("cannot read {path}: {e}"))?;
280    host::reset_host();
281    // Top-level `require` in `node app.js` resolves from the entry file's dir.
282    let dir = std::path::Path::new(path)
283        .parent()
284        .filter(|p| !p.as_os_str().is_empty())
285        .map(std::path::Path::to_path_buf)
286        .or_else(|| std::env::current_dir().ok())
287        .unwrap_or_default();
288    let dir = std::fs::canonicalize(&dir).unwrap_or(dir);
289    module::set_entry_dir(dir);
290    // `__filename` is the entry script's REALPATH, not the path that was typed:
291    // Node's loader calls `toRealPath` on the main module, so a script reached
292    // through a symlinked directory reports the link TARGET. (`process.argv[1]`
293    // is the opposite — it keeps the spelling; both measured on node v26.7.0.)
294    let entry = std::fs::canonicalize(path)
295        .map(|p| p.to_string_lossy().into_owned())
296        .unwrap_or_else(|_| stdlib::path::resolve_one(path));
297    module::install_entry_globals(&entry);
298    run_compiled(compile_or_load(&src)?)
299}
300
301/// Read and run a `.js` file under the DAP debugger.
302pub fn eval_file_debug(path: &str) -> Result<Value, String> {
303    let src = std::fs::read_to_string(path).map_err(|e| format!("cannot read {path}: {e}"))?;
304    let prog = compile_debug(&src)?;
305    host::reset_host();
306    host::set_debug_mode(true);
307    let r = run_compiled(prog);
308    host::set_debug_mode(false);
309    r
310}