Initial commit
This commit is contained in:
commit
6ed7b14058
3 changed files with 55 additions and 0 deletions
8
go.mod
Normal file
8
go.mod
Normal file
|
@ -0,0 +1,8 @@
|
|||
module lua
|
||||
|
||||
go 1.24.2
|
||||
|
||||
require (
|
||||
github.com/yuin/gopher-lua v1.1.1
|
||||
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
|
||||
)
|
4
go.sum
Normal file
4
go.sum
Normal file
|
@ -0,0 +1,4 @@
|
|||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf h1:rRz0YsF7VXj9fXRF6yQgFI7DzST+hsI3TeFSGupntu0=
|
||||
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf/go.mod h1:ivKkcY8Zxw5ba0jldhZCYYQfGdb2K6u9tbYK1AwMIBc=
|
43
pkg.go
Normal file
43
pkg.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
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(L *gopherLua.LState, 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, 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) Run(script string) (err error) {
|
||||
return l.state.DoString(script)
|
||||
}
|
Loading…
Add table
Reference in a new issue