fix: eval 脚本 var 声明落全局 configurable:true——is_eval_script 穿透

This commit is contained in:
2026-09-09 19:03:23 +08:00
parent d9c2847730
commit 3f393b6acb
14 changed files with 188 additions and 40 deletions
+11 -3
View File
@@ -7,7 +7,7 @@ use oxide_ir::lower::lower;
/// 结构哈希 / 编译模块哈希 re-export(供编译缓存作键)。
pub use crate::hash::{compiled_module_hash, structural_hash};
/// 编译入口:状态 = DCE 开关 + RegAlloc 开关 + REPL 持久开关,编译其余状态在 emit/lower 内部。
/// 编译入口:状态 = DCE 开关 + RegAlloc 开关 + REPL 持久开关 + eval 脚本标记,编译其余状态在 emit/lower 内部。
pub struct Compiler {
/// 是否在 emit 与 lower 之间运行死代码消除(默认开启)。
enable_dce: bool,
@@ -16,15 +16,18 @@ pub struct Compiler {
enable_regalloc: bool,
/// 是否为 REPL 持久模式:脚本顶层 let/const 也写全局对象属性。
repl_persist: bool,
/// 是否为 eval 脚本:顶层 var/function 声明落全局属性 configurable:true。
is_eval_script: bool,
}
impl Compiler {
/// 构造 `Compiler`:默认开启 DCE 与 RegAlloc,不开启 REPL 持久。
/// 构造 `Compiler`:默认开启 DCE 与 RegAlloc,不开启 REPL 持久与 eval 脚本标记。
pub fn new() -> Self {
Self {
enable_dce: true,
enable_regalloc: true,
repl_persist: false,
is_eval_script: false,
}
}
@@ -46,10 +49,15 @@ impl Compiler {
Self { repl_persist: enable, ..self }
}
/// 标记为 eval 脚本(顶层 var/function 声明落全局属性 configurable:true)。
pub fn with_eval_script(self, enable: bool) -> Self {
Self { is_eval_script: enable, ..self }
}
/// 编译整个 script program:AST → IR(`Emitter::emit_program`)→ 统一 IR 管线。
pub fn compile(&self, program: &oxide_parser::Program) -> Result<CompiledModule, String> {
crate::compiler_debug!("compile: starting...");
let ir = Emitter::new().emit_program(program, self.repl_persist)?;
let ir = Emitter::new().emit_program(program, self.repl_persist, self.is_eval_script)?;
self.compile_ir(ir)
}