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

Function name scope problem #354

Open
RuralHunter opened this issue May 14, 2021 · 10 comments
Open

Function name scope problem #354

RuralHunter opened this issue May 14, 2021 · 10 comments
Labels
js-engine Issues related to the js engine

Comments

@RuralHunter
Copy link
Contributor

RuralHunter commented May 14, 2021

This code snippet:

function e()
{
    console.log("f="+f);
    if(!f)
    {
        function f(){}
    }
}

var f=function(){};
e();

In Chrome, the output is "f=undefined". In HtmlUnit, the output is "f=function(){}".

@RuralHunter
Copy link
Contributor Author

Just found if I set the optimization level of the context to 0 or above, I can get the same result as Chrome.

@rbri
Copy link
Member

rbri commented May 20, 2021

Sounds strange will have a look over the weekend

@RuralHunter
Copy link
Contributor Author

Based on the link above, the example can be simplified:

console.log("foo="+foo);
if (false) {
  function foo(){ return 1; }
}

@RuralHunter
Copy link
Contributor Author

RuralHunter commented Jun 2, 2021

It seems the declaration of 'foo' is removed here:

before transform:
SCRIPT [source name: /varscope.js] [encoded source length: 52] [base line: 1] [end line: 5] [scope #1: foo ] 1
    EXPR_RESULT 1
        CALL 1
            NAME print 1
            ADD
                STRING foo=
                NAME foo 1
    BLOCK 3

SCRIPT [source name: /varscope.js] [encoded source length: 52] [base line: 1] [end line: 5] [scope #1: foo ] 1
    EXPR_RESULT 1
        CALL 1
            NAME print 1
            ADD
                STRING foo=
                NAME foo 1
    BLOCK 3

FUNCTION foo [source name: /varscope.js] [encoded source length: 18] [base line: 4] [end line: 4] 4
    BLOCK 4
        RETURN 4
            NUMBER 1.0 4

after transform:
SCRIPT [source name: /varscope.js] [encoded source length: 52] [base line: 1] [end line: 5] [scope #1: foo ] 1
    EXPR_RESULT 1
        CALL 1
            NAME print 1
            ADD
                STRING foo=
                NAME foo 1
    BLOCK 3

@RuralHunter
Copy link
Contributor Author

Maybe not. It seems the tree printed out is not quite complete.

@RuralHunter RuralHunter changed the title Javascript variable scope problem Function name scope problem Jun 10, 2021
@RuralHunter
Copy link
Contributor Author

RuralHunter commented Jun 10, 2021

I found the key point. The function definition is skipped here in runtime. Apparently InterpretedFunction.hasFunctionNamed(name) does not do what its name implies. It returns false for functions declared with expression, returns true otherwise, event it's not a function at all. I changed InterpretedFunction.hasFunctionNamed to simply return a fixed true and then the script runs same with browsers. I don't see any other problem with that change but I might miss something else since I'm not familiar with Rhino.

@rbri
Copy link
Member

rbri commented Apr 10, 2023

Hi @RuralHunter,
sorry but this get lost a bit - looks like your findings are correct, will try to get the fix into the Rhino codebase soon.
(mozilla/rhino#1318)

@RuralHunter
Copy link
Contributor Author

Great and thanks as always.

@rbri rbri added the js-engine Issues related to the js engine label Mar 27, 2024
@rbri
Copy link
Member

rbri commented Mar 27, 2024

see #755

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js-engine Issues related to the js engine
Projects
None yet
Development

No branches or pull requests

2 participants