package lua import ( // External gopherLua "github.com/yuin/gopher-lua" luaJSON "layeh.com/gopher-json" // Standard "encoding/json" ) type LUA struct { state *gopherLua.LState } func NewLUA() (lua LUA) { lua.state = gopherLua.NewState() luaJSON.Preload(lua.state) return } func (l *LUA) SetStructInLUA(varname string, strct any) (ltable gopherLua.LValue, err error) { // JSON is used as a communications layer. j, _ := json.Marshal(strct) // JSON is decoded to a Lua table. ltable, err = luaJSON.Decode(l.state, j) if err != nil { return } l.state.SetGlobal(varname, ltable) return } func (l *LUA) GetStructFromLUA(ltable gopherLua.LValue, strct any) (err error) { j, _ := luaJSON.Encode(ltable) err = json.Unmarshal(j, strct) return } func (l *LUA) GetJSONFromLUA(ltable gopherLua.LValue) (j []byte) { j, _ = luaJSON.Encode(ltable) return } func (l *LUA) Run(script string) (err error) { return l.state.DoString(script) }