LibJS: Pass the Interpreter& to binary/logical/unary helpers

This commit is contained in:
Andreas Kling 2020-04-15 09:28:41 +02:00
parent 994f1a79ad
commit 63499c2c9f
Notes: sideshowbarker 2024-07-19 07:35:23 +09:00
3 changed files with 76 additions and 76 deletions

View file

@ -281,45 +281,45 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
switch (m_op) {
case BinaryOp::Addition:
return add(lhs_result, rhs_result);
return add(interpreter, lhs_result, rhs_result);
case BinaryOp::Subtraction:
return sub(lhs_result, rhs_result);
return sub(interpreter, lhs_result, rhs_result);
case BinaryOp::Multiplication:
return mul(lhs_result, rhs_result);
return mul(interpreter, lhs_result, rhs_result);
case BinaryOp::Division:
return div(lhs_result, rhs_result);
return div(interpreter, lhs_result, rhs_result);
case BinaryOp::Modulo:
return mod(lhs_result, rhs_result);
return mod(interpreter, lhs_result, rhs_result);
case BinaryOp::Exponentiation:
return exp(lhs_result, rhs_result);
return exp(interpreter, lhs_result, rhs_result);
case BinaryOp::TypedEquals:
return typed_eq(lhs_result, rhs_result);
return typed_eq(interpreter, lhs_result, rhs_result);
case BinaryOp::TypedInequals:
return Value(!typed_eq(lhs_result, rhs_result).as_bool());
return Value(!typed_eq(interpreter, lhs_result, rhs_result).as_bool());
case BinaryOp::AbstractEquals:
return eq(lhs_result, rhs_result);
return eq(interpreter, lhs_result, rhs_result);
case BinaryOp::AbstractInequals:
return Value(!eq(lhs_result, rhs_result).as_bool());
return Value(!eq(interpreter, lhs_result, rhs_result).as_bool());
case BinaryOp::GreaterThan:
return greater_than(lhs_result, rhs_result);
return greater_than(interpreter, lhs_result, rhs_result);
case BinaryOp::GreaterThanEquals:
return greater_than_equals(lhs_result, rhs_result);
return greater_than_equals(interpreter, lhs_result, rhs_result);
case BinaryOp::LessThan:
return less_than(lhs_result, rhs_result);
return less_than(interpreter, lhs_result, rhs_result);
case BinaryOp::LessThanEquals:
return less_than_equals(lhs_result, rhs_result);
return less_than_equals(interpreter, lhs_result, rhs_result);
case BinaryOp::BitwiseAnd:
return bitwise_and(lhs_result, rhs_result);
return bitwise_and(interpreter, lhs_result, rhs_result);
case BinaryOp::BitwiseOr:
return bitwise_or(lhs_result, rhs_result);
return bitwise_or(interpreter, lhs_result, rhs_result);
case BinaryOp::BitwiseXor:
return bitwise_xor(lhs_result, rhs_result);
return bitwise_xor(interpreter, lhs_result, rhs_result);
case BinaryOp::LeftShift:
return left_shift(lhs_result, rhs_result);
return left_shift(interpreter, lhs_result, rhs_result);
case BinaryOp::RightShift:
return right_shift(lhs_result, rhs_result);
return right_shift(interpreter, lhs_result, rhs_result);
case BinaryOp::InstanceOf:
return instance_of(lhs_result, rhs_result);
return instance_of(interpreter, lhs_result, rhs_result);
}
ASSERT_NOT_REACHED();
@ -363,13 +363,13 @@ Value UnaryExpression::execute(Interpreter& interpreter) const
return {};
switch (m_op) {
case UnaryOp::BitwiseNot:
return bitwise_not(lhs_result);
return bitwise_not(interpreter, lhs_result);
case UnaryOp::Not:
return Value(!lhs_result.to_boolean());
case UnaryOp::Plus:
return unary_plus(lhs_result);
return unary_plus(interpreter, lhs_result);
case UnaryOp::Minus:
return unary_minus(lhs_result);
return unary_minus(interpreter, lhs_result);
case UnaryOp::Typeof:
switch (lhs_result.type()) {
case Value::Type::Empty:
@ -704,25 +704,25 @@ Value AssignmentExpression::execute(Interpreter& interpreter) const
lhs_result = m_lhs->execute(interpreter);
if (interpreter.exception())
return {};
rhs_result = add(lhs_result, rhs_result);
rhs_result = add(interpreter, lhs_result, rhs_result);
break;
case AssignmentOp::SubtractionAssignment:
lhs_result = m_lhs->execute(interpreter);
if (interpreter.exception())
return {};
rhs_result = sub(lhs_result, rhs_result);
rhs_result = sub(interpreter, lhs_result, rhs_result);
break;
case AssignmentOp::MultiplicationAssignment:
lhs_result = m_lhs->execute(interpreter);
if (interpreter.exception())
return {};
rhs_result = mul(lhs_result, rhs_result);
rhs_result = mul(interpreter, lhs_result, rhs_result);
break;
case AssignmentOp::DivisionAssignment:
lhs_result = m_lhs->execute(interpreter);
if (interpreter.exception())
return {};
rhs_result = div(lhs_result, rhs_result);
rhs_result = div(interpreter, lhs_result, rhs_result);
break;
}
if (interpreter.exception())
@ -1063,7 +1063,7 @@ Value SwitchStatement::execute(Interpreter& interpreter) const
auto test_result = switch_case.test()->execute(interpreter);
if (interpreter.exception())
return {};
if (!eq(discriminant_result, test_result).to_boolean())
if (!eq(interpreter, discriminant_result, test_result).to_boolean())
continue;
}
falling_through = true;

View file

@ -180,32 +180,32 @@ double Value::to_double() const
return to_number().as_double();
}
Value greater_than(Value lhs, Value rhs)
Value greater_than(Interpreter&, Value lhs, Value rhs)
{
return Value(lhs.to_number().as_double() > rhs.to_number().as_double());
}
Value greater_than_equals(Value lhs, Value rhs)
Value greater_than_equals(Interpreter&, Value lhs, Value rhs)
{
return Value(lhs.to_number().as_double() >= rhs.to_number().as_double());
}
Value less_than(Value lhs, Value rhs)
Value less_than(Interpreter&, Value lhs, Value rhs)
{
return Value(lhs.to_number().as_double() < rhs.to_number().as_double());
}
Value less_than_equals(Value lhs, Value rhs)
Value less_than_equals(Interpreter&, Value lhs, Value rhs)
{
return Value(lhs.to_number().as_double() <= rhs.to_number().as_double());
}
Value bitwise_and(Value lhs, Value rhs)
Value bitwise_and(Interpreter&, Value lhs, Value rhs)
{
return Value((i32)lhs.to_number().as_double() & (i32)rhs.to_number().as_double());
}
Value bitwise_or(Value lhs, Value rhs)
Value bitwise_or(Interpreter&, Value lhs, Value rhs)
{
bool lhs_invalid = lhs.is_undefined() || lhs.is_null() || lhs.is_nan() || lhs.is_infinity();
bool rhs_invalid = rhs.is_undefined() || rhs.is_null() || rhs.is_nan() || rhs.is_infinity();
@ -222,62 +222,62 @@ Value bitwise_or(Value lhs, Value rhs)
return Value((i32)lhs.to_number().as_double() | (i32)rhs.to_number().as_double());
}
Value bitwise_xor(Value lhs, Value rhs)
Value bitwise_xor(Interpreter&, Value lhs, Value rhs)
{
return Value((i32)lhs.to_number().as_double() ^ (i32)rhs.to_number().as_double());
}
Value bitwise_not(Value lhs)
Value bitwise_not(Interpreter&, Value lhs)
{
return Value(~(i32)lhs.to_number().as_double());
}
Value unary_plus(Value lhs)
Value unary_plus(Interpreter&, Value lhs)
{
return lhs.to_number();
}
Value unary_minus(Value lhs)
Value unary_minus(Interpreter&, Value lhs)
{
if (lhs.to_number().is_nan())
return js_nan();
return Value(-lhs.to_number().as_double());
}
Value left_shift(Value lhs, Value rhs)
Value left_shift(Interpreter&, Value lhs, Value rhs)
{
return Value((i32)lhs.to_number().as_double() << (i32)rhs.to_number().as_double());
}
Value right_shift(Value lhs, Value rhs)
Value right_shift(Interpreter&, Value lhs, Value rhs)
{
return Value((i32)lhs.to_number().as_double() >> (i32)rhs.to_number().as_double());
}
Value add(Value lhs, Value rhs)
Value add(Interpreter& interpreter, Value lhs, Value rhs)
{
if (lhs.is_string() || rhs.is_string())
return js_string((lhs.is_string() ? lhs : rhs).as_string()->heap(), String::format("%s%s", lhs.to_string().characters(), rhs.to_string().characters()));
return js_string(interpreter.heap(), String::format("%s%s", lhs.to_string().characters(), rhs.to_string().characters()));
return Value(lhs.to_number().as_double() + rhs.to_number().as_double());
}
Value sub(Value lhs, Value rhs)
Value sub(Interpreter&, Value lhs, Value rhs)
{
return Value(lhs.to_number().as_double() - rhs.to_number().as_double());
}
Value mul(Value lhs, Value rhs)
Value mul(Interpreter&, Value lhs, Value rhs)
{
return Value(lhs.to_number().as_double() * rhs.to_number().as_double());
}
Value div(Value lhs, Value rhs)
Value div(Interpreter&, Value lhs, Value rhs)
{
return Value(lhs.to_number().as_double() / rhs.to_number().as_double());
}
Value mod(Value lhs, Value rhs)
Value mod(Interpreter&, Value lhs, Value rhs)
{
if (lhs.to_number().is_nan() || rhs.to_number().is_nan())
return js_nan();
@ -289,12 +289,12 @@ Value mod(Value lhs, Value rhs)
return Value(index - trunc * period);
}
Value exp(Value lhs, Value rhs)
Value exp(Interpreter&, Value lhs, Value rhs)
{
return Value(pow(lhs.to_number().as_double(), rhs.to_number().as_double()));
}
Value typed_eq(Value lhs, Value rhs)
Value typed_eq(Interpreter&, Value lhs, Value rhs)
{
if (rhs.type() != lhs.type())
return Value(false);
@ -320,25 +320,25 @@ Value typed_eq(Value lhs, Value rhs)
ASSERT_NOT_REACHED();
}
Value eq(Value lhs, Value rhs)
Value eq(Interpreter& interpreter, Value lhs, Value rhs)
{
if (lhs.type() == rhs.type())
return typed_eq(lhs, rhs);
return typed_eq(interpreter, lhs, rhs);
if ((lhs.is_undefined() || lhs.is_null()) && (rhs.is_undefined() || rhs.is_null()))
return Value(true);
if (lhs.is_object() && rhs.is_boolean())
return eq(lhs.as_object().to_primitive(), rhs.to_number());
return eq(interpreter, lhs.as_object().to_primitive(), rhs.to_number());
if (lhs.is_boolean() && rhs.is_object())
return eq(lhs.to_number(), rhs.as_object().to_primitive());
return eq(interpreter, lhs.to_number(), rhs.as_object().to_primitive());
if (lhs.is_object())
return eq(lhs.as_object().to_primitive(), rhs);
return eq(interpreter, lhs.as_object().to_primitive(), rhs);
if (rhs.is_object())
return eq(lhs, rhs.as_object().to_primitive());
return eq(interpreter, lhs, rhs.as_object().to_primitive());
if (lhs.is_number() || rhs.is_number())
return Value(lhs.to_number().as_double() == rhs.to_number().as_double());
@ -349,7 +349,7 @@ Value eq(Value lhs, Value rhs)
return Value(false);
}
Value instance_of(Value lhs, Value rhs)
Value instance_of(Interpreter&, Value lhs, Value rhs)
{
if (!lhs.is_object() || !rhs.is_object())
return Value(false);

View file

@ -187,27 +187,27 @@ inline Value js_negative_infinity()
return Value(-__builtin_huge_val());
}
Value greater_than(Value lhs, Value rhs);
Value greater_than_equals(Value lhs, Value rhs);
Value less_than(Value lhs, Value rhs);
Value less_than_equals(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 unary_plus(Value);
Value unary_minus(Value);
Value left_shift(Value lhs, Value rhs);
Value right_shift(Value lhs, Value rhs);
Value add(Value lhs, Value rhs);
Value sub(Value lhs, Value rhs);
Value mul(Value lhs, Value rhs);
Value div(Value lhs, Value rhs);
Value mod(Value lhs, Value rhs);
Value exp(Value lhs, Value rhs);
Value eq(Value lhs, Value rhs);
Value typed_eq(Value lhs, Value rhs);
Value instance_of(Value lhs, Value rhs);
Value greater_than(Interpreter&, Value lhs, Value rhs);
Value greater_than_equals(Interpreter&, Value lhs, Value rhs);
Value less_than(Interpreter&, Value lhs, Value rhs);
Value less_than_equals(Interpreter&, Value lhs, Value rhs);
Value bitwise_and(Interpreter&, Value lhs, Value rhs);
Value bitwise_or(Interpreter&, Value lhs, Value rhs);
Value bitwise_xor(Interpreter&, Value lhs, Value rhs);
Value bitwise_not(Interpreter&, Value);
Value unary_plus(Interpreter&, Value);
Value unary_minus(Interpreter&, Value);
Value left_shift(Interpreter&, Value lhs, Value rhs);
Value right_shift(Interpreter&, Value lhs, Value rhs);
Value add(Interpreter&, Value lhs, Value rhs);
Value sub(Interpreter&, Value lhs, Value rhs);
Value mul(Interpreter&, Value lhs, Value rhs);
Value div(Interpreter&, Value lhs, Value rhs);
Value mod(Interpreter&, Value lhs, Value rhs);
Value exp(Interpreter&, Value lhs, Value rhs);
Value eq(Interpreter&, Value lhs, Value rhs);
Value typed_eq(Interpreter&, Value lhs, Value rhs);
Value instance_of(Interpreter&, Value lhs, Value rhs);
const LogStream& operator<<(const LogStream&, const Value&);