Selaa lähdekoodia

Use correct createArgsRest variant when arguments are not in stash. Fixes #327

Dmitry Panov 4 vuotta sitten
vanhempi
commit
994d93d2a4
2 muutettua tiedostoa jossa 36 lisäystä ja 1 poistoa
  1. 1 1
      compiler_expr.go
  2. 35 0
      compiler_test.go

+ 1 - 1
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 {

+ 35 - 0
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}) {