Skip to content

Mapping identifier names based on variable declaration kind #494

@TobiasWienand

Description

@TobiasWienand

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions