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

Closure scope leak in for loops #707

Closed
1 task done
oblador opened this issue Mar 21, 2022 · 2 comments
Closed
1 task done

Closure scope leak in for loops #707

oblador opened this issue Mar 21, 2022 · 2 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@oblador
Copy link

oblador commented Mar 21, 2022

Bug Description

Hermes uses var semantics for let/const variables, leading to scope leaking in for loops.

  • I have run gradle clean and confirmed this bug does not occur with JSC

Hermes version: 0.11.0
React Native version (if any): N/A
OS version (if any): macOS
Platform (most likely one of arm64-v8a, armeabi-v7a, x86, x86_64): arm64-v8a

Steps To Reproduce

const keys = ['a', 'b', 'c'];
const obj = {};
for (let i = 0; i < keys.length; i++) {
  const key = keys[i];
  obj[key] = () => key;
}
console.log([obj.a(), obj.b(), obj.c()]);

Hermes output: ['c', 'c', 'c'] 🛑

The Expected Behavior

JSC output: ['a', 'b', 'c']
Node output: ['a', 'b', 'c']

@oblador oblador added the bug Something isn't working label Mar 21, 2022
@neildhar
Copy link
Contributor

Hi @oblador, Hermes does not support block scoping, and we instead rely on babel transforms to lower code like this into something that Hermes can correctly run. Please see #575 for more details.

@neildhar neildhar added duplicate This issue or pull request already exists enhancement New feature or request and removed bug Something isn't working labels Mar 21, 2022
@oblador
Copy link
Author

oblador commented Mar 22, 2022

Wouldn't it be better to not support the let/const syntax alltogether?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants