serenity icon indicating copy to clipboard operation
serenity copied to clipboard

LibJS: `let` variables are not captured correctly

Open tcl3 opened this issue 11 months ago • 1 comments

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 avatar Mar 11 '24 18:03 tcl3