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

Mapping identifier names based on variable declaration kind #494

Open
TobiasWienand opened this issue Jan 23, 2025 · 0 comments
Open

Mapping identifier names based on variable declaration kind #494

TobiasWienand opened this issue Jan 23, 2025 · 0 comments

Comments

@TobiasWienand
Copy link
Contributor

Here is a problem
sample.js

{
    var a = 1
}
console.log(a)

is identical to liftToJS(compile(sample.js))

Yet, if we look at the FuzzIL representation, we see that the Compiler didn't use v1 for console.log but v2 instead, which is unnecessary.
The problem is that the map function maps the identifier a to a FuzzIL variable in the most recently opened scope, i.e. the BlockStatement. Outside of the BlockStatement lookupIdentifier doesn't find the FuzzIL variable anymore and therefore creates a new one.

BeginBlockStatement
    v0 <- LoadInteger '1'
    v1 <- CreateNamedVariable 'a', 'var', v0
EndBlockStatement
v2 <- CreateNamedVariable 'a', 'none'
v3 <- CreateNamedVariable 'console', 'none'
v4 <- CallMethod v3, 'log', [v2]

I assume that the map function works correctly for variable declarations of the let and const type but not for globals or var. I would propose giving the map function a parameter that controls in which scope on the stack the identifier should be saved instead of just using scopes.top.

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

1 participant