moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

How can I call ls.NewClosure and set local variable

Open moxianfeng opened this issue 4 years ago • 1 comments

How can I call ls.NewClosure and set local variable

func Test() {
    L := lua.NewState()
    defer L.Close()
    L.SetGlobal("i", lua.LNumber(11))
    L.NewClosure(func (ls *lua.LState) int {
        // what can i do at here
        // maybe ls.Push ? but it's no name
        // maybe ls.GetStack and ls.SetLocal, but I don't know how to set the local variable named i
        ls.DoString("return i")
    })
}

like lua code

i = 11
return (function()
    local i = 10
    return i
end)()

the return value is 10

moxianfeng avatar Nov 11 '21 15:11 moxianfeng

i = 11
(->
  local i
  i = 10
  i)!

pigpigyyy avatar Nov 12 '21 06:11 pigpigyyy