-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc.go
419 lines (311 loc) · 7.91 KB
/
c.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
package gobia
/*
#cgo LDFLAGS: -lbia
#include <bia/cbia.h>
#include <stdlib.h>
#include <stdint.h>
extern bia_creation_t functionBridge(bia_parameters_t params, void* arg);
static int engine_put_function_bridge(bia_engine_t engine, const char* name, bia_function_t function, uintptr_t arg)
{
return bia_engine_put_function(engine, name, function, (void*)arg);
}
*/
import "C"
import (
"errors"
"fmt"
"reflect"
"strings"
"sync"
"unsafe"
)
type Parameters struct {
ptr C.bia_parameters_t
lock sync.Locker
}
type Function func(*Parameters) interface{}
type argBridge struct {
function Function
}
type Engine struct {
ptr C.bia_engine_t
fptrs map[string]*argBridge
}
type Member struct {
ptr C.bia_member_t
}
type GC struct {
ptr C.bia_gc_t
}
type Creation struct {
ptr C.bia_creation_t
}
// NewEngine creates a new Bia engine.
func NewEngine() (Engine, error) {
if ptr := C.bia_engine_new(); ptr != nil {
return Engine{ptr, make(map[string]*argBridge)}, nil
}
return Engine{}, errors.New("failed to create engine")
}
// Close frees the resources associated with the engine.
func (e *Engine) Close() error {
C.bia_engine_free(e.ptr)
*e = Engine{}
return nil
}
func (e *Engine) GetGC() GC {
return GC{C.bia_engine_get_gc(e.ptr)}
}
// UseBSL binds Bia's standard library.
func (e *Engine) UseBSL(args []string) error {
cargs := C.malloc(C.size_t(len(args)) * C.size_t(unsafe.Sizeof(uintptr(0))))
a := (*[1<<30 - 1]*C.char)(cargs)
defer func() {
for i := 0; i < len(args); i++ {
C.free(unsafe.Pointer(a[i]))
}
C.free(cargs)
}()
for i, e := range args {
a[i] = C.CString(e)
}
if C.bia_engine_use_bsl(e.ptr, (**C.char)(cargs), C.size_t(len(args))) != 0 {
return errors.New("failed to register bsl modules")
}
return nil
}
// PutFunction binds a Go function to Bia.
func (e *Engine) PutFunction(name string, function Function) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
fptr := &argBridge{function}
if C.engine_put_function_bridge(e.ptr, cname, (C.bia_function_t)(unsafe.Pointer(C.functionBridge)), C.uintptr_t(uintptr(unsafe.Pointer(fptr)))) != 0 {
return errors.New("failed to put function inplace")
}
// to prevent GC from destroying the function
e.fptrs[name] = fptr
return nil
}
func (e *Engine) Put(name string, value interface{}) error {
gc := e.GetGC()
n, err := gc.Create(name)
if err != nil {
return err
}
defer n.Close()
v, err := gc.Create(value)
if err != nil {
return err
}
defer v.Close()
if C.bia_engine_put(e.ptr, n.Peek().ptr, v.Peek().ptr) != 0 {
return errors.New("failed to put value")
}
n.StartMonitoring()
v.StartMonitoring()
return nil
}
func (e *Engine) Run(code []byte) error {
ccode := C.CBytes(code)
defer C.free(ccode)
if C.bia_run(e.ptr, ccode, C.size_t(len(code))) != 0 {
return errors.New("failed to run code")
}
return nil
}
//export functionBridge
func functionBridge(params C.bia_parameters_t, arg unsafe.Pointer) C.bia_creation_t {
p := &Parameters{params, &sync.Mutex{}}
defer p.invalidate()
if val := (*argBridge)(arg).function(p); val != nil {
gc, _ := ActiveGC()
result, _ := gc.Create(val)
return result.ptr
}
return nil
}
func (p *Parameters) Size() (int, error) {
p.lock.Lock()
defer p.lock.Unlock()
var s C.size_t
if p.ptr == nil {
return 0, errors.New("invalid parameters")
} else if C.bia_parameters_count(p.ptr, &s) != 0 {
return 0, errors.New("failed to get count")
}
return int(s), nil
}
func (p *Parameters) At(index int) (Member, error) {
p.lock.Lock()
defer p.lock.Unlock()
var s C.bia_member_t
if p.ptr == nil {
return Member{}, errors.New("invalid parameters")
} else if C.bia_parameters_at(p.ptr, C.size_t(index), &s) != 0 {
return Member{}, errors.New("failed to get count")
}
return Member{s}, nil
}
func (p *Parameters) Get(name string) (Member, error) {
p.lock.Lock()
defer p.lock.Unlock()
var s C.bia_member_t
if p.ptr == nil {
return Member{}, errors.New("invalid parameters")
}
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
if C.bia_parameters_kwargs_find(p.ptr, cname, &s) != 0 {
return Member{}, errors.New("failed to get count")
}
return Member{s}, nil
}
func (p *Parameters) invalidate() {
p.lock.Lock()
defer p.lock.Unlock()
p.ptr = nil
}
func (m Member) getInt() (int64, error) {
var c C.longlong
if C.bia_member_cast_llong(m.ptr, &c) != 0 {
return 0, errors.New("failed to cast to int")
}
return int64(c), nil
}
func (m Member) IsNull() bool {
return m.ptr == nil
}
func (m Member) Cast(out interface{}) error {
switch v := out.(type) {
case *int:
c, err := m.getInt()
*v = int(c)
return err
case *int8:
c, err := m.getInt()
*v = int8(c)
return err
case *int16:
c, err := m.getInt()
*v = int16(c)
return err
case *int32:
c, err := m.getInt()
*v = int32(c)
return err
case *int64:
c, err := m.getInt()
*v = c
return err
case *string:
var c *C.char
if C.bia_member_cast_cstring(m.ptr, &c) != 0 {
return errors.New("failed to cast to string")
}
*v = C.GoString(c)
case *float32:
var c C.double
if C.bia_member_cast_double(m.ptr, &c) != 0 {
return errors.New("failed to cast to double")
}
*v = float32(c)
case *float64:
var c C.double
if C.bia_member_cast_double(m.ptr, &c) != 0 {
return errors.New("failed to cast to double")
}
*v = float64(c)
default:
return errors.New("invalid type")
}
return nil
}
func ActiveGC() (GC, error) {
if ptr := C.bia_active_gc(); ptr != nil {
return GC{ptr}, nil
}
return GC{}, errors.New("no gc active")
}
func (g GC) Create(value interface{}) (Creation, error) {
var result C.bia_creation_t
switch v := value.(type) {
case int:
if C.bia_create_llong(g.ptr, C.longlong(v), &result) != 0 {
return Creation{}, errors.New("failed to create member")
}
case int8:
if C.bia_create_llong(g.ptr, C.longlong(v), &result) != 0 {
return Creation{}, errors.New("failed to create member")
}
case int16:
if C.bia_create_llong(g.ptr, C.longlong(v), &result) != 0 {
return Creation{}, errors.New("failed to create member")
}
case int32:
if C.bia_create_llong(g.ptr, C.longlong(v), &result) != 0 {
return Creation{}, errors.New("failed to create member")
}
case int64:
if C.bia_create_llong(g.ptr, C.longlong(v), &result) != 0 {
return Creation{}, errors.New("failed to create member")
}
case float32:
if C.bia_create_double(g.ptr, C.double(v), &result) != 0 {
return Creation{}, errors.New("failed to create member")
}
case float64:
if C.bia_create_double(g.ptr, C.double(v), &result) != 0 {
return Creation{}, errors.New("failed to create member")
}
case string:
c := C.CString(v)
defer C.free(unsafe.Pointer(c))
if C.bia_create_cstring(g.ptr, c, &result) != 0 {
return Creation{}, errors.New("failed to screate member")
}
default:
if strings.HasPrefix(fmt.Sprintf("%T", value), "map[string]") {
if C.bia_create_dict(g.ptr, &result) != 0 {
return Creation{}, errors.New("failed to create member")
}
iter := reflect.ValueOf(value).MapRange()
for iter.Next() {
(Creation{result}).Put(g, iter.Key().Interface().(string), iter.Value().Interface())
}
} else {
return Creation{}, errors.New("invalid type")
}
}
return Creation{result}, nil
}
func (c Creation) Put(gc GC, key string, value interface{}) error {
k, err := gc.Create(key)
if err != nil {
return err
}
defer k.Close()
v, err := gc.Create(value)
if err != nil {
return err
}
defer v.Close()
if C.bia_creation_dict_put(c.ptr, k.Peek().ptr, v.Peek().ptr) != 0 {
return errors.New("failed to put value")
}
k.StartMonitoring()
v.StartMonitoring()
return nil
}
func (c Creation) Close() error {
C.bia_creation_free(c.ptr)
return nil
}
func (c Creation) Peek() Member {
return Member{C.bia_creation_peek(c.ptr)}
}
func (c Creation) StartMonitoring() error {
if C.bia_creation_start_monitoring(c.ptr) != 0 {
return errors.New("failed to start monitoring")
}
return nil
}