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

Nullish Coalescing operator - functions shouldn't be evaluated multiple times #1623

Closed
0xe opened this issue Sep 12, 2024 · 0 comments · Fixed by #1624
Closed

Nullish Coalescing operator - functions shouldn't be evaluated multiple times #1623

0xe opened this issue Sep 12, 2024 · 0 comments · Fixed by #1624

Comments

@0xe
Copy link
Contributor

0xe commented Sep 12, 2024

Spec clearly says to evaluate the CoalesceExpression once:

https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#prod-CoalesceExpression

CoalesceExpression : CoalesceExpressionHead ?? BitwiseORExpression

  1. Let lRef be ? Evaluation of CoalesceExpressionHead.
  2. Let lVal be ? GetValue(lRef).
  3. If lVal is either undefined or null, then
    a. Let rRef be ? Evaluation of BitwiseORExpression.
    b. Return ? GetValue(rRef).
  4. Else,
    a. Return lVal.

Yet, we generate tree like this, that evaluates it multiple times.

    VAR 2
        NAME eval1 2
            HOOK
                OR
                    SHEQ
                        NULL
                        CALL 2
                            NAME f 2
                    SHEQ
                        NAME undefined
                        CALL 2
                            NAME f 2
                NUMBER 42.0 2
                CALL 2
                    NAME f 2

I'm working on fixing this. (Thanks @andreabergia for noticing!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant