refactor!: use go plugins instead of hashicorp plugins

native-modules
sammyette 2023-02-19 15:27:25 -04:00
parent f8d66f4b81
commit 8aa9f3cb3a
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 8 additions and 62 deletions

View File

@ -1,20 +1,14 @@
package main package main
import ( import (
"encoding/gob" "plugin"
"errors"
"os"
"os/exec"
"hilbish/util" "hilbish/util"
"github.com/Rosettea/Malvales"
"github.com/hashicorp/go-plugin"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
) )
func moduleLoader(rtm *rt.Runtime) *rt.Table { func moduleLoader(rtm *rt.Runtime) *rt.Table {
gob.Register(os.File{})
exports := map[string]util.LuaExport{ exports := map[string]util.LuaExport{
"load": {moduleLoad, 2, false}, "load": {moduleLoad, 2, false},
} }
@ -26,7 +20,7 @@ func moduleLoader(rtm *rt.Runtime) *rt.Table {
} }
func moduleLoad(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func moduleLoad(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.CheckNArgs(2); err != nil { if err := c.CheckNArgs(1); err != nil {
return nil, err return nil, err
} }
@ -35,46 +29,22 @@ func moduleLoad(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return nil, err return nil, err
} }
name, err := c.StringArg(1) p, err := plugin.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// plugin is just go executable; check if it is (or exists) value, err := p.Lookup("Loader")
if err := findExecutable(path, false, false); err != nil {
return nil, err
}
moduleHandshake := plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "HSH_PLUGIN",
MagicCookieValue: name,
}
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: moduleHandshake,
Plugins: map[string]plugin.Plugin{
"entry": &malvales.Entry{},
},
Cmd: exec.Command(path),
})
rpcClient, err := client.Client()
if err != nil { if err != nil {
return nil, err return nil, err
} }
ret, err := rpcClient.Dispense("entry") loader, ok := value.(func(*rt.Runtime) rt.Value)
if err != nil {
return nil, err
}
plug, ok := ret.(malvales.Plugin)
if !ok { if !ok {
return nil, errors.New("did not get plugin from module") return nil, nil
} }
val := plug.Loader(t.Runtime) val := loader(t.Runtime)
return c.PushingNext1(t.Runtime, val), nil return c.PushingNext1(t.Runtime, val), nil
} }

View File

@ -1,33 +1,9 @@
package main package main
import ( import (
"github.com/Rosettea/Malvales"
"github.com/hashicorp/go-plugin"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
) )
type TestPlugin struct {} func Loader(rtm *rt.Runtime) rt.Value {
func (t *TestPlugin) Loader(rtm *rt.Runtime) rt.Value {
println("hello")
return rt.StringValue("hello world!") return rt.StringValue("hello world!")
} }
var handshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "HSH_PLUGIN",
MagicCookieValue: "testplugin",
}
func main() {
test := &TestPlugin{}
var pluginMap = map[string]plugin.Plugin{
"entry": &malvales.Entry{P: test},
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
})
}