rokucommunity / brs

An interpreter for the BrightScript language that runs on non-Roku platforms.
MIT License
4 stars 2 forks source link

Complex nested objects with anonymous functions access wrong object `m` #38

Closed lvcabral closed 7 months ago

lvcabral commented 7 months ago

After the fix for issue #9 a side effect happened, sometimes the anonymous functions are getting the root m instead of the context of the parent object. Below a code to reproduce the issue:

sub main()
    m.name = "root"
    level1 = {name: "level1", param: "test result was: "}
    level1.run = sub()
        print m.name
        print createLevel2().testResult(m.param)
    end sub
    level1.run()
end sub

function createLevel2()
    instance = __level2_builder()
    instance.new()
    return instance
end function

function __level2_builder()
    print m.name
    instance = {name: "level2"}
    instance.new = sub()
        print m.name
        m.name = "newName"
    end sub
    instance.testResult = function(param) as string
         if m.name = "newName"
            result = "success"
        else
            result = "fail"
        end if
        return param + result
    end function
    return instance
end function

This code must print test result was: success but in the current version 0.45.3 it shows test result was: fail