feat: 写路径四路加函数名不可写守卫,sloppy 静默 strict 抛 TypeError

This commit is contained in:
2026-10-09 22:20:20 +00:00
parent 4f6bd5d947
commit 6ba2043599
10 changed files with 150 additions and 30 deletions
+10
View File
@@ -116,6 +116,9 @@ pub struct CompileCtx {
/// 本函数从父函数捕获的 const 绑定名:子 ctx 不继承父函数作用域符号表,
/// 捕获 const 信息随 upvalue 收集一并快照,供 const 写检查(编译期拦截)使用。
pub(crate) upvalue_const_flags: HashSet<String>,
/// 本函数从父函数捕获的函数名不可写绑定名:子 ctx 不继承父函数作用域符号表,
/// 捕获不可写信息随 upvalue 收集一并快照,供写路径 non_writable 守卫(编译期拦截)使用。
pub(crate) upvalue_non_writable_flags: HashSet<String>,
/// 未声明标识符读所分配的全局槽寄存器集合:标识符首次读未命中任何作用域时,
/// `lookup_or_builtin` 按隐式全局登记并记录其寄存器,后续读取据此发射
/// LOAD_GLOBAL(运行期查 global object 属性,缺失抛 ReferenceError)。
@@ -286,6 +289,7 @@ impl CompileCtx {
pending_for_head_names: HashSet::new(),
global_tier_names: HashSet::new(),
upvalue_const_flags: HashSet::new(),
upvalue_non_writable_flags: HashSet::new(),
implicit_global_reads: HashSet::new(),
implicit_global_writes: HashSet::new(),
function_length: 0,
@@ -568,6 +572,12 @@ impl CompileCtx {
self.scopes.symbols.lookup_is_const(name) || self.upvalue_const_flags.contains(name)
}
/// 写路径 non_writable 守卫查询:名字是否为函数名不可写绑定(本作用域或 upvalue 捕获)。
pub(crate) fn lookup_non_writable_flag(&self, name: &str) -> bool {
// upvalue 捕获的函数名:子 ctx 符号表不含父函数作用域绑定,查快照标志。
self.scopes.symbols.lookup_non_writable(name) || self.upvalue_non_writable_flags.contains(name)
}
pub(crate) fn init_var(&mut self, name: &str) {
self.scopes.symbols.init_var(name);
}
+8 -2
View File
@@ -72,10 +72,16 @@ impl Emitter {
/// const 写检查:已初始化的 const 绑定再赋值编译期抛 TypeError(与槽值无关)。
/// 简单赋值在 RHS 求值之后、复合/更新在读旧值之前调用;解构赋值在写目标时调用。
///
/// # 边界与前提
/// - 函数名不可写绑定同形拦截:strict 抛 TypeError;sloppy 不抛(put 静默失败),
/// 由调用点跳过写指令(值照算)。
///
/// # 副作用
/// - const 命中时发射 THROW 指令序列,其后写指令不可达但保持寄存器良定义。
/// - const 或 strict 函数名不可写命中时发射 THROW 指令序列,其后写指令不可达但保持寄存器良定义。
pub(crate) fn emit_const_write_guard(&self, name: &str, ctx: &mut CompileCtx) -> Result<(), String> {
if ctx.lookup_const_flag(name) {
// const 与 strict 函数名不可写同形抛 TypeError(值无关);sloppy 函数名不可写不抛,
// 由调用点跳过写指令。
if ctx.lookup_const_flag(name) || (ctx.lookup_non_writable_flag(name) && ctx.is_strict) {
let _ = self.emit_throw_error("TypeError", "Assignment to constant variable", ctx)?;
}
Ok(())
+39
View File
@@ -24,6 +24,32 @@ impl Emitter {
// 循环 update 段的 let/const 循环变量是 per-iteration 可变绑定,复合写同样合法,豁免。
if !ctx.register_update_names.iter().any(|n| n == name) {
self.emit_const_write_guard(name, ctx)?;
// 函数名不可写绑定:put 永不成功——sloppy 值照算(表达式值 = 运算结果),
// 跳过槽写;strict 已在 emit_const_write_guard 抛 TypeError(不可达)。
if ctx.lookup_non_writable_flag(name) {
let val_reg = ctx.alloc_reg();
let uv_idx = ctx.current_upvalue_captures.iter().position(|u| u.name == name);
if let Some(uv) = uv_idx {
ctx.inst(Inst::new(
OpCode::LOAD_UPVALUE,
Operand::Reg(val_reg),
Operand::Imm(uv as u16),
Operand::None,
));
} else if let Some((cell_idx, binding_reg)) = ctx.visible_cell(name) {
ctx.inst(Inst::new(
OpCode::CELL_GET,
Operand::Reg(val_reg),
Operand::Reg(binding_reg),
Operand::Imm(cell_idx as u16),
));
} else {
let var_reg = ctx.lookup_or_global(name);
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(val_reg), Operand::Reg(var_reg), Operand::None));
}
ctx.inst(Inst::new(op, Operand::Reg(val_reg), Operand::Reg(rhs), Operand::None));
return Ok(val_reg);
}
}
// 循环 update 段:被捕获绑定走寄存器 RMW——C 风格 for 的 let/const
// 循环变量每迭代新分配一个 cell,update 写寄存器供其拷入,不污染本迭代
@@ -433,6 +459,19 @@ impl Emitter {
let val_reg = self.emit_expression(&assign.right, ctx)?;
// 短路未通过才写:const 目标在此抛 TypeError(编译期拦截,值无关)。
self.emit_const_write_guard(name, ctx)?;
// 函数名不可写绑定(sloppy):put 永不成功,跳过写与命名空间写穿,
// 表达式值 = RHS(短路已通过故取 val_reg)。strict 已在
// emit_const_write_guard 抛 TypeError(不可达)。
if ctx.lookup_non_writable_flag(name) {
ctx.inst(Inst::new(
OpCode::LOAD_VAR,
Operand::Reg(result_reg),
Operand::Reg(val_reg),
Operand::None,
));
ctx.labels.set_label_pos(end_label, ctx.insts.len());
return Ok(result_reg);
}
let const_flag = if ctx.lookup_const_flag(name) { 1 } else { 0 };
if let Some(uv) = uv_idx {
ctx.inst(Inst::new(
+8
View File
@@ -181,6 +181,14 @@ impl Emitter {
let _ = self.emit_throw_error("TypeError", "Assignment to constant variable", ctx)?;
return Ok(());
}
// 函数名不可写绑定:put 永不成功——sloppy 静默跳过写指令(RHS 已求值,副作用保留),
// strict 抛 TypeError(与 const 路径同形,值无关)。with 回退写经本入口同形覆盖。
if !in_loop_update && ctx.lookup_non_writable_flag(name) {
if ctx.is_strict {
let _ = self.emit_throw_error("TypeError", "Assignment to constant variable", ctx)?;
}
return Ok(());
}
// 循环 update 段:被捕获绑定走寄存器而非 cell——C 风格 for 的 let/const
// 循环变量每迭代新分配一个 cell,update 写寄存器供其拷入,不污染本迭代
// 闭包捕获的 cell(机制见 `compile_ctx.rs` 的 `register_update_names` 字段文档)。
@@ -659,6 +659,41 @@ impl Emitter {
self.emit_identifier_tdz_guard(name, ctx)?;
if !ctx.register_update_names.iter().any(|n| n == name) {
self.emit_const_write_guard(name, ctx)?;
// 函数名不可写绑定(sloppy):put 永不成功,值照算(前缀返回新值、后缀返回
// 旧值),跳过槽写;strict 已在 emit_const_write_guard 抛 TypeError(不可达)。
if ctx.lookup_non_writable_flag(name) {
let uv_idx = ctx.current_upvalue_captures.iter().position(|u| u.name == name);
let old_reg = ctx.alloc_reg();
if let Some(uv) = uv_idx {
ctx.inst(Inst::new(
OpCode::LOAD_UPVALUE,
Operand::Reg(old_reg),
Operand::Imm(uv as u16),
Operand::None,
));
} else if let Some((cell_idx, binding_reg)) = ctx.visible_cell(name) {
ctx.inst(Inst::new(
OpCode::CELL_GET,
Operand::Reg(old_reg),
Operand::Reg(binding_reg),
Operand::Imm(cell_idx as u16),
));
} else {
let var_reg = ctx.lookup_or_global(name);
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(old_reg), Operand::Reg(var_reg), Operand::None));
}
let one_idx = ctx.add_constant(Constant::Int(1));
let one_reg = ctx.alloc_reg();
ctx.inst(Inst::load_const(Operand::Reg(one_reg), one_idx));
let op = if update.operator == UpdateOperator::Increment {
OpCode::ADD
} else {
OpCode::SUB
};
let new_reg = ctx.alloc_reg();
ctx.inst(Inst::new(op, Operand::Reg(new_reg), Operand::Reg(old_reg), Operand::Reg(one_reg)));
return Ok(if update.prefix { new_reg } else { old_reg });
}
}
let uv_idx = ctx.current_upvalue_captures.iter().position(|u| u.name == name);
// 循环 update 段:被捕获绑定走寄存器 INC/DEC——C 风格 for 的 let/const
+16
View File
@@ -826,6 +826,22 @@ impl Emitter {
})
.map(|u| u.name.clone())
.collect();
// 捕获函数名不可写信息快照:与 const 快照同口径,供子函数写路径
// non_writable 守卫(sloppy 静默 no-op / strict 抛 TypeError)使用。
ctx.upvalue_non_writable_flags = ctx
.current_upvalue_captures
.iter()
.filter(|u| u.parent_uv_idx.is_none())
.filter(|u| {
parent_ctx
.scopes
.symbols
.lookup_any_binding(u.name.as_str())
.map(|(b, _)| b.non_writable)
.unwrap_or(false)
})
.map(|u| u.name.clone())
.collect();
// 类字段 computed key 数组等合成捕获:直接追加 upvalue(cell_idx 由父分配)。
for (name, cell_idx) in extra_upvalue_names {
if !ctx.own_bindings.contains(*name) && !ctx.current_upvalue_captures.iter().any(|u| u.name == *name) {