Skip to main content

FnBody

Struct FnBody 

Source
pub struct FnBody {
    state: RefCell<FnBodyState>,
    pub strict: bool,
    uses_args: Cell<u8>,
    has_func_decl: Cell<u8>,
    var_names: RefCell<Option<Rc<Vec<Rc<str>>>>>,
    code: RefCell<Option<BodyCode>>,
    code_calls: Cell<u32>,
    shared_info: RefCell<Option<Weak<SharedFnInfo>>>,
    source_text: RefCell<Option<(Rc<str>, u32, u32)>>,
    eval_ctx: RefCell<Option<EvalSyntaxCtx>>,
    is_method: Cell<bool>,
}
Expand description

【2026-10-17】YouTube のアプリ JS(10.9MB)・player(3MB)は関数の大半が呼ばれないのに、 全関数の構文木を作るとヒープ 512MB に収まらなかった。未解析の本体同士は入れ子に ならない(外側を解析した時点で内側が作られる)ので、保持する文字列の合計はソース以下。 パーサは文脈(generator / async)に依存しないので、後から本体だけ解析しても結果は同じ。

Fields§

§state: RefCell<FnBodyState>§strict: bool

strict モードの本体か("use strict" の指令・strict な外側・クラス)。 非 strict の関数は this が undefined/null のときグローバル(window)になる。

§uses_args: Cell<u8>

本体(または内側のアロー関数)が arguments / eval を参照するか(0=未計算, 1=いいえ, 2=はい)。

§has_func_decl: Cell<u8>

本体のトップレベルに FunctionDeclaration(または ExportDecl { FunctionDeclaration })があるか(0=未計算, 1=なし, 2=あり)。

§var_names: RefCell<Option<Rc<Vec<Rc<str>>>>>

本体の var 巻き上げの名前(構文木をキャッシュしている間だけ保持)。 【2026-10-24】従来は呼び出しのたびに本体全体をたどって名前を集め直していた (文字列の確保つき)。YouTube の pako の関数のように何十万回も呼ばれると効いていた。

§code: RefCell<Option<BodyCode>>

バイトコード(構文木をキャッシュした本体だけ。None はまだ変換していない)。 generator の本体は通常の関数のバイトコードを使わないので、同じ欄に generator の VM の命令列を置く(2026-11-30)。

§code_calls: Cell<u32>

変換までに数える呼び出しの回数。

§shared_info: RefCell<Option<Weak<SharedFnInfo>>>

関数の静的コード情報キャッシュ(同一関数定義から作られる全インスタンスで共有)。

§source_text: RefCell<Option<(Rc<str>, u32, u32)>>

定義全体(function f(a) { ... })のソースと範囲(Function.prototype.toString 用。記録できた関数だけ)。 【2026-11-04】従来の toString() は function f() { [code] } という仮の文字列で、BotGuard の VM が それを評価して code is not defined になり、環境の改ざんと見なされた(TODO B0)。

§eval_ctx: RefCell<Option<EvalSyntaxCtx>>§is_method: Cell<bool>

メソッド・getter・setter の本体か(new できない。§15.4.4 のメソッド定義は [[Construct]] を持たない)。

Implementations§

Source§

impl FnBody

Source

pub fn parsed(stmts: Vec<Statement>) -> Rc<Self>

Source

pub fn parsed_with(stmts: Vec<Statement>, strict: bool) -> Rc<Self>

Source

pub fn from_rc(stmts: Rc<Vec<Statement>>) -> Rc<Self>

Source

pub fn lazy(source: String, strict: bool) -> Rc<Self>

Source

pub fn lazy_range( src: Rc<str>, start: u32, end: u32, strict: bool, atoms: Option<SharedAtoms>, ) -> Rc<Self>

Source

pub fn set_eval_ctx(&self, ctx: EvalSyntaxCtx)

Source

pub fn eval_ctx(&self) -> Option<EvalSyntaxCtx>

Source

pub fn mark_method(&self)

メソッド定義の本体として印を付ける(構文解析器から)。

Source

pub fn is_method(&self) -> bool

Source

pub fn set_source_text(&self, src: Rc<str>, start: u32, end: u32)

定義全体のソースの範囲を記録する(構文解析器から)。

Source

pub fn copy_source_from(&self, other: &FnBody)

別の本体のソース範囲を写す(クラスの全体のソースを、コンストラクタの本体へ移す)。

Source

pub fn source_text(&self) -> Option<String>

定義全体のソース(記録できていなければ None)。

Source

pub fn source_head(&self, n: usize) -> Option<String>

定義のソースの先頭 n 文字だけ(診断の表示用)。

【2026-11-19】TODO A7: 全体を複製する source_text() を診断が数万ステップごとに呼ぶと、 スクリプト全体を包む関数(YouTube の kevlar は 10.4MB)のたびに 10MB の一括確保が起きていた。

Source

pub fn get_or_create_shared_info( self: &Rc<Self>, name: &str, params: &Rc<Vec<Param>>, flags: u8, ) -> Rc<SharedFnInfo>

関数の静的メタデータ(SharedFnInfo)を取得または生成してキャッシュする。

Source

pub fn cached_var_names(&self) -> Option<Rc<Vec<Rc<str>>>>

保存済みの var 名(構文木をキャッシュ済みで、名前を計算済みのときだけ)。

Source

pub fn store_var_names( &self, stmts: &Rc<Vec<Statement>>, names: Rc<Vec<Rc<str>>>, )

var 名を保存する(構文木をキャッシュ済みで、stmts がその構文木のときだけ。 1 回だけ解析して捨てる本体では保存しない)。

Source

pub(crate) fn code(&self, stmts: &Rc<Vec<Statement>>) -> Option<Rc<Code>>

バイトコード。何度も呼ばれる本体だけ変換する。構文木をキャッシュ済みで stmts がその構文木の ときに限る(解析して捨てる本体は変換しない)。ループを含む関数は初回からバイトコードへ変換する。

Source

pub(crate) fn gen_code(&self, stmts: &Rc<Vec<Statement>>) -> Option<Rc<GenCode>>

generator 本体の VM 命令列(interp::gen_vm)。構文木をキャッシュ済みで stmts がその構文木なら 本体に覚えて使い回す(そうでなければ毎回変換する)。None は VM で扱えない本体(従来の replay で動かす)。

Source

pub fn is_lazy(&self) -> bool

まだ解析していないか。

Source

pub fn stmts(&self) -> Rc<Vec<Statement>>

本体の文(初回は構文木を保持せず返し、2 回目以降だけ構文木に置き換える)。

【2026-10-19】従来は 1 回呼ばれただけの関数でも構文木(ソース文字列の約 5〜8 倍の 大きさ)を self.state に永続保持していた。特に YouTube の kevlar_base(10.88MB)や base.js(2.97MB)の最外の即時関数は 1 回しか実行されないのに、約 40MB の構文木が クロージャ経由で最後まで残り続けていた。

  • 64KB 以上の巨大な本体(バンドル全体の即時関数など)は、1 回目は構文木を捨ててソースのまま残す (2026-11-18 から。2 回目に呼ばれたら下と同じく構文木を残す)。
  • それ以外は 1 回目の呼び出しではソースのまま残して構文木は呼び出し終了時に解放し、 2 回目に呼ばれた関数だけ構文木をキャッシュする。
Source

pub fn uses_arguments(&self, stmts: &[Statement]) -> bool

この関数本体(および内側のアロー関数)が arguments または eval を参照するか。

Source

pub fn has_function_decls(&self, stmts: &[Statement]) -> bool

本体のトップレベルに FunctionDeclaration(または ExportDecl { FunctionDeclaration })が含まれるか。

Source

fn raw_might_use_arguments(&self) -> bool

Trait Implementations§

Source§

impl Debug for FnBody

Source§

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

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

impl PartialEq for FnBody

同じ本体(同じ定義)かどうかで比べる。

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

Auto Trait Implementations§

§

impl !Freeze for FnBody

§

impl !RefUnwindSafe for FnBody

§

impl !Send for FnBody

§

impl !Sync for FnBody

§

impl !UnwindSafe for FnBody

§

impl Unpin for FnBody

§

impl UnsafeUnpin for FnBody

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> 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> Same for T

Source§

type Output = T

Should always be Self
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.