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

[Debugger] Incorrect scope variable information for variable initialized in for-loop #1355

Open
Beanyy opened this issue Mar 15, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@Beanyy
Copy link
Contributor

Beanyy commented Mar 15, 2024

Bug Description

I'm running into some issues with a CDP Debugger (vscode-js-debug) connected to Hermes. Namely, when a variable is initialized in a for-loop, like the following:

for (let i=0; i <10; i++) {
    {... some code}
}

The variables in the for-loop initializer don't get updated in the debugger. I've narrowed down the root cause to Hermes sending two copies of the variable when Runtime.getProperties is called on the local scope and two copies of the variable are returned. One for the initial value, and one for the current value:

            {"configurable":false,"enumerable":true,"name":"i","value":{"type":"number","value":2}},
            {"configurable":false,"enumerable":true,"name":"i","value":{"type":"number","value":0}},

Digging through the code I think it may be how Hermes is transforming a for block, creating two copies of the variable initialized in the initializer const/let x = init; and const/let x = temp_x;:

/// const/let x = init;
/// let temp_x = x;
/// let first = true;
/// undefined;
/// for (;;) {
/// const/let x = temp_x;
/// if (first) {
/// first = false;
/// } else {
/// update;
/// }
/// if (!test) break;
/// control = true;
/// [label:]* for (; control; control = false, temp_x = x) {
/// body
/// }
/// if (control) break;
/// }
/// }

Moving the variable initialization outside of the for-loop fixes the issue.

Do you think you guys can help with this issue? Thanks!

Hermes git revision (if applicable): 7d9baea7221a1f69828764938fb5de0737419f6b
React Native version: n/a
OS: Android
Platform: arm64-v8a

Steps To Reproduce

  1. Single step through the following code:
for (let i=0; i <10; i++) {
    {... some code}
}
  1. Each time Runtime.getProperties is called on the local scope object, it will return two copies of the variable i:
            {"configurable":false,"enumerable":true,"name":"i","value":{"type":"number","value":2}},
            {"configurable":false,"enumerable":true,"name":"i","value":{"type":"number","value":0}},

The Expected Behavior

Expected behaviour is for there to just be one copy of the for loop's initializer variable to be sent in the debugger info.

@Beanyy Beanyy added the bug Something isn't working label Mar 15, 2024
@tmikov
Copy link
Contributor

tmikov commented Mar 16, 2024

Are you using the --block-scoping CLI option? It is experimental and unsupported - we have meanwhile switched to a completely different approach for implementing block scoping.

(With that said, we can take a look if this is something easily fixable)

@Beanyy
Copy link
Contributor Author

Beanyy commented Mar 18, 2024

Disabling block scoping resolved the issue I was seeing. Thanks for the help!
How do I enable the new implementation of block scoping?

@tmikov
Copy link
Contributor

tmikov commented Mar 18, 2024

@Beanyy the new implementation will be in the next major version (codename Static Hermes), which we are hoping to release as a drop-in replacement of Hermes by the middle of the year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants