Permalink
Browse files

Fix some edge cases in the property access optimizer that would cause…

… it to generate method calls on the LHS of an assignment.
  • Loading branch information...
1 parent 7bda2d4 commit aa0d21f1d80c42bc33fc99497d1f1a1823a168fb @kg kg committed Aug 22, 2012
@@ -82,26 +82,25 @@ class ConvertPropertyAccessesToInvocations : JSAstVisitor {
return replacement;
}
- public static bool IsPropertyGetterInvocation (JSNode parentNode, JSPropertyAccess pa) {
- var parentBoe = parentNode as JSBinaryOperatorExpression;
- var parentUoe = parentNode as JSUnaryOperatorExpression;
-
- bool isMutation = (
- (parentUoe != null) &&
- (parentUoe.Operator is JSUnaryMutationOperator)
- ) || (
- (parentBoe != null) &&
- (parentBoe.Operator is JSAssignmentOperator) &&
- (pa == parentBoe.Left)
- );
+ public bool IsPropertyGetterInvocation (JSPropertyAccess pa) {
+ if (Stack.OfType<JSBinaryOperatorExpression>().Any(
+ (boe) =>
+ (boe.Operator is JSAssignmentOperator) &&
+ (boe.Left.SelfAndChildrenRecursive.Contains(pa))
+ ))
+ return false;
+ else if (Stack.OfType<JSUnaryOperatorExpression>().Any(
+ (uoe) =>
+ uoe.Operator is JSUnaryMutationOperator
+ ))
+ return false;
return !pa.IsWrite &&
- !isMutation &&
CanConvertToInvocation(pa);
}
public void VisitNode (JSPropertyAccess pa) {
- if (IsPropertyGetterInvocation(ParentNode, pa)) {
+ if (IsPropertyGetterInvocation(pa)) {
// getter
var replacement = ConstructInvocation(pa);
@@ -112,9 +111,9 @@ class ConvertPropertyAccessesToInvocations : JSAstVisitor {
}
}
- public static bool IsPropertySetterInvocation (JSNode parentNode, JSBinaryOperatorExpression boe, out JSPropertyAccess pa) {
+ public bool IsPropertySetterInvocation (JSBinaryOperatorExpression boe, out JSPropertyAccess pa) {
var isValidParent =
- (parentNode is JSExpressionStatement);
+ (ParentNode is JSExpressionStatement);
pa = boe.Left as JSPropertyAccess;
@@ -127,7 +126,7 @@ class ConvertPropertyAccessesToInvocations : JSAstVisitor {
public void VisitNode (JSBinaryOperatorExpression boe) {
JSPropertyAccess pa;
- if (IsPropertySetterInvocation(ParentNode, boe, out pa)) {
+ if (IsPropertySetterInvocation(boe, out pa)) {
// setter
var invocation = ConstructInvocation(pa, boe.Right);
@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+
+public static class Program {
+ public const int ShockwaveVelocity = 384;
+ public const int KinematicObstructionGridSize = 8;
+
+ public static void Main (string[] args) {
+ var objects = new List<Obj> {
+ new Obj(), new Obj(), new Obj()
+ };
+
+ for (int i = 0; i < objects.Count; i++) {
+ Str d = new Str { Value = i };
+ objects[i].Property += d;
+ }
+
+ foreach (var o in objects)
+ Console.WriteLine(o.Property.Value);
+ }
+}
+
+public class Obj {
+ private int p;
+
+ public Str Property {
+ get {
+ return new Str { Value = p };
+ }
+ set {
+ p = value.Value;
+ }
+ }
+}
+
+public struct Str {
+ public int Value;
+
+ public static Str operator + (Str lhs, Str rhs) {
+ return new Str {
+ Value = lhs.Value + rhs.Value
+ };
+ }
+}
View
@@ -417,5 +417,16 @@ public class AnalysisTests : GenericTestFixture {
Console.WriteLine(generatedJs);
}
+
+ [Test]
+ public void AddToStructProperty () {
+ var output = "0\r\n1\r\n2";
+ var generatedJs = GenericTest(
+ @"AnalysisTestCases\AddToStructProperty.cs",
+ output, output
+ );
+
+ Console.WriteLine(generatedJs);
+ }
}
}
View
@@ -69,6 +69,7 @@
<None Include="AnalysisTestCases\ReuseEnumeratorLocal.cs" />
<None Include="AnalysisTestCases\PropertyTemporaries.cs" />
<None Include="AnalysisTestCases\TemporaryArraySize.cs" />
+ <None Include="AnalysisTestCases\AddToStructProperty.cs" />
<Compile Include="ComparisonTests.cs" />
<None Include="SimpleTestCases\BaseAutoProperties.cs" />
<None Include="FailingTestCases\BaseFields.cs" />
@@ -22,6 +22,7 @@
<mimeMap fileExtension=".xnb" mimeType="application/octet-stream" />
<mimeMap fileExtension=".resj" mimeType="application/octet-stream" />
<mimeMap fileExtension=".maaap" mimeType="application/octet-stream" />
+ <mimeMap fileExtension=".soundBank" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>

0 comments on commit aa0d21f

Please sign in to comment.