From b858a05f15cb75b21309b37ad7e072565cced4b8 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 27 Jan 2015 08:40:19 +0100 Subject: [PATCH] Make operators &&, || behave like in JavaScript fixes #8290 --- doc/15-language-reference.md | 10 +++++----- lib/config/expression.cpp | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/doc/15-language-reference.md b/doc/15-language-reference.md index 71bfff057..ea4112ccc 100644 --- a/doc/15-language-reference.md +++ b/doc/15-language-reference.md @@ -163,8 +163,8 @@ Operator | Examples (Result) | Description ^ | 17 ^ 12 (29) | Bitwise XOR & | 7 & 3 (3) | Binary AND | | 2 | 3 (3) | Binary OR -&& | true && false (false) | Logical AND -|| | true || false (true) | Logical OR +&& | true && false (false), 3 && 7 (7), 0 && 7 (0) | Logical AND +|| | true || false (true), 0 || 7 (7)| Logical OR < | 3 < 5 (true) | Less than > | 3 > 5 (false) | Greater than <= | 3 <= 3 (true) | Less than or equal @@ -432,9 +432,9 @@ UserGroup | user ## Boolean Values -The `assign where` and `ignore where` statements, the `!`, `&&` and `||` -operators as well as the `bool()` function convert their arguments to a -boolean value based on the following rules: +The `assign where`, `ignore where`, `if` and `while` statements, the `!` operator as +well as the `bool()` function convert their arguments to a boolean value based on the +following rules: Description | Example Value | Boolean Value ---------------------|-------------------|-------------- diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index 2fbcfea5e..e48ff0214 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -249,12 +249,22 @@ Value NotInExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const Value LogicalAndExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { - return m_Operand1->Evaluate(frame).ToBool() && m_Operand2->Evaluate(frame).ToBool(); + Value left = m_Operand1->Evaluate(frame); + + if (!left.ToBool()) + return left; + else + return m_Operand2->Evaluate(frame); } Value LogicalOrExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const { - return m_Operand1->Evaluate(frame).ToBool() || m_Operand2->Evaluate(frame).ToBool(); + Value left = m_Operand1->Evaluate(frame); + + if (left.ToBool()) + return left; + else + return m_Operand2->Evaluate(frame); } Value FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const -- 2.40.0