external plugins can now request for dependencies
This commit is contained in:
+1
-45
@@ -5,9 +5,7 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"plugin"
|
||||
)
|
||||
|
||||
//-------- plugin term
|
||||
@@ -22,48 +20,6 @@ func newPluginTerm(tk *Token) (inst *term) {
|
||||
}
|
||||
}
|
||||
|
||||
func importPlugin(ctx ExprContext, dirList []string, name string) (err error) {
|
||||
var filePath string
|
||||
var p *plugin.Plugin
|
||||
var sym plugin.Symbol
|
||||
var moduleName string
|
||||
var importFunc func(ctx ExprContext)
|
||||
var ok bool
|
||||
|
||||
decoratedName := fmt.Sprintf("expr-%s-plugin.so", name)
|
||||
|
||||
if filePath, err = makeFilepath(decoratedName, dirList); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if p, err = plugin.Open(filePath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if sym, err = p.Lookup("MODULE_NAME"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if moduleName = *sym.(*string); moduleName == "" {
|
||||
err = fmt.Errorf("plugin %q does not provide a valid module name", decoratedName)
|
||||
return
|
||||
}
|
||||
|
||||
if sym, err = p.Lookup("ImportFuncs"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if importFunc, ok = sym.(func(ExprContext)); !ok {
|
||||
err = fmt.Errorf("plugin %q does not provide a valid import function", decoratedName)
|
||||
return
|
||||
}
|
||||
|
||||
registerPlugin(moduleName, p)
|
||||
importFunc(globalCtx)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func evalPlugin(ctx ExprContext, self *term) (v any, err error) {
|
||||
var childValue any
|
||||
var moduleSpec any
|
||||
@@ -77,7 +33,7 @@ func evalPlugin(ctx ExprContext, self *term) (v any, err error) {
|
||||
it := NewAnyIterator(childValue)
|
||||
for moduleSpec, err = it.Next(); err == nil; moduleSpec, err = it.Next() {
|
||||
if module, ok := moduleSpec.(string); ok {
|
||||
if err = importPlugin(ctx, dirList, module); err != nil {
|
||||
if err = importPlugin(dirList, module); err != nil {
|
||||
break
|
||||
}
|
||||
count++
|
||||
|
||||
Reference in New Issue
Block a user