Permalink
Browse files

Fix bug related to unary operation in for loop.

The problem is due to in unary operation statement, local var is pushed to stack for operation, we need to remove it from the stack otherwise for loop will have exception (https://github.com/dynjs/dynjs/blob/master/src/main/java/org/dynjs/parser/statement/AbstractUnaryOperationStatement.java#L50).
  • Loading branch information...
jingweno committed Jul 20, 2012
1 parent afd0810 commit fbd43104916c40cef51adcb1869ba39b2aa285d8
@@ -59,6 +59,9 @@ public CodeBlock getCodeBlock() {
append(statement.getCodeBlock());
label(preIncrement);
append(increment.getCodeBlock());
+ if (increment instanceof AbstractUnaryOperationStatement) {
+ pop();
+ }
go_to(statement.getBeginLabel());
label(statement.getEndLabel());
labelStack.pop();
@@ -57,6 +57,9 @@ public CodeBlock getCodeBlock() {
append(statement.getCodeBlock());
label(preIncrement);
append(increment.getCodeBlock());
+ if (increment instanceof AbstractUnaryOperationStatement) {
+ pop();
+ }
go_to(statement.getBeginLabel());
label(statement.getEndLabel());
labelStack.pop();
@@ -59,7 +59,7 @@ public void assignsGlobalVariables() {
.isInstanceOf(String.class)
.isEqualTo("test");
}
-
+
@Test
public void defineUnInitializedGlobalVariables() {
dynJS.eval(context, "var x;");
@@ -172,7 +172,7 @@ public void testFor() {
check("var x = 0; for (var i =0;i < 10; i+=1){ x+=1;}; var result = x == 10");
check("var x = 0; var i =0; for (var w = 0;i < 10; i+=1){ x+=1;}; var result = i == 10");
check("var x = 0; for (; x < 10; x += 1) { x += 1; }; var result = x == 10");
- check("var i = 0; var x = 33; for(; i < 10; i += 1) { x -= 1} var result = x == 23");
+ check("var i = 0; var x = 33; for(; i < 10; i++) { x -= 1} var result = x == 23");
}
@Test

0 comments on commit fbd4310

Please sign in to comment.