Skip to content

Commit

Permalink
Fix param count for functions with rest parameters
Browse files Browse the repository at this point in the history
This also fixes rest parameters not being included in the function
initialization logic, which led to the values being incorrect.
  • Loading branch information
mattco98 committed Nov 19, 2023
1 parent 351d930 commit c208a2b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/main/java/org/mozilla/javascript/NativeFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ public boolean hasRest() {

@Override
public int getLength() {
int paramCount = getParamCount() - (hasRest() ? 1 : 0);
if (getLanguageVersion() != Context.VERSION_1_2) {
return paramCount;
return getParamCount();
}
Context cx = Context.getContext();
NativeCall activation = ScriptRuntime.findFunctionActivation(cx, this);
if (activation == null) {
return paramCount;
return getParamCount();
}
return activation.effectiveArgs.length;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,11 @@ private VariableDeclaration variables(int declType, int pos, boolean isStatement
}
}
defineSymbol(declType, ts.getString(), inForInit);

if (rest) {
// Decrement paramCount by 1
currentScriptOrFn.setParamCount(currentScriptOrFn.getParamCount() - 1);
}
}

int lineno = ts.lineno;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/mozilla/javascript/ast/ScriptNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ public int getParamCount() {
return paramCount;
}

public void setParamCount(int paramCount) {
this.paramCount = paramCount;
}

public int getParamAndVarCount() {
if (variableNames == null) codeBug();
return symbols.size();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/mozilla/javascript/optimizer/Codegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,7 @@ private void generatePrologue() {
paramCount = fnCurrent.fnode.getParamCount();
int varCount = fnCurrent.fnode.getParamAndVarCount();
boolean[] constDeclarations = fnCurrent.fnode.getParamAndVarConst();
boolean hasRest = fnCurrent.fnode.hasRest();

List<AstNode> params = fnCurrent.fnode.getParams();
Map<Integer, Node> defaultParams = fnCurrent.fnode.getDefaultParams();
Expand All @@ -1919,7 +1920,7 @@ private void generatePrologue() {
short firstUndefVar = -1;
for (int i = 0; i != varCount; ++i) {
short reg = -1;
if (i < paramCount) {
if (i < paramCount + (hasRest ? 1 : 0)) {
if (!inDirectCallFunction) {
reg = getNewWordLocal();

Expand Down

0 comments on commit c208a2b

Please sign in to comment.