pub struct ClosureEnv {
pub cells: Vec<Gc<Cell>>,
pub eval_scope: Option<Gc<EvalScope>>,
pub local_vars: Option<Gc<LocalVars>>,
pub local_phase: u8,
}Expand description
The reference cells a closure captured from enclosing scopes.
Fields§
§cells: Vec<Gc<Cell>>Indexed the way the bytecode’s closure-variable table indexes them.
eval_scope: Option<Gc<EvalScope>>The eval scope this closure was created in, if any.
A function built by eval has to keep seeing the caller’s bindings after the
eval has returned — for (let i …) eval("push(() => i)") collects closures that
read i later. Those bindings live in the caller’s frame, which is long gone
by then, so the environment carries them by name instead. Ordinary closures leave
this None and resolve globals exactly as before.
local_vars: Option<Gc<LocalVars>>Variables a direct eval declared in the frame this closure was created in, and
how much of that frame it may see.
A closure is created at a place in a function, and a direct eval can add
variables after that point. Those variables belong to the variable environment
that was current at the closure’s creation — the parameter list or the body — which
is what local_phase records.
local_phase: u80 for a closure created in a parameter list, 1 for one in a body. A parameter
list’s closure must not see variables a later eval in the body declared:
function f(a, p = () => c) { eval("var c = 1") } has p() resolve c outside
the function entirely.