Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibJS: let variables are not captured correctly #23552

Open
tcl3 opened this issue Mar 11, 2024 · 1 comment
Open

LibJS: let variables are not captured correctly #23552

tcl3 opened this issue Mar 11, 2024 · 1 comment
Labels
bug Something isn't working has-repro We have a way to reproduce this bug.

Comments

@tcl3
Copy link
Member

tcl3 commented Mar 11, 2024

When a let variable is captured by a function, it's value at the time it was captured should be used. Currently, Ladybird behaves as if a var variable was being captured.

Example:

<!DOCTYPE html>
<script>
    let result = "";
    let functionList = [];
    for (let i = 0; i < 9; i++) {
      functionList.push(function() {
          return i;
      });
    }
    for (let i = 0; i < functionList.length; i++) {
        result += functionList[i]();
    }
    // Output in Ladybird is: 999999999.
    // Output should be: 012345678.
    console.log(result); 
</script>

I initially spotted this while working on #23544, looking at this WPT subtest.

@tcl3 tcl3 added bug Something isn't working has-repro We have a way to reproduce this bug. labels Mar 11, 2024
@Lubrsi
Copy link
Member

Lubrsi commented Mar 11, 2024

This could potentially be us missing CreatePerIterationEnvironment: https://tc39.es/ecma262/multipage/ecmascript-language-statements-and-declarations.html#sec-createperiterationenvironment

For example, logging result in the function shows that result is captured by reference instead of by value:

    let result = "";
    let functionList = [];
    for (let i = 0; i < 9; i++) {
      result += i;
      functionList.push(function() {
          console.log(result);
          return i;
      });
    }
    for (let i = 0; i < functionList.length; i++) {
        result += functionList[i]();
    }
    console.log(result); 

Chrome:
image
Ladybird:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has-repro We have a way to reproduce this bug.
Projects
No open projects
Status: Todo
Development

No branches or pull requests

2 participants