Bug 238149 - Variable in top level block scope is incorrectly captured
Summary: Variable in top level block scope is incorrectly captured
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: Safari 15
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-03-21 12:02 PDT by neildhar
Modified: 2024-02-24 00:55 PST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description neildhar 2022-03-21 12:02:05 PDT
For the following snippet placed at the top level:

do {
    let w = "banana";
    function foo(){
        w = "bar";
    }
    foo();
    console.log(w);
} while(0)


JSC prints "banana", instead of "bar".
Comment 1 Radar WebKit Bug Importer 2022-03-22 09:07:31 PDT
<rdar://problem/90639204>
Comment 2 Jarred Sumner 2022-05-22 02:35:05 PDT
This seems to only happen when strict mode is off.


The following code doesn't reproduce the issue in jsc shell:

"use strict";

do {
  let w = "banana";
  function foo() {
    w = "bar";
  }
  foo();
  print(w);
} while (0);

But this code does:

do {
  let w = "banana";
  function foo() {
    w = "bar";
  }
  foo();
  print(w);
} while (0);
Comment 3 uncomment 2024-02-24 00:55:46 PST
The variable is not even visible within the function:

do {
    let w = "banana";
    function foo(){
        console.log(w);
    }
    foo();
} while(0)

Result (unexpected): ReferenceError: Can't find variable: w

Unlike with arrow function:

do {
    let w = "banana";
    foo=()=>{
        console.log(w);
    };
    foo();
} while(0)

Result (as expected): banana