1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:58:11 +00:00

LibJS: Add tests for bitwise & and ^

And fix some edge case conversion bugs found by the tests.
This commit is contained in:
Nico Weber 2020-07-22 20:27:32 -04:00 committed by Andreas Kling
parent bbc7e8429b
commit 79a5ba58a5
3 changed files with 132 additions and 2 deletions

View file

@ -409,8 +409,11 @@ Value bitwise_and(Interpreter& interpreter, Value lhs, Value rhs)
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (both_number(lhs_numeric, rhs_numeric))
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number())
return Value(0);
return Value((i32)lhs_numeric.as_double() & (i32)rhs_numeric.as_double());
}
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer()));
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise AND");
@ -448,8 +451,15 @@ Value bitwise_xor(Interpreter& interpreter, Value lhs, Value rhs)
auto rhs_numeric = rhs.to_numeric(interpreter);
if (interpreter.exception())
return {};
if (both_number(lhs_numeric, rhs_numeric))
if (both_number(lhs_numeric, rhs_numeric)) {
if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number())
return Value(0);
if (!lhs_numeric.is_finite_number())
return rhs_numeric;
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
return Value((i32)lhs_numeric.as_double() ^ (i32)rhs_numeric.as_double());
}
if (both_bigint(lhs_numeric, rhs_numeric))
return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer()));
interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise XOR");