pub struct Function {Show 21 fields
pub name: String,
pub kind: FunctionKind,
pub strict: bool,
pub is_arrow: bool,
pub has_parameter_expressions: bool,
pub body_start: (u32, u32),
pub args: Vec<VarDef>,
pub defined_args: u32,
pub vars: Vec<VarDef>,
pub closure_vars: Vec<ClosureVar>,
pub cpool: Vec<Constant>,
pub atoms: Vec<Atom>,
pub code: Vec<u8>,
pub stack_size: u32,
pub positions: Vec<LineCol>,
pub var_ref_count: u32,
pub source: Option<String>,
pub line: u32,
pub column: u32,
pub filename: Option<String>,
pub eval_sites: Vec<EvalSite>,
}Expand description
A compiled function.
Fields§
§name: StringThe function’s name; <eval> for a script body.
kind: FunctionKindThe declaration form.
strict: boolWhether the body is strict.
is_arrow: boolWhether this came from an arrow function.
An arrow has no this and no new.target of its own, so the runtime has to know
to look outward for both — an eval inside an arrow sees the enclosing function’s
receiver, not the arrow’s.
has_parameter_expressions: boolWhether the parameter list contains a default expression, and so gets a variable
environment of its own (PARAM_SCOPE in the compiler).
The runtime needs this for two things a direct eval can observe: where a var
it declares belongs (the parameter environment, while the defaults are still being
evaluated), and which bindings a closure created there may see.
body_start: (u32, u32)Where the body begins in source order, as (line, column).
A closure whose creation site is before this was created by a default expression,
so it sees the parameter environment and not the body’s. Recorded as a position
rather than a bytecode offset because the instruction stream is rebuilt (frame
prologues are prepended) after the point where the compiler knows the boundary.
(0, 0) means “unknown”, which is read as “the whole function is its body”.
args: Vec<VarDef>Parameters.
defined_args: u32How many parameters count towards Function.prototype.length.
A parameter with a default or a rest parameter ends the count, because .length
is the arity the function can be called with before anything is substituted.
vars: Vec<VarDef>Locals, including the implicit return-value slot named <ret>.
closure_vars: Vec<ClosureVar>Captured variables of enclosing functions.
cpool: Vec<Constant>Constant pool.
atoms: Vec<Atom>Atom table: the names this function mentions. The runtime shares one interned table across the whole program; indexing it per function keeps the bytecode self-contained and makes disassembly readable.
code: Vec<u8>Resolved bytecode.
stack_size: u32Maximum value-stack depth.
positions: Vec<LineCol>Source positions, ordered by bytecode offset.
var_ref_count: u32Number of reference cells allocated on entry.
source: Option<String>Source text, kept for Function.prototype.toString and stack traces.
line: u32Line of the function’s declaration.
column: u32Column of the function’s declaration.
filename: Option<String>File name, if known.
eval_sites: Vec<EvalSite>What each direct eval(...) call in this function can see.
Slot resolution for ordinary code happens at compile time, but a direct eval
resolves names at run time, from a string the compiler never reads. Without
this table the runtime has to guess by name across the whole frame, which picks
the wrong binding when a name is declared twice in sibling scopes:
for (let i = 0; i < 3; i++) f(eval); // this `i`
for (let i = 0; i < 3; i++) f(eval); // and this oneThe compiler knows exactly which of them is in scope at each call, so it records the answer here. It is data, not an instruction, so the bytecode stays identical to the reference’s.
Implementations§
Source§impl Function
impl Function
Sourcepub fn set_filename(&mut self, name: &str)
pub fn set_filename(&mut self, name: &str)
Name the script this function belongs to.
Nested functions inherit it, because a stack trace names every frame with the file the code came from and the parser does not know file names — the host does, after the source has already been compiled.
Sourcepub fn position_at(&self, pc: u32) -> Option<(u32, u32)>
pub fn position_at(&self, pc: u32) -> Option<(u32, u32)>
The source position of the instruction at pc.
The last entry at or before pc wins, so a position applies until the next one
starts. None when the function carries no positions at all — a script compiled
without source information.
Sourcepub fn instructions(&self) -> Vec<(usize, Instruction)>
pub fn instructions(&self) -> Vec<(usize, Instruction)>
Decode the instruction stream back into instructions.
Labels are already resolved, so a label operand is an absolute byte
offset.
Sourcepub fn jump_targets(&self) -> Vec<usize>
pub fn jump_targets(&self) -> Vec<usize>
Byte offsets that are the target of a jump, which the disassembler marks.
Sourcepub fn slot_count(&self) -> u32
pub fn slot_count(&self) -> u32
Total number of variable slots, as the serialiser stores them.