diff --git a/crates/oxide_vm/src/vm.rs b/crates/oxide_vm/src/vm.rs index fb8f5d9d..1c1759ab 100644 --- a/crates/oxide_vm/src/vm.rs +++ b/crates/oxide_vm/src/vm.rs @@ -524,6 +524,10 @@ pub struct Vm { /// 本次 run 顶层脚本的严格模式标志(`module.is_strict`,run 时填充): /// 无帧且无 inline(顶层脚本赋值)时写路径的 strict/sloppy 判定来源。 pub(crate) top_level_strict: bool, + /// 顶层 this 值(run 初始化时记录:脚本 = 全局对象,ES module = undefined)。 + /// rerun 清空寄存器文件后据此恢复 regs[254]——不恢复则重执行时依赖 this + /// 的顶层写(顶层 var 全局同步写)在非对象 this 上静默 no-op。 + pub(crate) top_level_this: JsValue, /// inline 同步调用寄存器窗口缓冲池:`save_inline_state` 取出复用、 /// `restore_inline_state` 归还。热回调循环内 save/restore 反复使用同一块 /// 缓冲,只在嵌套(池已被外层取走)时新分配。 diff --git a/crates/oxide_vm/src/vm_runtime.rs b/crates/oxide_vm/src/vm_runtime.rs index 31dc8805..8edbbaa7 100644 --- a/crates/oxide_vm/src/vm_runtime.rs +++ b/crates/oxide_vm/src/vm_runtime.rs @@ -525,10 +525,16 @@ impl Vm { } /// 重新执行当前已加载的 bytecode:清空执行状态并重置 IC 缓存后再次 dispatch。 + /// + /// # 注意事项 + /// - 清空寄存器文件后按 run 初始状态恢复顶层 this(regs[254]):不恢复则 + /// 重执行中依赖 this 的顶层写(顶层 var 全局同步写)在 undefined 上 + /// 静默 no-op,循环计数不推进。 pub fn rerun(&mut self) -> Result { vm_info!("rerun: clearing IC caches"); self.clear_execution_state(); self.active_reg_limit = self.root_reg_limit; + self.regs[254] = self.top_level_this; crate::ic_helper::clear_ic_caches(self.bytecode_mut()); self.dispatch() } @@ -609,13 +615,16 @@ impl Vm { } // 顶层 this:脚本为全局对象(ECMA-262 全局执行上下文); - // ES module 顶层环境 GetThisBinding 返回 undefined。 + // ES module 顶层环境 GetThisBinding 返回 undefined。记录到 + // top_level_this:rerun 清空寄存器文件后按此恢复顶层 this。 let global = self.session.global_object(); - self.regs[254] = if module.is_es_module { + let this_val = if module.is_es_module { JsValue::undefined() } else { JsValue::from_js_object(global.as_ptr() as *mut JsObject) }; + self.top_level_this = this_val; + self.regs[254] = this_val; let result = self.dispatch(); // 顶层执行结束后 drain 微任务队列:Promise reactions 与 thenable 委托在此执行。 diff --git a/crates/oxide_vm/src/vm_support.rs b/crates/oxide_vm/src/vm_support.rs index 5bc14ecb..dda4ce06 100644 --- a/crates/oxide_vm/src/vm_support.rs +++ b/crates/oxide_vm/src/vm_support.rs @@ -99,6 +99,7 @@ impl Vm { inline_strict: false, inline_frames_base: 0, top_level_strict: false, + top_level_this: JsValue::undefined(), inline_reg_pool: None, generator_suspended: None, delegated_iterator: None, @@ -227,6 +228,7 @@ impl Vm { inline_strict: false, inline_frames_base: 0, top_level_strict: false, + top_level_this: JsValue::undefined(), inline_reg_pool: None, generator_suspended: None, delegated_iterator: None,