feat: 添加严格比较运算符与一元加法

This commit is contained in:
爱特每羊队
2026-06-06 20:08:10 +08:00
parent 33314265eb
commit e245ab347a
7 changed files with 263 additions and 0 deletions
+42
View File
@@ -214,6 +214,48 @@ fn panic_to_object() -> ! {
panic!("ToObject failed: null or undefined cannot be converted to object")
}
pub fn same_value(lhs: JsValue, rhs: JsValue) -> bool {
if lhs.is_double() && rhs.is_double() {
let a = lhs.as_double();
let b = rhs.as_double();
if a.is_nan() && b.is_nan() {
return true;
}
if a == 0.0 && b == 0.0 {
let a_neg = a.is_sign_negative();
let b_neg = b.is_sign_negative();
return a_neg == b_neg;
}
return a == b;
}
if lhs.is_int() && rhs.is_int() {
return lhs.as_int() == rhs.as_int();
}
if lhs.is_bool() && rhs.is_bool() {
return lhs.as_bool() == rhs.as_bool();
}
if lhs.is_null() && rhs.is_null() {
return true;
}
if lhs.is_undefined() && rhs.is_undefined() {
return true;
}
if lhs.is_string() && rhs.is_string() {
return lhs == rhs;
}
if lhs.is_object() && rhs.is_object() {
return lhs.as_ptr() == rhs.as_ptr();
}
false
}
pub fn strict_equality(lhs: JsValue, rhs: JsValue) -> bool {
if lhs.is_double() && rhs.is_double() {
return strict_double_eq(lhs.as_double(), rhs.as_double());
}
same_value(lhs, rhs)
}
pub fn to_object(val: JsValue, vm: &mut Vm) -> Result<JsValue, &'static str> {
if val.is_object() {
return Ok(val);
+15
View File
@@ -262,6 +262,21 @@ impl Vm {
self.regs[rd] = JsValue::bool(!rel.unwrap_or(true));
}
OpCode::STRICT_EQ => {
let eq = coercion::strict_equality(self.regs[a], self.regs[b]);
self.regs[rd] = JsValue::bool(eq);
}
OpCode::STRICT_NEQ => {
let ne = !coercion::strict_equality(self.regs[a], self.regs[b]);
self.regs[rd] = JsValue::bool(ne);
}
OpCode::UNARY_PLUS => {
let v = coercion::to_number(self.regs[a], self.kernel.string_forge().as_ref());
self.regs[rd] = JsValue::float(v);
}
OpCode::JMP => {
let offset = opcode::offset16(instr) as isize;
self.pc = ((self.pc as isize) + offset - 1) as usize;
+55
View File
@@ -1,5 +1,7 @@
use oxide_compiler::compiler::Compiler;
use oxide_parser::Allocator;
use oxide_types::value::JsValue;
use oxide_vm::coercion;
use oxide_vm::vm::Vm;
fn eval(source: &str) -> String {
@@ -33,3 +35,56 @@ fn eval_coercion_bool_equals_int() {
fn eval_not_falsy() {
assert_eq!(eval("!0"), "true");
}
#[test]
fn test_same_value_signed_zero() {
assert!(!coercion::same_value(
JsValue::float(0.0),
JsValue::float(-0.0)
));
}
#[test]
fn test_strict_equality_signed_zero() {
assert!(coercion::strict_equality(
JsValue::float(0.0),
JsValue::float(-0.0)
));
}
#[test]
fn test_same_value_nan() {
assert!(coercion::same_value(
JsValue::float(f64::NAN),
JsValue::float(f64::NAN)
));
}
#[test]
fn test_same_value_type_mismatch() {
assert!(!coercion::same_value(JsValue::int(1), JsValue::float(1.0)));
}
#[test]
fn test_strict_equality_nan() {
assert!(!coercion::strict_equality(
JsValue::float(f64::NAN),
JsValue::float(f64::NAN)
));
}
#[test]
fn test_strict_equality_type_mismatch() {
assert!(!coercion::strict_equality(
JsValue::int(1),
JsValue::float(1.0)
));
}
#[test]
fn test_strict_equality_null_undefined() {
assert!(!coercion::strict_equality(
JsValue::null(),
JsValue::undefined()
));
}
+64
View File
@@ -58,3 +58,67 @@ fn eval_comparison_gte() {
fn eval_string_number_eq() {
assert_eq!(eval("5 == 5"), "true");
}
#[test]
fn eval_strict_eq_true() {
assert_eq!(eval("1 === 1"), "true");
}
#[test]
fn eval_strict_eq_false_type() {
assert_eq!(eval("1 === '1'"), "false");
}
#[test]
fn eval_strict_eq_null_undefined() {
let source = r#"
var undef;
undef === null
"#;
assert_eq!(eval(source), "false");
}
#[test]
fn eval_strict_eq_bool_number() {
assert_eq!(eval("true === 1"), "false");
}
#[test]
fn eval_strict_eq_signed_zero() {
assert_eq!(eval("+0 === -0"), "true");
}
#[test]
fn eval_strict_neq_true() {
assert_eq!(eval("1 !== '1'"), "true");
}
#[test]
fn eval_strict_neq_false() {
assert_eq!(eval("1 !== 1"), "false");
}
#[test]
fn eval_unary_plus_number() {
assert_eq!(eval("+'42'"), "42");
}
#[test]
fn eval_unary_plus_bool() {
assert_eq!(eval("+true"), "1");
}
#[test]
fn eval_unary_plus_null() {
assert_eq!(eval("+null"), "0");
}
#[test]
fn eval_unary_plus_nan() {
assert_eq!(eval("+'hello'"), "NaN");
}
#[test]
fn eval_unary_plus_false() {
assert_eq!(eval("+false"), "0");
}