pub struct JsRuntime {
pub global: Rc<RefCell<Scope>>,
pub out: String,
pub dom: Rc<RefCell<DomBridge>>,
pub microtasks: Rc<RefCell<VecDeque<Job>>>,
pub macrotasks: Rc<RefCell<VecDeque<(Value, Vec<Value>, u64)>>>,
pub base_url: String,
pub modules: ModuleRegistry,
pub last_syntax_errors: Vec<ParseError>,
}Expand description
ページ単位で永続するランタイム状態。
Fields§
§global: Rc<RefCell<Scope>>§out: Stringconsole.* の累積出力(ページ表示やテスト用)。
dom: Rc<RefCell<DomBridge>>JS ⇄ レンダラの DOM ブリッジ(ページ寿命を通じて永続。リスナを保持)。
microtasks: Rc<RefCell<VecDeque<Job>>>Promise の then 反応を遅延実行するマイクロタスクキュー。
macrotasks: Rc<RefCell<VecDeque<(Value, Vec<Value>, u64)>>>setTimeout のコールバック(マクロタスク: callback, args, id)。
base_url: String現在ページの絶対URL(fetch/XHR の相対URL解決の基準。未設定なら空)。
modules: ModuleRegistry登録済み ES モジュール(指定子 → ソース/評価済みエクスポート)。
last_syntax_errors: Vec<ParseError>直近の eval() で検出した構文エラー。
【2026-07-28】パーサはベストエフォートで回復して継続する設計だが、 従来は回復した事実を誰にも伝えていなかったため、壊れたスクリプトが 正常に動いていないのか、そもそも解析に失敗していたのかを区別できなかった。 実行は従来どおり継続しつつ、呼び出し側が参照できるようここへ残す。
Implementations§
Source§impl JsRuntime
impl JsRuntime
pub fn new() -> Self
fn new_interp(&self) -> Interp
Sourcepub fn set_page_url(&mut self, url: &str)
pub fn set_page_url(&mut self, url: &str)
現在ページの絶対URLを設定。fetch/XHR の相対URL基準(base_url)と JS の window/document.location オブジェクトを同時に更新する。
Sourcepub fn define_module(&mut self, specifier: &str, source: &str)
pub fn define_module(&mut self, specifier: &str, source: &str)
ES モジュールをソース付きで登録する(指定子 → ソース)。 import 時に遅延評価される。同じ指定子の再登録は上書き(未評価状態に戻す)。
Sourcepub fn eval(&mut self, source: &str) -> Result<Value, String>
pub fn eval(&mut self, source: &str) -> Result<Value, String>
ソースを評価。戻り値は最後の式の値、または throw された値の文字列化。
構文エラーがあっても(従来どおり)ベストエフォートで実行を続けるが、
検出した構文エラーは必ずログへ出し、last_syntax_errors に残す。
「スクリプトが動かない」ときに、解析で諦めた箇所が分かるようにするため。
Sourcepub fn dispatch_click(&mut self, node_idx: usize) -> bool
pub fn dispatch_click(&mut self, node_idx: usize) -> bool
指定ノードの click イベントを発火する(バブリング対応)。ホストがクリック処理から呼ぶ。
DOM 変更があれば dom.borrow().dirty が立つので、呼び元が再レイアウトする。
Sourcepub fn dispatch_event(&mut self, node_idx: usize, event_type: &str) -> bool
pub fn dispatch_event(&mut self, node_idx: usize, event_type: &str) -> bool
汎用イベントディスパッチ(追加プロパティ無し)。発火したか返す。
Sourcepub fn dispatch_mouse(
&mut self,
node_idx: usize,
event_type: &str,
x: i32,
y: i32,
) -> (bool, bool)
pub fn dispatch_mouse( &mut self, node_idx: usize, event_type: &str, x: i32, y: i32, ) -> (bool, bool)
マウスイベント(click/mouseover/mouseout/mousemove 等)。座標 clientX/clientY/
pageX/pageY と button を付与。戻り値: (リスナ発火, preventDefault されたか)。
Sourcepub fn dispatch_mouse_full(
&mut self,
node_idx: usize,
event_type: &str,
x: i32,
y: i32,
button: i32,
wheel_delta: i32,
) -> (bool, bool)
pub fn dispatch_mouse_full( &mut self, node_idx: usize, event_type: &str, x: i32, y: i32, button: i32, wheel_delta: i32, ) -> (bool, bool)
マウスイベント完全版。button(0=左,1=中,2=右)と wheel_delta を指定でき、
MouseEvent 標準プロパティ(clientX/Y, pageX/Y, screenX/Y, offsetX/Y, movementX/Y,
button, buttons, detail, 修飾キー shiftKey/ctrlKey/altKey/metaKey)を付与する。
wheel イベントの場合は deltaX/deltaY/deltaMode も付与する。
Sourcefn push_modifier_props(extra: &mut Vec<(String, Value)>)
fn push_modifier_props(extra: &mut Vec<(String, Value)>)
現在のグローバル修飾キー状態を shiftKey/ctrlKey/altKey/metaKey として extra へ追加する。
Sourcepub fn dispatch_key(
&mut self,
node_idx: usize,
event_type: &str,
key: &str,
) -> (bool, bool)
pub fn dispatch_key( &mut self, node_idx: usize, event_type: &str, key: &str, ) -> (bool, bool)
キーイベント(keydown/keyup/keypress/input 等)。key/keyCode/which/code/
location/repeat と修飾キーを付与。key は呼び出し側で正規化済みの値を渡す
(“a”, “Enter”, “ArrowLeft” 等)。戻り値: (リスナ発火, preventDefault されたか)。
Sourcepub fn dispatch_key_full(
&mut self,
node_idx: usize,
event_type: &str,
key: &str,
repeat: bool,
) -> (bool, bool)
pub fn dispatch_key_full( &mut self, node_idx: usize, event_type: &str, key: &str, repeat: bool, ) -> (bool, bool)
キーイベント完全版。repeat(オートリピート)を指定できる。
keyCode/which は legacy 仕様に沿って特殊キーへ既定コードを割り当てる。
Sourcefn key_attributes(key: &str) -> (u32, String, u32)
fn key_attributes(key: &str) -> (u32, String, u32)
key 値から (keyCode, code, location) を導出する。
特殊キーは UI Events 仕様の標準 code と legacy keyCode に対応づける。
Sourcepub fn dispatch_event_with(
&mut self,
node_idx: usize,
event_type: &str,
extra: &[(String, Value)],
) -> (bool, bool)
pub fn dispatch_event_with( &mut self, node_idx: usize, event_type: &str, extra: &[(String, Value)], ) -> (bool, bool)
汎用イベントディスパッチ本体: capture(祖先→target)→ target → bubble(target→祖先)の
3フェーズで各ノードのリスナを発火。Event は type/target/currentTarget/eventPhase/bubbles/
defaultPrevented + preventDefault/stopPropagation/stopImmediatePropagation/composedPath を
持ち、extra の各プロパティも付与する。once リスナは発火後に削除する。
戻り値: (リスナが発火したか, defaultPrevented か)。
Sourcepub fn take_pending_reset(&mut self) -> Option<usize>
pub fn take_pending_reset(&mut self) -> Option<usize>
form.reset() が積んだリセット要求を回収する。戻り値: Some(form node idx)。 ホストが JS 実行後に呼んで実リセットする。
history.back/forward/go が積んだナビゲーション要求を回収し 0 にリセットする。 戻り値: 相対移動量(負=戻る / 正=進む / 0=要求なし)。ホストが JS 実行後に呼ぶ。
Sourcepub fn take_pending_location(&mut self) -> Option<(String, String)>
pub fn take_pending_location(&mut self) -> Option<(String, String)>
location への代入や assign/replace/reload が積んだナビゲーション要求を回収する。 戻り値: Some((url, mode)) — mode は “assign”(履歴に積む) / “replace”(置換) / “hash”(同一ページ内フラグメント) / “reload”(再読込)。要求が無ければ None。 ホストが JS 実行後に呼んで実ナビゲーションする。
Sourcefn window_listeners(&self, key: &str) -> Vec<Value>
fn window_listeners(&self, key: &str) -> Vec<Value>
window の隠し props 配列(_scroll_listeners / _popstate_listeners 等)に
登録されたリスナを複製して返す(無ければ空)。エントリは
addEventListener の {signal} 対応(2026-07-14 発見・実装。
builtins::push_window_listener 参照)のため [cb, signal] 形式で
格納されており、ここで abort 済みのものを除外しつつ cb のみへ
展開する。
Sourcefn fire_window_listeners(
&mut self,
listeners: &[Value],
event_type: &str,
extra: &[(String, Value)],
)
fn fire_window_listeners( &mut self, listeners: &[Value], event_type: &str, extra: &[(String, Value)], )
指定リスナ群を共有イベントオブジェクト(type + extra props)で順に発火する。
Sourcepub fn fire_popstate(&mut self)
pub fn fire_popstate(&mut self)
window に登録された popstate リスナを発火する(history.back/forward/go の実ナビ後にホストが呼ぶ)。
state は history.state を引き継ぐ。
Sourcepub fn has_spa_back(&self) -> bool
pub fn has_spa_back(&self) -> bool
SPA back スタック(pushState で積んだ旧 URL/state)にエントリがあるか。
Sourcepub fn spa_go_back(&mut self) -> Option<String>
pub fn spa_go_back(&mut self) -> Option<String>
SPA back スタックから1エントリ取り出す。history.state・location.href を復元し popstate を発火する。戻り値: 復元した URL(呼び元がアドレスバー更新に使う)。
Sourcepub fn spa_go_forward(&mut self) -> Option<String>
pub fn spa_go_forward(&mut self) -> Option<String>
SPA forward スタックから1エントリ取り出す。戻り値: 復元した URL。
Sourcepub fn has_spa_forward(&self) -> bool
pub fn has_spa_forward(&self) -> bool
SPA forward スタックにエントリがあるか。
Sourcepub fn fire_scroll(&mut self, scroll_y: i32)
pub fn fire_scroll(&mut self, scroll_y: i32)
window に登録された scroll リスナを発火する(スクロール位置が変化したときホストが呼ぶ)。 併せて window.scrollY / pageYOffset を更新し、Event に scrollY を載せる。
Sourcepub fn fire_hashchange(&mut self, old_url: &str, new_url: &str)
pub fn fire_hashchange(&mut self, old_url: &str, new_url: &str)
window に登録された hashchange リスナを発火する(location.hash 変更 / アンカー(#)ナビ後にホストが呼ぶ)。 HashChangeEvent 互換で oldURL / newURL を載せる。併せて location.hash を新値へ更新する。
Sourcepub fn fire_resize(&mut self, width: i32, height: i32)
pub fn fire_resize(&mut self, width: i32, height: i32)
window に登録された resize リスナを発火する(ウィンドウ/ビューポートサイズ変化時にホストが呼ぶ)。 併せて window.innerWidth / innerHeight を更新し、Event に幅・高さを載せる。