瀏覽代碼

LibJS: Implement bitwise unsigned right shift operator (>>>)

Linus Groh 5 年之前
父節點
當前提交
396ecfa2d7

+ 15 - 0
Libraries/LibJS/AST.cpp

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  * All rights reserved.
  * All rights reserved.
  *
  *
  * Redistribution and use in source and binary forms, with or without
  * Redistribution and use in source and binary forms, with or without
@@ -328,6 +329,8 @@ Value BinaryExpression::execute(Interpreter& interpreter) const
         return left_shift(interpreter, lhs_result, rhs_result);
         return left_shift(interpreter, lhs_result, rhs_result);
     case BinaryOp::RightShift:
     case BinaryOp::RightShift:
         return right_shift(interpreter, lhs_result, rhs_result);
         return right_shift(interpreter, lhs_result, rhs_result);
+    case BinaryOp::UnsignedRightShift:
+        return unsigned_right_shift(interpreter, lhs_result, rhs_result);
     case BinaryOp::InstanceOf:
     case BinaryOp::InstanceOf:
         return instance_of(interpreter, lhs_result, rhs_result);
         return instance_of(interpreter, lhs_result, rhs_result);
     }
     }
@@ -506,6 +509,9 @@ void BinaryExpression::dump(int indent) const
     case BinaryOp::RightShift:
     case BinaryOp::RightShift:
         op_string = ">>";
         op_string = ">>";
         break;
         break;
+    case BinaryOp::UnsignedRightShift:
+        op_string = ">>>";
+        break;
     case BinaryOp::InstanceOf:
     case BinaryOp::InstanceOf:
         op_string = "instanceof";
         op_string = "instanceof";
         break;
         break;
@@ -761,6 +767,12 @@ Value AssignmentExpression::execute(Interpreter& interpreter) const
             return {};
             return {};
         rhs_result = right_shift(interpreter, lhs_result, rhs_result);
         rhs_result = right_shift(interpreter, lhs_result, rhs_result);
         break;
         break;
+    case AssignmentOp::UnsignedRightShiftAssignment:
+        lhs_result = m_lhs->execute(interpreter);
+        if (interpreter.exception())
+            return {};
+        rhs_result = unsigned_right_shift(interpreter, lhs_result, rhs_result);
+        break;
     }
     }
     if (interpreter.exception())
     if (interpreter.exception())
         return {};
         return {};
@@ -834,6 +846,9 @@ void AssignmentExpression::dump(int indent) const
     case AssignmentOp::RightShiftAssignment:
     case AssignmentOp::RightShiftAssignment:
         op_string = ">>=";
         op_string = ">>=";
         break;
         break;
+    case AssignmentOp::UnsignedRightShiftAssignment:
+        op_string = ">>>=";
+        break;
     }
     }
 
 
     ASTNode::dump(indent);
     ASTNode::dump(indent);

+ 3 - 0
Libraries/LibJS/AST.h

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  * All rights reserved.
  * All rights reserved.
  *
  *
  * Redistribution and use in source and binary forms, with or without
  * Redistribution and use in source and binary forms, with or without
@@ -341,6 +342,7 @@ enum class BinaryOp {
     BitwiseXor,
     BitwiseXor,
     LeftShift,
     LeftShift,
     RightShift,
     RightShift,
+    UnsignedRightShift,
     InstanceOf,
     InstanceOf,
 };
 };
 
 
@@ -569,6 +571,7 @@ enum class AssignmentOp {
     DivisionAssignment,
     DivisionAssignment,
     LeftShiftAssignment,
     LeftShiftAssignment,
     RightShiftAssignment,
     RightShiftAssignment,
+    UnsignedRightShiftAssignment,
 };
 };
 
 
 class AssignmentExpression : public Expression {
 class AssignmentExpression : public Expression {

+ 9 - 0
Libraries/LibJS/Parser.cpp

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
  * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
+ * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  * All rights reserved.
  * All rights reserved.
  *
  *
  * Redistribution and use in source and binary forms, with or without
  * Redistribution and use in source and binary forms, with or without
@@ -589,6 +590,12 @@ NonnullRefPtr<Expression> Parser::parse_secondary_expression(NonnullRefPtr<Expre
     case TokenType::ShiftRightEquals:
     case TokenType::ShiftRightEquals:
         consume();
         consume();
         return create_ast_node<AssignmentExpression>(AssignmentOp::RightShiftAssignment, move(lhs), parse_expression(min_precedence, associativity));
         return create_ast_node<AssignmentExpression>(AssignmentOp::RightShiftAssignment, move(lhs), parse_expression(min_precedence, associativity));
+    case TokenType::UnsignedShiftRight:
+        consume();
+        return create_ast_node<BinaryExpression>(BinaryOp::UnsignedRightShift, move(lhs), parse_expression(min_precedence, associativity));
+    case TokenType::UnsignedShiftRightEquals:
+        consume();
+        return create_ast_node<AssignmentExpression>(AssignmentOp::UnsignedRightShiftAssignment, move(lhs), parse_expression(min_precedence, associativity));
     case TokenType::ParenOpen:
     case TokenType::ParenOpen:
         return parse_call_expression(move(lhs));
         return parse_call_expression(move(lhs));
     case TokenType::Equals:
     case TokenType::Equals:
@@ -1078,6 +1085,8 @@ bool Parser::match_secondary_expression() const
         || type == TokenType::ShiftLeftEquals
         || type == TokenType::ShiftLeftEquals
         || type == TokenType::ShiftRight
         || type == TokenType::ShiftRight
         || type == TokenType::ShiftRightEquals
         || type == TokenType::ShiftRightEquals
+        || type == TokenType::UnsignedShiftRight
+        || type == TokenType::UnsignedShiftRightEquals
         || type == TokenType::DoubleAmpersand
         || type == TokenType::DoubleAmpersand
         || type == TokenType::DoublePipe
         || type == TokenType::DoublePipe
         || type == TokenType::DoubleQuestionMark;
         || type == TokenType::DoubleQuestionMark;

+ 11 - 0
Libraries/LibJS/Runtime/Value.cpp

@@ -273,6 +273,17 @@ Value right_shift(Interpreter&, Value lhs, Value rhs)
     return Value((i32)lhs_number.as_double() >> (i32)rhs_number.as_double());
     return Value((i32)lhs_number.as_double() >> (i32)rhs_number.as_double());
 }
 }
 
 
+Value unsigned_right_shift(Interpreter&, Value lhs, Value rhs)
+{
+    auto lhs_number = lhs.to_number();
+    if (!lhs_number.is_finite_number())
+        return Value(0);
+    auto rhs_number = rhs.to_number();
+    if (!rhs_number.is_finite_number())
+        return lhs_number;
+    return Value((unsigned)lhs_number.as_double() >> (i32)rhs_number.as_double());
+}
+
 Value add(Interpreter& interpreter, Value lhs, Value rhs)
 Value add(Interpreter& interpreter, Value lhs, Value rhs)
 {
 {
     auto lhs_primitive = lhs.to_primitive(interpreter);
     auto lhs_primitive = lhs.to_primitive(interpreter);

+ 7 - 0
Libraries/LibJS/Runtime/Value.h

@@ -82,6 +82,12 @@ public:
         m_value.as_double = value;
         m_value.as_double = value;
     }
     }
 
 
+    explicit Value(unsigned value)
+        : m_type(Type::Number)
+    {
+        m_value.as_double = static_cast<double>(value);
+    }
+
     explicit Value(i32 value)
     explicit Value(i32 value)
         : m_type(Type::Number)
         : m_type(Type::Number)
     {
     {
@@ -207,6 +213,7 @@ Value unary_plus(Interpreter&, Value);
 Value unary_minus(Interpreter&, Value);
 Value unary_minus(Interpreter&, Value);
 Value left_shift(Interpreter&, Value lhs, Value rhs);
 Value left_shift(Interpreter&, Value lhs, Value rhs);
 Value right_shift(Interpreter&, Value lhs, Value rhs);
 Value right_shift(Interpreter&, Value lhs, Value rhs);
+Value unsigned_right_shift(Interpreter&, Value lhs, Value rhs);
 Value add(Interpreter&, Value lhs, Value rhs);
 Value add(Interpreter&, Value lhs, Value rhs);
 Value sub(Interpreter&, Value lhs, Value rhs);
 Value sub(Interpreter&, Value lhs, Value rhs);
 Value mul(Interpreter&, Value lhs, Value rhs);
 Value mul(Interpreter&, Value lhs, Value rhs);

+ 63 - 0
Libraries/LibJS/Tests/binary-bitwise-unsigned-right-shift.js

@@ -0,0 +1,63 @@
+load("test-common.js");
+
+try {
+    assert((0 >>> 0) === 0);
+    assert((0 >>> 1) === 0);
+    assert((0 >>> 2) === 0);
+    assert((0 >>> 3) === 0);
+    assert((0 >>> 4) === 0);
+    assert((0 >>> 5) === 0);
+
+    assert((1 >>> 0) === 1);
+    assert((1 >>> 1) === 0);
+    assert((1 >>> 2) === 0);
+    assert((1 >>> 3) === 0);
+    assert((1 >>> 4) === 0);
+    assert((1 >>> 5) === 0);
+
+    assert((5 >>> 0) === 5);
+    assert((5 >>> 1) === 2);
+    assert((5 >>> 2) === 1);
+    assert((5 >>> 3) === 0);
+    assert((5 >>> 4) === 0);
+    assert((5 >>> 5) === 0);
+
+    assert((42 >>> 0) === 42);
+    assert((42 >>> 1) === 21);
+    assert((42 >>> 2) === 10);
+    assert((42 >>> 3) === 5);
+    assert((42 >>> 4) === 2);
+    assert((42 >>> 5) === 1);
+
+    assert((-1 >>> 0) === 4294967295);
+    assert((-1 >>> 1) === 2147483647);
+    assert((-1 >>> 2) === 1073741823);
+    assert((-1 >>> 3) === 536870911);
+    assert((-1 >>> 4) === 268435455);
+    assert((-1 >>> 5) === 134217727);
+
+    assert((-5 >>> 0) === 4294967291);
+    assert((-5 >>> 1) === 2147483645);
+    assert((-5 >>> 2) === 1073741822);
+    assert((-5 >>> 3) === 536870911);
+    assert((-5 >>> 4) === 268435455);
+    assert((-5 >>> 5) === 134217727);
+
+    var x = -67;
+    var y = 4;
+    assert(("-42" >>> 3) === 536870906);
+    assert((x >>> y) === 268435451);
+    assert((x >>> [[[[5]]]]) === 134217725);
+    assert((undefined >>> y) === 0);
+    assert(("a" >>> "b") === 0);
+    assert((null >>> null) === 0);
+    assert((undefined >>> undefined) === 0);
+    assert((NaN >>> NaN) === 0);
+    assert((6 >>> NaN) === 6);
+    assert((Infinity >>> Infinity) === 0);
+    assert((-Infinity >>> Infinity) === 0);
+
+    console.log("PASS");
+} catch (e) {
+    console.log("FAIL: " + e);
+}