try/finally without catch doesn't work right!

RESOLVED FIXED

Status

Rhino
Core
P3
normal
RESOLVED FIXED
18 years ago
18 years ago

People

(Reporter: David Flanagan, Assigned: Norris Boyd)

Tracking

Details

(Reporter)

Description

18 years ago
I'm using rhino from the file rhinoTip.zip, of 9/11/00.

The following code doesn't work as it should unless you uncomment
the catch clause.  Otherwise, I get a bogus exception from the 
finally clause().

var i = 0, total = 0; 
var a = [1,2,3,4];
while(i < a.length) {
    try {
        print(i);
        print(a[i]);
        if ((typeof a[i] != "number") || isNaN(a[i])) // If it is not a number
            continue;  // then go on to the next iteration of the loop.
        total += a[i]; // Otherwise, add the number to the total.
        print("total: " + total);
    }
/*    catch(e) { print(e); } */
    finally {
        i++;  // Always increment i, even if we used continue above
    }
}
(Assignee)

Comment 1

18 years ago
Just occurs in interpretive mode.
Status: NEW → ASSIGNED
(Assignee)

Comment 2

18 years ago
This patch fixes it. Roger, would you review this change?

Index: org/mozilla/javascript/Interpreter.java
===================================================================
RCS file: /cvsroot/mozilla/js/rhino/org/mozilla/javascript/Interpreter.java,v
retrieving revision 1.33
diff -u -r1.33 Interpreter.java
--- Interpreter.java    2000/08/15 15:54:11     1.33
+++ Interpreter.java    2000/10/12 16:08:31
@@ -493,8 +493,7 @@
                 */
                     Node target = (Node)(node.getProp(Node.TARGET_PROP));
                     target.putProp(Node.FINALLY_PROP, node);
-                    iCodeTop = addGoto(node, TokenStream.GOSUB,
-                                                        iCodeTop);
+                    iCodeTop = addGoto(node, TokenStream.GOSUB, iCodeTop);
                 }
                 break;

@@ -848,20 +847,25 @@
                         exception object.
                     */
                     while (child != null) {
-                        if (lastChild == catchTarget) {
+                        if (catchTarget != null && lastChild == catchTarget) {
                             itsStackDepth = 1;
                             if (itsStackDepth > itsData.itsMaxStack)
                                 itsData.itsMaxStack = itsStackDepth;
                         }
                         /*
-                            When the following child is the catchTarget,
+                            When the following child is the catchTarget
+                            (or the finallyTarget if there are no catches),
                             the current child is the goto at the end of
                             the try statemets, we need to emit the endtry
                             before that goto.
                         */
-                        if (child.getNextSibling() == catchTarget)
+                        Node nextSibling = child.getNextSibling();
+                        if (nextSibling == catchTarget ||
+                            nextSibling == finallyTarget)
+                        {
                             iCodeTop = addByte((byte) TokenStream.ENDTRY,
-                                                                iCodeTop);
+                                               iCodeTop);
+                        }
                         iCodeTop = generateICode(child, iCodeTop);
                         lastChild = child;
                         child = child.getNextSibling();

Checking in org/mozilla/javascript/Interpreter.java;
/cvsroot/mozilla/js/rhino/org/mozilla/javascript/Interpreter.java,v  <--  Interp
reter.java
new revision: 1.34; previous revision: 1.33
done
Status: ASSIGNED → RESOLVED
Last Resolved: 18 years ago
Resolution: --- → FIXED

Comment 3

18 years ago
looks good to me

Comment 4

18 years ago
Compare bug 56716 in JS Engine -
You need to log in before you can comment on or make changes to this bug.