Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Yusuke Inuzuka
    @yuin
    I think it will be possible if the lua debug functions are added.
    I don't have those plans right now.
    hagna
    @hagna
    Now that the go compiler is in go why use an embedded language like lua when you could just compile go?
    Amanda Cameron
    @AmandaCameron
    Hey, I'm playing with gopher-lua for a toy project of mine, and I'm having some trouble with it. I'd like to have a coroutine-based event loop, where I Resume() with events and their args, and then the Lua-side yields to get more events out. I'm not sure if I'm handling the Go-side coroutine creation correctly.
    This is what I have currently to run and handle the main loop:
    
    func runTest1(state *lua.LState) {
        defer func() {
            logger.Print(recover())
        }()
    
        buff, err := Asset("test1.lua")
        if err != nil {
            log.Fatal(err)
        }
    
        biosThread := state.NewThread()
        biosThread.Panic = func(state *lua.LState) {
            log.Fatal(state.Get(1))
        }
    
        biosFunc, err := state.Load(bytes.NewReader(buff), "test1.lua")
        if err != nil {
            log.Fatal(err)
        }
    
        tickTimer := time.NewTicker(time.Second / 30)
        defer tickTimer.Stop()
    
        logger.Printf("Starting main loop.")
        logger.Printf("Status of biosThread: %s", state.Status(biosThread))
        biosState, err, args := state.Resume(biosThread, biosFunc)
    
        logger.Printf("Got state: %s", biosState)
        for {
            if biosState == lua.ResumeError {
                logger.Print("Main thread error: ", err)
    
                return
            } else if biosState == lua.ResumeYield || biosState == lua.ResumeOK {
                event := <-events
                logger.Print("Sending event %q into lua-land", event.Name)
    
                evtFilter := lua.LVAsString(args[0])
                if evtFilter != "" && event.Name == evtFilter {
                    eventArgs := []lua.LValue{
                        lua.LString(event.Name),
                    }
                    eventArgs = append(eventArgs, event.Args...)
    
                    biosState, err, args = state.Resume(biosThread, biosFunc, eventArgs...)
                } else {
                    <-tickTimer.C
                }
            }
        }
    }
    Amanda Cameron
    @AmandaCameron
    Maybe Iโ€™m mis-understanding the code at some point, but it seems that commenting this line out makes it not just immediately die. https://github.com/yuin/gopher-lua/blob/master/vm.go#L47 โ€” However itโ€™s almost like the coroutines are running in an actual goroutine, instead of doing like goroutines do on GOMAXPROC=1 which is run one at a time.
    Kristin Davidson
    @aphistic
    Hey, I was wondering if there's a way to create a new LState that's only the bare minimum to run? Like without io, channel, etc? Should I just remove them from _G manually?
    Brent Pedersen
    @brentp
    Is there a simple way to convert a lua table (LTable) to a go map or slice?
    Yusuke Inuzuka
    @yuin
    You can use the ToGoValue function in https://github.com/yuin/gluamapper
    Brent Pedersen
    @brentp
    henjue
    @henjue
    how to set package.path Specify the scripts directory as the Lua root directory
    Yusuke Inuzuka
    @yuin
    What do you mean by 'Lua root'? If Lua root means a directory where the loaded lua script file exists, debug.getinfo(1).source may just be possible.
    Nickolay
    @handicraftsman
    Hello?
    Alex Roitman
    @rshura
    Hi there! I have a silly question, forgive me if this was asked and answered already. If I compile a lua script, is it possible to store the result of this compilation for the later use?
    Alvaro Carvajal
    @Raggaer
    You can create a state, call DoFile and store the state if thats what you mean
    But gopherlua does not support luac
    Alex Roitman
    @rshura
    Thanks, I've already figured!
    Andy Bednoff
    @andrcmdr

    @yuin Hi Yusuke!

    Me and our team just comprehend some code in last few days and have a little question about coroutines and channels in Gopher Lua.

    https://github.com/yuin/gopher-lua/blob/master/coroutinelib.go
    https://github.com/yuin/gopher-lua/blob/master/_glua-tests/coroutine.lua

    https://github.com/yuin/gopher-lua/blob/master/channellib.go
    https://github.com/yuin/gopher-lua/blob/master/channellib_test.go

    It's really good if we can have a coroutines and channels for CSP (as in Go) in Lua and mapping'em to goroutines in the Go run-time - this would be very efficient upon use of resources.

    So, I understand that I just need to declare some definitions to include Lua modules, in Gopher Lua, like it mentioned in source (https://github.com/yuin/gopher-lua/blob/master/linit.go#L31) and mentioned in example (https://github.com/yuin/gopher-lua#opening-a-subset-of-builtin-modules):

    var luaLibs = []luaLib{
        luaLib{LoadLibName, OpenPackage},
        luaLib{BaseLibName, OpenBase},
        luaLib{TabLibName, OpenTable},
        luaLib{IoLibName, OpenIo},
        luaLib{OsLibName, OpenOs},
        luaLib{StringLibName, OpenString},
        luaLib{MathLibName, OpenMath},
        luaLib{DebugLibName, OpenDebug},
        luaLib{ChannelLibName, OpenChannel},
        luaLib{CoroutineLibName, OpenCoroutine},
    }

    Is it really would work and we can write some beautiful Lua code with coroutines and channels... or... The only tricky part I'm unsure of with coroutines is how the coroutine maintains a copy of the LState when it executes.
    I'm unclear on the internal VM implementation around how the LState is shared across goroutines and whether it will incur race conditions. Here's the question.

    So we can face with shared mutable state, dead-lock of threads or/and race conditions. I assume we can also use Go race detection tools (https://blog.golang.org/race-detector, https://golang.org/doc/articles/race_detector.html).
    If we build the binaries (of Gopher Lua) with --race the race detector will dump stack traces if we face with race conditions in the execution of Lua code with coroutines.

    https://github.com/yuin/gopher-lua/blob/master/README.rst#goroutines

    The LState is not goroutine-safe. It is recommended to use one LState per goroutine and communicate between goroutines by using channels.
    
    Channels are represented by channel objects in GopherLua. And a channel table provides functions for performing channel operations.
    
    Some objects can not be sent over channels due to having non-goroutine-safe objects inside itself.
    
       a thread(state)
       a function
       an userdata
       a table with a metatable
    
    You must not send these objects from Go APIs to channels.

    Docs about LState type contains more details:
    https://godoc.org/github.com/yuin/gopher-lua#LState

    https://github.com/yuin/gopher-lua/blob/master/auxlib.go#L92
    https://github.com/yuin/gopher-lua/blob/master/auxlib.go#L434

    https://github.com/yuin/gopher-lua/blob/master/state.go#L68
    https://github.com/yuin/gopher-lua/blob/master/state.go#L1207
    https://github.com/yuin/gopher-lua/blob/master/state.go#L1726
    https://github.com/yuin/gopher-lua/blob/master/state.go#L1738
    https://github.com/yuin/gopher-lua/blob/master/state.go#L1798

    I think the potential risk in Gopher Lua interpreter is low, but still be present, similarly as in plane Go with goroutines usage (as Bras Fitzpatrick said in his presentation "Go 90% perfect 100% of time").

    Yusuke Inuzuka
    @yuin
    @andrcmdr You can not share LStates across goroutines.
    As you mentioned, the LState is not goroutine-safe.
    You have to create LStates( L.NewState, not L.NewThread ) per groutines.
    Andy Bednoff
    @andrcmdr
    @yuin Thanks a lot! You're dispelled our doubts! ๐Ÿ‘
    Andy Bednoff
    @andrcmdr
    @yuin Hi Yusuke! We have a really hard problem with load and execution of our scripts - hope you can help, just send direct message to you with link to error logs
    Nick Lanng
    @nicklanng
    Hey everyone, I'm using gopher-lua to add scripting to a mud engine. I'm wondering how many different states I should spin up. I don't know if its silly to spin up a brand new state for every scriptable item/room. Could be hundreds. Would that run ok or would i run out of resources?
    Patrick Gundlach
    @pgundlach
    Hi all, I have a question about tables on a stack.
    Let's say I pass a table as an argument in a function call, so the stack contains that table. I use L.Get(1) (L is the lua state) to get the table
    How can I get the length of that table?
    Shivaprasad Bhat
    @spy16
    Hey All., How to do i set/append additional module search path so that i can import lua modules from there. LUA_PATH works. But i want to be able to do this inside the code.
    Pedro Oliveira
    @phenrigomes
    Hey guys! Can I limit memory and cpu with gopher-lua?
    Ghost
    @ghost~5e7ecff2d73408ce4fde4d6a
    • -
    Ghost
    @ghost~5e7ecff2d73408ce4fde4d6a
    2
    ll
    oo
    00
    99
    00
    12
    00
    88
    Josh Montoya
    @itsmontoya
    Hey guys!
    I'm trying to set a func as a value of a table
    Tmpl = {
        firstName = {"bar"},
        lastName = {"bar"},
        phoneNumber = {"bar"},
        email = {"bar"},
        status = {"bar"},
        modifiers = {
            test = "hi",
            firstName = Capitalize,
            lastName = Capitalize
        },
    }
    
    function Capitalize(str)
        return str:sub(1,1):upper()..str:sub(2):lower()
    end
    When I try to reference modifiers["firstName"] it comes up as nil
    panic:  attempt to call a non-function object
    stack traceback:
            [G]: ?
    Josh Montoya
    @itsmontoya
    I'm sure I'm just accessing it incorrectly
    Josh Montoya
    @itsmontoya
    OH, l.Getfield
    Rayan Desfossez
    @rayandfz
    hello
    func state(l *lua.LState, s int) func() {
        return func() {
            l.ToFunction(s)
        }
    }
    i've this
    but how to exec l.ToFunction ?
    Fevadev
    @Fevadev
    Hello. Help how to properly start working with a table in Golang.
    I am currently trying to get a table like this.
    GetTable: = lua_vm.GetGlobal ("nametable")
    or
    GetTableKey: = lua_vm.GetField (GetTable, "key")
    I don't understand how to get the data of all keys.
    From GetTable.
    Thanks for any help!