From 83ed0b939f98b8edab6626312460dc3fa2565171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Barzowski?= Date: Thu, 9 May 2019 13:26:54 +0200 Subject: [PATCH] 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. --- builtins.go | 21 ++++++++++++++------- testdata/object_comp_local.golden | 3 +++ testdata/object_comp_local.jsonnet | 1 + testdata/object_comp_local2.golden | 3 +++ testdata/object_comp_local2.jsonnet | 1 + testdata/object_comp_local3.golden | 3 +++ testdata/object_comp_local3.jsonnet | 1 + 7 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 testdata/object_comp_local.golden create mode 100644 testdata/object_comp_local.jsonnet create mode 100644 testdata/object_comp_local2.golden create mode 100644 testdata/object_comp_local2.jsonnet create mode 100644 testdata/object_comp_local3.golden create mode 100644 testdata/object_comp_local3.jsonnet diff --git a/builtins.go b/builtins.go index cc66696..9f22371 100644 --- a/builtins.go +++ b/builtins.go @@ -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, diff --git a/testdata/object_comp_local.golden b/testdata/object_comp_local.golden new file mode 100644 index 0000000..126cb2c --- /dev/null +++ b/testdata/object_comp_local.golden @@ -0,0 +1,3 @@ +{ + "a": "170b" +} diff --git a/testdata/object_comp_local.jsonnet b/testdata/object_comp_local.jsonnet new file mode 100644 index 0000000..db82588 --- /dev/null +++ b/testdata/object_comp_local.jsonnet @@ -0,0 +1 @@ +local a = "b"; {local tmp = a, [name]: std.parseHex("aa") + tmp for name in ["a"]} \ No newline at end of file diff --git a/testdata/object_comp_local2.golden b/testdata/object_comp_local2.golden new file mode 100644 index 0000000..7e027be --- /dev/null +++ b/testdata/object_comp_local2.golden @@ -0,0 +1,3 @@ +{ + "a": 425 +} diff --git a/testdata/object_comp_local2.jsonnet b/testdata/object_comp_local2.jsonnet new file mode 100644 index 0000000..89d7155 --- /dev/null +++ b/testdata/object_comp_local2.jsonnet @@ -0,0 +1 @@ +{local tmp = std.parseHex("ff"), [name]: std.parseHex("aa") + tmp for name in ["a"]} \ No newline at end of file diff --git a/testdata/object_comp_local3.golden b/testdata/object_comp_local3.golden new file mode 100644 index 0000000..ac4cd93 --- /dev/null +++ b/testdata/object_comp_local3.golden @@ -0,0 +1,3 @@ +{ + "a": 255 +} diff --git a/testdata/object_comp_local3.jsonnet b/testdata/object_comp_local3.jsonnet new file mode 100644 index 0000000..5d43bd3 --- /dev/null +++ b/testdata/object_comp_local3.jsonnet @@ -0,0 +1 @@ +{local tmp = std.parseHex("ff"), [name]: tmp for name in ["a"]} \ No newline at end of file