From 8aa9f3cb3a5d168a77a33569100581d415b7a773 Mon Sep 17 00:00:00 2001 From: sammyette Date: Sun, 19 Feb 2023 15:27:25 -0400 Subject: [PATCH] refactor!: use go plugins instead of hashicorp plugins --- module.go | 44 +++++++--------------------------------- testplugin/testplugin.go | 26 +----------------------- 2 files changed, 8 insertions(+), 62 deletions(-) diff --git a/module.go b/module.go index 85ec67d..6adabad 100644 --- a/module.go +++ b/module.go @@ -1,20 +1,14 @@ package main import ( - "encoding/gob" - "errors" - "os" - "os/exec" + "plugin" "hilbish/util" - "github.com/Rosettea/Malvales" - "github.com/hashicorp/go-plugin" rt "github.com/arnodel/golua/runtime" ) func moduleLoader(rtm *rt.Runtime) *rt.Table { - gob.Register(os.File{}) exports := map[string]util.LuaExport{ "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) { - if err := c.CheckNArgs(2); err != nil { + if err := c.CheckNArgs(1); err != nil { return nil, err } @@ -35,46 +29,22 @@ func moduleLoad(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return nil, err } - name, err := c.StringArg(1) + p, err := plugin.Open(path) if err != nil { return nil, err } - // plugin is just go executable; check if it is (or exists) - 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() + value, err := p.Lookup("Loader") if err != nil { return nil, err } - ret, err := rpcClient.Dispense("entry") - if err != nil { - return nil, err - } - - plug, ok := ret.(malvales.Plugin) + loader, ok := value.(func(*rt.Runtime) rt.Value) 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 } diff --git a/testplugin/testplugin.go b/testplugin/testplugin.go index 2e35164..2d8a41b 100644 --- a/testplugin/testplugin.go +++ b/testplugin/testplugin.go @@ -1,33 +1,9 @@ package main import ( - "github.com/Rosettea/Malvales" - "github.com/hashicorp/go-plugin" rt "github.com/arnodel/golua/runtime" ) -type TestPlugin struct {} - -func (t *TestPlugin) Loader(rtm *rt.Runtime) rt.Value { - println("hello") +func Loader(rtm *rt.Runtime) rt.Value { 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, - }) -}