You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2019/02/04 21:31:23 UTC

[royale-compiler] branch develop updated: compiler-jx: simple numeric literals assigned to int or uint values are now coerced at compile time (references #74)

This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git


The following commit(s) were added to refs/heads/develop by this push:
     new 7708d9e  compiler-jx: simple numeric literals assigned to int or uint values are now coerced at compile time (references #74)
7708d9e is described below

commit 7708d9e7af8029900035ceceb6f5b887761bf915
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Mon Feb 4 13:31:18 2019 -0800

    compiler-jx: simple numeric literals assigned to int or uint values are now coerced at compile time (references #74)
    
    Uses toInt32() and toUint32() methods on INumericLiteralNode.INumericValue that are actually intended for this purpose.
---
 .../compiler/internal/codegen/js/JSEmitter.java    | 20 +++----
 .../codegen/js/jx/BinaryOperatorEmitter.java       | 61 +++++++++++-----------
 .../codegen/js/jx/VarDeclarationEmitter.java       | 24 +++++----
 .../codegen/js/royale/TestRoyaleExpressions.java   | 45 +++++++++++++++-
 .../codegen/js/royale/TestRoyaleFieldMembers.java  | 24 +++++++++
 .../codegen/js/royale/TestRoyaleStatements.java    | 13 ++++-
 .../js/sourcemaps/TestSourceMapExpressions.java    | 30 +++++++++++
 7 files changed, 164 insertions(+), 53 deletions(-)

diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/JSEmitter.java b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/JSEmitter.java
index 997c562..ddb6e0b 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/JSEmitter.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/JSEmitter.java
@@ -534,10 +534,11 @@ public class JSEmitter extends ASEmitter implements IJSEmitter
 			if (assignedNode instanceof INumericLiteralNode)
 			{
 				INumericLiteralNode numericLiteral = (INumericLiteralNode) assignedNode;
-				if (!BuiltinType.INT.equals(numericLiteral.getNumericValue().getAssumedType()))
-				{
-					needsCoercion = true;
-				}
+                INumericLiteralNode.INumericValue numericValue = numericLiteral.getNumericValue();
+                startMapping(assignedNode);
+                write(Integer.toString(numericValue.toInt32()));
+                endMapping(assignedNode);
+                return;
 			}
 			else if(!project.getBuiltinType(BuiltinType.INT).equals(assignedDef))
 			{
@@ -554,11 +555,12 @@ public class JSEmitter extends ASEmitter implements IJSEmitter
 			boolean needsCoercion = false;
 			if (assignedNode instanceof INumericLiteralNode)
 			{
-				INumericLiteralNode numericLiteral = (INumericLiteralNode) assignedNode;
-				if (!BuiltinType.UINT.equals(numericLiteral.getNumericValue().getAssumedType()))
-				{
-					needsCoercion = true;
-				}
+                INumericLiteralNode numericLiteral = (INumericLiteralNode) assignedNode;
+                INumericLiteralNode.INumericValue numericValue = numericLiteral.getNumericValue();
+                startMapping(assignedNode);
+                write(Long.toString(numericValue.toUint32()));
+                endMapping(assignedNode);
+                return;
 			}
 			else if(!project.getBuiltinType(BuiltinType.UINT).equals(assignedDef))
 			{
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/BinaryOperatorEmitter.java b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/BinaryOperatorEmitter.java
index 0c164d1..629a0d9 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/BinaryOperatorEmitter.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/BinaryOperatorEmitter.java
@@ -448,6 +448,7 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements
             }
 			String coercionStart = null;
 			String coercionEnd = null;
+			String coercedValue = null;
             if (isAssignment)
             {
 				if (getProject().getBuiltinType(BuiltinType.INT).equals(leftDef))
@@ -455,11 +456,9 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements
 					boolean needsCoercion = false;
 					if (rNode instanceof INumericLiteralNode)
 					{
-						INumericLiteralNode rNumber = (INumericLiteralNode) rNode;
-						if (!BuiltinType.INT.equals(rNumber.getNumericValue().getAssumedType()))
-						{
-							needsCoercion = true;
-						}
+						INumericLiteralNode numericLiteral = (INumericLiteralNode) rNode;
+						INumericLiteralNode.INumericValue numericValue = numericLiteral.getNumericValue();
+						coercedValue = Integer.toString(numericValue.toInt32());
 					}
 					else if(!getProject().getBuiltinType(BuiltinType.INT).equals(rightDef))
 					{
@@ -476,11 +475,9 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements
 					boolean needsCoercion = false;
 					if (rNode instanceof INumericLiteralNode)
 					{
-						INumericLiteralNode rNumber = (INumericLiteralNode) rNode;
-						if (!BuiltinType.UINT.equals(rNumber.getNumericValue().getAssumedType()))
-						{
-							needsCoercion = true;
-						}
+						INumericLiteralNode numericLiteral = (INumericLiteralNode) rNode;
+						INumericLiteralNode.INumericValue numericValue = numericLiteral.getNumericValue();
+						coercedValue = Long.toString(numericValue.toUint32());
 					}
 					else if(!getProject().getBuiltinType(BuiltinType.UINT).equals(rightDef))
 					{
@@ -518,8 +515,8 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements
 						}
 					}
 				}
-            }
-            super_emitBinaryOperator(node, coercionStart, coercionEnd);
+			}
+            super_emitBinaryOperator(node, coercionStart, coercionEnd, coercedValue);
             	
             /*
             IExpressionNode leftSide = node.getLeftOperandNode();
@@ -600,7 +597,7 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements
     	return false;
     }
 
-    private void super_emitBinaryOperator(IBinaryOperatorNode node, String coercionStart, String coercionEnd)
+    private void super_emitBinaryOperator(IBinaryOperatorNode node, String coercionStart, String coercionEnd, String coercedValue)
     {
         if (ASNodeUtils.hasParenOpen(node))
             write(ASEmitterTokens.PAREN_OPEN);
@@ -671,24 +668,26 @@ public class BinaryOperatorEmitter extends JSSubEmitter implements
 			{
 				write(coercionStart);
 			}
-            /*
-            IDefinition definition = node.getRightOperandNode().resolve(getProject());
-        	if (definition instanceof FunctionDefinition &&
-        			(!(definition instanceof AccessorDefinition)))
-        	{
-        	}
-        	else */
-        		getWalker().walk(node.getRightOperandNode());
-                if (node.getNodeID() == ASTNodeID.Op_InID &&
-                        ((JSRoyaleEmitter)getEmitter()).isXML(node.getRightOperandNode()))
-                {
-                	write(".elementNames()");
-                }   
-                else if (node.getNodeID() == ASTNodeID.Op_InID &&
-                        ((JSRoyaleEmitter)getEmitter()).isProxy(node.getRightOperandNode()))
-                {
-                	write(".propertyNames()");
-                }
+			if (coercedValue != null)
+			{
+				startMapping(node.getRightOperandNode());
+				write(coercedValue);
+				endMapping(node.getRightOperandNode());
+			}
+			else
+			{
+				getWalker().walk(node.getRightOperandNode());
+			}
+			if (node.getNodeID() == ASTNodeID.Op_InID &&
+					((JSRoyaleEmitter)getEmitter()).isXML(node.getRightOperandNode()))
+			{
+				write(".elementNames()");
+			}   
+			else if (node.getNodeID() == ASTNodeID.Op_InID &&
+					((JSRoyaleEmitter)getEmitter()).isProxy(node.getRightOperandNode()))
+			{
+				write(".propertyNames()");
+			}
         }
 
 		if (coercionStart != null)
diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/VarDeclarationEmitter.java b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/VarDeclarationEmitter.java
index b149de8..6adceae 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/VarDeclarationEmitter.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/VarDeclarationEmitter.java
@@ -183,16 +183,15 @@ public class VarDeclarationEmitter extends JSSubEmitter implements
             }
             String coercionStart = null;
             String coercionEnd = null;
+            String coercedValue = null;
             if (getProject().getBuiltinType(BuiltinType.INT).equals(variableDef))
             {
                 boolean needsCoercion = false;
                 if (avnode instanceof INumericLiteralNode)
                 {
                     INumericLiteralNode numericLiteral = (INumericLiteralNode) avnode;
-                    if (!BuiltinType.INT.equals(numericLiteral.getNumericValue().getAssumedType()))
-                    {
-                        needsCoercion = true;
-                    }
+                    INumericLiteralNode.INumericValue numericValue = numericLiteral.getNumericValue();
+                    coercedValue = Integer.toString(numericValue.toInt32());
                 }
                 else if(!getProject().getBuiltinType(BuiltinType.INT).equals(avdef))
                 {
@@ -210,10 +209,8 @@ public class VarDeclarationEmitter extends JSSubEmitter implements
                 if (avnode instanceof INumericLiteralNode)
                 {
                     INumericLiteralNode numericLiteral = (INumericLiteralNode) avnode;
-                    if (!BuiltinType.UINT.equals(numericLiteral.getNumericValue().getAssumedType()))
-                    {
-                        needsCoercion = true;
-                    }
+                    INumericLiteralNode.INumericValue numericValue = numericLiteral.getNumericValue();
+                    coercedValue = Long.toString(numericValue.toUint32());
                 }
                 else if(!getProject().getBuiltinType(BuiltinType.UINT).equals(avdef))
                 {
@@ -239,7 +236,16 @@ public class VarDeclarationEmitter extends JSSubEmitter implements
 			{
                 write(coercionStart);
             }
-            fjs.emitAssignedValue(avnode);
+            if (coercedValue != null)
+            {
+                startMapping(avnode);
+                write(coercedValue);
+                endMapping(avnode);
+            }
+            else
+            {
+                fjs.emitAssignedValue(avnode);
+            }
 			if (coercionStart != null)
 			{
 				if (coercionEnd != null)
diff --git a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleExpressions.java b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleExpressions.java
index 3e660a8..c575b74 100644
--- a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleExpressions.java
+++ b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleExpressions.java
@@ -37,6 +37,7 @@ import org.apache.royale.compiler.tree.as.IFileNode;
 import org.apache.royale.compiler.tree.as.IFunctionCallNode;
 import org.apache.royale.compiler.tree.as.IFunctionNode;
 import org.apache.royale.compiler.tree.as.IMemberAccessExpressionNode;
+import org.apache.royale.compiler.tree.as.IReturnNode;
 import org.apache.royale.compiler.tree.as.IVariableNode;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -297,7 +298,7 @@ public class TestRoyaleExpressions extends TestGoogExpressions
     {
         IBinaryOperatorNode node = getBinaryNode("var numToInt:int;numToInt = 123.4");
         asBlockWalker.visitBinaryOperator(node);
-        assertOut("numToInt = (123.4) >> 0");
+        assertOut("numToInt = 123");
     }
 
     @Test
@@ -309,6 +310,14 @@ public class TestRoyaleExpressions extends TestGoogExpressions
     }
 
     @Test
+    public void testVisitBinaryOperatorNode_AssignmentNegativeIntLiteralToInt()
+    {
+        IBinaryOperatorNode node = getBinaryNode("var numToInt:int;numToInt = -321");
+        asBlockWalker.visitBinaryOperator(node);
+        assertOut("numToInt = -321");
+    }
+
+    @Test
     public void testVisitBinaryOperatorNode_AssignmentUintVarToUint()
     {
         IBinaryOperatorNode node = getBinaryNode("var unsigned_integer1:uint;var unsigned_integer2:uint;unsigned_integer1 = unsigned_integer2");
@@ -337,7 +346,15 @@ public class TestRoyaleExpressions extends TestGoogExpressions
     {
         IBinaryOperatorNode node = getBinaryNode("var numToUint:uint;numToUint = 123.4");
         asBlockWalker.visitBinaryOperator(node);
-        assertOut("numToUint = (123.4) >>> 0");
+        assertOut("numToUint = 123");
+    }
+
+    @Test
+    public void testVisitBinaryOperatorNode_AssignmentNegativeIntLiteralToUint()
+    {
+        IBinaryOperatorNode node = getBinaryNode("var numToUint:uint;numToUint = -123");
+        asBlockWalker.visitBinaryOperator(node);
+        assertOut("numToUint = 4294967173");
     }
 
     @Test
@@ -1500,6 +1517,30 @@ public class TestRoyaleExpressions extends TestGoogExpressions
         assertOut("new Fn(\"a\", \"b\", \"return a + b;\")(1, 2)");
     }
 
+    @Test
+    public void testVisitReturnIntWithDecimalValue()
+    {
+        IReturnNode node = (IReturnNode) getNode("function():int { return -123.4; }", IReturnNode.class);
+        asBlockWalker.visitReturn(node);
+        assertOut("return -123");
+    }
+
+    @Test
+    public void testVisitReturnUintWithDecimalValue()
+    {
+        IReturnNode node = (IReturnNode) getNode("function():uint { return 123.4; }", IReturnNode.class);
+        asBlockWalker.visitReturn(node);
+        assertOut("return 123");
+    }
+
+    @Test
+    public void testVisitReturnUintWithNegativeValue()
+    {
+        IReturnNode node = (IReturnNode) getNode("function():uint { return -123; }", IReturnNode.class);
+        asBlockWalker.visitReturn(node);
+        assertOut("return 4294967173");
+    }
+
     protected IBackend createBackend()
     {
         return new RoyaleBackend();
diff --git a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleFieldMembers.java b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleFieldMembers.java
index f1bea4f..b8be801 100644
--- a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleFieldMembers.java
+++ b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleFieldMembers.java
@@ -82,6 +82,30 @@ public class TestRoyaleFieldMembers extends TestGoogFieldMembers
     }
 
     @Test
+    public void testField_withTypeValue_IntDecimal()
+    {
+        IVariableNode node = getField("var foo:int = -123.4;");
+        asBlockWalker.visitVariable(node);
+        assertOut("/**\n * @export\n * @type {number}\n */\nRoyaleTest_A.prototype.foo = -123");
+    }
+
+    @Test
+    public void testField_withTypeValue_UintDecimal()
+    {
+        IVariableNode node = getField("var foo:uint = 123.4;");
+        asBlockWalker.visitVariable(node);
+        assertOut("/**\n * @export\n * @type {number}\n */\nRoyaleTest_A.prototype.foo = 123");
+    }
+
+    @Test
+    public void testField_withTypeValue_UintNegative()
+    {
+        IVariableNode node = getField("var foo:uint = -123;");
+        asBlockWalker.visitVariable(node);
+        assertOut("/**\n * @export\n * @type {number}\n */\nRoyaleTest_A.prototype.foo = 4294967173");
+    }
+
+    @Test
     public void testVariable_withTypeValue_ArrayElementType()
     {
         IVariableNode node = (IVariableNode)getNode("public class A { function foobar():void {var foo:Number = bar[0];var bar:B;}}\n[ArrayElementType(\"Number\")]\nclass B {}",
diff --git a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleStatements.java b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleStatements.java
index 2f82899..a51da05 100644
--- a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleStatements.java
+++ b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleStatements.java
@@ -102,7 +102,7 @@ public class TestRoyaleStatements extends TestGoogStatements
         IVariableNode node = (IVariableNode) getNode("var a:int = 123.4;",
                 IVariableNode.class);
         asBlockWalker.visitVariable(node);
-        assertOut("var /** @type {number} */ a = (123.4) >> 0");
+        assertOut("var /** @type {number} */ a = 123");
     }
 
     @Test
@@ -111,7 +111,16 @@ public class TestRoyaleStatements extends TestGoogStatements
         IVariableNode node = (IVariableNode) getNode("var a:uint = 123.4;",
                 IVariableNode.class);
         asBlockWalker.visitVariable(node);
-        assertOut("var /** @type {number} */ a = (123.4) >>> 0");
+        assertOut("var /** @type {number} */ a = 123");
+    }
+
+    @Test
+    public void testVarDeclaration_withTypeUintAndAssignedNegative()
+    {
+        IVariableNode node = (IVariableNode) getNode("var a:uint = -123;",
+                IVariableNode.class);
+        asBlockWalker.visitVariable(node);
+        assertOut("var /** @type {number} */ a = 4294967173");
     }
 
     //----------------------------------
diff --git a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/sourcemaps/TestSourceMapExpressions.java b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/sourcemaps/TestSourceMapExpressions.java
index 72beed5..98978db 100644
--- a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/sourcemaps/TestSourceMapExpressions.java
+++ b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/sourcemaps/TestSourceMapExpressions.java
@@ -199,6 +199,36 @@ public class TestSourceMapExpressions extends SourceMapTestBase
         assertMapping(node, 0, 4, 0, 4, 0, 5); // b
     }
 
+    @Test
+    public void testVisitBinaryOperatorNode_AssignmentLiteral()
+    {
+        IBinaryOperatorNode node = getBinaryNode("a = 123.2");
+        asBlockWalker.visitBinaryOperator(node);
+        assertMapping(node, 0, 0, 0, 0, 0, 1); // a
+        assertMapping(node, 0, 1, 0, 1, 0, 4); // =
+        assertMapping(node, 0, 4, 0, 4, 0, 9); // 123.2
+    }
+
+    @Test
+    public void testVisitBinaryOperatorNode_AssignmentLiteralWithCompileTimeIntCoercion()
+    {
+        IBinaryOperatorNode node = getBinaryNode("var a:int;a = 123.2");
+        asBlockWalker.visitBinaryOperator(node);
+        assertMapping(node, 0, 0, 0, 0, 0, 1); // a
+        assertMapping(node, 0, 1, 0, 1, 0, 4); // =
+        assertMapping(node, 0, 4, 0, 4, 0, 7); // 123
+    }
+
+    @Test
+    public void testVisitBinaryOperatorNode_AssignmentLiteralWithCompileTimeUintCoercion()
+    {
+        IBinaryOperatorNode node = getBinaryNode("var a:uint;a = -123");
+        asBlockWalker.visitBinaryOperator(node);
+        assertMapping(node, 0, 0, 0, 0, 0, 1); // a
+        assertMapping(node, 0, 1, 0, 1, 0, 4); // =
+        assertMapping(node, 0, 4, 0, 4, 0, 14); // 4294967173
+    }
+
     //----------------------------------
     // Bitwise
     //----------------------------------