-
Notifications
You must be signed in to change notification settings - Fork 227
/
c-bindings.go
678 lines (564 loc) · 17.3 KB
/
c-bindings.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"unsafe"
"github.com/google/go-jsonnet"
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/formatter"
// #cgo CXXFLAGS: -std=c++11 -Wall -I../cpp-jsonnet/include
// #include "internal.h"
"C"
)
import (
"errors"
"sort"
"strings"
)
type vm struct {
*jsonnet.VM
importer *jsonnet.FileImporter
formatOptions formatter.Options
}
type jsonValue struct {
val interface{}
// these objects are exclusively owned by this jsonValue
owned []*C.struct_JsonnetJsonValue
}
type importer struct {
cb *C.JsonnetImportCallback
ctx unsafe.Pointer
// An additional level of cache which allows
// using this API as a drop-in replacement for
// the C++ version.
//
// Importer contract requires returning the same
// contents every time. This enforces that the same
// imported file will always have the same contents.
// This caching is not performed on the Go side normally,
// because in many cases the proper caching can only
// be performed within the importer. In particular,
// when multiple paths are tried, the presence and contents
// of each should be cached.
//
// In this case, the API requires us to take ownership
// of the provided string, so we the implementation needs
// to provide a new one each time, so we need to fake
// good behavior.
contentCache map[string]jsonnet.Contents
}
// Import fetches data from a given path by using c.JsonnetImportCallback
func (i *importer) Import(importedFrom, importedPath string) (contents jsonnet.Contents, foundAt string, err error) {
var (
buflen = C.size_t(0)
msgC *C.char
bufPtr unsafe.Pointer
dir, _ = path.Split(importedFrom)
foundHereC *C.char
)
// TODO(sbarzowski) Consider supporting returning null for paths,
// which are already resolved. We cannot expect cross-language interface
// to let us easily return the same Go Contents. Instead, we can allow
// returning nothing (NULL), if they know that we have the contents
// cached anyway.
success := C.jsonnet_internal_execute_import(i.cb, i.ctx, C.CString(dir), C.CString(importedPath), &foundHereC, &msgC, &bufPtr, &buflen)
if success != 0 {
// Failure
msg := C.GoString(msgC)
C.jsonnet_internal_free_string(msgC)
return jsonnet.Contents{}, "", errors.New("importer error: " + msg)
}
result := C.GoBytes(bufPtr, C.int(buflen))
C.jsonnet_internal_free_pointer(bufPtr)
foundHere := C.GoString(foundHereC)
C.jsonnet_internal_free_string(foundHereC)
if _, isCached := i.contentCache[foundHere]; !isCached {
i.contentCache[foundHere] = jsonnet.MakeContentsRaw(result)
}
return i.contentCache[foundHere], foundHere, nil
}
var handles = newHandlesTable()
var versionString *C.char
//export jsonnet_version
func jsonnet_version() *C.char {
if versionString == nil {
version := jsonnet.Version() + " (go-jsonnet)"
versionString = C.CString(version)
}
return versionString
}
//export jsonnet_make
func jsonnet_make() *C.struct_JsonnetVm {
newVM := &vm{jsonnet.MakeVM(), &jsonnet.FileImporter{}, formatter.DefaultOptions()}
newVM.Importer(newVM.importer)
id, err := handles.make(newVM)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
return C.jsonnet_internal_make_vm_with_id(C.uintptr_t(id))
}
//export jsonnet_destroy
func jsonnet_destroy(vmRef *C.struct_JsonnetVm) {
if err := handles.free(uintptr(vmRef.id)); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
C.jsonnet_internal_free_vm(vmRef)
}
func getVM(vmRef *C.struct_JsonnetVm) *vm {
ref, err := handles.get(uintptr(vmRef.id))
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
v, ok := ref.(*vm)
if !ok {
fmt.Fprintln(os.Stderr, "provided handle has a different type")
os.Exit(1)
}
return v
}
func evaluateSnippet(vmRef *C.struct_JsonnetVm, filename string, code string, e *C.int) *C.char {
vm := getVM(vmRef)
// We still use a deprecated function to keep backwards compatible behavior.
//nolint:staticcheck
out, err := vm.EvaluateSnippet(filename, code)
var result *C.char
if err != nil {
*e = 1
result = C.CString(err.Error())
} else {
*e = 0
result = C.CString(out)
}
return result
}
func evaluateSnippetStream(vmRef *C.struct_JsonnetVm, filename string, code string, e *C.int) *C.char {
vm := getVM(vmRef)
// We still use a deprecated function to keep backwards compatible behavior.
//nolint:staticcheck
out, err := vm.EvaluateSnippetStream(filename, code)
var result *C.char
if err != nil {
*e = 1
result = C.CString(err.Error())
} else {
*e = 0
var buf strings.Builder
for _, s := range out {
buf.WriteString(s)
buf.WriteByte(0)
}
result = C.CString(buf.String())
}
return result
}
func evaluateSnippetMulti(vmRef *C.struct_JsonnetVm, filename string, code string, e *C.int) *C.char {
vm := getVM(vmRef)
// We still use a deprecated function to keep backwards compatible behavior.
//nolint:staticcheck
out, err := vm.EvaluateSnippetMulti(filename, code)
var result *C.char
if err != nil {
*e = 1
result = C.CString(err.Error())
} else {
*e = 0
// We go through filenames in sorted order to get deterministic output.
filenames := make([]string, 0, len(out))
for filename := range out {
filenames = append(filenames, filename)
}
sort.Strings(filenames)
var buf strings.Builder
for _, filename := range filenames {
buf.WriteString(filename)
buf.WriteByte(0)
buf.WriteString(out[filename])
buf.WriteByte(0)
}
result = C.CString(buf.String())
}
return result
}
//export jsonnet_evaluate_snippet
func jsonnet_evaluate_snippet(vmRef *C.struct_JsonnetVm, filename *C.char, code *C.char, e *C.int) *C.char {
f := C.GoString(filename)
s := C.GoString(code)
return evaluateSnippet(vmRef, f, s, e)
}
//export jsonnet_evaluate_snippet_stream
func jsonnet_evaluate_snippet_stream(vmRef *C.struct_JsonnetVm, filename *C.char, code *C.char, e *C.int) *C.char {
f := C.GoString(filename)
s := C.GoString(code)
return evaluateSnippetStream(vmRef, f, s, e)
}
//export jsonnet_evaluate_snippet_multi
func jsonnet_evaluate_snippet_multi(vmRef *C.struct_JsonnetVm, filename *C.char, code *C.char, e *C.int) *C.char {
f := C.GoString(filename)
s := C.GoString(code)
return evaluateSnippetMulti(vmRef, f, s, e)
}
//export jsonnet_evaluate_file
func jsonnet_evaluate_file(vmRef *C.struct_JsonnetVm, filename *C.char, e *C.int) *C.char {
f := C.GoString(filename)
data, err := ioutil.ReadFile(f)
if err != nil {
*e = 1
return C.CString(fmt.Sprintf("Failed to read input file: %s: %s", f, err.Error()))
}
return evaluateSnippet(vmRef, f, string(data), e)
}
//export jsonnet_evaluate_file_stream
func jsonnet_evaluate_file_stream(vmRef *C.struct_JsonnetVm, filename *C.char, e *C.int) *C.char {
f := C.GoString(filename)
data, err := ioutil.ReadFile(f)
if err != nil {
*e = 1
return C.CString(fmt.Sprintf("Failed to read input file: %s: %s", f, err.Error()))
}
return evaluateSnippetStream(vmRef, f, string(data), e)
}
//export jsonnet_evaluate_file_multi
func jsonnet_evaluate_file_multi(vmRef *C.struct_JsonnetVm, filename *C.char, e *C.int) *C.char {
f := C.GoString(filename)
data, err := ioutil.ReadFile(f)
if err != nil {
*e = 1
return C.CString(fmt.Sprintf("Failed to read input file: %s: %s", f, err.Error()))
}
return evaluateSnippetMulti(vmRef, f, string(data), e)
}
//export jsonnet_max_stack
func jsonnet_max_stack(vmRef *C.struct_JsonnetVm, v C.uint) {
vm := getVM(vmRef)
vm.MaxStack = int(v) // potentially dangerous conversion
}
//export jsonnet_string_output
func jsonnet_string_output(vmRef *C.struct_JsonnetVm, v C.int) {
vm := getVM(vmRef)
vm.StringOutput = v != 0
}
//export jsonnet_max_trace
func jsonnet_max_trace(vmRef *C.struct_JsonnetVm, v C.uint) {
vm := getVM(vmRef)
vm.ErrorFormatter.SetMaxStackTraceSize(int(v)) // potentially dangerous conversion
}
//export jsonnet_jpath_add
func jsonnet_jpath_add(vmRef *C.struct_JsonnetVm, path *C.char) {
vm := getVM(vmRef)
vm.importer.JPaths = append(vm.importer.JPaths, C.GoString(path))
}
//export jsonnet_ext_var
func jsonnet_ext_var(vmRef *C.struct_JsonnetVm, key, value *C.char) {
vm := getVM(vmRef)
vm.ExtVar(C.GoString(key), C.GoString(value))
}
//export jsonnet_ext_code
func jsonnet_ext_code(vmRef *C.struct_JsonnetVm, key, value *C.char) {
vm := getVM(vmRef)
vm.ExtCode(C.GoString(key), C.GoString(value))
}
//export jsonnet_tla_var
func jsonnet_tla_var(vmRef *C.struct_JsonnetVm, key, value *C.char) {
vm := getVM(vmRef)
vm.TLAVar(C.GoString(key), C.GoString(value))
}
//export jsonnet_tla_code
func jsonnet_tla_code(vmRef *C.struct_JsonnetVm, key, value *C.char) {
vm := getVM(vmRef)
vm.TLACode(C.GoString(key), C.GoString(value))
}
//export jsonnet_native_callback
func jsonnet_native_callback(vmRef *C.struct_JsonnetVm, name *C.char, cb *C.JsonnetNativeCallback, ctx unsafe.Pointer, params **C.char) {
vm := getVM(vmRef)
p := unsafe.Pointer(params)
sz := unsafe.Sizeof(*params)
var paramNames ast.Identifiers
for i := 0; ; i++ {
param := (**C.char)(unsafe.Pointer(uintptr(p) + uintptr(i)*sz))
if *param == nil {
break
}
paramNames = append(paramNames, ast.Identifier(C.GoString(*param)))
}
f := &jsonnet.NativeFunction{
Name: C.GoString(name),
Params: paramNames,
Func: func(x []interface{}) (interface{}, error) {
var (
arr []*C.struct_JsonnetJsonValue
argv **C.struct_JsonnetJsonValue
success = C.int(0)
)
if len(x) > 0 {
arr = make([]*C.struct_JsonnetJsonValue, 0, len(x))
for _, json := range x {
arr = append(arr, createJSONValue(vmRef, json))
}
argv = &(arr[0])
}
result := C.jsonnet_internal_execute_native(cb, ctx, argv, &success)
v := getJSONValue(result)
for _, val := range arr {
jsonnet_json_destroy(vmRef, val)
}
jsonnet_json_destroy(vmRef, result)
if success != 1 {
return nil, fmt.Errorf("failed to execute native callback, code: %d", success)
}
return v.val, nil
},
}
vm.NativeFunction(f)
}
//export jsonnet_import_callback
func jsonnet_import_callback(vmRef *C.struct_JsonnetVm, cb *C.JsonnetImportCallback, ctx unsafe.Pointer) {
vm := getVM(vmRef)
vm.Importer(&importer{
ctx: ctx,
cb: cb,
contentCache: make(map[string]jsonnet.Contents),
})
}
//export jsonnet_json_extract_string
func jsonnet_json_extract_string(vmRef *C.struct_JsonnetVm, json *C.struct_JsonnetJsonValue) *C.char {
v := getJSONValue(json)
str, ok := v.val.(string)
if !ok {
return nil
}
return C.CString(str)
}
//export jsonnet_json_extract_number
func jsonnet_json_extract_number(vmRef *C.struct_JsonnetVm, json *C.struct_JsonnetJsonValue, out *C.double) C.int {
v := getJSONValue(json)
switch f := v.val.(type) {
case float64:
*out = C.double(f)
return 1
case int:
*out = C.double(f)
return 1
}
return 0
}
//export jsonnet_json_extract_bool
func jsonnet_json_extract_bool(vmRef *C.struct_JsonnetVm, json *C.struct_JsonnetJsonValue) C.int {
v := getJSONValue(json)
b, ok := v.val.(bool)
if !ok {
return 2
}
if b {
return 1
}
return 0
}
//export jsonnet_json_extract_null
func jsonnet_json_extract_null(vmRef *C.struct_JsonnetVm, json *C.struct_JsonnetJsonValue) C.int {
v := getJSONValue(json)
if v.val == nil {
return 1
}
return 0
}
//export jsonnet_json_make_string
func jsonnet_json_make_string(vmRef *C.struct_JsonnetVm, v *C.char) *C.struct_JsonnetJsonValue {
return createJSONValue(vmRef, C.GoString(v))
}
//export jsonnet_json_make_number
func jsonnet_json_make_number(vmRef *C.struct_JsonnetVm, v C.double) *C.struct_JsonnetJsonValue {
return createJSONValue(vmRef, float64(v))
}
//export jsonnet_json_make_bool
func jsonnet_json_make_bool(vmRef *C.struct_JsonnetVm, v C.int) *C.struct_JsonnetJsonValue {
return createJSONValue(vmRef, v != 0)
}
//export jsonnet_json_make_null
func jsonnet_json_make_null(vmRef *C.struct_JsonnetVm) *C.struct_JsonnetJsonValue {
return createJSONValue(vmRef, nil)
}
//export jsonnet_json_make_array
func jsonnet_json_make_array(vmRef *C.struct_JsonnetVm) *C.struct_JsonnetJsonValue {
return createJSONValue(vmRef, []interface{}{})
}
//export jsonnet_json_array_append
func jsonnet_json_array_append(vmRef *C.struct_JsonnetVm, arr *C.struct_JsonnetJsonValue, v *C.struct_JsonnetJsonValue) {
json := getJSONValue(arr)
slice, ok := json.val.([]interface{})
if !ok {
fmt.Fprintf(os.Stderr, "array should be provided")
os.Exit(1)
}
json.val = append(slice, getJSONValue(v).val)
json.owned = append(json.owned, v)
}
//export jsonnet_json_make_object
func jsonnet_json_make_object(vmRef *C.struct_JsonnetVm) *C.struct_JsonnetJsonValue {
return createJSONValue(vmRef, make(map[string]interface{}))
}
//export jsonnet_json_object_append
func jsonnet_json_object_append(
vmRef *C.struct_JsonnetVm,
obj *C.struct_JsonnetJsonValue,
f *C.char,
v *C.struct_JsonnetJsonValue,
) {
d := getJSONValue(obj)
table, ok := d.val.(map[string]interface{})
if !ok {
fmt.Fprintf(os.Stderr, "object should be provided")
os.Exit(1)
}
table[C.GoString(f)] = getJSONValue(v).val
d.owned = append(d.owned, v)
}
//export jsonnet_json_destroy
func jsonnet_json_destroy(vmRef *C.struct_JsonnetVm, v *C.struct_JsonnetJsonValue) {
for _, child := range getJSONValue(v).owned {
jsonnet_json_destroy(vmRef, child)
}
if err := handles.free(uintptr(v.id)); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
C.jsonnet_internal_free_json(v)
}
func createJSONValue(vmRef *C.struct_JsonnetVm, val interface{}) *C.struct_JsonnetJsonValue {
id, err := handles.make(&jsonValue{val: val})
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
return C.jsonnet_internal_make_json_with_id(C.uintptr_t(id))
}
func getJSONValue(jsonRef *C.struct_JsonnetJsonValue) *jsonValue {
ref, err := handles.get(uintptr(jsonRef.id))
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
v, ok := ref.(*jsonValue)
if !ok {
fmt.Fprintf(os.Stderr, "provided handle has a different type")
os.Exit(1)
}
return v
}
//export jsonnet_fmt_indent
func jsonnet_fmt_indent(vmRef *C.struct_JsonnetVm, n C.int) {
vm := getVM(vmRef)
vm.formatOptions.Indent = int(n)
}
//export jsonnet_fmt_max_blank_lines
func jsonnet_fmt_max_blank_lines(vmRef *C.struct_JsonnetVm, n C.int) {
vm := getVM(vmRef)
vm.formatOptions.MaxBlankLines = int(n)
}
//export jsonnet_fmt_string
func jsonnet_fmt_string(vmRef *C.struct_JsonnetVm, c C.int) {
vm := getVM(vmRef)
switch c {
case 'd':
vm.formatOptions.StringStyle = formatter.StringStyleDouble
case 's':
vm.formatOptions.StringStyle = formatter.StringStyleSingle
case 'l':
vm.formatOptions.StringStyle = formatter.StringStyleLeave
default:
vm.formatOptions.StringStyle = formatter.StringStyleLeave
}
}
//export jsonnet_fmt_comment
func jsonnet_fmt_comment(vmRef *C.struct_JsonnetVm, c C.int) {
vm := getVM(vmRef)
switch c {
case 'h':
vm.formatOptions.CommentStyle = formatter.CommentStyleHash
case 's':
vm.formatOptions.CommentStyle = formatter.CommentStyleSlash
case 'l':
vm.formatOptions.CommentStyle = formatter.CommentStyleLeave
default:
vm.formatOptions.CommentStyle = formatter.CommentStyleLeave
}
}
//export jsonnet_fmt_pad_arrays
func jsonnet_fmt_pad_arrays(vmRef *C.struct_JsonnetVm, v C.int) {
vm := getVM(vmRef)
vm.formatOptions.PadArrays = v != 0
}
//export jsonnet_fmt_pad_objects
func jsonnet_fmt_pad_objects(vmRef *C.struct_JsonnetVm, v C.int) {
vm := getVM(vmRef)
vm.formatOptions.PadObjects = v != 0
}
//export jsonnet_fmt_pretty_field_names
func jsonnet_fmt_pretty_field_names(vmRef *C.struct_JsonnetVm, v C.int) {
vm := getVM(vmRef)
vm.formatOptions.PrettyFieldNames = v != 0
}
//export jsonnet_fmt_sort_imports
func jsonnet_fmt_sort_imports(vmRef *C.struct_JsonnetVm, v C.int) {
vm := getVM(vmRef)
vm.formatOptions.SortImports = v != 0
}
//export jsonnet_fmt_snippet
func jsonnet_fmt_snippet(vmRef *C.struct_JsonnetVm, filename *C.char, code *C.char, e *C.int) *C.char {
f := C.GoString(filename)
s := C.GoString(code)
return formatSnippet(vmRef, f, s, e)
}
//export jsonnet_fmt_file
func jsonnet_fmt_file(vmRef *C.struct_JsonnetVm, filename *C.char, e *C.int) *C.char {
f := C.GoString(filename)
data, err := ioutil.ReadFile(f)
if err != nil {
*e = 1
return C.CString(fmt.Sprintf("Failed to read input file: %s: %s", f, err.Error()))
}
return formatSnippet(vmRef, f, string(data), e)
}
func formatSnippet(vmRef *C.struct_JsonnetVm, filename string, code string, e *C.int) *C.char {
vm := getVM(vmRef)
out, err := formatter.Format(filename, code, vm.formatOptions)
var result *C.char
if err != nil {
*e = 1
result = C.CString(err.Error())
} else {
*e = 0
result = C.CString(out)
}
return result
}
type traceOut struct {
cb *C.JsonnetIoWriterCallback
}
func (o *traceOut) Write(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
success := C.int(0)
var n C.int = C.jsonnet_internal_execute_writer(o.cb, unsafe.Pointer(&p[0]),
C.size_t(len(p)), &success)
if success != 1 {
return int(n), errors.New("std.trace() failed to write to output stream")
}
return int(n), nil
}
//export jsonnet_set_trace_out_callback
func jsonnet_set_trace_out_callback(vmRef *C.struct_JsonnetVm, cb *C.JsonnetIoWriterCallback) {
vm := getVM(vmRef)
vm.SetTraceOut(&traceOut{cb})
}
//export jsonnet_realloc
func jsonnet_realloc(vmRef *C.struct_JsonnetVm, buf *C.char, sz C.size_t) *C.char {
return C.jsonnet_internal_realloc(vmRef, buf, sz)
}
func main() {
}