mattn / anko

Scriptable interpreter written in golang

Home Page:http://play-anko.appspot.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Imported `type` is not visible in child scope

ipsusila opened this issue · comments

vm is initialized using the following method:

//Host golang code
env := vm.NewEnv()
core.Import(env)
packages.DefineImport(env)

and when executing the following Anko script:

var time = import('time')

//work as expected
d1 = new(time.Duration)
println(d1)

func foo() {
    //work as expected
    println(time.Now())
    
    //error: "no namespace called: time"
    d2 = new(time.Duration)
    println(d2)
}

//call foo()
foo()

error happened when trying to create instance of time.Duration (variable d2) inside the function foo. Temporary workaround is adding import statement inside the function, but it won't be available inside its child scope, e.g. the following Anko script won't work due to similar reason:

var time = import('time')

//work as expected
d1 = new(time.Duration)
println(d1)

func foo() {
    var time = import('time') //temporary workaround

    //work as expected
    d2 = new(time.Duration)
    println(d2)

    if d2 != nil {    
        //error: "no namespace called: time"
        d3 = new(time.Duration)
        println(d2)
    }
}

//call foo()
foo()

Thank you for finding this issue. Welcome to test on new PR #310 or can wait for merge.

@ipsusila Please test and close if fixed.

@MichaelS11 Thank you. I've tested the latest commit and now it is working as expected.