From 9b6ae7fdf7a2433bcc399107b49405b1013ebe9a Mon Sep 17 00:00:00 2001 From: "igor%mir2.org" Date: Sat, 2 Nov 2002 18:01:26 +0000 Subject: [PATCH] Allow for TokenStream.COMMA node in the parsed tree to contain more then 2 children to avoid too-deep recursion during code generation for long array and object initialization lists. --- .../src/org/mozilla/javascript/IRFactory.java | 26 +++++++++---------- .../org/mozilla/javascript/Interpreter.java | 9 ++++--- .../mozilla/javascript/optimizer/Codegen.java | 15 +++++++---- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/js/rhino/src/org/mozilla/javascript/IRFactory.java b/js/rhino/src/org/mozilla/javascript/IRFactory.java index 2542706bae01..e4ea31b79b4d 100644 --- a/js/rhino/src/org/mozilla/javascript/IRFactory.java +++ b/js/rhino/src/org/mozilla/javascript/IRFactory.java @@ -584,14 +584,13 @@ public class IRFactory { */ public Object createArrayLiteral(Object obj) { Node array; - Node result; - array = result = new Node(TokenStream.NEW, - Node.newString(TokenStream.NAME, "Array")); - Node temp = createNewTemp(result); - result = temp; + array = new Node(TokenStream.NEW, + Node.newString(TokenStream.NAME, "Array")); + Node temp = createNewTemp(array); Node elem = null; int i = 0; + Node comma = new Node(TokenStream.COMMA, temp); for (Node cursor = ((Node) obj).getFirstChild(); cursor != null;) { // Move cursor to cursor.next before elem.next can be // altered in new Node constructor @@ -606,7 +605,7 @@ public class IRFactory { Node addelem = new Node(TokenStream.SETELEM, createUseTemp(temp), Node.newNumber(i), elem); i++; - result = new Node(TokenStream.COMMA, result, addelem); + comma.addChildToBack(addelem); } /* @@ -630,15 +629,15 @@ public class IRFactory { { Node setlength = new Node(TokenStream.SETPROP, createUseTemp(temp), - Node.newString( - "length"), + Node.newString("length"), Node.newNumber(i)); - result = new Node(TokenStream.COMMA, result, setlength); + comma.addChildToBack(setlength); } } else { array.addChildToBack(Node.newNumber(i)); } - return new Node(TokenStream.COMMA, result, createUseTemp(temp)); + comma.addChildToBack(createUseTemp(temp)); + return comma; } /** @@ -651,8 +650,8 @@ public class IRFactory { Node result = new Node(TokenStream.NEW, Node.newString(TokenStream.NAME, "Object")); Node temp = createNewTemp(result); - result = temp; + Node comma = new Node(TokenStream.COMMA, temp); for (Node cursor = ((Node) obj).getFirstChild(); cursor != null;) { Node n = cursor; cursor = cursor.getNextSibling(); @@ -663,9 +662,10 @@ public class IRFactory { Node next = cursor; cursor = cursor.getNextSibling(); Node addelem = new Node(op, createUseTemp(temp), n, next); - result = new Node(TokenStream.COMMA, result, addelem); + comma.addChildToBack(addelem); } - return new Node(TokenStream.COMMA, result, createUseTemp(temp)); + comma.addChildToBack(createUseTemp(temp)); + return comma; } /** diff --git a/js/rhino/src/org/mozilla/javascript/Interpreter.java b/js/rhino/src/org/mozilla/javascript/Interpreter.java index 7a61d9acf8a5..4852361fdd83 100644 --- a/js/rhino/src/org/mozilla/javascript/Interpreter.java +++ b/js/rhino/src/org/mozilla/javascript/Interpreter.java @@ -320,10 +320,11 @@ public class Interpreter { case TokenStream.COMMA : iCodeTop = generateICode(child, iCodeTop); - iCodeTop = addByte(TokenStream.POP, iCodeTop); - itsStackDepth--; - child = child.getNextSibling(); - iCodeTop = generateICode(child, iCodeTop); + while (null != (child = child.getNextSibling())) { + iCodeTop = addByte(TokenStream.POP, iCodeTop); + itsStackDepth--; + iCodeTop = generateICode(child, iCodeTop); + } break; case TokenStream.SWITCH : { diff --git a/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java b/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java index 6e3b80bda80f..a43ec854cee2 100644 --- a/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java +++ b/js/rhino/src/org/mozilla/javascript/optimizer/Codegen.java @@ -618,12 +618,17 @@ public class Codegen extends Interpreter { visitSwitch(node, child); break; - case TokenStream.COMMA: - generateCodeFromNode(child, node, -1, -1); - addByteCode(ByteCode.POP); - generateCodeFromNode(child.getNextSibling(), - node, trueLabel, falseLabel); + case TokenStream.COMMA: { + Node next = child.getNextSibling(); + while (next != null) { + generateCodeFromNode(child, node, -1, -1); + addByteCode(ByteCode.POP); + child = next; + next = next.getNextSibling(); + } + generateCodeFromNode(child, node, trueLabel, falseLabel); break; + } case TokenStream.NEWSCOPE: addScriptRuntimeInvoke("newScope", "()",