fix: rerun 恢复顶层 this——清空寄存器文件后依赖 this 的顶层写(顶层 var 全局同步写)在重执行中不再静默丢弃

This commit is contained in:
2026-09-12 21:36:10 +00:00
parent d9c469e9d0
commit 60274baca8
3 changed files with 17 additions and 2 deletions
+4
View File
@@ -524,6 +524,10 @@ pub struct Vm {
/// 本次 run 顶层脚本的严格模式标志(`module.is_strict`,run 时填充): /// 本次 run 顶层脚本的严格模式标志(`module.is_strict`,run 时填充):
/// 无帧且无 inline(顶层脚本赋值)时写路径的 strict/sloppy 判定来源。 /// 无帧且无 inline(顶层脚本赋值)时写路径的 strict/sloppy 判定来源。
pub(crate) top_level_strict: bool, 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` 取出复用、 /// inline 同步调用寄存器窗口缓冲池:`save_inline_state` 取出复用、
/// `restore_inline_state` 归还。热回调循环内 save/restore 反复使用同一块 /// `restore_inline_state` 归还。热回调循环内 save/restore 反复使用同一块
/// 缓冲,只在嵌套(池已被外层取走)时新分配。 /// 缓冲,只在嵌套(池已被外层取走)时新分配。
+11 -2
View File
@@ -525,10 +525,16 @@ impl Vm {
} }
/// 重新执行当前已加载的 bytecode:清空执行状态并重置 IC 缓存后再次 dispatch。 /// 重新执行当前已加载的 bytecode:清空执行状态并重置 IC 缓存后再次 dispatch。
///
/// # 注意事项
/// - 清空寄存器文件后按 run 初始状态恢复顶层 this(regs[254]):不恢复则
/// 重执行中依赖 this 的顶层写(顶层 var 全局同步写)在 undefined 上
/// 静默 no-op,循环计数不推进。
pub fn rerun(&mut self) -> Result<JsValue, String> { pub fn rerun(&mut self) -> Result<JsValue, String> {
vm_info!("rerun: clearing IC caches"); vm_info!("rerun: clearing IC caches");
self.clear_execution_state(); self.clear_execution_state();
self.active_reg_limit = self.root_reg_limit; self.active_reg_limit = self.root_reg_limit;
self.regs[254] = self.top_level_this;
crate::ic_helper::clear_ic_caches(self.bytecode_mut()); crate::ic_helper::clear_ic_caches(self.bytecode_mut());
self.dispatch() self.dispatch()
} }
@@ -609,13 +615,16 @@ impl Vm {
} }
// 顶层 this:脚本为全局对象(ECMA-262 全局执行上下文); // 顶层 this:脚本为全局对象(ECMA-262 全局执行上下文);
// ES module 顶层环境 GetThisBinding 返回 undefined。 // ES module 顶层环境 GetThisBinding 返回 undefined。记录到
// top_level_this:rerun 清空寄存器文件后按此恢复顶层 this。
let global = self.session.global_object(); let global = self.session.global_object();
self.regs[254] = if module.is_es_module { let this_val = if module.is_es_module {
JsValue::undefined() JsValue::undefined()
} else { } else {
JsValue::from_js_object(global.as_ptr() as *mut JsObject) 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(); let result = self.dispatch();
// 顶层执行结束后 drain 微任务队列:Promise reactions 与 thenable 委托在此执行。 // 顶层执行结束后 drain 微任务队列:Promise reactions 与 thenable 委托在此执行。
+2
View File
@@ -99,6 +99,7 @@ impl Vm {
inline_strict: false, inline_strict: false,
inline_frames_base: 0, inline_frames_base: 0,
top_level_strict: false, top_level_strict: false,
top_level_this: JsValue::undefined(),
inline_reg_pool: None, inline_reg_pool: None,
generator_suspended: None, generator_suspended: None,
delegated_iterator: None, delegated_iterator: None,
@@ -227,6 +228,7 @@ impl Vm {
inline_strict: false, inline_strict: false,
inline_frames_base: 0, inline_frames_base: 0,
top_level_strict: false, top_level_strict: false,
top_level_this: JsValue::undefined(),
inline_reg_pool: None, inline_reg_pool: None,
generator_suspended: None, generator_suspended: None,
delegated_iterator: None, delegated_iterator: None,