Proper env for obj locals in comprehensions

We didn't set the environment (upvalues) for objects
created as comprehensions - we set them for each field
separately, but that meant missing the locals.
This commit is contained in:
Stanisław Barzowski 2019-05-09 13:26:54 +02:00
parent 33b6dcfa53
commit 83ed0b939f
7 changed files with 26 additions and 7 deletions

View File

@ -777,7 +777,7 @@ func builtinSplitLimit(i *interpreter, trace TraceElement, strv, cv, maxSplitsV
if maxSplits == -1 {
strs = strings.SplitN(sStr, sC, -1)
} else {
strs = strings.SplitN(sStr, sC, maxSplits + 1)
strs = strings.SplitN(sStr, sC, maxSplits+1)
}
res := make([]*cachedThunk, len(strs))
for i := range strs {
@ -821,8 +821,7 @@ func builtinUglyObjectFlatMerge(i *interpreter, trace TraceElement, x value) (va
return &valueSimpleObject{}, nil
}
newFields := make(simpleObjectFieldMap)
var locals []objectLocal
var upValues bindingFrame
var anyObj *valueSimpleObject
for _, elem := range objarr.elements {
obj, err := i.evaluateObject(elem, trace)
if err != nil {
@ -853,14 +852,22 @@ func builtinUglyObjectFlatMerge(i *interpreter, trace TraceElement, x value) (va
bindings: simpleObj.upValues,
},
}
// another ugliness - we just take the locals of our last object,
// we assume that the locals are the same for each of merged objects
locals = simpleObj.locals
}
anyObj = simpleObj
}
var locals []objectLocal
var localUpValues bindingFrame
if len(objarr.elements) > 0 {
// another ugliness - we just take the locals of our last object,
// we assume that the locals are the same for each of merged objects
locals = anyObj.locals
// note that there are already holes for object locals
localUpValues = anyObj.upValues
}
return makeValueSimpleObject(
upValues,
localUpValues,
newFields,
[]unboundField{}, // No asserts allowed
locals,

3
testdata/object_comp_local.golden vendored Normal file
View File

@ -0,0 +1,3 @@
{
"a": "170b"
}

1
testdata/object_comp_local.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
local a = "b"; {local tmp = a, [name]: std.parseHex("aa") + tmp for name in ["a"]}

3
testdata/object_comp_local2.golden vendored Normal file
View File

@ -0,0 +1,3 @@
{
"a": 425
}

1
testdata/object_comp_local2.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
{local tmp = std.parseHex("ff"), [name]: std.parseHex("aa") + tmp for name in ["a"]}

3
testdata/object_comp_local3.golden vendored Normal file
View File

@ -0,0 +1,3 @@
{
"a": 255
}

1
testdata/object_comp_local3.jsonnet vendored Normal file
View File

@ -0,0 +1 @@
{local tmp = std.parseHex("ff"), [name]: tmp for name in ["a"]}