]> granicus.if.org Git - icinga2/commitdiff
Make operators &&, || behave like in JavaScript
authorGunnar Beutner <gunnar@beutner.name>
Tue, 27 Jan 2015 07:40:19 +0000 (08:40 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Tue, 27 Jan 2015 07:40:19 +0000 (08:40 +0100)
fixes #8290

doc/15-language-reference.md
lib/config/expression.cpp

index 71bfff0572e75b9e091f7a747db3a27b0712ee2c..ea4112cccea8d37d85d5918bff0b97ef1d1cda87 100644 (file)
@@ -163,8 +163,8 @@ Operator | Examples (Result)                             | Description
 ^        | 17 ^ 12 (29)                                  | Bitwise XOR
 &        | 7 & 3 (3)                                     | Binary AND
 &#124;   | 2 &#124; 3 (3)                                | Binary OR
-&&       | true && false (false)                         | Logical AND
-&#124;&#124; | true &#124;&#124; false (true)            | Logical OR
+&&       | true && false (false), 3 && 7 (7), 0 && 7 (0) | Logical AND
+&#124;&#124; | true &#124;&#124; 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
 
 ## <a id="boolean-values"></a> 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
 ---------------------|-------------------|--------------
index 2fbcfea5ecfb31fa44e8a4ac75da2708c11b3b6a..e48ff02149dafad23525a29d9e7eaeba324ac0f7 100644 (file)
@@ -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