stdlib.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package stdlib
  11. import (
  12. "fmt"
  13. "plugin"
  14. "reflect"
  15. "strings"
  16. "devt.de/krotik/ecal/util"
  17. )
  18. /*
  19. internalStdlibFuncMap holds all registered functions
  20. */
  21. var internalStdlibFuncMap = make(map[string]util.ECALFunction)
  22. /*
  23. internalStdlibDocMap holds the docstrings for all registered functions
  24. */
  25. var internalStdlibDocMap = make(map[string]string)
  26. /*
  27. pluginLookup is an interface for required function of the plugin object - only used for unit testing.
  28. */
  29. type pluginLookup interface {
  30. Lookup(symName string) (plugin.Symbol, error)
  31. }
  32. /*
  33. pluginTestLookup override plugin object - only used for unit testing.
  34. */
  35. var pluginTestLookup pluginLookup
  36. /*
  37. AddStdlibPkg adds a package to stdlib. A package needs to be added before functions
  38. can be added.
  39. */
  40. func AddStdlibPkg(pkg string, docstring string) error {
  41. _, ok1 := GetPkgDocString(pkg)
  42. _, ok2 := internalStdlibDocMap[pkg]
  43. if ok1 || ok2 {
  44. return fmt.Errorf("Package %v already exists", pkg)
  45. }
  46. internalStdlibDocMap[pkg] = docstring
  47. return nil
  48. }
  49. /*
  50. AddStdlibFunc adds a function to stdlib.
  51. */
  52. func AddStdlibFunc(pkg string, name string, funcObj util.ECALFunction) error {
  53. _, ok1 := GetPkgDocString(pkg)
  54. _, ok2 := internalStdlibDocMap[pkg]
  55. if !ok1 && !ok2 {
  56. return fmt.Errorf("Package %v does not exist", pkg)
  57. }
  58. internalStdlibFuncMap[fmt.Sprintf("%v.%v", pkg, name)] = funcObj
  59. return nil
  60. }
  61. /*
  62. LoadStdlibPlugins attempts to load stdlib functions from a given list of definitions.
  63. */
  64. func LoadStdlibPlugins(jsonObj []interface{}) []error {
  65. var errs []error
  66. for _, i := range jsonObj {
  67. if err := LoadStdlibPlugin(i.(map[string]interface{})); err != nil {
  68. errs = append(errs, err)
  69. }
  70. }
  71. return errs
  72. }
  73. /*
  74. LoadStdlibPlugin attempts to load a stdlib function from a given definition.
  75. */
  76. func LoadStdlibPlugin(jsonObj map[string]interface{}) error {
  77. pkg := fmt.Sprint(jsonObj["package"])
  78. name := fmt.Sprint(jsonObj["name"])
  79. path := fmt.Sprint(jsonObj["path"])
  80. symName := fmt.Sprint(jsonObj["symbol"])
  81. return AddStdlibPluginFunc(pkg, name, path, symName)
  82. }
  83. /*
  84. AddStdlibPluginFunc adds a function to stdlib via a loaded plugin.
  85. The plugin needs to be build as a Go plugin (https://golang.org/pkg/plugin):
  86. go build -buildmode=plugin -o myfunc.so myfunc.go
  87. And have an exported variable (passed here as symName) which conforms
  88. to util.ECALPluginFunction.
  89. */
  90. func AddStdlibPluginFunc(pkg string, name string, path string, symName string) error {
  91. var err error
  92. var plug pluginLookup
  93. AddStdlibPkg(pkg, "Functions provided by plugins")
  94. if plug, err = plugin.Open(path); err == nil || pluginTestLookup != nil {
  95. var sym plugin.Symbol
  96. if pluginTestLookup != nil {
  97. plug = pluginTestLookup
  98. }
  99. if sym, err = plug.Lookup(symName); err == nil {
  100. if stdlibPluginFunc, ok := sym.(util.ECALPluginFunction); ok {
  101. adapterFunc := func(a ...interface{}) (interface{}, error) {
  102. return stdlibPluginFunc.Run(a)
  103. }
  104. err = AddStdlibFunc(pkg, name, &ECALFunctionAdapter{
  105. reflect.ValueOf(adapterFunc), stdlibPluginFunc.DocString()})
  106. } else {
  107. err = fmt.Errorf("Symbol %v is not a stdlib function", symName)
  108. }
  109. }
  110. }
  111. return err
  112. }
  113. /*
  114. GetStdlibSymbols returns all available packages of stdlib and their constant
  115. and function symbols.
  116. */
  117. func GetStdlibSymbols() ([]string, []string, []string) {
  118. var constSymbols, funcSymbols []string
  119. var packageNames []string
  120. packageSet := make(map[string]bool)
  121. addSym := func(sym string, suffix string, symMap map[interface{}]interface{},
  122. ret []string) []string {
  123. if strings.HasSuffix(sym, suffix) {
  124. trimSym := strings.TrimSuffix(sym, suffix)
  125. packageSet[trimSym] = true
  126. for k := range symMap {
  127. ret = append(ret, fmt.Sprintf("%v.%v", trimSym, k))
  128. }
  129. }
  130. return ret
  131. }
  132. for k, v := range genStdlib {
  133. sym := fmt.Sprint(k)
  134. if symMap, ok := v.(map[interface{}]interface{}); ok {
  135. constSymbols = addSym(sym, "-const", symMap, constSymbols)
  136. funcSymbols = addSym(sym, "-func", symMap, funcSymbols)
  137. }
  138. }
  139. for k := range packageSet {
  140. packageNames = append(packageNames, k)
  141. }
  142. // Add internal stuff
  143. for k := range internalStdlibDocMap {
  144. packageNames = append(packageNames, k)
  145. }
  146. for k := range internalStdlibFuncMap {
  147. funcSymbols = append(funcSymbols, k)
  148. }
  149. return packageNames, constSymbols, funcSymbols
  150. }
  151. /*
  152. GetStdlibConst looks up a constant from stdlib.
  153. */
  154. func GetStdlibConst(name string) (interface{}, bool) {
  155. var res interface{}
  156. var resok bool
  157. if m, n := splitModuleAndName(name); n != "" {
  158. if cmap, ok := genStdlib[fmt.Sprintf("%v-const", m)]; ok {
  159. res, resok = cmap.(map[interface{}]interface{})[n]
  160. }
  161. }
  162. return res, resok
  163. }
  164. /*
  165. GetStdlibFunc looks up a function from stdlib.
  166. */
  167. func GetStdlibFunc(name string) (util.ECALFunction, bool) {
  168. var res util.ECALFunction
  169. var resok bool
  170. if m, n := splitModuleAndName(name); n != "" {
  171. if fmap, ok := genStdlib[fmt.Sprintf("%v-func", m)]; ok {
  172. if fn, ok := fmap.(map[interface{}]interface{})[n]; ok {
  173. res = fn.(util.ECALFunction)
  174. resok = true
  175. }
  176. }
  177. }
  178. if !resok {
  179. res, resok = internalStdlibFuncMap[name]
  180. }
  181. return res, resok
  182. }
  183. /*
  184. GetPkgDocString returns the docstring of a stdlib package.
  185. */
  186. func GetPkgDocString(name string) (string, bool) {
  187. var res string
  188. s, ok := genStdlib[fmt.Sprintf("%v-synopsis", name)]
  189. if ok {
  190. res = fmt.Sprint(s)
  191. } else {
  192. res, ok = internalStdlibDocMap[name]
  193. }
  194. return res, ok
  195. }
  196. /*
  197. splitModuleAndName splits up a given full function name in module and function name part.
  198. */
  199. func splitModuleAndName(fullname string) (string, string) {
  200. var module, name string
  201. ccSplit := strings.SplitN(fullname, ".", 2)
  202. if len(ccSplit) != 0 {
  203. module = ccSplit[0]
  204. name = strings.Join(ccSplit[1:], "")
  205. }
  206. return module, name
  207. }