extensions.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2025 The tk9.0-go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tk9_0 // import "modernc.org/tk9.0"
  5. import (
  6. "errors"
  7. "fmt"
  8. "sort"
  9. "strings"
  10. )
  11. var (
  12. _ Extension = (*extension)(nil)
  13. _ ExtensionContext = extensionContext{}
  14. // Extensions register Tk extensions or extra packages. User code must
  15. // not directly mutate Extensions.
  16. Extensions = map[ExtensionKey]Extension{}
  17. AlreadyInitialized = errors.New("Already initialized")
  18. NotInitialized = errors.New("Not initialized")
  19. )
  20. // RegisterExtension registers e.
  21. func RegisterExtension(name string, e Extension) (r ExtensionKey, err error) {
  22. k := ExtensionKey{Type: typeName(e), Name: name}
  23. if _, ok := Extensions[k]; ok {
  24. return r, AlreadyRegistered
  25. }
  26. Extensions[k] = &extension{inner: e}
  27. return k, nil
  28. }
  29. type extension struct {
  30. inner Extension
  31. initialized bool
  32. }
  33. // ExtensionKey indexes Extensions
  34. type ExtensionKey struct {
  35. Type string
  36. Name string
  37. }
  38. // ExtensionContext provides context to Extension methods.
  39. type ExtensionContext interface {
  40. // Eval evaluates the tcl script.
  41. Eval(tcl string) (r string, err error)
  42. // EvalErr is like Eval, but handles errors according to the current value of
  43. // [ErrorMode].
  44. EvalErr(tcl string) (r string)
  45. RegisterWindow(path string) *Window
  46. Collect(w *Window, options ...any) string
  47. // Returns a single Tcl string, no braces, except "{}" is returned for s == "".
  48. TclSafeString(string) string
  49. }
  50. type extensionContext struct{}
  51. func newExtensionContext() (r extensionContext) {
  52. return r
  53. }
  54. func (extensionContext) TclSafeString(s string) (r string) {
  55. return tclSafeString(s)
  56. }
  57. func (extensionContext) Eval(tcl string) (r string, err error) {
  58. return eval(tcl)
  59. }
  60. func (extensionContext) EvalErr(tcl string) (r string) {
  61. return evalErr(tcl)
  62. }
  63. func (extensionContext) RegisterWindow(path string) (w *Window) {
  64. w = &Window{path}
  65. windowIndex[path] = w
  66. return w
  67. }
  68. func (extensionContext) Collect(w *Window, options ...any) string {
  69. var a []string
  70. for _, v := range options {
  71. switch x := v.(type) {
  72. case Opt:
  73. a = append(a, x.optionString(w))
  74. default:
  75. a = append(a, tclSafeString(fmt.Sprint(x)))
  76. }
  77. }
  78. return strings.Join(a, " ")
  79. }
  80. // Extension handles Tk extensions. When calling Extension methods registered
  81. // in Extension, the context argument is ignored and an instance is created
  82. // automatically.
  83. type Extension interface {
  84. // Initialize is called to perform any one-time initialization of an extension.
  85. // The Initialize method of an extension in Extensions can be called multiple
  86. // times but only the first successful call to Initialize will have any effect.
  87. Initialize(context ExtensionContext) error
  88. }
  89. func (e *extension) Initialize(context ExtensionContext) (err error) {
  90. context = newExtensionContext()
  91. defer func() {
  92. if err == nil {
  93. e.initialized = true
  94. }
  95. }()
  96. if e.initialized {
  97. return AlreadyInitialized
  98. }
  99. return e.inner.Initialize(context)
  100. }
  101. // InitializeExtension searches [Extensions] to find first extension named 'name'
  102. // and call its Initialize method. The search is case sensitive, white space is
  103. // normalized. If there's no match, InitializeExtension returns [NotFound].
  104. //
  105. // Any package can register extensions but only the main package can initialize
  106. // an extension.
  107. func InitializeExtension(name string) (err error) {
  108. if !isCalledFromMain() {
  109. return NotInitialized
  110. }
  111. var keys []ExtensionKey
  112. for k := range Extensions {
  113. keys = append(keys, k)
  114. }
  115. sort.Slice(keys, func(a, b int) bool {
  116. c, d := keys[a], keys[b]
  117. e, f := matchName(c.Name), matchName(d.Name)
  118. if e < f {
  119. return true
  120. }
  121. if e > f {
  122. return false
  123. }
  124. return c.Type < d.Type
  125. })
  126. name = matchName(name)
  127. for _, k := range keys {
  128. if matchName(k.Name) == name {
  129. return Extensions[k].Initialize(nil)
  130. }
  131. }
  132. return NotFound
  133. }
  134. func extensionInitialized(name string) bool {
  135. for extName, ext := range Extensions {
  136. if extName.Name == name && ext.(*extension).initialized {
  137. return true
  138. }
  139. }
  140. return false
  141. }