Closed Bug 1022967 Opened 5 years ago Closed 3 years ago

Separate environment for default parameter and function body if default parameter contains expression

Categories

(Core :: JavaScript Engine, defect)

defect
Not set

Tracking

()

RESOLVED FIXED

People

(Reporter: arai, Assigned: shu)

References

Details

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0 (Beta/Release)
Build ID: 20140605174243

Steps to reproduce:

(Separated from 1018628)

Evaluate following code in Firefox 29:

> var z = 10;
> function f(b=(() => z) {
>   var z = 20;
>   return b();
> }
> f()



Actual results:

|f()| returns |20|.



Expected results:

|f()| should return |10|.

This is caused by that variable |z| inside function body is visible from closure in default parameter, and the closure captures it instead of |z| in outer binding.

Related to step 28 of 9.2.13 in the spec:
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-functiondeclarationinstantiation
Blocks: es6, 884372
Depends on: 1018628, 1022962
OS: Mac OS X → All
Hardware: x86 → All
There is a missing parens in your code above (and the >). Repasting one which is easier to copy:

var z = 10;
function f(b=(() => z)) {
  var z = 20;
  return b();
}
console.log(f())
Status: UNCONFIRMED → NEW
Ever confirmed: true
Blocks: es6:let
Summary: Separate environment for default parameter and function body if default parameter contains closure → Separate environment for default parameter and function body if default parameter contains expression
This bug will also have effect for the case when default parameter refers the function declared in the function body.
So I changed the Summary to follow the spec:
  6. Let needsParameterEnvironment be ContainsExpression of formals.
Duplicate of this bug: 1054998
Duplicate of this bug: 1213254
Assignee: nobody → shu
Duplicate of this bug: 1107712
See Also: → 1281359
No longer blocks: es6
No longer blocks: 884372
Fixed by bug 1263355.
Status: NEW → RESOLVED
Closed: 3 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.