├── examples └── simple │ ├── plugins │ └── happy.lua │ └── simple.go ├── README.md └── luaplugins.go /examples/simple/plugins/happy.lua: -------------------------------------------------------------------------------- 1 | 2 | implements("ProgramObserver") 3 | 4 | function ProgramStarted() 5 | print("Lua: started") 6 | end 7 | 8 | function ProgramEnded() 9 | print("Lua: ended") 10 | end 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lua for go-plugins 2 | 3 | A runtime for [go-plugins](https://github.com/progrium/go-plugins) powered by [golua](https://github.com/aarzilli/golua) and [luar](https://github.com/stevedonovan/luar). 4 | 5 | ## Usage 6 | 7 | Just register the runtime: 8 | 9 | plugins.RegisterRuntime(luaplugins.GetRuntime()) 10 | 11 | 12 | ## License 13 | 14 | BSD -------------------------------------------------------------------------------- /examples/simple/simple.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/progrium/go-plugins" 6 | "github.com/progrium/go-plugins-lua" 7 | ) 8 | 9 | type ProgramObserver struct { 10 | ProgramStarted func() 11 | ProgramEnded func() 12 | } 13 | var ProgramObserverExt struct { 14 | Plugin func(string) ProgramObserver 15 | Plugins func() []ProgramObserver 16 | } 17 | 18 | func main() { 19 | plugins.RegisterRuntime(luaplugins.GetRuntime()) 20 | plugins.LoadFromPath() 21 | 22 | plugins.ExtensionPoint(&ProgramObserverExt) 23 | 24 | for _, observer := range ProgramObserverExt.Plugins() { 25 | observer.ProgramStarted() 26 | } 27 | 28 | fmt.Println("Hello World") 29 | 30 | for _, observer := range ProgramObserverExt.Plugins() { 31 | observer.ProgramEnded() 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /luaplugins.go: -------------------------------------------------------------------------------- 1 | package luaplugins 2 | 3 | import ( 4 | "github.com/aarzilli/golua/lua" 5 | "github.com/stevedonovan/luar" 6 | ) 7 | 8 | 9 | type Runtime struct { 10 | plugins map[string]*lua.State 11 | } 12 | 13 | func GetRuntime() *Runtime { 14 | return &Runtime{ 15 | plugins: make(map[string]*lua.State), 16 | } 17 | } 18 | 19 | func (r Runtime) FileExtension() string { 20 | return ".lua" 21 | } 22 | 23 | func (r Runtime) InitPlugin(name, source string, implements func(string)) error { 24 | context := luar.Init() 25 | luar.Register(context, "", luar.Map{ 26 | "implements": func(interfaceName string) { 27 | implements(interfaceName) 28 | }, 29 | }) 30 | context.DoString(source) 31 | r.plugins[name] = context 32 | return nil 33 | } 34 | 35 | func (r Runtime) CallPlugin(name, function string, args []interface{}) (interface{}, error) { 36 | luafn := luar.NewLuaObjectFromName(r.plugins[name], function) 37 | value, err := luafn.Call(args...) 38 | if err != nil { 39 | return nil, err 40 | } 41 | return value, nil 42 | } 43 | 44 | func (r Runtime) LoadEnvironment(environment interface{}) { 45 | 46 | } 47 | --------------------------------------------------------------------------------