vtables.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //go:build windows
  2. package combridge
  3. import (
  4. "fmt"
  5. "reflect"
  6. "sync"
  7. "golang.org/x/sys/windows"
  8. )
  9. var (
  10. vTablesL sync.Mutex
  11. vTables = make(map[string]*vTable)
  12. )
  13. // RegisterVTable registers the vtable trampoline methods for the specified ComInterface
  14. // TBase is the base interface of T, and must be another ComInterface which roots in IUnknown or IUnknown itself.
  15. // The first paramter of the fn is always the uintptr of the ComObject and the GoObject can be resolved with Resolve().
  16. // After having resolved the GoObject the call must be redirected to the GoObject.
  17. // Typically a trampoline FN looks like this.
  18. //
  19. // func _ICoreWebView2NavigationCompletedEventHandlerInvoke(this uintptr, sender *ICoreWebView2, args *ICoreWebView2NavigationCompletedEventArgs) uintptr {
  20. // return combridge.Resolve[_ICoreWebView2NavigationCompletedEventHandler](this).NavigationCompleted(sender, args)
  21. // }
  22. //
  23. // The order of registration must be in the correct order as specified in the IDL of the interface.
  24. func RegisterVTable[TParent, T IUnknown](guid string, fns ...interface{}) {
  25. registerVTableInternal[TParent, T](guid, false, fns...)
  26. }
  27. type vTable struct {
  28. Parent *vTable
  29. Name string
  30. ComGUID string
  31. ComVTable uintptr
  32. ComProcs []uintptr
  33. }
  34. func registerVTableInternal[TParent, T IUnknown](guid string, isInternal bool, fns ...interface{}) {
  35. vTablesL.Lock()
  36. defer vTablesL.Unlock()
  37. t, tName := typeInterfaceToString[T]()
  38. tParent, tParentName := typeInterfaceToString[TParent]()
  39. if !t.Implements(tParent) {
  40. panic(fmt.Errorf("RegisterVTable '%s': '%s' must implement '%s'", tName, tName, tParentName))
  41. }
  42. if !isInternal {
  43. if t == reflect.TypeOf((*IUnknown)(nil)).Elem() {
  44. panic(fmt.Errorf("RegisterVTable '%s' IUnknown can't be registered", tName))
  45. }
  46. if t == tParent {
  47. panic(fmt.Errorf("RegisterVTable '%s': T and TParent can't be the same type", tName))
  48. }
  49. }
  50. var parent *vTable
  51. var parentProcs []uintptr
  52. var parentProcsCount int
  53. if t != tParent {
  54. parent = vTables[tParentName]
  55. if parent == nil {
  56. panic(fmt.Errorf("RegisterVTable '%s': Parent VTable '%s' not registered", tName, tParentName))
  57. }
  58. parentProcs = parent.ComProcs
  59. parentProcsCount = len(parentProcs)
  60. }
  61. comGuid, err := windows.GUIDFromString(guid)
  62. if err != nil {
  63. panic(fmt.Errorf("RegisterVTable '%s': invalid guid: %s", tName, err))
  64. }
  65. vTable := &vTable{
  66. Parent: parent,
  67. Name: tName,
  68. ComGUID: comGuid.String(),
  69. }
  70. vTable.ComVTable, vTable.ComProcs = allocUintptrObject(parentProcsCount + len(fns))
  71. for i, proc := range parentProcs {
  72. vTable.ComProcs[i] = proc
  73. }
  74. for i, fn := range fns {
  75. vTable.ComProcs[parentProcsCount+i] = windows.NewCallback(fn)
  76. }
  77. vTables[tName] = vTable
  78. }
  79. func typeInterfaceToString[T any]() (reflect.Type, string) {
  80. t := reflect.TypeOf((*T)(nil))
  81. if t.Kind() != reflect.Pointer {
  82. panic("must be a (*yourInterfaceType)(nil)")
  83. }
  84. t = t.Elem()
  85. return t, t.PkgPath() + "/" + t.Name()
  86. }
  87. func typeInterfaceToStringOnly[T any]() string {
  88. _, nane := typeInterfaceToString[T]()
  89. return nane
  90. }
  91. func guidOf[T any]() string {
  92. vtable := vTableOf[T]()
  93. if vtable == nil {
  94. return ""
  95. }
  96. return vtable.ComGUID
  97. }
  98. func vTableOf[T any]() *vTable {
  99. name := typeInterfaceToStringOnly[T]()
  100. vTablesL.Lock()
  101. defer vTablesL.Unlock()
  102. return vTables[name]
  103. }
  104. type ifceImpl interface {
  105. impl() any
  106. ifce() (*vTable, error)
  107. }
  108. type ifceDef[T any] struct {
  109. objImpl any
  110. }
  111. func (i ifceDef[T]) impl() any {
  112. return i.objImpl
  113. }
  114. func (i ifceDef[T]) ifce() (*vTable, error) {
  115. vtable := vTableOf[T]()
  116. if vtable == nil {
  117. return nil, fmt.Errorf("Unable to find vTable for %s", typeInterfaceToStringOnly[T]())
  118. }
  119. return vtable, nil
  120. }