Skip to content

Commit

Permalink
Fix const declarations in function scopes
Browse files Browse the repository at this point in the history
The code to avoid duplicate const assignments is no longer needed, as
the Parser generates an error for it.
  • Loading branch information
mattco98 committed Dec 25, 2023
1 parent 96d0c07 commit 9826483
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/mozilla/javascript/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package org.mozilla.javascript;

import org.mozilla.javascript.ast.*;
import org.mozilla.javascript.optimizer.Codegen;

import java.util.Iterator;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -67,7 +66,8 @@ public class Node implements Iterable<Node> {
PRIVATE_ACCESS_PROP = 34,
EXPORT_PROP = 35,
INITIALIZE_PROP = 36,
PARTIAL_PROP = 37;
PARTIAL_PROP = 37,
CONST_DECL_PROP = 38;

// values of ISNUMBER_PROP to specify
// which of the children are Number types
Expand Down
33 changes: 1 addition & 32 deletions src/main/java/org/mozilla/javascript/optimizer/Codegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -6045,15 +6045,7 @@ private void visitSetVar(Node node, Node child, boolean needValue) {
generateExpression(child.getNext(), node);
boolean isNumber = (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1);
short reg = varRegisters[varIndex];
boolean[] constDeclarations = fnCurrent.fnode.getParamAndVarConst();
if (constDeclarations[varIndex]) {
if (!needValue) {
if (isNumber)
cfw.add(ByteCode.POP2);
else
cfw.add(ByteCode.POP);
}
} else if (varIsDirectCallParameter(varIndex)) {
if (varIsDirectCallParameter(varIndex)) {
if (isNumber) {
if (needValue) cfw.add(ByteCode.DUP2);
cfw.addALoad(reg);
Expand Down Expand Up @@ -6102,40 +6094,17 @@ private void visitSetConstVar(Node node, Node child, boolean needValue) {
generateExpression(child.getNext(), node);
boolean isNumber = (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1);
short reg = varRegisters[varIndex];
int beyond = cfw.acquireLabel();
int noAssign = cfw.acquireLabel();
if (isNumber) {
cfw.addILoad(reg + 2);
cfw.add(ByteCode.IFNE, noAssign);
short stack = cfw.getStackTop();
cfw.addPush(1);
cfw.addIStore(reg + 2);
cfw.addDStore(reg);
if (needValue) {
cfw.addDLoad(reg);
cfw.markLabel(noAssign, stack);
} else {
cfw.add(ByteCode.GOTO, beyond);
cfw.markLabel(noAssign, stack);
cfw.add(ByteCode.POP2);
}
} else {
cfw.addILoad(reg + 1);
cfw.add(ByteCode.IFNE, noAssign);
short stack = cfw.getStackTop();
cfw.addPush(1);
cfw.addIStore(reg + 1);
cfw.addAStore(reg);
if (needValue) {
cfw.addALoad(reg);
cfw.markLabel(noAssign, stack);
} else {
cfw.add(ByteCode.GOTO, beyond);
cfw.markLabel(noAssign, stack);
cfw.add(ByteCode.POP);
}
}
cfw.markLabel(beyond);
}

private void visitGetProp(Node node, Node child) {
Expand Down

0 comments on commit 9826483

Please sign in to comment.