Skip to main content

Function

Struct Function 

Source
pub struct Function {
Show 22 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>, pub module: Option<Box<ModuleMeta>>,
}
Expand description

A compiled function.

Fields§

§name: String

The function’s name; <eval> for a script body.

§kind: FunctionKind

The declaration form.

§strict: bool

Whether the body is strict.

§is_arrow: bool

Whether 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: bool

Whether 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: u32

How 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: u32

Maximum value-stack depth.

§positions: Vec<LineCol>

Source positions, ordered by bytecode offset.

§var_ref_count: u32

Number of reference cells allocated on entry.

§source: Option<String>

Source text, kept for Function.prototype.toString and stack traces.

§line: u32

Line of the function’s declaration.

§column: u32

Column 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 one

The 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.

§module: Option<Box<ModuleMeta>>

The module this body is, when it is one.

None for a script and for every nested function. A module’s requests, imports and exports are data rather than instructions — the body itself reads its bindings through ordinary closure-variable opcodes, and this is what the host needs in order to wire those cells up before the body runs.

Implementations§

Source§

impl Function

Source

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.

Source

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.

Source

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.

Source

pub fn jump_targets(&self) -> Vec<usize>

Byte offsets that are the target of a jump, which the disassembler marks.

Source

pub fn slot_count(&self) -> u32

Total number of variable slots, as the serialiser stores them.

Trait Implementations§

Source§

impl Clone for Function

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Function

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Function

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Function

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.