Skip to content

Commit af509c6

Browse files
committed
cue: eliminate context type
Change-Id: Ifa11b59cd5f96e6a70cbf13aa380c84970e8d323 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9364 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent c0fe9ce commit af509c6

File tree

9 files changed

+116
-146
lines changed

9 files changed

+116
-146
lines changed

cue/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func init() {
5050

5151
internal.CheckAndForkRuntime = func(runtime, value interface{}) interface{} {
5252
r := runtime.(*Runtime)
53-
idx := value.(Value).ctx().index
53+
idx := value.(Value).idx
5454
if idx != r.idx {
5555
panic("value not from same runtime")
5656
}

cue/builtin.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@ func init() {
3838
// TODO: unroll this function. Should no longer be necessary to be internal.
3939
internal.UnifyBuiltin = func(val interface{}, kind string) interface{} {
4040
v := val.(Value)
41-
ctx := v.ctx()
4241

4342
p := strings.Split(kind, ".")
4443
pkg, name := p[0], p[1]
4544
s, _ := runtime.SharedRuntime.LoadImport(pkg)
4645
if s == nil {
4746
return v
4847
}
49-
a := s.Lookup(ctx.Label(name, false))
48+
a := s.Lookup(v.idx.Label(name, false))
5049
if a == nil {
5150
return v
5251
}

cue/context.go

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,35 @@ import (
2020
"cuelang.org/go/internal/core/eval"
2121
)
2222

23-
// context manages evaluation state.
24-
type context struct {
25-
opCtx *adt.OpContext
26-
*index
27-
}
28-
2923
// newContext returns a new evaluation context.
30-
func newContext(idx *index) *context {
31-
c := &context{
32-
index: idx,
33-
}
34-
if idx != nil {
35-
c.opCtx = eval.NewContext(idx.Runtime, nil)
24+
func newContext(idx *index) *adt.OpContext {
25+
if idx == nil {
26+
return nil
3627
}
37-
return c
28+
return eval.NewContext(idx.Runtime, nil)
3829
}
3930

40-
func debugStr(ctx *context, v adt.Node) string {
41-
return debug.NodeString(ctx.opCtx, v, nil)
31+
func debugStr(ctx *adt.OpContext, v adt.Node) string {
32+
return debug.NodeString(ctx, v, nil)
4233
}
4334

44-
func (c *context) str(v adt.Node) string {
35+
func str(c *adt.OpContext, v adt.Node) string {
4536
return debugStr(c, v)
4637
}
4738

48-
func (c *context) mkErr(src adt.Node, args ...interface{}) *adt.Bottom {
49-
return c.index.mkErr(src, args...)
50-
}
51-
52-
func (c *context) vertex(v *adt.Vertex) *adt.Vertex {
53-
return v
54-
}
55-
56-
// vertex returns the evaluated vertex of v.
57-
func (v Value) vertex(ctx *context) *adt.Vertex {
58-
return ctx.vertex(v.v)
59-
}
60-
6139
// eval returns the evaluated value. This may not be the vertex.
6240
//
6341
// Deprecated: use ctx.value
64-
func (v Value) eval(ctx *context) adt.Value {
42+
func (v Value) eval(ctx *adt.OpContext) adt.Value {
6543
if v.v == nil {
6644
panic("undefined value")
6745
}
68-
x := ctx.manifest(v.v)
46+
x := manifest(ctx, v.v)
6947
return x.Value()
7048
}
7149

72-
// func (v Value) evalFull(u value) (Value, adt.Value) {
73-
// ctx := v.ctx()
74-
// x := ctx.manifest(u)
75-
// }
76-
7750
// TODO: change from Vertex to Vertex.
78-
func (c *context) manifest(v *adt.Vertex) *adt.Vertex {
79-
v.Finalize(c.opCtx)
51+
func manifest(ctx *adt.OpContext, v *adt.Vertex) *adt.Vertex {
52+
v.Finalize(ctx)
8053
return v
8154
}

cue/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ var errNotExists = &adt.Bottom{
9494
Err: errors.Newf(token.NoPos, "undefined value"),
9595
}
9696

97-
func (idx *index) mkErr(src adt.Node, args ...interface{}) *adt.Bottom {
97+
func mkErr(idx *index, src adt.Node, args ...interface{}) *adt.Bottom {
9898
var e *adt.Bottom
9999
var code adt.ErrorCode = -1
100100
outer:

cue/instance.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Instance struct {
4545
// complete bool // for cycle detection
4646
}
4747

48-
func (x *index) addInst(p *Instance) *Instance {
48+
func addInst(x *index, p *Instance) *Instance {
4949
if p.inst == nil {
5050
p.inst = &build.Instance{
5151
ImportPath: p.ImportPath,
@@ -128,7 +128,7 @@ func init() {
128128
st = &adt.Vertex{}
129129
st.AddConjunct(adt.MakeRootConjunct(nil, x))
130130
}
131-
return v.ctx().index.addInst(&Instance{
131+
return addInst(v.idx, &Instance{
132132
root: st,
133133
})
134134
}
@@ -167,9 +167,9 @@ func (inst *Instance) setError(err errors.Error) {
167167
inst.Err = errors.Append(inst.Err, err)
168168
}
169169

170-
func (inst *Instance) eval(ctx *context) adt.Value {
170+
func (inst *Instance) eval(ctx *adt.OpContext) adt.Value {
171171
// TODO: remove manifest here?
172-
v := ctx.manifest(inst.root)
172+
v := manifest(ctx, inst.root)
173173
return v
174174
}
175175

@@ -178,7 +178,7 @@ func init() {
178178
v := value.(Value)
179179
e := expr.(ast.Expr)
180180
ctx := newContext(v.idx)
181-
return newValueRoot(ctx, evalExpr(ctx, v.vertex(ctx), e))
181+
return newValueRoot(v.idx, ctx, evalExpr(ctx, v.v, e))
182182
}
183183
}
184184

@@ -188,7 +188,7 @@ func pkgID() string {
188188
}
189189

190190
// evalExpr evaluates expr within scope.
191-
func evalExpr(ctx *context, scope *adt.Vertex, expr ast.Expr) adt.Value {
191+
func evalExpr(ctx *adt.OpContext, scope *adt.Vertex, expr ast.Expr) adt.Value {
192192
cfg := &compile.Config{
193193
Scope: scope,
194194
Imports: func(x *ast.Ident) (pkgPath string) {
@@ -199,13 +199,13 @@ func evalExpr(ctx *context, scope *adt.Vertex, expr ast.Expr) adt.Value {
199199
},
200200
}
201201

202-
c, err := compile.Expr(cfg, ctx.opCtx, pkgID(), expr)
202+
c, err := compile.Expr(cfg, ctx, pkgID(), expr)
203203
if err != nil {
204204
return &adt.Bottom{Err: err}
205205
}
206-
return adt.Resolve(ctx.opCtx, c)
206+
return adt.Resolve(ctx, c)
207207

208-
// scope.Finalize(ctx.opCtx) // TODO: not appropriate here.
208+
// scope.Finalize(ctx) // TODO: not appropriate here.
209209
// switch s := scope.Value.(type) {
210210
// case *bottom:
211211
// return s
@@ -214,7 +214,7 @@ func evalExpr(ctx *context, scope *adt.Vertex, expr ast.Expr) adt.Value {
214214
// return ctx.mkErr(scope, "instance is not a struct, found %s", scope.Kind())
215215
// }
216216

217-
// c := ctx.opCtx
217+
// c := ctx
218218

219219
// x, err := compile.Expr(&compile.Config{Scope: scope}, c.Runtime, expr)
220220
// if err != nil {
@@ -270,8 +270,8 @@ func (inst *Instance) Doc() []*ast.CommentGroup {
270270
// top-level values.
271271
func (inst *Instance) Value() Value {
272272
ctx := newContext(inst.index)
273-
inst.root.Finalize(ctx.opCtx)
274-
return newVertexRoot(ctx, inst.root)
273+
inst.root.Finalize(ctx)
274+
return newVertexRoot(inst.index, ctx, inst.root)
275275
}
276276

277277
// Eval evaluates an expression within an existing instance.
@@ -280,9 +280,9 @@ func (inst *Instance) Value() Value {
280280
func (inst *Instance) Eval(expr ast.Expr) Value {
281281
ctx := newContext(inst.index)
282282
v := inst.root
283-
v.Finalize(ctx.opCtx)
283+
v.Finalize(ctx)
284284
result := evalExpr(ctx, v, expr)
285-
return newValueRoot(ctx, result)
285+
return newValueRoot(inst.index, ctx, result)
286286
}
287287

288288
// DO NOT USE.
@@ -292,7 +292,7 @@ func Merge(inst ...*Instance) *Instance {
292292
v := &adt.Vertex{}
293293

294294
i := inst[0]
295-
ctx := newContext(i.index).opCtx
295+
ctx := newContext(i.index)
296296

297297
// TODO: interesting test: use actual unification and then on K8s corpus.
298298

@@ -302,7 +302,7 @@ func Merge(inst ...*Instance) *Instance {
302302
}
303303
v.Finalize(ctx)
304304

305-
p := i.index.addInst(&Instance{
305+
p := addInst(i.index, &Instance{
306306
root: v,
307307
// complete: true,
308308
})
@@ -343,7 +343,7 @@ func (inst *Instance) Build(p *build.Instance) *Instance {
343343
}
344344

345345
func (inst *Instance) value() Value {
346-
return newVertexRoot(newContext(inst.index), inst.root)
346+
return newVertexRoot(inst.index, newContext(inst.index), inst.root)
347347
}
348348

349349
// Lookup reports the value at a path starting from the top level struct. The
@@ -416,7 +416,7 @@ func (inst *Instance) Fill(x interface{}, path ...string) (*Instance, error) {
416416
u.AddConjunct(adt.MakeRootConjunct(nil, expr))
417417
u.Finalize(ctx)
418418
}
419-
inst = inst.index.addInst(&Instance{
419+
inst = addInst(inst.index, &Instance{
420420
root: u,
421421
inst: nil,
422422

cue/marshal.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ func (r *Runtime) Unmarshal(b []byte) ([]*Instance, error) {
129129
// The stored instances are functionally the same, but preserving of file
130130
// information is only done on a best-effort basis.
131131
func (r *Runtime) Marshal(instances ...*Instance) (b []byte, err error) {
132-
ctx := newContext(r.index())
133-
134132
staged := []instanceData{}
135133
done := map[string]int{}
136134

@@ -192,7 +190,7 @@ func (r *Runtime) Marshal(instances ...*Instance) (b []byte, err error) {
192190
p := len(staged) - 1
193191

194192
for _, imp := range imports {
195-
i := getImportFromPath(ctx.index, imp)
193+
i := getImportFromPath(r.idx, imp)
196194
if i == nil || !strings.Contains(imp, ".") {
197195
continue // a builtin package.
198196
}

cue/query.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ func getScopePrefix(v Value, p Path) *adt.Vertex {
4141
func errFn(pos token.Pos, msg string, args ...interface{}) {}
4242

4343
// resolveExpr binds unresolved expressions to values in the expression or v.
44-
func resolveExpr(ctx *context, v *adt.Vertex, x ast.Expr) adt.Value {
44+
func resolveExpr(ctx *adt.OpContext, v *adt.Vertex, x ast.Expr) adt.Value {
4545
cfg := &compile.Config{Scope: v}
4646

4747
astutil.ResolveExpr(x, errFn)
4848

49-
c, err := compile.Expr(cfg, ctx.opCtx, pkgID(), x)
49+
c, err := compile.Expr(cfg, ctx, pkgID(), x)
5050
if err != nil {
5151
return &adt.Bottom{Err: err}
5252
}
53-
return adt.Resolve(ctx.opCtx, c)
53+
return adt.Resolve(ctx, c)
5454
}
5555

5656
// LookupPath reports the value for path p relative to v.
@@ -59,7 +59,7 @@ func (v Value) LookupPath(p Path) Value {
5959
return Value{}
6060
}
6161
n := v.v
62-
ctx := v.ctx().opCtx
62+
ctx := v.ctx()
6363

6464
outer:
6565
for _, sel := range p.path {
@@ -88,7 +88,7 @@ outer:
8888
x = &adt.Bottom{Err: err.Error}
8989
} else {
9090
// TODO: better message.
91-
x = v.idx.mkErr(n, adt.NotExistError, "field %q not found", sel.sel)
91+
x = mkErr(v.idx, n, adt.NotExistError, "field %q not found", sel.sel)
9292
}
9393
v := makeValue(v.idx, n)
9494
return newErrValue(v, x)

0 commit comments

Comments
 (0)