Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit fbd43104916c40cef51adcb1869ba39b2aa285d8 1 parent afd0810
@jingweno jingweno authored
View
3  src/main/java/org/dynjs/parser/statement/ForStepExprStatement.java
@@ -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();
View
3  src/main/java/org/dynjs/parser/statement/ForStepVarStatement.java
@@ -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();
View
4 src/test/java/org/dynjs/runtime/DynJSTest.java
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.