feat: 支持位运算字节码与 ToInt32/ToUint32

This commit is contained in:
爱特每羊队
2026-06-09 14:42:58 +08:00
parent f54383ef63
commit ad8e870997
9 changed files with 347 additions and 1 deletions
+50
View File
@@ -103,3 +103,53 @@ fn eval_compound_add_expr_value() {
fn eval_compound_undefined() {
assert_eq!(eval("var x; x+=1; x"), "NaN");
}
#[test]
fn eval_bitwise_direct_ops() {
assert_eq!(eval("5 & 3"), "1");
assert_eq!(eval("5 | 2"), "7");
assert_eq!(eval("5 ^ 1"), "4");
assert_eq!(eval("1 << 3"), "8");
assert_eq!(eval("-8 >> 1"), "-4");
assert_eq!(eval("-1 >>> 0"), "4294967295");
assert_eq!(eval("~0"), "-1");
}
#[test]
fn eval_bitwise_coercion() {
assert_eq!(eval("'3' | 0"), "3");
assert_eq!(eval("'x' | 0"), "0");
assert_eq!(eval("undefined | 0"), "0");
assert_eq!(eval("NaN | 0"), "0");
assert_eq!(eval("1.9 | 0"), "1");
assert_eq!(eval("-1.9 | 0"), "-1");
}
#[test]
fn eval_shift_masking() {
assert_eq!(eval("1 << 32"), "1");
assert_eq!(eval("1 << 33"), "2");
assert_eq!(eval("8 >> 33"), "4");
}
#[test]
fn eval_ushr_boundaries() {
assert_eq!(eval("2147483647 >>> 0"), "2147483647");
assert_eq!(eval("2147483648 >>> 0"), "2147483648");
assert_eq!(eval("-1 >>> 1"), "2147483647");
}
#[test]
fn eval_compound_bitwise_ops() {
assert_eq!(eval("var x=5; x &= 3; x"), "1");
assert_eq!(eval("var x=5; x |= 2; x"), "7");
assert_eq!(eval("var x=5; x ^= 1; x"), "4");
assert_eq!(eval("var x=1; x <<= 5; x"), "32");
assert_eq!(eval("var x=-8; x >>= 1; x"), "-4");
assert_eq!(eval("var x=-1; x >>>= 0; x"), "4294967295");
}
#[test]
fn eval_bitwise_in_control_flow() {
assert_eq!(eval("var x=3; if ((x & 1) == 1) { 7 } else { 9 }"), "7");
}
+23
View File
@@ -82,3 +82,26 @@ fn test_strict_equality_int_float_equal() {
fn test_strict_equality_null_undefined() {
assert!(!coercion::strict_equality(JsValue::null(), JsValue::undefined()));
}
#[test]
fn test_to_int32_and_to_uint32() {
let vm = Vm::new();
let sf = vm.kernel().string_forge().as_ref();
let string_three = {
let (idx, hash) = vm.kernel().string_forge().intern("3");
JsValue::string(idx, hash)
};
let string_bad = {
let (idx, hash) = vm.kernel().string_forge().intern("x");
JsValue::string(idx, hash)
};
assert_eq!(coercion::to_int32(string_three, sf), 3);
assert_eq!(coercion::to_int32(string_bad, sf), 0);
assert_eq!(coercion::to_int32(JsValue::undefined(), sf), 0);
assert_eq!(coercion::to_int32(JsValue::float(1.9), sf), 1);
assert_eq!(coercion::to_int32(JsValue::float(-1.9), sf), -1);
assert_eq!(coercion::to_int32(JsValue::float(f64::NAN), sf), 0);
assert_eq!(coercion::to_int32(JsValue::float(f64::INFINITY), sf), 0);
assert_eq!(coercion::to_uint32(JsValue::int(-1), sf), 4_294_967_295);
}