mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
feat: 添加严格比较运算符与一元加法
This commit is contained in:
@@ -399,6 +399,8 @@ impl Compiler {
|
||||
BinaryOperator::LessEqualThan => OpCode::LTE,
|
||||
BinaryOperator::GreaterEqualThan => OpCode::GTE,
|
||||
BinaryOperator::In => OpCode::IN,
|
||||
BinaryOperator::StrictEquality => OpCode::STRICT_EQ,
|
||||
BinaryOperator::StrictInequality => OpCode::STRICT_NEQ,
|
||||
_ => return Err(format!("unsupported binary operator: {:?}", bin.operator)),
|
||||
};
|
||||
let r = ctx.alloc_reg();
|
||||
@@ -428,6 +430,11 @@ impl Compiler {
|
||||
ctx.emit(opcode::encode(OpCode::NOT, r, arg, 0));
|
||||
Ok(r)
|
||||
}
|
||||
UnaryOperator::UnaryPlus => {
|
||||
let r = ctx.alloc_reg();
|
||||
ctx.emit(opcode::encode(OpCode::UNARY_PLUS, r, arg, 0));
|
||||
Ok(r)
|
||||
}
|
||||
_ => Err(format!("unsupported unary operator: {:?}", un.operator)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,9 @@ pub enum OpCode {
|
||||
NOT = 0x17,
|
||||
AND = 0x18,
|
||||
OR = 0x19,
|
||||
STRICT_EQ = 0x1A,
|
||||
STRICT_NEQ = 0x1C,
|
||||
UNARY_PLUS = 0x1D,
|
||||
|
||||
// -- Control Flow (0x20-0x2F) --
|
||||
JMP = 0x20,
|
||||
@@ -111,6 +114,9 @@ impl TryFrom<u8> for OpCode {
|
||||
0x17 => Ok(OpCode::NOT),
|
||||
0x18 => Ok(OpCode::AND),
|
||||
0x19 => Ok(OpCode::OR),
|
||||
0x1A => Ok(OpCode::STRICT_EQ),
|
||||
0x1C => Ok(OpCode::STRICT_NEQ),
|
||||
0x1D => Ok(OpCode::UNARY_PLUS),
|
||||
0x20 => Ok(OpCode::JMP),
|
||||
0x21 => Ok(OpCode::JMP_IF_FALSE),
|
||||
0x22 => Ok(OpCode::JMP_IF_TRUE),
|
||||
@@ -167,6 +173,9 @@ impl fmt::Display for OpCode {
|
||||
OpCode::NOT => "NOT",
|
||||
OpCode::AND => "AND",
|
||||
OpCode::OR => "OR",
|
||||
OpCode::STRICT_EQ => "STRICT_EQ",
|
||||
OpCode::STRICT_NEQ => "STRICT_NEQ",
|
||||
OpCode::UNARY_PLUS => "UNARY_PLUS",
|
||||
OpCode::JMP => "JMP",
|
||||
OpCode::JMP_IF_FALSE => "JMP_IF_FALSE",
|
||||
OpCode::JMP_IF_TRUE => "JMP_IF_TRUE",
|
||||
|
||||
@@ -131,3 +131,74 @@ fn regression_coalesce_consistency() {
|
||||
"a ?? b should produce an error (not silently misbehave)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_strict_eq() {
|
||||
let module = compile_source("1 === 2");
|
||||
assert!(
|
||||
module
|
||||
.bytecode
|
||||
.iter()
|
||||
.any(|&i| opcode::opcode(i) == OpCode::STRICT_EQ),
|
||||
"1 === 2 should emit STRICT_EQ opcode"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_strict_neq() {
|
||||
let module = compile_source("1 !== 2");
|
||||
assert!(
|
||||
module
|
||||
.bytecode
|
||||
.iter()
|
||||
.any(|&i| opcode::opcode(i) == OpCode::STRICT_NEQ),
|
||||
"1 !== 2 should emit STRICT_NEQ opcode"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_unary_plus() {
|
||||
let module = compile_source("+'hello'");
|
||||
assert!(
|
||||
module
|
||||
.bytecode
|
||||
.iter()
|
||||
.any(|&i| opcode::opcode(i) == OpCode::UNARY_PLUS),
|
||||
"+'hello' should emit UNARY_PLUS opcode"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_typeof_strict_eq() {
|
||||
let module = compile_source("typeof 42 === 'number'");
|
||||
assert!(
|
||||
module
|
||||
.bytecode
|
||||
.iter()
|
||||
.any(|&i| opcode::opcode(i) == OpCode::TYPEOF),
|
||||
"should emit TYPEOF opcode"
|
||||
);
|
||||
assert!(
|
||||
module
|
||||
.bytecode
|
||||
.iter()
|
||||
.any(|&i| opcode::opcode(i) == OpCode::STRICT_EQ),
|
||||
"should emit STRICT_EQ opcode"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_strict_eq_no_error() {
|
||||
let result = std::panic::catch_unwind(|| {
|
||||
compile_source("1 === 2");
|
||||
});
|
||||
assert!(result.is_ok(), "1 === 2 should compile without error");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_unary_plus_no_error() {
|
||||
let result = std::panic::catch_unwind(|| {
|
||||
compile_source("+'hello'");
|
||||
});
|
||||
assert!(result.is_ok(), "+'hello' should compile without error");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user