forked from nsf/gothic
/
interpreter.go
487 lines (420 loc) · 12.5 KB
/
interpreter.go
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package gothic
/*
#cgo LDFLAGS: -ltcl8.5 -ltk8.5
#cgo CFLAGS: -I/usr/include/tcl8.5
#include "interpreter.h"
*/
import "C"
import (
"errors"
"reflect"
"runtime"
"unsafe"
"bytes"
"image"
"sync"
"fmt"
)
const (
debug = false
alot = 999999
)
//------------------------------------------------------------------------------
// Utils
//------------------------------------------------------------------------------
func cgo_string_to_go_string(p *C.char, n C.int) string {
var x reflect.StringHeader
x.Data = uintptr(unsafe.Pointer(p))
x.Len = int(n)
return *(*string)(unsafe.Pointer(&x))
}
func go_string_to_cgo_string(s string) (*C.char, C.int) {
x := *(*reflect.StringHeader)(unsafe.Pointer(&s))
return (*C.char)(unsafe.Pointer(x.Data)), C.int(x.Len)
}
func c_interface_to_go_interface(iface [2]unsafe.Pointer) interface{} {
return *(*interface{})(unsafe.Pointer(&iface))
}
func go_interface_to_c_interface(iface interface{}) *unsafe.Pointer {
return (*unsafe.Pointer)(unsafe.Pointer(&iface))
}
// A handle that is used to manipulate a TCL interpreter. All handle methods
// can be safely invoked from different threads. Each method invokation is
// synchronous, it means that the method will be blocked until the action is
// actually executed.
type Interpreter struct {
ir *interpreter
Done <-chan int
}
func NewInterpreter(init interface{}) *Interpreter {
initdone := make(chan int)
done := make(chan int)
ir := new(Interpreter)
ir.Done = done
go func() {
var err error
runtime.LockOSThread()
ir.ir, err = new_interpreter()
if err != nil {
panic(err)
}
switch realinit := init.(type) {
case string:
err = ir.ir.eval(realinit)
if err != nil {
panic(err)
}
case func(*Interpreter):
realinit(ir)
}
initdone <- 0
C.Tk_MainLoop()
done <- 0
}()
<-initdone
return ir
}
func (ir *Interpreter) Eval(args ...interface{}) error {
if C.Tcl_GetCurrentThread() == ir.ir.thread {
return ir.ir.filt(ir.ir.eval(args...))
}
return ir.ir.run_and_wait(func() error {
return ir.ir.filt(ir.ir.eval(args...))
})
}
func (ir *Interpreter) EvalAs(out interface{}, args ...interface{}) error {
if C.Tcl_GetCurrentThread() == ir.ir.thread {
return ir.ir.filt(ir.ir.eval_as(out, args...))
}
return ir.ir.run_and_wait(func() error {
return ir.ir.filt(ir.ir.eval_as(out, args...))
})
}
func (ir *Interpreter) Set(name string, val interface{}) error {
if C.Tcl_GetCurrentThread() == ir.ir.thread {
return ir.ir.filt(ir.ir.set(name, val))
}
return ir.ir.run_and_wait(func() error {
return ir.ir.filt(ir.ir.set(name, val))
})
}
func (ir *Interpreter) ErrorFilter(filt func(error)error) {
if C.Tcl_GetCurrentThread() == ir.ir.thread {
ir.ir.errfilt = filt
}
ir.ir.run_and_wait(func() error {
ir.ir.errfilt = filt
return nil
})
}
func (ir *Interpreter) UploadImage(name string, img image.Image) error {
if C.Tcl_GetCurrentThread() == ir.ir.thread {
return ir.ir.filt(ir.ir.upload_image(name, img))
}
return ir.ir.run_and_wait(func() error {
return ir.ir.filt(ir.ir.upload_image(name, img))
})
}
func (ir *Interpreter) RegisterCommand(name string, cbfunc interface{}) error {
if C.Tcl_GetCurrentThread() == ir.ir.thread {
return ir.ir.filt(ir.ir.register_command(name, cbfunc))
}
return ir.ir.run_and_wait(func() error {
return ir.ir.filt(ir.ir.register_command(name, cbfunc))
})
}
func (ir *Interpreter) UnregisterCommand(name string) error {
if C.Tcl_GetCurrentThread() == ir.ir.thread {
return ir.ir.filt(ir.ir.unregister_command(name))
}
return ir.ir.run_and_wait(func() error {
return ir.ir.filt(ir.ir.unregister_command(name))
})
}
//------------------------------------------------------------------------------
// interpreter
//------------------------------------------------------------------------------
type interpreter struct {
C *C.Tcl_Interp
errfilt func(error) error
// registered commands
commands map[string]interface{}
// registered channels
channels map[string]interface{}
// just a buffer to avoid allocs in _gotk_go_command_handler
valuesbuf []reflect.Value
thread C.Tcl_ThreadId
queue chan async_action
cmdbuf bytes.Buffer
}
func new_interpreter() (*interpreter, error) {
ir := &interpreter{
C: C.Tcl_CreateInterp(),
errfilt: func(err error) error { return err },
commands: make(map[string]interface{}),
channels: make(map[string]interface{}),
valuesbuf: make([]reflect.Value, 0, 10),
queue: make(chan async_action, 50),
thread: C.Tcl_GetCurrentThread(),
}
status := C.Tcl_Init(ir.C)
if status != C.TCL_OK {
return nil, errors.New(C.GoString(C.Tcl_GetStringResult(ir.C)))
}
status = C.Tk_Init(ir.C)
if status != C.TCL_OK {
return nil, errors.New(C.GoString(C.Tcl_GetStringResult(ir.C)))
}
return ir, nil
}
func (ir *interpreter) filt(err error) error {
errfilt := ir.errfilt
ir.errfilt = nil
if errfilt != nil {
err = errfilt(err)
}
ir.errfilt = errfilt
return err
}
func (ir *interpreter) eval(args ...interface{}) error {
ir.cmdbuf.Reset()
fmt.Fprint(&ir.cmdbuf, args...)
ir.cmdbuf.WriteByte(0)
status := C.Tcl_Eval(ir.C, (*C.char)(unsafe.Pointer(&ir.cmdbuf.Bytes()[0])))
if status != C.TCL_OK {
return errors.New(C.GoString(C.Tcl_GetStringResult(ir.C)))
}
return nil
}
func (ir *interpreter) eval_as(out interface{}, args ...interface{}) error {
pv := reflect.ValueOf(out)
if pv.Kind() != reflect.Ptr || pv.IsNil() {
panic("gothic: EvalAs expected a non-nil pointer argument")
}
v := pv.Elem()
err := ir.eval(args...)
if err != nil {
return err
}
return ir.tcl_obj_to_go_value(C.Tcl_GetObjResult(ir.C), v)
}
func go_value_to_tcl_obj(value interface{}) *C.Tcl_Obj {
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Uint()))
case reflect.Float32, reflect.Float64:
return C.Tcl_NewDoubleObj(C.double(v.Float()))
case reflect.Bool:
if v.Bool() {
return C.Tcl_NewBooleanObj(1)
}
return C.Tcl_NewBooleanObj(0)
case reflect.String:
s := v.String()
sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
return C.Tcl_NewStringObj((*C.char)(unsafe.Pointer(sh.Data)), C.int(len(s)))
}
return nil
}
func (ir *interpreter) set(name string, value interface{}) error {
obj := go_value_to_tcl_obj(value)
if obj == nil {
return errors.New("gothic: cannot convert Go value to TCL object")
}
cname := C.CString(name)
obj = C.Tcl_SetVar2Ex(ir.C, cname, nil, obj, C.TCL_LEAVE_ERR_MSG)
C.free(unsafe.Pointer(cname))
if obj == nil {
return errors.New(C.GoString(C.Tcl_GetStringResult(ir.C)))
}
return nil
}
func (ir *interpreter) upload_image(name string, img image.Image) error {
nrgba, ok := img.(*image.NRGBA)
if !ok {
// let's do it slowpoke
bounds := img.Bounds()
nrgba = image.NewNRGBA(bounds)
for x := 0; x < bounds.Max.X; x++ {
for y := 0; y < bounds.Max.Y; y++ {
nrgba.Set(x, y, img.At(x, y))
}
}
}
cname := C.CString(name)
handle := C.Tk_FindPhoto(ir.C, cname)
if handle == nil {
ir.eval("image create photo ", name)
handle = C.Tk_FindPhoto(ir.C, cname)
if handle == nil {
return errors.New("failed to create an image handle")
}
}
C.free(unsafe.Pointer(cname))
block := C.Tk_PhotoImageBlock{
(*C.uchar)(unsafe.Pointer(&nrgba.Pix[0])),
C.int(nrgba.Rect.Max.X),
C.int(nrgba.Rect.Max.Y),
C.int(nrgba.Stride),
4,
[...]C.int{0, 1, 2, 3},
}
status := C.Tk_PhotoPutBlock(ir.C, handle, &block, 0, 0,
C.int(nrgba.Rect.Max.X), C.int(nrgba.Rect.Max.Y),
C.TK_PHOTO_COMPOSITE_SET)
if status != C.TCL_OK {
return errors.New(C.GoString(C.Tcl_GetStringResult(ir.C)))
}
return nil
}
func (ir *interpreter) tcl_obj_to_go_value(obj *C.Tcl_Obj, v reflect.Value) error {
var status C.int
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var out C.Tcl_WideInt
status = C.Tcl_GetWideIntFromObj(ir.C, obj, &out)
if status == C.TCL_OK {
v.SetInt(int64(out))
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
var out C.Tcl_WideInt
status = C.Tcl_GetWideIntFromObj(ir.C, obj, &out)
if status == C.TCL_OK {
v.SetUint(uint64(out))
}
case reflect.String:
var n C.int
out := C.Tcl_GetStringFromObj(obj, &n)
v.SetString(C.GoStringN(out, n))
case reflect.Float32, reflect.Float64:
var out C.double
status = C.Tcl_GetDoubleFromObj(ir.C, obj, &out)
if status == C.TCL_OK {
v.SetFloat(float64(out))
}
case reflect.Bool:
var out C.int
status = C.Tcl_GetBooleanFromObj(ir.C, obj, &out)
if status == C.TCL_OK {
v.SetBool(out == 1)
}
default:
return fmt.Errorf("gothic: cannot convert TCL object to Go type: %s", v.Type())
}
if status != C.TCL_OK {
return errors.New(C.GoString(C.Tcl_GetStringResult(ir.C)))
}
return nil
}
//------------------------------------------------------------------------------
// interpreter.commands
//------------------------------------------------------------------------------
//export _gotk_go_command_handler
func _gotk_go_command_handler(clidataup unsafe.Pointer, objc int, objv unsafe.Pointer) int {
// TODO: There is an idea of optimizing everything by a large margin,
// we can preprocess the type of a command in RegisterCommand function
// and then avoid calling reflect.New for every argument passed to that
// function. And we can even do additional error checks for unsupported
// argument types and handle multiple return values case.
clidata := (*C.GoTkClientData)(clidataup)
ir := (*interpreter)(clidata.go_interp)
args := (*(*[alot]*C.Tcl_Obj)(objv))[1:objc]
cb := c_interface_to_go_interface(clidata.iface)
f := reflect.ValueOf(cb)
ft := f.Type()
ir.valuesbuf = ir.valuesbuf[:0]
for i, n := 0, ft.NumIn(); i < n; i++ {
in := ft.In(i)
// use default value, if there is not enough args
if len(args) <= i {
ir.valuesbuf = append(ir.valuesbuf, reflect.New(in).Elem())
continue
}
v := reflect.New(in).Elem()
err := ir.tcl_obj_to_go_value(args[i], v)
if err != nil {
C._gotk_c_tcl_set_result(ir.C, C.CString(err.Error()))
return C.TCL_ERROR
}
ir.valuesbuf = append(ir.valuesbuf, v)
}
// TODO: handle return value
f.Call(ir.valuesbuf)
return C.TCL_OK
}
//export _gotk_go_command_deleter
func _gotk_go_command_deleter(data unsafe.Pointer) {
clidata := (*C.GoTkClientData)(data)
ir := (*interpreter)(clidata.go_interp)
delete(ir.commands, cgo_string_to_go_string(clidata.strp, clidata.strn))
}
func (ir *interpreter) register_command(name string, cbfunc interface{}) error {
typ := reflect.TypeOf(cbfunc)
if typ.Kind() != reflect.Func {
return errors.New("gothic: RegisterCommand only accepts func type as a second argument")
}
if _, ok := ir.commands[name]; ok {
return errors.New("gothic: command with the same name was already registered")
}
ir.commands[name] = cbfunc
cp, cn := go_string_to_cgo_string(name)
cname := C.CString(name)
C._gotk_c_add_command(ir.C, cname, unsafe.Pointer(ir), cp, cn,
go_interface_to_c_interface(cbfunc))
C.free(unsafe.Pointer(cname))
return nil
}
func (ir *interpreter) unregister_command(name string) error {
if _, ok := ir.commands[name]; !ok {
return errors.New("gothic: trying to unregister a non-existent command")
}
cname := C.CString(name)
status := C.Tcl_DeleteCommand(ir.C, cname)
C.free(unsafe.Pointer(cname))
if status != C.TCL_OK {
return errors.New(C.GoString(C.Tcl_GetStringResult(ir.C)))
}
return nil
}
//------------------------------------------------------------------------------
// interpreter.async
//------------------------------------------------------------------------------
type async_action struct {
result *error
action func() error
cond *sync.Cond
}
func (ir *interpreter) run_and_wait(action func() error) (err error) {
cond := sync.NewCond(&sync.Mutex{})
cond.L.Lock()
// send event
ir.queue <- async_action{result: &err, action: action, cond: cond}
ev := C._gotk_c_new_async_event(unsafe.Pointer(ir))
C.Tcl_ThreadQueueEvent(ir.thread, ev, C.TCL_QUEUE_TAIL)
C.Tcl_ThreadAlert(ir.thread)
// wait for result
cond.Wait()
cond.L.Unlock()
return
}
//export _gotk_go_async_handler
func _gotk_go_async_handler(ev unsafe.Pointer, flags int) int {
if flags != C.TK_ALL_EVENTS {
return 0
}
event := (*C.GoTkAsyncEvent)(ev)
ir := (*interpreter)(event.go_interp)
action := <-ir.queue
if action.result == nil {
action.action()
} else {
*action.result = action.action()
}
action.cond.L.Lock()
action.cond.Signal()
action.cond.L.Unlock()
return 1
}