Skip to content

Commit

Permalink
Merge pull request #3 from koculu/develop
Browse files Browse the repository at this point in the history
Fixes logical operator issue.
  • Loading branch information
koculu authored Mar 23, 2022
2 parents 5ae3029 + 9d89a77 commit ac50a33
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
28 changes: 28 additions & 0 deletions src/Tenray.Topaz.Test/ArithmeticAccuracyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,33 @@ public void TestArithmeticComparison()
foreach (var item in (IEnumerable)(model.a))
Assert.IsTrue((bool)item);
}


[Test]
public void TestLogicalOperators()
{

var engine = new TopazEngine();
engine.Options.LiteralNumbersAreConvertedToDouble = false;
engine.Options.NumbersAreConvertedToDoubleInArithmeticOperations = false;
dynamic model = new JsObject();
engine.SetValue("model", model);
engine.ExecuteScript(@"
var x = 7
var y = 3
var a = y > x
var b = y < x
var c = a && b
var d = b && a
model.a = a
model.b = b
model.c = c
model.d = d
");
Assert.IsFalse((bool)model.a);
Assert.IsTrue((bool)model.b);
Assert.IsFalse((bool)model.c);
Assert.IsFalse((bool)model.d);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ private static object ExecuteBinaryOperatorDouble(
BinaryOperator.InstanceOf => Exceptions.ThrowRightHandSideOfInstanceOfIsNotObject(),
BinaryOperator.In => Exceptions.ThrowCannotUseInOperatorToSearchForIn(d1, d2),
BinaryOperator.LogicalAnd =>
(JavascriptTypeUtility.IsObjectTrue(d1)
&&
JavascriptTypeUtility.IsObjectTrue(d2)) ? d2 : d1,
JavascriptTypeUtility.AndLogicalOperator(d1, d2),
BinaryOperator.LogicalOr =>
JavascriptTypeUtility.IsObjectTrue(d1) ? d1 : d2,
BinaryOperator.Exponentiation => Math.Pow(d1, d2),
Expand Down Expand Up @@ -89,9 +87,7 @@ private static object ExecuteBinaryOperatorLong(
BinaryOperator.InstanceOf => Exceptions.ThrowRightHandSideOfInstanceOfIsNotObject(),
BinaryOperator.In => Exceptions.ThrowCannotUseInOperatorToSearchForIn(d1, d2),
BinaryOperator.LogicalAnd =>
(JavascriptTypeUtility.IsObjectTrue(d1)
&&
JavascriptTypeUtility.IsObjectTrue(d2)) ? d2 : d1,
JavascriptTypeUtility.AndLogicalOperator(d1, d2),
BinaryOperator.LogicalOr =>
JavascriptTypeUtility.IsObjectTrue(d1) ? d1 : d2,
BinaryOperator.Exponentiation => Math.Pow(d1, d2),
Expand Down Expand Up @@ -138,9 +134,7 @@ private static object ExecuteBinaryOperatorInt(
BinaryOperator.InstanceOf => Exceptions.ThrowRightHandSideOfInstanceOfIsNotObject(),
BinaryOperator.In => Exceptions.ThrowCannotUseInOperatorToSearchForIn(d1, d2),
BinaryOperator.LogicalAnd =>
(JavascriptTypeUtility.IsObjectTrue(d1)
&&
JavascriptTypeUtility.IsObjectTrue(d2)) ? d2 : d1,
JavascriptTypeUtility.AndLogicalOperator(d1, d2),
BinaryOperator.LogicalOr =>
JavascriptTypeUtility.IsObjectTrue(d1) ? d1 : d2,
BinaryOperator.Exponentiation => Math.Pow(d1, d2),
Expand Down Expand Up @@ -181,9 +175,7 @@ private static object ExecuteBinaryOperatorString(BinaryOperator binaryOperator,
BinaryOperator.InstanceOf => d1?.GetType().FullName == d2,
BinaryOperator.In => Exceptions.ThrowCannotUseInOperatorToSearchForIn(d1, d2),
BinaryOperator.LogicalAnd =>
(JavascriptTypeUtility.IsObjectTrue(d1)
&&
JavascriptTypeUtility.IsObjectTrue(d2)) ? d2 : d1,
JavascriptTypeUtility.AndLogicalOperator(d1, d2),
BinaryOperator.LogicalOr =>
JavascriptTypeUtility.IsObjectTrue(d1) ? d1 : d2,
BinaryOperator.Exponentiation => Exceptions.ThrowBinaryOperatorIsNotSupportedOn(binaryOperator, d1, d2),
Expand Down Expand Up @@ -218,9 +210,7 @@ private static object ExecuteBinaryOperatorString(BinaryOperator binaryOperator,
BinaryOperator.InstanceOf => d1?.GetType().FullName == d2?.ToString(),
BinaryOperator.In => Exceptions.ThrowCannotUseInOperatorToSearchForIn(d1, d2),
BinaryOperator.LogicalAnd =>
(JavascriptTypeUtility.IsObjectTrue(d1)
&&
JavascriptTypeUtility.IsObjectTrue(d2)) ? d2 : d1,
JavascriptTypeUtility.AndLogicalOperator(d1, d2),
BinaryOperator.LogicalOr =>
JavascriptTypeUtility.IsObjectTrue(d1) ? d1 : d2,
BinaryOperator.Exponentiation => Exceptions.ThrowBinaryOperatorIsNotSupportedOn(binaryOperator, d1, d2),
Expand Down Expand Up @@ -333,9 +323,7 @@ private static object ExecuteBinaryOperatorDynamic(
right.Contains(left) :
Exceptions.ThrowCannotUseInOperatorToSearchForIn(left, right),
BinaryOperator.LogicalAnd =>
(JavascriptTypeUtility.IsObjectTrue(left)
&&
JavascriptTypeUtility.IsObjectTrue(right)) ? right : left,
JavascriptTypeUtility.AndLogicalOperator(left, right),
BinaryOperator.LogicalOr =>
JavascriptTypeUtility.IsObjectTrue(left) ? left : right,
BinaryOperator.Exponentiation => allowNumeric ? Math.Pow(left ?? 0, right ?? 0) : 0,
Expand Down
4 changes: 2 additions & 2 deletions src/Tenray.Topaz/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Authors>Ahmed Yasin Koculu</Authors>
<PackageId>Topaz</PackageId>
<Title>Topaz</Title>
<ProductVersion>1.2.3.0</ProductVersion>
<Version>1.2.3.0</Version>
<ProductVersion>1.2.4.0</ProductVersion>
<Version>1.2.4.0</Version>
<Authors>Ahmed Yasin Koculu</Authors>
<AssemblyTitle>Topaz</AssemblyTitle>
<Description>Multithreaded Javascript Engine for .NET</Description>
Expand Down
7 changes: 7 additions & 0 deletions src/Tenray.Topaz/Utility/JavascriptTypeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ namespace Tenray.Topaz
{
internal class JavascriptTypeUtility
{
internal static object AndLogicalOperator(object left, object right)
{
if (IsObjectFalse(left))
return left;
return right;
}

internal static bool IsObjectTrue(object value)
{
// https://developer.mozilla.org/en-US/docs/Glossary/Truthy
Expand Down

0 comments on commit ac50a33

Please sign in to comment.