mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:07:35 +00:00
LibJS: Move Value ops into Value.cpp and tweak BinaryOp names
This commit is contained in:
parent
fe6bd9650f
commit
7de35118a9
4 changed files with 93 additions and 84 deletions
|
@ -123,61 +123,6 @@ const Value typed_eq(const Value lhs, const Value rhs)
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value greater(Value lhs, Value rhs)
|
|
||||||
{
|
|
||||||
ASSERT(lhs.is_number());
|
|
||||||
ASSERT(rhs.is_number());
|
|
||||||
return Value(lhs.as_double() > rhs.as_double());
|
|
||||||
}
|
|
||||||
|
|
||||||
Value smaller(Value lhs, Value rhs)
|
|
||||||
{
|
|
||||||
ASSERT(lhs.is_number());
|
|
||||||
ASSERT(rhs.is_number());
|
|
||||||
return Value(lhs.as_double() < rhs.as_double());
|
|
||||||
}
|
|
||||||
|
|
||||||
Value bit_and(Value lhs, Value rhs)
|
|
||||||
{
|
|
||||||
ASSERT(lhs.is_number());
|
|
||||||
ASSERT(rhs.is_number());
|
|
||||||
return Value((i32)lhs.as_double() & (i32)rhs.as_double());
|
|
||||||
}
|
|
||||||
|
|
||||||
Value bit_or(Value lhs, Value rhs)
|
|
||||||
{
|
|
||||||
ASSERT(lhs.is_number());
|
|
||||||
ASSERT(rhs.is_number());
|
|
||||||
return Value((i32)lhs.as_double() | (i32)rhs.as_double());
|
|
||||||
}
|
|
||||||
|
|
||||||
Value bit_xor(Value lhs, Value rhs)
|
|
||||||
{
|
|
||||||
ASSERT(lhs.is_number());
|
|
||||||
ASSERT(rhs.is_number());
|
|
||||||
return Value((i32)lhs.as_double() ^ (i32)rhs.as_double());
|
|
||||||
}
|
|
||||||
|
|
||||||
Value bit_not(Value lhs)
|
|
||||||
{
|
|
||||||
ASSERT(lhs.is_number());
|
|
||||||
return Value(~(i32)lhs.as_double());
|
|
||||||
}
|
|
||||||
|
|
||||||
Value bit_left(Value lhs, Value rhs)
|
|
||||||
{
|
|
||||||
ASSERT(lhs.is_number());
|
|
||||||
ASSERT(rhs.is_number());
|
|
||||||
return Value((i32)lhs.as_double() << (i32)rhs.as_double());
|
|
||||||
}
|
|
||||||
|
|
||||||
Value bit_right(Value lhs, Value rhs)
|
|
||||||
{
|
|
||||||
ASSERT(lhs.is_number());
|
|
||||||
ASSERT(rhs.is_number());
|
|
||||||
return Value((i32)lhs.as_double() >> (i32)rhs.as_double());
|
|
||||||
}
|
|
||||||
|
|
||||||
Value BinaryExpression::execute(Interpreter& interpreter) const
|
Value BinaryExpression::execute(Interpreter& interpreter) const
|
||||||
{
|
{
|
||||||
auto lhs_result = m_lhs->execute(interpreter);
|
auto lhs_result = m_lhs->execute(interpreter);
|
||||||
|
@ -192,20 +137,20 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
|
||||||
return typed_eq(lhs_result, rhs_result);
|
return typed_eq(lhs_result, rhs_result);
|
||||||
case BinaryOp::TypedInequals:
|
case BinaryOp::TypedInequals:
|
||||||
return Value(!typed_eq(lhs_result, rhs_result).to_boolean());
|
return Value(!typed_eq(lhs_result, rhs_result).to_boolean());
|
||||||
case BinaryOp::Greater:
|
case BinaryOp::GreaterThan:
|
||||||
return greater(lhs_result, rhs_result);
|
return greater_than(lhs_result, rhs_result);
|
||||||
case BinaryOp::Smaller:
|
case BinaryOp::LessThan:
|
||||||
return smaller(lhs_result, rhs_result);
|
return less_than(lhs_result, rhs_result);
|
||||||
case BinaryOp::BitAnd:
|
case BinaryOp::BitwiseAnd:
|
||||||
return bit_and(lhs_result, rhs_result);
|
return bitwise_and(lhs_result, rhs_result);
|
||||||
case BinaryOp::BitOr:
|
case BinaryOp::BitwiseOr:
|
||||||
return bit_or(lhs_result, rhs_result);
|
return bitwise_or(lhs_result, rhs_result);
|
||||||
case BinaryOp::BitXor:
|
case BinaryOp::BitwiseXor:
|
||||||
return bit_xor(lhs_result, rhs_result);
|
return bitwise_xor(lhs_result, rhs_result);
|
||||||
case BinaryOp::BitLeftShift:
|
case BinaryOp::LeftShift:
|
||||||
return bit_left(lhs_result, rhs_result);
|
return left_shift(lhs_result, rhs_result);
|
||||||
case BinaryOp::BitRightShift:
|
case BinaryOp::RightShift:
|
||||||
return bit_right(lhs_result, rhs_result);
|
return right_shift(lhs_result, rhs_result);
|
||||||
}
|
}
|
||||||
|
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
|
@ -230,7 +175,7 @@ Value UnaryExpression::execute(Interpreter& interpreter) const
|
||||||
auto lhs_result = m_lhs->execute(interpreter);
|
auto lhs_result = m_lhs->execute(interpreter);
|
||||||
switch (m_op) {
|
switch (m_op) {
|
||||||
case UnaryOp::BitNot:
|
case UnaryOp::BitNot:
|
||||||
return bit_not(lhs_result);
|
return bitwise_not(lhs_result);
|
||||||
case UnaryOp::Not:
|
case UnaryOp::Not:
|
||||||
return Value(!lhs_result.to_boolean());
|
return Value(!lhs_result.to_boolean());
|
||||||
}
|
}
|
||||||
|
@ -273,25 +218,25 @@ void BinaryExpression::dump(int indent) const
|
||||||
case BinaryOp::TypedInequals:
|
case BinaryOp::TypedInequals:
|
||||||
op_string = "!==";
|
op_string = "!==";
|
||||||
break;
|
break;
|
||||||
case BinaryOp::Greater:
|
case BinaryOp::GreaterThan:
|
||||||
op_string = ">";
|
op_string = ">";
|
||||||
break;
|
break;
|
||||||
case BinaryOp::Smaller:
|
case BinaryOp::LessThan:
|
||||||
op_string = "<";
|
op_string = "<";
|
||||||
break;
|
break;
|
||||||
case BinaryOp::BitAnd:
|
case BinaryOp::BitwiseAnd:
|
||||||
op_string = "&";
|
op_string = "&";
|
||||||
break;
|
break;
|
||||||
case BinaryOp::BitOr:
|
case BinaryOp::BitwiseOr:
|
||||||
op_string = "|";
|
op_string = "|";
|
||||||
break;
|
break;
|
||||||
case BinaryOp::BitXor:
|
case BinaryOp::BitwiseXor:
|
||||||
op_string = "^";
|
op_string = "^";
|
||||||
break;
|
break;
|
||||||
case BinaryOp::BitLeftShift:
|
case BinaryOp::LeftShift:
|
||||||
op_string = "<<";
|
op_string = "<<";
|
||||||
break;
|
break;
|
||||||
case BinaryOp::BitRightShift:
|
case BinaryOp::RightShift:
|
||||||
op_string = ">>";
|
op_string = ">>";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -178,13 +178,13 @@ enum class BinaryOp {
|
||||||
Minus,
|
Minus,
|
||||||
TypedEquals,
|
TypedEquals,
|
||||||
TypedInequals,
|
TypedInequals,
|
||||||
Greater,
|
GreaterThan,
|
||||||
Smaller,
|
LessThan,
|
||||||
BitAnd,
|
BitwiseAnd,
|
||||||
BitOr,
|
BitwiseOr,
|
||||||
BitXor,
|
BitwiseXor,
|
||||||
BitLeftShift,
|
LeftShift,
|
||||||
BitRightShift,
|
RightShift,
|
||||||
};
|
};
|
||||||
|
|
||||||
class BinaryExpression : public Expression {
|
class BinaryExpression : public Expression {
|
||||||
|
|
|
@ -72,6 +72,61 @@ bool Value::to_boolean() const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value greater_than(Value lhs, Value rhs)
|
||||||
|
{
|
||||||
|
ASSERT(lhs.is_number());
|
||||||
|
ASSERT(rhs.is_number());
|
||||||
|
return Value(lhs.as_double() > rhs.as_double());
|
||||||
|
}
|
||||||
|
|
||||||
|
Value less_than(Value lhs, Value rhs)
|
||||||
|
{
|
||||||
|
ASSERT(lhs.is_number());
|
||||||
|
ASSERT(rhs.is_number());
|
||||||
|
return Value(lhs.as_double() < rhs.as_double());
|
||||||
|
}
|
||||||
|
|
||||||
|
Value bitwise_and(Value lhs, Value rhs)
|
||||||
|
{
|
||||||
|
ASSERT(lhs.is_number());
|
||||||
|
ASSERT(rhs.is_number());
|
||||||
|
return Value((i32)lhs.as_double() & (i32)rhs.as_double());
|
||||||
|
}
|
||||||
|
|
||||||
|
Value bitwise_or(Value lhs, Value rhs)
|
||||||
|
{
|
||||||
|
ASSERT(lhs.is_number());
|
||||||
|
ASSERT(rhs.is_number());
|
||||||
|
return Value((i32)lhs.as_double() | (i32)rhs.as_double());
|
||||||
|
}
|
||||||
|
|
||||||
|
Value bitwise_xor(Value lhs, Value rhs)
|
||||||
|
{
|
||||||
|
ASSERT(lhs.is_number());
|
||||||
|
ASSERT(rhs.is_number());
|
||||||
|
return Value((i32)lhs.as_double() ^ (i32)rhs.as_double());
|
||||||
|
}
|
||||||
|
|
||||||
|
Value bitwise_not(Value lhs)
|
||||||
|
{
|
||||||
|
ASSERT(lhs.is_number());
|
||||||
|
return Value(~(i32)lhs.as_double());
|
||||||
|
}
|
||||||
|
|
||||||
|
Value left_shift(Value lhs, Value rhs)
|
||||||
|
{
|
||||||
|
ASSERT(lhs.is_number());
|
||||||
|
ASSERT(rhs.is_number());
|
||||||
|
return Value((i32)lhs.as_double() << (i32)rhs.as_double());
|
||||||
|
}
|
||||||
|
|
||||||
|
Value right_shift(Value lhs, Value rhs)
|
||||||
|
{
|
||||||
|
ASSERT(lhs.is_number());
|
||||||
|
ASSERT(rhs.is_number());
|
||||||
|
return Value((i32)lhs.as_double() >> (i32)rhs.as_double());
|
||||||
|
}
|
||||||
|
|
||||||
const LogStream& operator<<(const LogStream& stream, const Value& value)
|
const LogStream& operator<<(const LogStream& stream, const Value& value)
|
||||||
{
|
{
|
||||||
return stream << value.to_string();
|
return stream << value.to_string();
|
||||||
|
|
|
@ -136,6 +136,15 @@ inline Value js_null()
|
||||||
return Value(Value::Type::Null);
|
return Value(Value::Type::Null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value greater_than(Value lhs, Value rhs);
|
||||||
|
Value less_than(Value lhs, Value rhs);
|
||||||
|
Value bitwise_and(Value lhs, Value rhs);
|
||||||
|
Value bitwise_or(Value lhs, Value rhs);
|
||||||
|
Value bitwise_xor(Value lhs, Value rhs);
|
||||||
|
Value bitwise_not(Value);
|
||||||
|
Value left_shift(Value lhs, Value rhs);
|
||||||
|
Value right_shift(Value lhs, Value rhs);
|
||||||
|
|
||||||
const LogStream& operator<<(const LogStream&, const Value&);
|
const LogStream& operator<<(const LogStream&, const Value&);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue