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: boolstrict モードの本体か("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>変換までに数える呼び出しの回数。
関数の静的コード情報キャッシュ(同一関数定義から作られる全インスタンスで共有)。
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
impl FnBody
pub fn parsed(stmts: Vec<Statement>) -> Rc<Self>
pub fn parsed_with(stmts: Vec<Statement>, strict: bool) -> Rc<Self>
pub fn from_rc(stmts: Rc<Vec<Statement>>) -> Rc<Self>
pub fn lazy(source: String, strict: bool) -> Rc<Self>
pub fn lazy_range( src: Rc<str>, start: u32, end: u32, strict: bool, atoms: Option<SharedAtoms>, ) -> Rc<Self>
pub fn set_eval_ctx(&self, ctx: EvalSyntaxCtx)
pub fn eval_ctx(&self) -> Option<EvalSyntaxCtx>
Sourcepub fn mark_method(&self)
pub fn mark_method(&self)
メソッド定義の本体として印を付ける(構文解析器から)。
pub fn is_method(&self) -> bool
Sourcepub fn copy_source_from(&self, other: &FnBody)
pub fn copy_source_from(&self, other: &FnBody)
別の本体のソース範囲を写す(クラスの全体のソースを、コンストラクタの本体へ移す)。
Sourcepub fn source_text(&self) -> Option<String>
pub fn source_text(&self) -> Option<String>
定義全体のソース(記録できていなければ None)。
Sourcepub fn source_head(&self, n: usize) -> Option<String>
pub fn source_head(&self, n: usize) -> Option<String>
定義のソースの先頭 n 文字だけ(診断の表示用)。
【2026-11-19】TODO A7: 全体を複製する source_text() を診断が数万ステップごとに呼ぶと、
スクリプト全体を包む関数(YouTube の kevlar は 10.4MB)のたびに 10MB の一括確保が起きていた。
関数の静的メタデータ(SharedFnInfo)を取得または生成してキャッシュする。
Sourcepub fn cached_var_names(&self) -> Option<Rc<Vec<Rc<str>>>>
pub fn cached_var_names(&self) -> Option<Rc<Vec<Rc<str>>>>
保存済みの var 名(構文木をキャッシュ済みで、名前を計算済みのときだけ)。
Sourcepub fn store_var_names(
&self,
stmts: &Rc<Vec<Statement>>,
names: Rc<Vec<Rc<str>>>,
)
pub fn store_var_names( &self, stmts: &Rc<Vec<Statement>>, names: Rc<Vec<Rc<str>>>, )
var 名を保存する(構文木をキャッシュ済みで、stmts がその構文木のときだけ。
1 回だけ解析して捨てる本体では保存しない)。
Sourcepub(crate) fn code(&self, stmts: &Rc<Vec<Statement>>) -> Option<Rc<Code>>
pub(crate) fn code(&self, stmts: &Rc<Vec<Statement>>) -> Option<Rc<Code>>
バイトコード。何度も呼ばれる本体だけ変換する。構文木をキャッシュ済みで stmts がその構文木の
ときに限る(解析して捨てる本体は変換しない)。ループを含む関数は初回からバイトコードへ変換する。
Sourcepub(crate) fn gen_code(&self, stmts: &Rc<Vec<Statement>>) -> Option<Rc<GenCode>>
pub(crate) fn gen_code(&self, stmts: &Rc<Vec<Statement>>) -> Option<Rc<GenCode>>
generator 本体の VM 命令列(interp::gen_vm)。構文木をキャッシュ済みで stmts がその構文木なら
本体に覚えて使い回す(そうでなければ毎回変換する)。None は VM で扱えない本体(従来の replay で動かす)。
Sourcepub fn stmts(&self) -> Rc<Vec<Statement>>
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 回目に呼ばれた関数だけ構文木をキャッシュする。
Sourcepub fn uses_arguments(&self, stmts: &[Statement]) -> bool
pub fn uses_arguments(&self, stmts: &[Statement]) -> bool
この関数本体(および内側のアロー関数)が arguments または eval を参照するか。
Sourcepub fn has_function_decls(&self, stmts: &[Statement]) -> bool
pub fn has_function_decls(&self, stmts: &[Statement]) -> bool
本体のトップレベルに FunctionDeclaration(または ExportDecl { FunctionDeclaration })が含まれるか。