You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by ah...@apache.org on 2018/10/19 06:33:42 UTC

[royale-compiler] branch develop updated: improve constant handling in typedefs. @const is used in externs for both read-only and true constants because from GCC's perspective they are the same (you can't write to them) but we want to make certain constants actually be constants so they can be used as default parameter values and other places where compile-time constants are required

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

aharui 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 30bbf1b  improve constant handling in typedefs.  @const is used in externs for both read-only and true constants because from GCC's perspective they are the same (you can't write to them) but we want to make certain constants actually be constants so they can be used as default parameter values and other places where compile-time constants are required
30bbf1b is described below

commit 30bbf1b972325699cc833ec014acf7ba5c57e2f9
Author: Alex Harui <ah...@apache.org>
AuthorDate: Thu Oct 18 23:33:27 2018 -0700

    improve constant handling in typedefs.  @const is used in externs for both read-only and true constants because from GCC's perspective they are the same (you can't write to them) but we want to make certain constants actually be constants so they can be used as default parameter values and other places where compile-time constants are required
---
 .../compiler/clients/ExternCConfiguration.java     | 74 ++++++++++++++++++++++
 .../codegen/typedefs/reference/BaseReference.java  |  5 ++
 .../codegen/typedefs/reference/FieldReference.java | 34 +++++++++-
 .../typedefs/reference/MemberReference.java        |  8 +++
 .../codegen/typedefs/reference/ReferenceModel.java |  6 ++
 .../codegen/js/royale/TestRoyaleGlobalClasses.java |  9 +++
 6 files changed, 133 insertions(+), 3 deletions(-)

diff --git a/compiler-externc/src/main/java/org/apache/royale/compiler/clients/ExternCConfiguration.java b/compiler-externc/src/main/java/org/apache/royale/compiler/clients/ExternCConfiguration.java
index 518f736..00ed4cb 100644
--- a/compiler-externc/src/main/java/org/apache/royale/compiler/clients/ExternCConfiguration.java
+++ b/compiler-externc/src/main/java/org/apache/royale/compiler/clients/ExternCConfiguration.java
@@ -64,6 +64,7 @@ public class ExternCConfiguration extends Configuration
     private List<ExcludedMember> excludesField = new ArrayList<ExcludedMember>();
     private List<ExcludedMember> excludes = new ArrayList<ExcludedMember>();
     private List<ReadOnlyMember> readonly = new ArrayList<ReadOnlyMember>();
+    private List<TrueConstant> trueConstants = new ArrayList<TrueConstant>();
 
     public ExternCConfiguration()
     {
@@ -384,6 +385,43 @@ public class ExternCConfiguration extends Configuration
     	return null;
 	}
 
+    @Config(allowMultiple = true)
+    @Mapping("true-constant")
+    @Arguments({"class", "name", "value"})
+    public void setTrueConstant(ConfigurationValue cfgval, List<String> values) throws IncorrectArgumentCount
+    {
+        final int size = values.size();
+        if (size % 3 != 0)
+            throw new IncorrectArgumentCount(size + 1, size, cfgval.getVar(), cfgval.getSource(), cfgval.getLine());
+
+        for (int nameIndex = 0; nameIndex < size - 2; nameIndex += 3)
+        {
+            final String className = values.get(nameIndex);
+            final String fieldName = values.get(nameIndex + 1);
+            final String value = values.get(nameIndex + 2);
+            addTrueConstant(className, fieldName, value);
+        }
+    }
+
+    public void addTrueConstant(String className, String fieldName, String value)
+    {
+        trueConstants.add(new TrueConstant(className, fieldName, value));
+    }
+    
+    public TrueConstant isTrueConstant(ClassReference classReference,
+			   MemberReference memberReference)
+    {
+
+    	for (TrueConstant constant : trueConstants)
+    	{
+    		if ( constant.isTrueConstant(classReference, memberReference) )
+    			return constant;
+    	}
+    	return null;
+    }
+
+
+
 
     public static class ExcludedMember
     {
@@ -467,4 +505,40 @@ public class ExternCConfiguration extends Configuration
         }
     }
 
+    public static class TrueConstant
+    {
+        private String className;
+        private String name;
+        private String value;
+
+        public String getClassName()
+        {
+            return className;
+        }
+
+        public String getName()
+        {
+            return name;
+        }
+
+        public String getValue()
+        {
+            return value;
+        }
+
+        public TrueConstant(String className, String name, String value)
+        {
+            this.className = className;
+            this.name = name;
+            this.value = value;
+        }
+
+        public boolean isTrueConstant(BaseReference classReference,
+                                  MemberReference memberReference)
+        {
+            return classReference.getQualifiedName().equals(className)
+                    && memberReference.getQualifiedName().equals(name);
+        }
+        
+    }
 }
diff --git a/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/BaseReference.java b/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/BaseReference.java
index 31fc071..e2204b6 100644
--- a/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/BaseReference.java
+++ b/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/BaseReference.java
@@ -24,6 +24,7 @@ import java.util.Set;
 
 import org.apache.royale.compiler.clients.ExternCConfiguration.ExcludedMember;
 import org.apache.royale.compiler.clients.ExternCConfiguration.ReadOnlyMember;
+import org.apache.royale.compiler.clients.ExternCConfiguration.TrueConstant;
 
 import com.google.javascript.rhino.JSDocInfo;
 import com.google.javascript.rhino.JSDocInfo.Marker;
@@ -323,4 +324,8 @@ public abstract class BaseReference
             }
         }
     }
+
+	public TrueConstant isTrueConstant() {
+		return null;
+	}
 }
diff --git a/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/FieldReference.java b/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/FieldReference.java
index 49a0875..3f95e37 100644
--- a/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/FieldReference.java
+++ b/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/FieldReference.java
@@ -19,12 +19,16 @@
 
 package org.apache.royale.compiler.internal.codegen.typedefs.reference;
 
+import java.util.Collection;
+
 import org.apache.royale.compiler.clients.ExternCConfiguration.ExcludedMember;
 import org.apache.royale.compiler.clients.ExternCConfiguration.ReadOnlyMember;
+import org.apache.royale.compiler.clients.ExternCConfiguration.TrueConstant;
 import org.apache.royale.compiler.internal.codegen.typedefs.utils.FunctionUtils;
 import org.apache.royale.compiler.internal.codegen.typedefs.utils.JSTypeUtils;
 
 import com.google.javascript.rhino.JSDocInfo;
+import com.google.javascript.rhino.JSDocInfo.Marker;
 import com.google.javascript.rhino.JSTypeExpression;
 import com.google.javascript.rhino.Node;
 import com.google.javascript.rhino.jstype.JSType;
@@ -36,6 +40,7 @@ public class FieldReference extends MemberReference
     private boolean isConst;
     private String overrideStringType;
     private Node constantValueNode;
+    private String constantValue;
 
     public boolean isStatic()
     {
@@ -81,6 +86,16 @@ public class FieldReference extends MemberReference
             JSDocInfo comment, boolean isStatic)
     {
         super(model, classReference, node, name, comment);
+        Collection<Marker> markers = comment.getMarkers();
+        Marker[] markerArray = new Marker[markers.size()];
+        for (Marker marker : markers)
+        {
+        	if (marker.getAnnotation().getItem().equals("const"))
+        		this.isConst = true;
+        }
+        TrueConstant constant = isTrueConstant();
+        if (constant != null)
+        	constantValue = constant.getValue();
         this.isStatic = isStatic;
     }
 
@@ -113,7 +128,10 @@ public class FieldReference extends MemberReference
                 && !getClassReference().isPropertyInterfaceImplementation(getBaseName())
                 && (null == readOnly))
         {
-            emitVar(sb);
+        	if (isConst && constantValue == null)
+        		emitAccessor(sb, true); // const is used for readOnly as well.  If there is an initial value assume it is const
+        	else
+        		emitVar(sb);
         }
         else
         {
@@ -212,13 +230,23 @@ public class FieldReference extends MemberReference
     private void emitConstValue(StringBuilder sb)
     {
         sb.append(" = ");
-        sb.append(toConstValue(constantValueNode));
+        if (constantValueNode != null)
+        	sb.append(toConstValue(constantValueNode));
+        else
+        	sb.append(constantValue);
     }
 
     private String toConstValue(Node node)
     {
-        if (toTypeString().equals("Number"))
+    	String typeString = toTypeString();
+        if (typeString.equals("Number"))
             return Integer.toString(getClassReference().getEnumConstant());
+        if (node == null)
+        {
+        	if (typeString.equals("Number"))
+        		return "NaN";
+        	return "null";
+        }
         if (node.isString())
             return "'" + node.getString() + "'";
         return "undefined /* TODO type not set */";
diff --git a/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/MemberReference.java b/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/MemberReference.java
index 64be25d..484ae21 100644
--- a/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/MemberReference.java
+++ b/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/MemberReference.java
@@ -21,6 +21,7 @@ package org.apache.royale.compiler.internal.codegen.typedefs.reference;
 
 import org.apache.royale.compiler.clients.ExternCConfiguration.ExcludedMember;
 import org.apache.royale.compiler.clients.ExternCConfiguration.ReadOnlyMember;
+import org.apache.royale.compiler.clients.ExternCConfiguration.TrueConstant;
 
 import com.google.javascript.rhino.JSDocInfo;
 import com.google.javascript.rhino.Node;
@@ -56,4 +57,11 @@ public abstract class MemberReference extends BaseReference
     			getClassReference(), this);
     }
 
+    @Override
+    public TrueConstant isTrueConstant()
+    {
+    	return getClassReference().getModel().isTrueConstant(
+    			getClassReference(), this);
+    }
+
 }
diff --git a/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/ReferenceModel.java b/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/ReferenceModel.java
index fc598b7..f0fcd24 100644
--- a/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/ReferenceModel.java
+++ b/compiler-externc/src/main/java/org/apache/royale/compiler/internal/codegen/typedefs/reference/ReferenceModel.java
@@ -27,6 +27,7 @@ import java.util.List;
 import org.apache.royale.compiler.clients.ExternCConfiguration;
 import org.apache.royale.compiler.clients.ExternCConfiguration.ExcludedMember;
 import org.apache.royale.compiler.clients.ExternCConfiguration.ReadOnlyMember;
+import org.apache.royale.compiler.clients.ExternCConfiguration.TrueConstant;
 import org.apache.royale.compiler.clients.problems.ProblemQuery;
 import org.apache.royale.compiler.internal.codegen.typedefs.utils.DebugLogUtils;
 
@@ -375,4 +376,9 @@ public class ReferenceModel
         DebugLogUtils.err(message);
     }
 
+	public TrueConstant isTrueConstant(ClassReference classReference,
+			MemberReference memberReference) {
+    	return getConfiguration().isTrueConstant(classReference, memberReference);
+	}
+
 }
diff --git a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleGlobalClasses.java b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleGlobalClasses.java
index 533332a..f00b0d2 100644
--- a/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleGlobalClasses.java
+++ b/compiler-jx/src/test/java/org/apache/royale/compiler/internal/codegen/js/royale/TestRoyaleGlobalClasses.java
@@ -209,6 +209,15 @@ public class TestRoyaleGlobalClasses extends TestGoogGlobalClasses
         assertOut("var /** @type {number} */ a = 0");
     }
 
+    @Override
+    @Test
+    public void testMath()
+    {
+        IVariableNode node = getVariable("var a:Number = Math.PI;");
+        asBlockWalker.visitVariable(node);
+        assertOut("var /** @type {number} */ a = Math[\"PI\"]");
+    }
+
     @Test
     public void testDateSetSeconds()
     {