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

Inconsistent result #164

Open
yype opened this issue Jun 4, 2022 · 3 comments
Open

Inconsistent result #164

yype opened this issue Jun 4, 2022 · 3 comments

Comments

@yype
Copy link

yype commented Jun 4, 2022

Hi there, the POC below outputs differently on mujs than on v8/jsc/chakracore.

function a() {
    b = [, 1]
    a = function() {
        return b
    }
    return a()
}
console.log(a())
a().shift()
console.log(a())
> d8 ./poc.js
,1
1

> ch ./poc.js
,1
1

> mujs ./poc.js
,1
,1

This POC seems to represent the root cause of a larger program hanging on mujs but not on v8/jsc/chakracore.
Thanks.

@dankox
Copy link

dankox commented Jan 13, 2023

This is interesting problem. It seems like MuJS doesn't assign function name as variable in scope, because if you create the a function using var a = function() { then it works in the same way as all the others. Or maybe it's just different handling for "function expression" and "function declaration".

@ccxvii
Copy link
Owner

ccxvii commented Jan 22, 2024

MuJS treats the current function name "a" as a local variable.

Assigning to "a" changes the local variable, instead of setting a global variable. Therefore the second and third calls to a() are still calling the original function, rather than the anonymous inner function.

@ccxvii
Copy link
Owner

ccxvii commented Jan 22, 2024

The core issue here is how the difference between function declarations (statements) and function expressions work with binding the function name. For expressions, the binding is a local immutable binding. For declarations, the binding is in the outer scope. MuJS gets both of these wrong...

Example 1:

var a = function a(n) {
    a = 5
    console.log(1, a)
}
a()
console.log(2, a)

Example 2:

function b(n) {
    b = 5
    console.log(1, b)
}
b()
console.log(2, b)

ccxvii added a commit that referenced this issue Jan 22, 2024
TODO: Make binding for function expression names immutable!
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

No branches or pull requests

3 participants