pub fn eval_str_captured(
src: &str,
globals: &[(&str, &str)],
) -> (Result<Value, String>, String)Expand description
Run a JS source string on a fresh host with globals bound and the
program’s output captured in-process, returning the program’s outcome
alongside everything it wrote.
This is the entry point for an embedder rather than for the node binary,
and it exists because eval_str cannot serve one: it resets the host
first, which wipes any global installed beforehand, and it lets
console.log reach the real stdout, which corrupts a host that owns the
terminal. Both are fixed here — the globals are seeded after the reset,
and every write the program makes lands in the returned string.
The outcome and the output are returned separately (rather than the output only on success) because a program that prints and then throws produced both, and an embedder generally wants to show both.
Globals are given as text and interned as real JS strings here. They are
deliberately not Value: strings live on this host’s heap as
JsObj::Str, so a Value::Str a caller builds is at best coerced and at
worst method-less. Handing the host text and letting it intern removes that
trap, and matches the sibling runtimes’ embedder entry points.
let (result, out) = nodejs::eval_str_captured("console.log(stdin.toUpperCase())", &[("stdin", "hi")]);
assert!(result.is_ok());
assert_eq!(out, "HI\n");