Bug 265526

Summary: Incorrect `with` statement behavior - function call breaks out of `with` 'scope'
Product: WebKit Reporter: Joe <niseg98160>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: RESOLVED DUPLICATE    
Severity: Normal CC: ashvayka, karlcow, mark.lam, webkit-bug-importer, ysuzuki
Priority: P2 Keywords: InRadar
Version: Safari 17   
Hardware: Unspecified   
OS: Unspecified   

Description Joe 2023-11-29 08:45:49 PST
(Obligatory: The `with` statement has long been deprecated, but unfortunately since there is a lot of legacy code which relies on it, one cannot always avoid interacting with code that relies on `with`.)

The following code works fine on all other browsers. Safari throws "ReferenceError: Can't find variable: abc".

```
with({abc:3}) {
  function foo() {
    console.log(abc);
  }
  foo();
}
```

I am managing a large-ish (~4M MAU) platform for which this has become an issue in certain situations, and currently I just have to tell users "Please use Firefox or Chrome". I'd like to eventually get my hands dirty and try to implement a hacky fix here (it involves interaction with user-land code, so it's not easy), but if the `with` behavior could be brought into line with other engines that would be excellent.
Comment 1 Joe 2023-11-29 08:53:07 PST
One weird thing I noticed: If I pause the debugger, and then run the code in the console, it works fine. If the debugger is not paused and I run the code, then I get the ReferenceError that I mentioned in the previous message.
Comment 2 Karl Dubost 2023-11-29 19:08:53 PST
it's working for me with and without the debugger paused. 
So there is something else going on.
Comment 3 Radar WebKit Bug Importer 2023-12-06 08:46:13 PST
<rdar://problem/119258532>
Comment 4 Mark Lam 2024-01-23 10:43:37 PST
Hi Joe, can you tell me what website(s) are impacted by this, or if you don't want to disclose that publicly, can you email me the details?  That will help us prioritize this accordingly.  Thanks.
Comment 5 Alexey Shvayka 2024-01-23 14:01:14 PST
Thank you Joe for filing this so very nicely!

The issue was fixed in https://commits.webkit.org/268553@main, please retry with the newest Safari Technology Preview.

(In reply to Joe from comment #1)
> One weird thing I noticed: If I pause the debugger, and then run the code in
> the console, it works fine. If the debugger is not paused and I run the
> code, then I get the ReferenceError that I mentioned in the previous message.

This makes sense: before https://bugs.webkit.org/show_bug.cgi?id=163209 was fixed, functions declared in global scope but inside a block (`with` statement block) were erroneously created in global scope.

*** This bug has been marked as a duplicate of bug 163209 ***