Skip to main content

JsRuntime

Struct JsRuntime 

Source
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: String

console.* の累積出力(ページ表示やテスト用)。

§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

Source

pub fn new() -> Self

Source

fn new_interp(&self) -> Interp

Source

pub fn set_page_url(&mut self, url: &str)

現在ページの絶対URLを設定。fetch/XHR の相対URL基準(base_url)と JS の window/document.location オブジェクトを同時に更新する。

Source

pub fn define_module(&mut self, specifier: &str, source: &str)

ES モジュールをソース付きで登録する(指定子 → ソース)。 import 時に遅延評価される。同じ指定子の再登録は上書き(未評価状態に戻す)。

Source

pub fn eval(&mut self, source: &str) -> Result<Value, String>

ソースを評価。戻り値は最後の式の値、または throw された値の文字列化。

構文エラーがあっても(従来どおり)ベストエフォートで実行を続けるが、 検出した構文エラーは必ずログへ出し、last_syntax_errors に残す。 「スクリプトが動かない」ときに、解析で諦めた箇所が分かるようにするため。

Source

pub fn dispatch_click(&mut self, node_idx: usize) -> bool

指定ノードの click イベントを発火する(バブリング対応)。ホストがクリック処理から呼ぶ。 DOM 変更があれば dom.borrow().dirty が立つので、呼び元が再レイアウトする。

Source

pub fn dispatch_event(&mut self, node_idx: usize, event_type: &str) -> bool

汎用イベントディスパッチ(追加プロパティ無し)。発火したか返す。

Source

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/pageYbutton を付与。戻り値: (リスナ発火, preventDefault されたか)。

Source

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 も付与する。

Source

fn push_modifier_props(extra: &mut Vec<(String, Value)>)

現在のグローバル修飾キー状態を shiftKey/ctrlKey/altKey/metaKey として extra へ追加する。

Source

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 されたか)。

Source

pub fn dispatch_key_full( &mut self, node_idx: usize, event_type: &str, key: &str, repeat: bool, ) -> (bool, bool)

キーイベント完全版。repeat(オートリピート)を指定できる。 keyCode/which は legacy 仕様に沿って特殊キーへ既定コードを割り当てる。

Source

fn key_attributes(key: &str) -> (u32, String, u32)

key 値から (keyCode, code, location) を導出する。 特殊キーは UI Events 仕様の標準 code と legacy keyCode に対応づける。

Source

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 か)。

Source

pub fn take_pending_reset(&mut self) -> Option<usize>

form.reset() が積んだリセット要求を回収する。戻り値: Some(form node idx)。 ホストが JS 実行後に呼んで実リセットする。

Source

pub fn take_pending_nav(&mut self) -> i64

history.back/forward/go が積んだナビゲーション要求を回収し 0 にリセットする。 戻り値: 相対移動量(負=戻る / 正=進む / 0=要求なし)。ホストが JS 実行後に呼ぶ。

Source

pub fn take_pending_location(&mut self) -> Option<(String, String)>

location への代入や assign/replace/reload が積んだナビゲーション要求を回収する。 戻り値: Some((url, mode)) — mode は “assign”(履歴に積む) / “replace”(置換) / “hash”(同一ページ内フラグメント) / “reload”(再読込)。要求が無ければ None。 ホストが JS 実行後に呼んで実ナビゲーションする。

Source

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 のみへ 展開する。

Source

fn fire_window_listeners( &mut self, listeners: &[Value], event_type: &str, extra: &[(String, Value)], )

指定リスナ群を共有イベントオブジェクト(type + extra props)で順に発火する。

Source

pub fn fire_popstate(&mut self)

window に登録された popstate リスナを発火する(history.back/forward/go の実ナビ後にホストが呼ぶ)。 state は history.state を引き継ぐ。

Source

pub fn has_spa_back(&self) -> bool

SPA back スタック(pushState で積んだ旧 URL/state)にエントリがあるか。

Source

pub fn spa_go_back(&mut self) -> Option<String>

SPA back スタックから1エントリ取り出す。history.state・location.href を復元し popstate を発火する。戻り値: 復元した URL(呼び元がアドレスバー更新に使う)。

Source

pub fn spa_go_forward(&mut self) -> Option<String>

SPA forward スタックから1エントリ取り出す。戻り値: 復元した URL。

Source

pub fn has_spa_forward(&self) -> bool

SPA forward スタックにエントリがあるか。

Source

pub fn fire_scroll(&mut self, scroll_y: i32)

window に登録された scroll リスナを発火する(スクロール位置が変化したときホストが呼ぶ)。 併せて window.scrollY / pageYOffset を更新し、Event に scrollY を載せる。

Source

pub fn fire_hashchange(&mut self, old_url: &str, new_url: &str)

window に登録された hashchange リスナを発火する(location.hash 変更 / アンカー(#)ナビ後にホストが呼ぶ)。 HashChangeEvent 互換で oldURL / newURL を載せる。併せて location.hash を新値へ更新する。

Source

pub fn fire_resize(&mut self, width: i32, height: i32)

window に登録された resize リスナを発火する(ウィンドウ/ビューポートサイズ変化時にホストが呼ぶ)。 併せて window.innerWidth / innerHeight を更新し、Event に幅・高さを載せる。

Trait Implementations§

Source§

impl Default for JsRuntime

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.