fix: 修复不支持特性直接崩溃问题

This commit is contained in:
2026-06-09 10:16:24 +08:00
parent 270bc86933
commit 38e1492928
2 changed files with 33 additions and 2 deletions
+9 -2
View File
@@ -908,7 +908,9 @@ impl Compiler {
AssignmentOperator::Division => OpCode::COMPOUND_MEMBER_DIV, AssignmentOperator::Division => OpCode::COMPOUND_MEMBER_DIV,
AssignmentOperator::Remainder => OpCode::COMPOUND_MEMBER_MOD, AssignmentOperator::Remainder => OpCode::COMPOUND_MEMBER_MOD,
AssignmentOperator::Exponential => OpCode::COMPOUND_MEMBER_EXP, AssignmentOperator::Exponential => OpCode::COMPOUND_MEMBER_EXP,
_ => unreachable!(), _ => {
return Err(format!("compound assignment operator {:?} not supported", assign.operator))
}
}; };
ctx.emit(opcode::encode(op, obj_reg, val_reg, key_reg)); ctx.emit(opcode::encode(op, obj_reg, val_reg, key_reg));
ctx.emit(0); ctx.emit(0);
@@ -947,7 +949,12 @@ impl Compiler {
AssignmentOperator::Division => OpCode::COMPOUND_DIV, AssignmentOperator::Division => OpCode::COMPOUND_DIV,
AssignmentOperator::Remainder => OpCode::COMPOUND_MOD, AssignmentOperator::Remainder => OpCode::COMPOUND_MOD,
AssignmentOperator::Exponential => OpCode::COMPOUND_EXP, AssignmentOperator::Exponential => OpCode::COMPOUND_EXP,
_ => unreachable!(), _ => {
return Err(format!(
"compound assignment operator {:?} not supported",
assign.operator
))
}
}; };
ctx.emit(opcode::encode(op, var_reg, rhs, 0)); ctx.emit(opcode::encode(op, var_reg, rhs, 0));
Ok(var_reg) Ok(var_reg)
@@ -9,6 +9,16 @@ fn compile_source(source: &str) -> oxide_compiler::module::CompiledModule {
compiler.compile(&program).expect("compile failed") compiler.compile(&program).expect("compile failed")
} }
fn compile_source_err(source: &str) -> String {
let allocator = Allocator::default();
let program = oxide_parser::parse(&allocator, source).expect("parse failed");
let compiler = Compiler::new();
match compiler.compile(&program) {
Ok(_) => panic!("compile should fail"),
Err(err) => err,
}
}
#[test] #[test]
fn compile_simple_expr() { fn compile_simple_expr() {
let module = compile_source("1 + 2"); let module = compile_source("1 + 2");
@@ -20,6 +30,20 @@ fn compile_simple_expr() {
assert_eq!(last, OpCode::HALT); assert_eq!(last, OpCode::HALT);
} }
#[test]
fn unsupported_logical_assignment_returns_compile_error() {
let err = compile_source_err("let x = 0; x ||= 1;");
assert!(err.contains("compound assignment operator"));
assert!(err.contains("not supported"));
}
#[test]
fn unsupported_member_logical_assignment_returns_compile_error() {
let err = compile_source_err("let obj = { x: 0 }; obj.x ||= 1;");
assert!(err.contains("compound assignment operator"));
assert!(err.contains("not supported"));
}
#[test] #[test]
fn compile_constants() { fn compile_constants() {
let module = compile_source("42"); let module = compile_source("42");