mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-26 19:51:11 +08:00
fix: delete 补括号剥层与非引用目标恒真臂,未声明名归真
This commit is contained in:
@@ -81,7 +81,60 @@ impl Emitter {
|
|||||||
|
|
||||||
fn emit_unary_expression(&self, un: &oxide_parser::UnaryExpression, ctx: &mut CompileCtx) -> Result<u32, String> {
|
fn emit_unary_expression(&self, un: &oxide_parser::UnaryExpression, ctx: &mut CompileCtx) -> Result<u32, String> {
|
||||||
if matches!(un.operator, UnaryOperator::Delete) {
|
if matches!(un.operator, UnaryOperator::Delete) {
|
||||||
return match &un.argument {
|
// 括号的引用性透传内层操作数:先逐层剥括号,再按内层形态分派。
|
||||||
|
let mut operand = &un.argument;
|
||||||
|
while let Expression::ParenthesizedExpression(paren) = operand {
|
||||||
|
operand = &paren.expression;
|
||||||
|
}
|
||||||
|
return self.emit_delete_operand(operand, ctx);
|
||||||
|
}
|
||||||
|
let arg = if matches!(un.operator, UnaryOperator::Typeof) {
|
||||||
|
self.emit_typeof_operand(&un.argument, ctx)?
|
||||||
|
} else {
|
||||||
|
self.emit_expression(&un.argument, ctx)?
|
||||||
|
};
|
||||||
|
match un.operator {
|
||||||
|
UnaryOperator::UnaryNegation => {
|
||||||
|
ctx.inst(Inst::new(OpCode::NEG, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
||||||
|
Ok(arg)
|
||||||
|
}
|
||||||
|
UnaryOperator::Typeof => {
|
||||||
|
ctx.inst(Inst::new(OpCode::TYPEOF, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
||||||
|
Ok(arg)
|
||||||
|
}
|
||||||
|
UnaryOperator::Void => {
|
||||||
|
ctx.inst(Inst::new(OpCode::VOID, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
||||||
|
Ok(arg)
|
||||||
|
}
|
||||||
|
UnaryOperator::LogicalNot => {
|
||||||
|
ctx.inst(Inst::new(OpCode::NOT, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
||||||
|
Ok(arg)
|
||||||
|
}
|
||||||
|
UnaryOperator::BitwiseNot => {
|
||||||
|
ctx.inst(Inst::new(OpCode::BIT_NOT, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
||||||
|
Ok(arg)
|
||||||
|
}
|
||||||
|
UnaryOperator::UnaryPlus => {
|
||||||
|
ctx.inst(Inst::new(OpCode::UNARY_PLUS, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
||||||
|
Ok(arg)
|
||||||
|
}
|
||||||
|
UnaryOperator::Delete => Err("invalid delete target".into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// delete 操作数求值:引用形态(标识符/静态成员/计算成员/链式)按引用语义
|
||||||
|
/// 删除并返回删除结果;非引用操作数返回 true。
|
||||||
|
///
|
||||||
|
/// # 步骤
|
||||||
|
/// 1. 标识符:with 体动态解析、可删全局内置镜像槽先行;未声明名(含隐式
|
||||||
|
/// 全局槽,引用不可解析或全局属性可配置)返回 true;其余已声明绑定
|
||||||
|
/// (var/let/函数名/参数,非可配置或属性引用)返回 false。严格模式的
|
||||||
|
/// delete 标识符是早期错误,由语义分析阶段拦截,不在此出现。
|
||||||
|
/// 2. 静态/计算成员与链式成员:从对象真删属性,结果为删除返回值。
|
||||||
|
/// 3. 其余形态(字面量/调用/new/this/一元形/序列/嵌套 delete 等):先求值
|
||||||
|
/// 操作数(副作用与操作数求值期错误不被吞),再发常量 true。
|
||||||
|
fn emit_delete_operand(&self, operand: &Expression, ctx: &mut CompileCtx) -> Result<u32, String> {
|
||||||
|
match operand {
|
||||||
Expression::Identifier(ident) => {
|
Expression::Identifier(ident) => {
|
||||||
let name = ident.name.as_str();
|
let name = ident.name.as_str();
|
||||||
// with 体内自由标识符:对象有该属性则删除对象属性(返回删除结果),
|
// with 体内自由标识符:对象有该属性则删除对象属性(返回删除结果),
|
||||||
@@ -104,12 +157,7 @@ impl Emitter {
|
|||||||
// 复制 obj 到临时寄存器执行删除:DELETE_PROP_DYNAMIC 把结果写回
|
// 复制 obj 到临时寄存器执行删除:DELETE_PROP_DYNAMIC 把结果写回
|
||||||
// rd 槽,直接用它会把 with 对象寄存器覆盖为布尔值。
|
// rd 槽,直接用它会把 with 对象寄存器覆盖为布尔值。
|
||||||
let tmp_reg = ctx.alloc_reg();
|
let tmp_reg = ctx.alloc_reg();
|
||||||
ctx.inst(Inst::new(
|
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(tmp_reg), Operand::Reg(obj_reg), Operand::None));
|
||||||
OpCode::LOAD_VAR,
|
|
||||||
Operand::Reg(tmp_reg),
|
|
||||||
Operand::Reg(obj_reg),
|
|
||||||
Operand::None,
|
|
||||||
));
|
|
||||||
ctx.inst(Inst::new(
|
ctx.inst(Inst::new(
|
||||||
OpCode::DELETE_PROP_DYNAMIC,
|
OpCode::DELETE_PROP_DYNAMIC,
|
||||||
Operand::Reg(tmp_reg),
|
Operand::Reg(tmp_reg),
|
||||||
@@ -139,10 +187,17 @@ impl Emitter {
|
|||||||
ctx.inst(Inst::delete_global_prop_c(Operand::Reg(reg), Operand::Reg(slot_reg), key_idx));
|
ctx.inst(Inst::delete_global_prop_c(Operand::Reg(reg), Operand::Reg(slot_reg), key_idx));
|
||||||
return Ok(reg);
|
return Ok(reg);
|
||||||
}
|
}
|
||||||
// 其余标识符(三常量 c:false / 局部遮蔽 / var 名等)非严格语义
|
// 已声明绑定(upvalue/捕获 cell/局部 var/let/函数名/参数/顶层 var)
|
||||||
// 返回 false;严格模式的 delete 标识符由 oxc_semantic 提前拦截
|
// 是非属性引用或全局对象上不可配置绑定,返回 false;未声明名
|
||||||
// 为早期错误(未声明名 delete 应返 true 的缺失是既有缺口)。
|
// (引用不可解析)与隐式全局槽(全局属性可配置)返回 true。
|
||||||
let idx = ctx.add_constant(Constant::Boolean(false));
|
// 严格模式的 delete 标识符由语义分析提前拦截为早期错误。
|
||||||
|
let declared = ctx.captured_bindings.contains_key(name)
|
||||||
|
|| ctx.current_upvalue_captures.iter().any(|u| u.name == name)
|
||||||
|
|| match ctx.lookup(name) {
|
||||||
|
Err(_) => false,
|
||||||
|
Ok(reg) => !ctx.is_implicit_global_reg(reg),
|
||||||
|
};
|
||||||
|
let idx = ctx.add_constant(Constant::Boolean(!declared));
|
||||||
let reg = ctx.alloc_reg();
|
let reg = ctx.alloc_reg();
|
||||||
ctx.inst(Inst::load_const(Operand::Reg(reg), idx));
|
ctx.inst(Inst::load_const(Operand::Reg(reg), idx));
|
||||||
Ok(reg)
|
Ok(reg)
|
||||||
@@ -216,40 +271,15 @@ impl Emitter {
|
|||||||
ctx.labels.set_label_pos(end_label, ctx.insts.len());
|
ctx.labels.set_label_pos(end_label, ctx.insts.len());
|
||||||
Ok(result_reg)
|
Ok(result_reg)
|
||||||
}
|
}
|
||||||
_ => Err("invalid delete target".into()),
|
_ => {
|
||||||
};
|
// 非引用操作数恒 true,但操作数本身须先求值:副作用与操作数
|
||||||
|
// 求值期错误(如未声明基引用)不被吞。
|
||||||
|
let _ = self.emit_expression(operand, ctx)?;
|
||||||
|
let idx = ctx.add_constant(Constant::Boolean(true));
|
||||||
|
let reg = ctx.alloc_reg();
|
||||||
|
ctx.inst(Inst::load_const(Operand::Reg(reg), idx));
|
||||||
|
Ok(reg)
|
||||||
}
|
}
|
||||||
let arg = if matches!(un.operator, UnaryOperator::Typeof) {
|
|
||||||
self.emit_typeof_operand(&un.argument, ctx)?
|
|
||||||
} else {
|
|
||||||
self.emit_expression(&un.argument, ctx)?
|
|
||||||
};
|
|
||||||
match un.operator {
|
|
||||||
UnaryOperator::UnaryNegation => {
|
|
||||||
ctx.inst(Inst::new(OpCode::NEG, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
|
||||||
Ok(arg)
|
|
||||||
}
|
|
||||||
UnaryOperator::Typeof => {
|
|
||||||
ctx.inst(Inst::new(OpCode::TYPEOF, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
|
||||||
Ok(arg)
|
|
||||||
}
|
|
||||||
UnaryOperator::Void => {
|
|
||||||
ctx.inst(Inst::new(OpCode::VOID, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
|
||||||
Ok(arg)
|
|
||||||
}
|
|
||||||
UnaryOperator::LogicalNot => {
|
|
||||||
ctx.inst(Inst::new(OpCode::NOT, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
|
||||||
Ok(arg)
|
|
||||||
}
|
|
||||||
UnaryOperator::BitwiseNot => {
|
|
||||||
ctx.inst(Inst::new(OpCode::BIT_NOT, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
|
||||||
Ok(arg)
|
|
||||||
}
|
|
||||||
UnaryOperator::UnaryPlus => {
|
|
||||||
ctx.inst(Inst::new(OpCode::UNARY_PLUS, Operand::Reg(arg), Operand::Reg(arg), Operand::None));
|
|
||||||
Ok(arg)
|
|
||||||
}
|
|
||||||
UnaryOperator::Delete => Err("invalid delete target".into()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user