linstrom/plugins/init.go
mstar 94106bb82f
Some checks failed
/ docker (push) Has been cancelled
Start work on lua based plugin system
2025-06-02 17:40:53 +02:00

62 lines
2 KiB
Go

package plugins
import (
"fmt"
"git.mstar.dev/mstar/goutils/other"
"github.com/PeerDB-io/gluabit32"
"github.com/cjoudrey/gluahttp"
"github.com/cosmotek/loguago"
"github.com/kohkimakimoto/gluatemplate"
gluajson "github.com/layeh/gopher-json"
"github.com/rs/zerolog/log"
"github.com/yuin/gluare"
lua "github.com/yuin/gopher-lua"
webshared "git.mstar.dev/mstar/linstrom/web/shared"
)
// "github.com/CuberL/glua-async" // For async support, see module readme for how to
// Use "github.com/layeh/gopher-luar" for implementing interface
// Create a new "blank" lua state for future use.
// Each created state has to be properly closed.
// A "blank" state has the base libraries preloaded and is technically ready to use.
// Plugin specific functions and data is not yet included
func NewBlankState() (*lua.LState, error) {
l := lua.NewState()
for _, pair := range []struct {
n string
f lua.LGFunction
}{
{lua.LoadLibName, lua.OpenPackage}, // Must always be first
{lua.BaseLibName, lua.OpenBase},
{lua.TabLibName, lua.OpenTable},
// {lua.IoLibName, lua.OpenIo},
{lua.OsLibName, lua.OpenOs},
{lua.StringLibName, lua.OpenString},
{lua.MathLibName, lua.OpenMath},
// {lua.DebugLibName, lua.OpenDebug},
{lua.ChannelLibName, lua.OpenChannel},
{lua.CoroutineLibName, lua.OpenCoroutine},
{"bit32", gluabit32.Loader},
{"json", gluajson.Loader},
{"log", loguago.NewLogger(log.Logger).Loader}, // TODO: Decide if used logger should be passed in via context
{"http", gluahttp.NewHttpModule(&webshared.RequestClient).Loader}, // TODO: Modify this to support signing
{"re", gluare.Loader},
{"texttemplate", gluatemplate.Loader}, // TODO: Copy this lib, but modify to use html/template instead
} {
if err := l.CallByParam(lua.P{
Fn: l.NewFunction(pair.f),
NRet: 0,
Protect: true,
}, lua.LString(pair.n)); err != nil {
return nil, other.Error(
"plugins",
fmt.Sprintf("Failed to preload lua library %q into state", pair.n),
err,
)
}
}
return l, nil
}