Reviewers: , Description: Syntax error due to inlining of numeric literal into jsni function
Java code: public class Main implements EntryPoint { private native static String toFixed(double value, int n) /*-{ return value.toFixed(n); }-*/; public void onModuleLoad() { Window.alert(toFixed(42.0, 2)); } } Output: $wnd.alert(42.toFixed(2)) // syntax error 42.toFixed(2) should be (42).toFixed(2) This patch handles both invocations (42.method()) and name refs (42.field). Please review this at http://gwt-code-reviews.appspot.com/47809 Affected files: dev/core/src/com/google/gwt/dev/js/JsToStringGenerationVisitor.java Index: dev/core/src/com/google/gwt/dev/js/JsToStringGenerationVisitor.java =================================================================== --- dev/core/src/com/google/gwt/dev/js/JsToStringGenerationVisitor.java (revision 5638) +++ dev/core/src/com/google/gwt/dev/js/JsToStringGenerationVisitor.java (working copy) @@ -982,6 +982,14 @@ p.print(CHARS_IF); } + // Issue #3796, inlining of numeric literals can produce illegal JS + // expressions. x = 42, x.method() is legal. 42.method() is illegal. + // The fix is to add parentheses, e.g. (42).method() + private boolean _isWeirdLiteralDot(JsExpression parent, JsExpression child) { + return child instanceof JsNumberLiteral && (parent instanceof JsInvocation + || parent instanceof JsNameRef); + } + private void _in() { p.print(CHARS_IN); } @@ -1044,9 +1052,10 @@ boolean wrongAssoc) { int parentPrec = JsPrecedenceVisitor.exec(parent); int childPrec = JsPrecedenceVisitor.exec(child); - return (parentPrec > childPrec || (parentPrec == childPrec && wrongAssoc)); + return (parentPrec > childPrec || (parentPrec == childPrec && wrongAssoc) || + _isWeirdLiteralDot(parent, child)); } - + private boolean _parenPop(JsExpression parent, JsExpression child, boolean wrongAssoc) { boolean doPop = _parenCalc(parent, child, wrongAssoc); --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---