-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcallback_bridge.go
More file actions
233 lines (211 loc) · 6.24 KB
/
Copy pathcallback_bridge.go
File metadata and controls
233 lines (211 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package compdf
/*
#cgo CFLAGS: -I. -Iinclude -D_CRT_SECURE_NO_WARNINGS
#cgo windows LDFLAGS: -L${SRCDIR}/lib/windows/amd64 -lcpdfconversionsdk
#cgo linux LDFLAGS: -L${SRCDIR}/lib/linux/amd64 -lcpdfconversionsdk -Wl,-rpath,$ORIGIN
#cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/lib/darwin/amd64 -lcpdfconversionsdk
#cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/lib/darwin/arm64 -lcpdfconversionsdk
#include <stdlib.h>
#include "bridge.h"
*/
import "C"
import (
"reflect"
"runtime/cgo"
"sync"
"unsafe"
)
// callbackRegistry stores active ConvertCallback values keyed by a cgo.Handle
// value that gets passed to the C bridge as an opaque integer.
//
// We use cgo.Handle (Go 1.17+) directly: NewHandle returns a uintptr that the
// C side carries verbatim. The trampolines call back into Go and resolve the
// handle back to the original ConvertCallback.
type callbackRegistry struct {
results sync.Map // uintptr(handle) -> *callbackBuffers
}
// callbackBuffers holds C-strings returned by the get_*_result trampolines.
// The C SDK requires the returned pointer remain valid until the next call to
// the same getter — we own one CString per result type per active callback
// and free it on the next overwrite or when the conversion finishes.
type callbackBuffers struct {
mu sync.Mutex
ocr *C.char
layout *C.char
tableRes *C.char
}
func (b *callbackBuffers) replace(target **C.char, val string) *C.char {
if *target != nil {
C.free(unsafe.Pointer(*target))
*target = nil
}
if val == "" {
return nil
}
*target = C.CString(val)
return *target
}
func (b *callbackBuffers) free() {
b.mu.Lock()
defer b.mu.Unlock()
for _, p := range [...]**C.char{&b.ocr, &b.layout, &b.tableRes} {
if *p != nil {
C.free(unsafe.Pointer(*p))
*p = nil
}
}
}
var registry callbackRegistry
// baseCallbackMethodPCs caches the function PC of each BaseCallback method so
// that we can tell at runtime whether the user overrode them in their own
// ConvertCallback type. We have to do this lazily because Go's reflect package
// can't be used in package init in some constrained environments, but a single
// sync.Once gets us a global table cheaply.
var (
baseCallbackPCs map[string]uintptr
baseCallbackPCsOnce sync.Once
)
func loadBaseCallbackPCs() {
baseCallbackPCsOnce.Do(func() {
base := reflect.ValueOf(BaseCallback{})
baseCallbackPCs = map[string]uintptr{}
for _, name := range []string{
"OnOCR", "OnLayout", "OnTable",
"GetOCRResult", "GetLayoutResult", "GetTableResult",
} {
m := base.MethodByName(name)
if m.IsValid() {
baseCallbackPCs[name] = m.Pointer()
}
}
})
}
// overrideFlags returns the GO_CB_FLAG_* bitmask describing which optional
// methods on cb are NOT the BaseCallback default. Promoted methods from an
// embedded BaseCallback share the same function PC as BaseCallback's own
// method values, so a simple pointer comparison is sufficient. If reflection
// can't resolve a method (e.g. user implemented ConvertCallback directly
// without embedding BaseCallback), we treat it as overridden to preserve
// existing behaviour for that path.
func overrideFlags(cb ConvertCallback) C.int {
if cb == nil {
return 0
}
loadBaseCallbackPCs()
v := reflect.ValueOf(cb)
var flags C.int
check := func(name string, bit C.int) {
m := v.MethodByName(name)
if !m.IsValid() {
return
}
basePC, ok := baseCallbackPCs[name]
if !ok || m.Pointer() != basePC {
flags |= bit
}
}
check("OnOCR", C.GO_CB_FLAG_OCR)
check("OnLayout", C.GO_CB_FLAG_LAYOUT)
check("OnTable", C.GO_CB_FLAG_TABLE)
check("GetOCRResult", C.GO_CB_FLAG_GET_OCR_RESULT)
check("GetLayoutResult", C.GO_CB_FLAG_GET_LAYOUT_RESULT)
check("GetTableResult", C.GO_CB_FLAG_GET_TABLE_RESULT)
return flags
}
// registerCallback wraps cb in a handle and a result-buffer record. The
// returned cleanup must be called once the C SDK is guaranteed to no longer
// touch the callback.
func registerCallback(cb ConvertCallback) (C.GoCallbackHandle, func()) {
if cb == nil {
return 0, func() {}
}
h := cgo.NewHandle(cb)
buf := &callbackBuffers{}
registry.results.Store(uintptr(h), buf)
cleanup := func() {
registry.results.Delete(uintptr(h))
buf.free()
h.Delete()
}
return C.GoCallbackHandle(uintptr(h)), cleanup
}
func resolveCallback(h C.GoCallbackHandle) (ConvertCallback, *callbackBuffers) {
if h == 0 {
return nil, nil
}
val := cgo.Handle(uintptr(h)).Value()
cb, _ := val.(ConvertCallback)
bufVal, _ := registry.results.Load(uintptr(h))
buf, _ := bufVal.(*callbackBuffers)
return cb, buf
}
/* ===== Exported trampolines invoked from the C bridge ===== */
//export goCBProgress
func goCBProgress(h C.GoCallbackHandle, current, total C.int) {
cb, _ := resolveCallback(h)
if cb != nil {
cb.OnProgress(int(current), int(total))
}
}
//export goCBCancel
func goCBCancel(h C.GoCallbackHandle) C.bool {
cb, _ := resolveCallback(h)
if cb != nil && cb.IsCancelled() {
return C.bool(true)
}
return C.bool(false)
}
//export goCBOcr
func goCBOcr(h C.GoCallbackHandle, imagePath *C.char) C.bool {
cb, _ := resolveCallback(h)
if cb == nil {
return C.bool(false)
}
return C.bool(cb.OnOCR(C.GoString(imagePath)))
}
//export goCBLayout
func goCBLayout(h C.GoCallbackHandle, imagePath *C.char) C.bool {
cb, _ := resolveCallback(h)
if cb == nil {
return C.bool(false)
}
return C.bool(cb.OnLayout(C.GoString(imagePath)))
}
//export goCBTable
func goCBTable(h C.GoCallbackHandle, imagePath *C.char) C.bool {
cb, _ := resolveCallback(h)
if cb == nil {
return C.bool(false)
}
return C.bool(cb.OnTable(C.GoString(imagePath)))
}
//export goCBGetOcrResult
func goCBGetOcrResult(h C.GoCallbackHandle) *C.char {
cb, buf := resolveCallback(h)
if cb == nil || buf == nil {
return nil
}
buf.mu.Lock()
defer buf.mu.Unlock()
return buf.replace(&buf.ocr, cb.GetOCRResult())
}
//export goCBGetLayoutResult
func goCBGetLayoutResult(h C.GoCallbackHandle) *C.char {
cb, buf := resolveCallback(h)
if cb == nil || buf == nil {
return nil
}
buf.mu.Lock()
defer buf.mu.Unlock()
return buf.replace(&buf.layout, cb.GetLayoutResult())
}
//export goCBGetTableResult
func goCBGetTableResult(h C.GoCallbackHandle) *C.char {
cb, buf := resolveCallback(h)
if cb == nil || buf == nil {
return nil
}
buf.mu.Lock()
defer buf.mu.Unlock()
return buf.replace(&buf.tableRes, cb.GetTableResult())
}