From 60274baca8c19b6d72a44e31b46b02c738da614c Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Sat, 12 Sep 2026 21:36:10 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20rerun=20=E6=81=A2=E5=A4=8D=E9=A1=B6?= =?UTF-8?q?=E5=B1=82=20this=E2=80=94=E2=80=94=E6=B8=85=E7=A9=BA=E5=AF=84?= =?UTF-8?q?=E5=AD=98=E5=99=A8=E6=96=87=E4=BB=B6=E5=90=8E=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=20this=20=E7=9A=84=E9=A1=B6=E5=B1=82=E5=86=99=EF=BC=88?= =?UTF-8?q?=E9=A1=B6=E5=B1=82=20var=20=E5=85=A8=E5=B1=80=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E5=86=99=EF=BC=89=E5=9C=A8=E9=87=8D=E6=89=A7=E8=A1=8C=E4=B8=AD?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E9=9D=99=E9=BB=98=E4=B8=A2=E5=BC=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_vm/src/vm.rs | 4 ++++ crates/oxide_vm/src/vm_runtime.rs | 13 +++++++++++-- crates/oxide_vm/src/vm_support.rs | 2 ++ 3 files changed, 17 insertions(+), 2 deletions(-) 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,