-
Notifications
You must be signed in to change notification settings - Fork 234
/
value.go
782 lines (660 loc) · 20 KB
/
value.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package jsonnet
import (
"errors"
"fmt"
"github.com/google/go-jsonnet/ast"
)
// value represents a concrete jsonnet value of a specific type.
// Various operations on values are allowed, depending on their type.
// All values are of course immutable.
type value interface {
aValue()
getType() *valueType
}
type valueType struct {
name string
}
var stringType = &valueType{"string"}
var numberType = &valueType{"number"}
var functionType = &valueType{"function"}
var objectType = &valueType{"object"}
var booleanType = &valueType{"boolean"}
var nullType = &valueType{"null"}
var arrayType = &valueType{"array"}
// potentialValue is something that may be evaluated to a concrete value.
// The result of the evaluation may *NOT* depend on the current state
// of the interpreter. The evaluation may fail.
//
// It can be used to represent lazy values (e.g. variables values in jsonnet
// are not calculated before they are used). It is also a useful abstraction
// in other cases like error handling.
//
// It may or may not require arbitrary computation when getValue is called the
// first time, but any subsequent calls will immediately return.
//
// TODO(sbarzowski) perhaps call it just "Thunk"?
type potentialValue interface {
// fromWhere keeps the information from where the evaluation was requested.
getValue(i *interpreter) (value, error)
aPotentialValue()
}
// A set of variables with associated thunks.
type bindingFrame map[ast.Identifier]*cachedThunk
type valueBase struct{}
func (v *valueBase) aValue() {}
// Primitive values
// -------------------------------------
type valueString interface {
value
length() int
getRunes() []rune
getGoString() string
index(i *interpreter, index int) (value, error)
}
// valueFlatString represents a string value, internally using a []rune for quick
// indexing.
type valueFlatString struct {
valueBase
// We use rune slices instead of strings for quick indexing
value []rune
}
func (s *valueFlatString) index(i *interpreter, index int) (value, error) {
if 0 <= index && index < s.length() {
return makeValueString(string(s.value[index])), nil
}
return nil, i.Error(fmt.Sprintf("Index %d out of bounds, not within [0, %v)", index, s.length()))
}
func (s *valueFlatString) getRunes() []rune {
return s.value
}
func (s *valueFlatString) getGoString() string {
return string(s.value)
}
func (s *valueFlatString) length() int {
return len(s.value)
}
func (s *valueFlatString) getType() *valueType {
return stringType
}
// TODO(sbarzowski) this was chosen on a hunch, be more systematic about it
const extendedStringMinLength = 30
// Indirect representation of long strings.
// It consists of two parts, left and right, concatenation of which is the whole string.
type valueStringTree struct {
valueBase
left, right valueString
len int
}
func buildFullString(s valueString, buf *[]rune) {
switch s := s.(type) {
case *valueFlatString:
*buf = append(*buf, s.value...)
case *valueStringTree:
buildFullString(s.left, buf)
buildFullString(s.right, buf)
}
}
func (s *valueStringTree) flattenToLeft() {
if s.right != nil {
result := make([]rune, 0, s.len)
buildFullString(s, &result)
s.left = makeStringFromRunes(result)
s.right = nil
}
}
func (s *valueStringTree) index(i *interpreter, index int) (value, error) {
if 0 <= index && index < s.len {
s.flattenToLeft()
return s.left.index(i, index)
}
return nil, i.Error(fmt.Sprintf("Index %d out of bounds, not within [0, %v)", index, s.length()))
}
func (s *valueStringTree) getRunes() []rune {
s.flattenToLeft()
return s.left.getRunes()
}
func (s *valueStringTree) getGoString() string {
s.flattenToLeft()
return s.left.getGoString()
}
func (s *valueStringTree) length() int {
return s.len
}
func (s *valueStringTree) getType() *valueType {
return stringType
}
func emptyString() *valueFlatString {
return &valueFlatString{}
}
func makeStringFromRunes(runes []rune) *valueFlatString {
return &valueFlatString{value: runes}
}
func concatStrings(a, b valueString) valueString {
aLen := a.length()
bLen := b.length()
if aLen == 0 {
return b
} else if bLen == 0 {
return a
} else if aLen+bLen < extendedStringMinLength {
runesA := a.getRunes()
runesB := b.getRunes()
result := make([]rune, 0, aLen+bLen)
result = append(result, runesA...)
result = append(result, runesB...)
return makeStringFromRunes(result)
} else {
return &valueStringTree{
left: a,
right: b,
len: aLen + bLen,
}
}
}
func stringCmp(a, b valueString) int {
runesA := a.getRunes()
runesB := b.getRunes()
var length int
if len(runesA) < len(runesB) {
length = len(runesA)
} else {
length = len(runesB)
}
for i := 0; i < length; i++ {
if runesA[i] != runesB[i] {
return runeCmp(runesA[i], runesB[i])
}
}
return intCmp(len(runesA), len(runesB))
}
func stringEqual(a, b valueString) bool {
runesA := a.getRunes()
runesB := b.getRunes()
if len(runesA) != len(runesB) {
return false
}
for i := 0; i < len(runesA); i++ {
if runesA[i] != runesB[i] {
return false
}
}
return true
}
func makeValueString(v string) valueString {
return &valueFlatString{value: []rune(v)}
}
type valueBoolean struct {
valueBase
value bool
}
func (*valueBoolean) getType() *valueType {
return booleanType
}
func makeValueBoolean(v bool) *valueBoolean {
return &valueBoolean{value: v}
}
type valueNumber struct {
valueBase
value float64
}
func (*valueNumber) getType() *valueType {
return numberType
}
func makeValueNumber(v float64) *valueNumber {
return &valueNumber{value: v}
}
func intToValue(i int) *valueNumber {
return makeValueNumber(float64(i))
}
func int64ToValue(i int64) *valueNumber {
return makeValueNumber(float64(i))
}
type valueNull struct {
valueBase
}
var nullValue valueNull
func makeValueNull() *valueNull {
return &nullValue
}
func (*valueNull) getType() *valueType {
return nullType
}
// ast.Array
// -------------------------------------
type valueArray struct {
valueBase
elements []*cachedThunk
}
func (arr *valueArray) index(i *interpreter, index int) (value, error) {
if 0 <= index && index < arr.length() {
return i.evaluatePV(arr.elements[index])
}
return nil, i.Error(fmt.Sprintf("Index %d out of bounds, not within [0, %v)", index, arr.length()))
}
func (arr *valueArray) length() int {
return len(arr.elements)
}
func makeValueArray(elements []*cachedThunk) *valueArray {
// We don't want to keep a bigger array than necessary
// so we create a new one with minimal capacity
var arrayElems []*cachedThunk
if len(elements) == cap(elements) {
arrayElems = elements
} else {
arrayElems = make([]*cachedThunk, len(elements))
copy(arrayElems, elements)
}
return &valueArray{
elements: arrayElems,
}
}
func concatArrays(a, b *valueArray) *valueArray {
result := make([]*cachedThunk, 0, len(a.elements)+len(b.elements))
result = append(result, a.elements...)
result = append(result, b.elements...)
return &valueArray{elements: result}
}
func (*valueArray) getType() *valueType {
return arrayType
}
// ast.Function
// -------------------------------------
type valueFunction struct {
valueBase
ec evalCallable
}
// TODO(sbarzowski) better name?
type evalCallable interface {
evalCall(args callArguments, i *interpreter) (value, error)
parameters() []namedParameter
}
func (f *valueFunction) call(i *interpreter, args callArguments) (value, error) {
err := checkArguments(i, args, f.parameters())
if err != nil {
return nil, err
}
return f.ec.evalCall(args, i)
}
func (f *valueFunction) parameters() []namedParameter {
return f.ec.parameters()
}
func checkArguments(i *interpreter, args callArguments, params []namedParameter) error {
numPositional := len(args.positional)
numNamed := len(args.named)
maxExpected := len(params)
if numPositional > maxExpected {
return i.Error(fmt.Sprintf("function expected %v positional argument(s), but got %v", maxExpected, numPositional))
}
// Parameter names the function will accept.
accepted := make(map[ast.Identifier]bool, maxExpected)
for _, param := range params {
accepted[param.name] = true
}
// Parameter names the call will bind.
received := make(map[ast.Identifier]bool, numPositional+numNamed)
for i := range args.positional {
received[params[i].name] = true
}
for _, arg := range args.named {
if _, present := received[arg.name]; present {
return i.Error(fmt.Sprintf("Argument %v already provided", arg.name))
}
if _, present := accepted[arg.name]; !present {
return i.Error(fmt.Sprintf("function has no parameter %v", arg.name))
}
received[arg.name] = true
}
for _, param := range params {
if _, present := received[param.name]; !present && param.defaultArg == nil {
return i.Error(fmt.Sprintf("Missing argument: %v", param.name))
}
}
return nil
}
func (f *valueFunction) getType() *valueType {
return functionType
}
type namedParameter struct {
defaultArg ast.Node
name ast.Identifier
}
type callArguments struct {
positional []*cachedThunk
named []namedCallArgument
tailstrict bool
}
type namedCallArgument struct {
pv *cachedThunk
name ast.Identifier
}
func args(xs ...*cachedThunk) callArguments {
return callArguments{positional: xs}
}
// Objects
// -------------------------------------
// Object is a value that allows indexing (taking a value of a field)
// and combining through mixin inheritence (operator +).
type valueObject struct {
valueBase
assertionError error
cache map[objectCacheKey]value
uncached uncachedObject
}
// Hack - we need to distinguish not-checked-yet and no error situations
// so we have a special value for no error and nil means that we don't know yet.
var errNoErrorInObjectInvariants = errors.New("no error - assertions passed")
type objectCacheKey struct {
field string
depth int
}
type selfBinding struct {
// self is the lexically nearest object we are in, or nil. Note
// that this is not the same as context, because we could be inside a function,
// inside an object and then context would be the function, but self would still point
// to the object.
self *valueObject
// superDepth is the "super" level of self. Sometimes, we look upwards in the
// inheritance tree, e.g. via an explicit use of super, or because a given field
// has been inherited. When evaluating a field from one of these super objects,
// we need to bind self to the concrete object (so self must point
// there) but uses of super should be resolved relative to the object whose
// field we are evaluating. Thus, we keep a second field for that. This is
// usually 0, unless we are evaluating a super object's field.
// TODO(sbarzowski) provide some examples
// TODO(sbarzowski) provide somewhere a complete explanation of the object model
superDepth int
}
func makeUnboundSelfBinding() selfBinding {
return selfBinding{
nil,
123456789, // poison value
}
}
func objectBinding(obj *valueObject) selfBinding {
return selfBinding{self: obj, superDepth: 0}
}
func (sb selfBinding) super() selfBinding {
return selfBinding{self: sb.self, superDepth: sb.superDepth + 1}
}
// hidden represents wether to include hidden fields in a lookup.
type hidden int
// With/without hidden fields
const (
withHidden hidden = iota
withoutHidden
)
func withHiddenFromBool(with bool) hidden {
if with {
return withHidden
}
return withoutHidden
}
func (*valueObject) getType() *valueType {
return objectType
}
func (obj *valueObject) index(i *interpreter, field string) (value, error) {
return objectIndex(i, objectBinding(obj), field)
}
func (obj *valueObject) assertionsChecked() bool {
// nil - not checked yet
// errNoErrorInObjectInvariants - we checked and there is no error (or checking in progress)
return obj.assertionError != nil
}
func (obj *valueObject) setAssertionsCheckResult(err error) {
if err != nil {
obj.assertionError = err
} else {
obj.assertionError = errNoErrorInObjectInvariants
}
}
func (obj *valueObject) getAssertionsCheckResult() error {
if obj.assertionError == nil {
panic("Assertions not checked yet")
}
if obj.assertionError == errNoErrorInObjectInvariants {
return nil
}
return obj.assertionError
}
type uncachedObject interface {
inheritanceSize() int
}
type objectLocal struct {
// Locals may depend on self and super so they are unbound fields and not simply thunks
node ast.Node
name ast.Identifier
}
// simpleObject represents a flat object (no inheritance).
// Note that it can be used as part of extended objects
// in inheritance using operator +.
//
// Fields are late bound (to object), so they are not values or potentialValues.
// This is important for inheritance, for example:
// Let a = {x: 42} and b = {y: self.x}. Evaluating b.y is an error,
// but (a+b).y evaluates to 42.
type simpleObject struct {
upValues bindingFrame
fields simpleObjectFieldMap
asserts []unboundField
locals []objectLocal
}
func checkAssertionsHelper(i *interpreter, obj *valueObject, curr uncachedObject, superDepth int) error {
switch curr := curr.(type) {
case *extendedObject:
err := checkAssertionsHelper(i, obj, curr.right, superDepth)
if err != nil {
return err
}
err = checkAssertionsHelper(i, obj, curr.left, superDepth+curr.right.inheritanceSize())
if err != nil {
return err
}
return nil
case *simpleObject:
for _, assert := range curr.asserts {
sb := selfBinding{self: obj, superDepth: superDepth}
fieldUpValues := prepareFieldUpvalues(sb, curr.upValues, curr.locals)
_, err := assert.evaluate(i, sb, fieldUpValues, "")
if err != nil {
return err
}
}
return nil
case nil:
return nil
default:
panic(fmt.Sprintf("Unknown object type %#v", curr))
}
}
func checkAssertions(i *interpreter, obj *valueObject) error {
if !obj.assertionsChecked() {
// Assertions may refer to the object that will normally
// trigger checking of assertions, resulting in an endless recursion.
// To avoid that, while we check them, we treat them as already passed.
obj.setAssertionsCheckResult(errNoErrorInObjectInvariants)
obj.setAssertionsCheckResult(checkAssertionsHelper(i, obj, obj.uncached, 0))
}
return obj.getAssertionsCheckResult()
}
func (*simpleObject) inheritanceSize() int {
return 1
}
func makeValueSimpleObject(b bindingFrame, fields simpleObjectFieldMap, asserts []unboundField, locals []objectLocal) *valueObject {
return &valueObject{
cache: make(map[objectCacheKey]value),
uncached: &simpleObject{
upValues: b,
fields: fields,
asserts: asserts,
locals: locals,
},
}
}
type simpleObjectFieldMap map[string]simpleObjectField
type simpleObjectField struct {
field unboundField
hide ast.ObjectFieldHide
}
// unboundField is a field that doesn't know yet in which object it is.
type unboundField interface {
evaluate(i *interpreter, sb selfBinding, origBinding bindingFrame, fieldName string) (value, error)
loc() *ast.LocationRange
}
// extendedObject represents an object created through inheritance (left + right).
// We represent it as the pair of objects. This results in a tree-like structure.
// Example:
// (A + B) + C
//
// +
// / \
// + C
// / \
// A B
//
// It is possible to create an arbitrary binary tree.
// Note however, that because + is associative the only thing that matters
// is the order of leafs.
//
// This represenation allows us to implement "+" in O(1),
// but requires going through the tree and trying subsequent leafs for field access.
type extendedObject struct {
left, right uncachedObject
totalInheritanceSize int
}
func (o *extendedObject) inheritanceSize() int {
return o.totalInheritanceSize
}
func makeValueExtendedObject(left, right *valueObject) *valueObject {
return &valueObject{
cache: make(map[objectCacheKey]value),
uncached: &extendedObject{
left: left.uncached,
right: right.uncached,
totalInheritanceSize: left.uncached.inheritanceSize() + right.uncached.inheritanceSize(),
},
}
}
// findField returns a field in object curr, with superDepth at least minSuperDepth
// It also returns an associated bindingFrame and actual superDepth that the field
// was found at.
func findField(curr uncachedObject, minSuperDepth int, f string) (bool, simpleObjectField, bindingFrame, []objectLocal, int) {
switch curr := curr.(type) {
case *extendedObject:
if curr.right.inheritanceSize() > minSuperDepth {
found, field, frame, locals, counter := findField(curr.right, minSuperDepth, f)
if found {
return true, field, frame, locals, counter
}
}
found, field, frame, locals, counter := findField(curr.left, minSuperDepth-curr.right.inheritanceSize(), f)
return found, field, frame, locals, counter + curr.right.inheritanceSize()
case *simpleObject:
if minSuperDepth <= 0 {
if field, ok := curr.fields[f]; ok {
return true, field, curr.upValues, curr.locals, 0
}
}
return false, simpleObjectField{}, nil, nil, 0
default:
panic(fmt.Sprintf("Unknown object type %#v", curr))
}
}
func prepareFieldUpvalues(sb selfBinding, upValues bindingFrame, locals []objectLocal) bindingFrame {
newUpValues := make(bindingFrame, len(upValues))
for k, v := range upValues {
newUpValues[k] = v
}
localThunks := make([]*cachedThunk, 0, len(locals))
for _, l := range locals {
th := &cachedThunk{
// We will fill upValues later
env: &environment{upValues: nil, selfBinding: sb},
body: l.node,
}
newUpValues[l.name] = th
localThunks = append(localThunks, th)
}
for _, th := range localThunks {
th.env.upValues = newUpValues
}
return newUpValues
}
func objectIndex(i *interpreter, sb selfBinding, fieldName string) (value, error) {
err := checkAssertions(i, sb.self)
if err != nil {
return nil, err
}
if sb.superDepth >= sb.self.uncached.inheritanceSize() {
return nil, i.Error("Attempt to use super when there is no super class.")
}
found, field, upValues, locals, foundAt := findField(sb.self.uncached, sb.superDepth, fieldName)
if !found {
return nil, i.Error(fmt.Sprintf("Field does not exist: %s", fieldName))
}
if val, ok := sb.self.cache[objectCacheKey{field: fieldName, depth: foundAt}]; ok {
return val, nil
}
fieldSelfBinding := selfBinding{self: sb.self, superDepth: foundAt}
fieldUpValues := prepareFieldUpvalues(fieldSelfBinding, upValues, locals)
val, err := field.field.evaluate(i, fieldSelfBinding, fieldUpValues, fieldName)
if err == nil {
sb.self.cache[objectCacheKey{field: fieldName, depth: foundAt}] = val
}
return val, err
}
func objectHasField(sb selfBinding, fieldName string) bool {
found, _, _, _, _ := findField(sb.self.uncached, sb.superDepth, fieldName)
return found
}
type fieldHideMap map[string]ast.ObjectFieldHide
func uncachedObjectFieldsVisibility(obj uncachedObject) fieldHideMap {
r := make(fieldHideMap)
switch obj := obj.(type) {
case *extendedObject:
r = uncachedObjectFieldsVisibility(obj.left)
rightMap := uncachedObjectFieldsVisibility(obj.right)
for k, v := range rightMap {
if v == ast.ObjectFieldInherit {
if _, alreadyExists := r[k]; !alreadyExists {
r[k] = v
}
} else {
r[k] = v
}
}
return r
case *simpleObject:
for fieldName, field := range obj.fields {
r[fieldName] = field.hide
}
}
return r
}
func objectFieldsVisibility(obj *valueObject) fieldHideMap {
return uncachedObjectFieldsVisibility(obj.uncached)
}
// Returns field names of an object. Gotcha: the order of fields is unpredictable.
func objectFields(obj *valueObject, h hidden) []string {
var r []string
for fieldName, hide := range objectFieldsVisibility(obj) {
if h == withHidden || hide != ast.ObjectFieldHidden {
r = append(r, fieldName)
}
}
return r
}
func duplicateFieldNameErrMsg(fieldName string) string {
return fmt.Sprintf("Duplicate field name: %s", unparseString(fieldName))
}