diff --git a/README.md b/README.md index 0c2465d..ee60cc5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ This branch uses unofficial goloader implementation, see https://pkg.go.dev/github.com/eh-steve/goloader, https://github.com/eh-steve/goloader and https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/src/branch/main/dev/resources.md#goloader-plugin-logic for more information + +--- +#### When building for windows: +- change `.vscode/settings.json` to `"go.buildFlags": ["-tags=windows"],` +- change `cmd/go-loader-test/plugin-manager.go` to `BuildEnv: []string{"GOOS=windows"},` + --- # Pros - Fast, Lightweight diff --git a/cmd/go-loader-test/plugin-manager.go b/cmd/go-loader-test/plugin-manager.go index 67491d6..718dfdc 100644 --- a/cmd/go-loader-test/plugin-manager.go +++ b/cmd/go-loader-test/plugin-manager.go @@ -8,11 +8,11 @@ import ( func PluginManager(params ...string) { conf := jit.BuildConfig{ - KeepTempFiles: false, // Files are copied/written to a temp dir to ensure it is writable. This retains the temporary copies - ExtraBuildFlags: []string{"-x"}, // Flags passed to go build command - BuildEnv: []string{"GOOS=windows"}, // Env vars to set for go build toolchain - TmpDir: "/tmp/inner", // To control where temporary files are copied - DebugLog: true, // + KeepTempFiles: false, // Files are copied/written to a temp dir to ensure it is writable. This retains the temporary copies + ExtraBuildFlags: []string{"-x"}, // Flags passed to go build command + BuildEnv: []string{"GOOS=darwin"}, // Env vars to set for go build toolchain + TmpDir: "/tmp/inner", // To control where temporary files are copied + DebugLog: false, // } loadable, err := jit.BuildGoPackage(conf, "plugins/tmptest/") @@ -26,12 +26,6 @@ func PluginManager(params ...string) { if err != nil { panic(err) } - // defer func() { - // err = module.Unload() - // if err != nil { - // panic(err) - // } - // }() switch f := symbols["MyFunc"].(type) { case func([]byte) (interface{}, error): result, err := f([]byte(`{"k":"v"}`)) @@ -48,4 +42,39 @@ func PluginManager(params ...string) { if err != nil { panic(err) } + + // + // + // + // + // + + loadable2, err := jit.BuildGoPackage(conf, "plugins/addsymbol/") + if err != nil { + panic(err) + } + + module2, err := loadable2.Load() + // module.SymbolsByPkg is a map[string]map[string]interface{} of all packages and their exported functions and global vars + symbols2 := module2.SymbolsByPkg[loadable2.ImportPath] + if err != nil { + panic(err) + } + switch f := symbols2["Symbol"].(type) { + case func(a, b string) (interface{}, error): + result, err := f("a", "b") + if err != nil { + panic(err) + } + fmt.Println(result) + default: + fmt.Println(f) + panic("Function signature was not what was expected") + } + + err = module2.Unload() + if err != nil { + panic(err) + } + } diff --git a/cmd/go-loader-test/plugin-opener.go b/cmd/go-loader-test/plugin-opener.go deleted file mode 100644 index 7a88095..0000000 --- a/cmd/go-loader-test/plugin-opener.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "fmt" - "os" - "plugin" - - "git.aliberksandikci.com.tr/asandikci/go-loader-test/pkg/utils" -) - -func LoadPlugin(plugName string, chn chan interface{}) { - - plug, err := plugin.Open(PluginDir + plugName + ".so") - utils.Check(err) - - symPlug, err := plug.Lookup(utils.FirstUpperEN(plugName) + "Connect") - utils.Check(err) - - var plugOut interface{} - plugOut, ok := symPlug.(interface{}) - if !ok { - fmt.Println("unexpected type from module symbol") - os.Exit(1) - } - chn <- plugOut -} diff --git a/go-loader-test b/go-loader-test new file mode 100755 index 0000000..5a90d90 Binary files /dev/null and b/go-loader-test differ diff --git a/plugins/addsymbol/symbol.go b/plugins/addsymbol/symbol.go new file mode 100644 index 0000000..3f477ac --- /dev/null +++ b/plugins/addsymbol/symbol.go @@ -0,0 +1,5 @@ +package addsymbol + +func Symbol(a, b string) (interface{}, error) { + return symbol(a, b), nil +} diff --git a/plugins/addsymbol/symbol_linux.go b/plugins/addsymbol/symbol_linux.go new file mode 100644 index 0000000..bd8d2aa --- /dev/null +++ b/plugins/addsymbol/symbol_linux.go @@ -0,0 +1,7 @@ +// go:build linux && !windows + +package addsymbol + +func symbol(a, b string) string { + return a + "+" + b + "=linux" +} diff --git a/plugins/addsymbol/symbol_windows.go b/plugins/addsymbol/symbol_windows.go new file mode 100644 index 0000000..133b2d3 --- /dev/null +++ b/plugins/addsymbol/symbol_windows.go @@ -0,0 +1,7 @@ +// go:build windows + +package addsymbol + +func symbol(a, b string) string { + return a + "-" + b + "=windows" +} diff --git a/plugins/tmptest/info.go b/plugins/tmptest/info.go index 305a2cb..cf75dd4 100644 --- a/plugins/tmptest/info.go +++ b/plugins/tmptest/info.go @@ -1,11 +1,11 @@ package mypackage -func Info() map[string]interface{} { - return map[string]interface{}{ - "name": "tmptest", - "version": "1.0.2", - "support": "any", - "description": "Temporary testing", - "developer": "asandikci@aliberksandikci.com.tr", - } -} +// func Info() map[string]interface{} { +// return map[string]interface{}{ +// "name": "tmptest", +// "version": "1.0.2", +// "support": "any", +// "description": "Temporary testing", +// "developer": "asandikci@aliberksandikci.com.tr", +// } +// }