diff --git a/compiler_expr.go b/compiler_expr.go index 030e021c..aca59407 100644 --- a/compiler_expr.go +++ b/compiler_expr.go @@ -1150,7 +1150,7 @@ func (e *compiledFunctionLiteral) emitGetter(putOnStack bool) { e.c.p.code[enterFunc2Mark] = ef2 } } - if emitArgsRestMark != -1 { + if emitArgsRestMark != -1 && s.argsInStash { e.c.p.code[emitArgsRestMark] = createArgsRestStash } } else { diff --git a/compiler_test.go b/compiler_test.go index 22215f2e..b9b0a7e8 100644 --- a/compiler_test.go +++ b/compiler_test.go @@ -3942,6 +3942,41 @@ func TestFuncParamRestStashSimple(t *testing.T) { testScript1(SCRIPT, asciiString("2,3"), t) } +func TestRestArgsNotInStash(t *testing.T) { + const SCRIPT = ` + function f(...rest) { + () => rest; + return rest.length; + } + f(1,2); + ` + testScript1(SCRIPT, valueInt(2), t) +} + +func TestRestArgsInStash(t *testing.T) { + const SCRIPT = ` + function f(first, ...rest) { + () => first; + () => rest; + return rest.length; + } + f(1,2); + ` + testScript1(SCRIPT, valueInt(1), t) +} + +func TestRestArgsInStashFwdRef(t *testing.T) { + const SCRIPT = ` + function f(first = eval(), ...rest) { + () => first; + () => rest; + return rest.length === 1 && rest[0] === 2; + } + f(1,2); + ` + testScript1(SCRIPT, valueTrue, t) +} + func TestFuncParamRestPattern(t *testing.T) { const SCRIPT = ` function f(arg1, ...{0: rest1, 1: rest2}) {