[Laszlo-checkins] r12946 - in openlaszlo/trunk: WEB-INF/lps/server/sc/src/org/openlaszlo/sc/parser WEB-INF/lps/server/src/org/openlaszlo/sc test/smoke

dda@openlaszlo.org dda at openlaszlo.org
Thu Feb 19 09:11:42 PST 2009


Author: dda
Date: 2009-02-19 09:11:37 -0800 (Thu, 19 Feb 2009)
New Revision: 12946

Modified:
   openlaszlo/trunk/WEB-INF/lps/server/sc/src/org/openlaszlo/sc/parser/ASTIdentifier.java
   openlaszlo/trunk/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
   openlaszlo/trunk/test/smoke/compiler.lzl
Log:
Change 20090217-dda-N by dda at lester-2.local on 2009-02-17 17:09:17 EST
    in /Users/dda/laszlo/src/svn/openlaszlo/trunk-g
    for http://svn.openlaszlo.org/openlaszlo/trunk

Summary: Use apply() rather than call() in catcherrors implementation. [revised]

New Features:

Bugs Fixed: [LPP-7722] SWF9: method having argument and variable with same name behave differently with catcherrors=true

Technical Reviewer: ptw (pending)
QA Reviewer: (pending)
Doc Reviewer: (pending)

Documentation:

Release Notes:

Details:
    ** To be applied also to 4.2.0.2?

    Changed catcherrors implementation to use apply() rather than call() so that arguments appear the same
    within the closure as in the original code.  The one gotcha is that the magic 'arguments' variable
    is not available when there is a variable argument declaration (...rest) present.  When there is a single 
    argument (e.g. function foo(...rest)) the variable name (e.g. rest) can be used in place of arguments.  When there
    are additional fixed arguments (e.g. function foo(a, b, ...rest)) the actual argument list array must be
    assembled from the args (e.g. [a,b].concat(rest))

    Added a compiler test to check this.

  Revised 2/18:
    Removed 'with (this)' from implementation, it is no longer needed.

    Added another compiler test to check that an argument is scoped nearer than object variables, which would
    have failed when inserting 'with (this)'.
 
Tests:
    Tried the test from the Jira.
    Smoke test all platforms, including SWF9 using catcherrors.
    Confirmed that new test is working by trying SWF9 with catcherrors on a fresh tree with just the test changed.
    {SWF9(with catcherrors=true),SWF8,dhtml} x {weather,lzpix}



Modified: openlaszlo/trunk/WEB-INF/lps/server/sc/src/org/openlaszlo/sc/parser/ASTIdentifier.java
===================================================================
--- openlaszlo/trunk/WEB-INF/lps/server/sc/src/org/openlaszlo/sc/parser/ASTIdentifier.java	2009-02-19 16:42:21 UTC (rev 12945)
+++ openlaszlo/trunk/WEB-INF/lps/server/sc/src/org/openlaszlo/sc/parser/ASTIdentifier.java	2009-02-19 17:11:37 UTC (rev 12946)
@@ -3,7 +3,7 @@
 * ****************************************************************************/
 
 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
-* Copyright 2001-2008 Laszlo Systems, Inc.  All Rights Reserved.              *
+* Copyright 2001-2009 Laszlo Systems, Inc.  All Rights Reserved.              *
 * Use is subject to license terms.                                            *
 * J_LZ_COPYRIGHT_END *********************************************************/
 
@@ -101,15 +101,16 @@
         this.isconstructor = value;
     }
 
-    public String toString() {
+    public String toJavascriptString() {
         String dots = ellipsis ? "..." : "";
-        String typesuffix = "";
-        if (type != null) {
-            typesuffix = ": " + type.toString();
-        }
-        return "ASTIdentifier(" + dots + name + typesuffix + ")";
+        String typesuffix = (type == null) ? "" : (": " + type.toString());
+        return dots + name + typesuffix;
     }
 
+    public String toString() {
+        return "ASTIdentifier(" + toJavascriptString() + ")";
+    }
+
     /** Accept the visitor */
     public Object jjtAccept(ParserVisitor visitor, Object data) {
         return visitor.visit(this, data);

Modified: openlaszlo/trunk/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java
===================================================================
--- openlaszlo/trunk/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java	2009-02-19 16:42:21 UTC (rev 12945)
+++ openlaszlo/trunk/WEB-INF/lps/server/src/org/openlaszlo/sc/JavascriptGenerator.java	2009-02-19 17:11:37 UTC (rev 12946)
@@ -1477,13 +1477,44 @@
         Map map = new HashMap();
         newStmts = visitStatement(newStmts);
         map.put("_1", newStmts);
-        String frag = "$lzsc$ret = (function()" + tryType + " {";
-        if (isStatic) {
-          frag += " { _1 }}).call(null);";
+
+        // build a typed parameter list for the function closure, which
+        // we will normally invoke using apply(this, arguments).
+        // When rest args (i.e. variable args) are present then 'arguments'
+        // is not available, so we must use the rest arg.  If there are fixed
+        // args in addition to rest args (e.g. function foo(a, b, ...rest))
+        // we'll need to assemble the new arg list array.
+        String paramlist = "";
+        String arglist = "";    // only used when fixed args mixed with ...rest
+        String applyarg = "arguments";
+        ParseTreePrinter ptp = new ParseTreePrinter();
+        for (int pcnt = 0, len = params.size(); pcnt < len; pcnt++) {
+          SimpleNode param = passThrough(params.get(pcnt));
+          // Must keep initializers, as apply honors them
+          if (param instanceof ASTIdentifier) {
+            ASTIdentifier paramid = (ASTIdentifier)param;
+            if (paramid.getEllipsis()) {
+              if (pcnt == 0) {
+                applyarg = paramid.getName();
+              }
+              else {
+                applyarg = "[" + arglist + "].concat(" + paramid.getName() + ")";
+              }
+              assert (pcnt + 1 == len) : "ellipsis param must be last";
+            }
+            if (pcnt > 0) {
+              paramlist += ",";
+              arglist += ",";
+            }
+            paramlist += paramid.toJavascriptString();
+            arglist += paramid.getName();
+          }
+          else if (param instanceof ASTFormalInitializer) {
+            paramlist += "=(" + ptp.text(param.get(0)) + ")";
+          }
         }
-        else {
-          frag += " with (this) { _1 }}).call(this);";
-        }
+        String frag = "$lzsc$ret = (function(" + paramlist + ")" + tryType +
+          " { _1 }).apply(" + (isStatic ? "null" : "this") + ", " + applyarg + ");";
         newStmts = new Compiler.PassThroughNode((new Compiler.Parser()).substitute(newStmts, frag, map));
       }
       tryNode.set(i++, newStmts);

Modified: openlaszlo/trunk/test/smoke/compiler.lzl
===================================================================
--- openlaszlo/trunk/test/smoke/compiler.lzl	2009-02-19 16:42:21 UTC (rev 12945)
+++ openlaszlo/trunk/test/smoke/compiler.lzl	2009-02-19 17:11:37 UTC (rev 12946)
@@ -2,7 +2,7 @@
     Compiler Tests
 -->
 <!-- * X_LZ_COPYRIGHT_BEGIN ***************************************************
-     * Copyright 2001-2006, 2008 Laszlo Systems, Inc.  All Rights Reserved.              *
+     * Copyright 2001-2009 Laszlo Systems, Inc.  All Rights Reserved.              *
      * Use is subject to license terms.                                            *
      * X_LZ_COPYRIGHT_END ****************************************************** -->
 
@@ -132,8 +132,26 @@
              }
       ]]>
     </method>
+
+    <method name="varScope" args="ar1">
+       assertEquals("argvalue", ar1, "argument original value is used")
+       var ar1 = "varvalue"; 
+       assertEquals("varvalue", ar1, "redeclared variable value is used")
+    </method>
+
+    <method name="argScope">
+        var foo = {a: 3, test: function (a, ...rest) { return a;}};
+        assertEquals("42", foo.test(42), "argument scope used before object scope");
+     </method>
+
+    <method name="testMiscellaneousCompilation">
+      varScope("argvalue");
+      argScope();
+    </method>
+
     <method name="addTests"> 
         this.addTest("testOptionalArguments");
+        this.addTest("testMiscellaneousCompilation");
         this.addTest("testConditionalCompilation");
     </method> 
   </class>



More information about the Laszlo-checkins mailing list