Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: eliminate context type
Browse files Browse the repository at this point in the history
Change-Id: Ifa11b59cd5f96e6a70cbf13aa380c84970e8d323
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9364
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Apr 13, 2021
1 parent c0fe9ce commit af509c6
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 146 deletions.
2 changes: 1 addition & 1 deletion cue/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init() {

internal.CheckAndForkRuntime = func(runtime, value interface{}) interface{} {
r := runtime.(*Runtime)
idx := value.(Value).ctx().index
idx := value.(Value).idx
if idx != r.idx {
panic("value not from same runtime")
}
Expand Down
3 changes: 1 addition & 2 deletions cue/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ func init() {
// TODO: unroll this function. Should no longer be necessary to be internal.
internal.UnifyBuiltin = func(val interface{}, kind string) interface{} {
v := val.(Value)
ctx := v.ctx()

p := strings.Split(kind, ".")
pkg, name := p[0], p[1]
s, _ := runtime.SharedRuntime.LoadImport(pkg)
if s == nil {
return v
}
a := s.Lookup(ctx.Label(name, false))
a := s.Lookup(v.idx.Label(name, false))
if a == nil {
return v
}
Expand Down
49 changes: 11 additions & 38 deletions cue/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,35 @@ import (
"cuelang.org/go/internal/core/eval"
)

// context manages evaluation state.
type context struct {
opCtx *adt.OpContext
*index
}

// newContext returns a new evaluation context.
func newContext(idx *index) *context {
c := &context{
index: idx,
}
if idx != nil {
c.opCtx = eval.NewContext(idx.Runtime, nil)
func newContext(idx *index) *adt.OpContext {
if idx == nil {
return nil
}
return c
return eval.NewContext(idx.Runtime, nil)
}

func debugStr(ctx *context, v adt.Node) string {
return debug.NodeString(ctx.opCtx, v, nil)
func debugStr(ctx *adt.OpContext, v adt.Node) string {
return debug.NodeString(ctx, v, nil)
}

func (c *context) str(v adt.Node) string {
func str(c *adt.OpContext, v adt.Node) string {
return debugStr(c, v)
}

func (c *context) mkErr(src adt.Node, args ...interface{}) *adt.Bottom {
return c.index.mkErr(src, args...)
}

func (c *context) vertex(v *adt.Vertex) *adt.Vertex {
return v
}

// vertex returns the evaluated vertex of v.
func (v Value) vertex(ctx *context) *adt.Vertex {
return ctx.vertex(v.v)
}

// eval returns the evaluated value. This may not be the vertex.
//
// Deprecated: use ctx.value
func (v Value) eval(ctx *context) adt.Value {
func (v Value) eval(ctx *adt.OpContext) adt.Value {
if v.v == nil {
panic("undefined value")
}
x := ctx.manifest(v.v)
x := manifest(ctx, v.v)
return x.Value()
}

// func (v Value) evalFull(u value) (Value, adt.Value) {
// ctx := v.ctx()
// x := ctx.manifest(u)
// }

// TODO: change from Vertex to Vertex.
func (c *context) manifest(v *adt.Vertex) *adt.Vertex {
v.Finalize(c.opCtx)
func manifest(ctx *adt.OpContext, v *adt.Vertex) *adt.Vertex {
v.Finalize(ctx)
return v
}
2 changes: 1 addition & 1 deletion cue/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var errNotExists = &adt.Bottom{
Err: errors.Newf(token.NoPos, "undefined value"),
}

func (idx *index) mkErr(src adt.Node, args ...interface{}) *adt.Bottom {
func mkErr(idx *index, src adt.Node, args ...interface{}) *adt.Bottom {
var e *adt.Bottom
var code adt.ErrorCode = -1
outer:
Expand Down
36 changes: 18 additions & 18 deletions cue/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Instance struct {
// complete bool // for cycle detection
}

func (x *index) addInst(p *Instance) *Instance {
func addInst(x *index, p *Instance) *Instance {
if p.inst == nil {
p.inst = &build.Instance{
ImportPath: p.ImportPath,
Expand Down Expand Up @@ -128,7 +128,7 @@ func init() {
st = &adt.Vertex{}
st.AddConjunct(adt.MakeRootConjunct(nil, x))
}
return v.ctx().index.addInst(&Instance{
return addInst(v.idx, &Instance{
root: st,
})
}
Expand Down Expand Up @@ -167,9 +167,9 @@ func (inst *Instance) setError(err errors.Error) {
inst.Err = errors.Append(inst.Err, err)
}

func (inst *Instance) eval(ctx *context) adt.Value {
func (inst *Instance) eval(ctx *adt.OpContext) adt.Value {
// TODO: remove manifest here?
v := ctx.manifest(inst.root)
v := manifest(ctx, inst.root)
return v
}

Expand All @@ -178,7 +178,7 @@ func init() {
v := value.(Value)
e := expr.(ast.Expr)
ctx := newContext(v.idx)
return newValueRoot(ctx, evalExpr(ctx, v.vertex(ctx), e))
return newValueRoot(v.idx, ctx, evalExpr(ctx, v.v, e))
}
}

Expand All @@ -188,7 +188,7 @@ func pkgID() string {
}

// evalExpr evaluates expr within scope.
func evalExpr(ctx *context, scope *adt.Vertex, expr ast.Expr) adt.Value {
func evalExpr(ctx *adt.OpContext, scope *adt.Vertex, expr ast.Expr) adt.Value {
cfg := &compile.Config{
Scope: scope,
Imports: func(x *ast.Ident) (pkgPath string) {
Expand All @@ -199,13 +199,13 @@ func evalExpr(ctx *context, scope *adt.Vertex, expr ast.Expr) adt.Value {
},
}

c, err := compile.Expr(cfg, ctx.opCtx, pkgID(), expr)
c, err := compile.Expr(cfg, ctx, pkgID(), expr)
if err != nil {
return &adt.Bottom{Err: err}
}
return adt.Resolve(ctx.opCtx, c)
return adt.Resolve(ctx, c)

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

// c := ctx.opCtx
// c := ctx

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

// Eval evaluates an expression within an existing instance.
Expand All @@ -280,9 +280,9 @@ func (inst *Instance) Value() Value {
func (inst *Instance) Eval(expr ast.Expr) Value {
ctx := newContext(inst.index)
v := inst.root
v.Finalize(ctx.opCtx)
v.Finalize(ctx)
result := evalExpr(ctx, v, expr)
return newValueRoot(ctx, result)
return newValueRoot(inst.index, ctx, result)
}

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

i := inst[0]
ctx := newContext(i.index).opCtx
ctx := newContext(i.index)

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

Expand All @@ -302,7 +302,7 @@ func Merge(inst ...*Instance) *Instance {
}
v.Finalize(ctx)

p := i.index.addInst(&Instance{
p := addInst(i.index, &Instance{
root: v,
// complete: true,
})
Expand Down Expand Up @@ -343,7 +343,7 @@ func (inst *Instance) Build(p *build.Instance) *Instance {
}

func (inst *Instance) value() Value {
return newVertexRoot(newContext(inst.index), inst.root)
return newVertexRoot(inst.index, newContext(inst.index), inst.root)
}

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

Expand Down
4 changes: 1 addition & 3 deletions cue/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ func (r *Runtime) Unmarshal(b []byte) ([]*Instance, error) {
// The stored instances are functionally the same, but preserving of file
// information is only done on a best-effort basis.
func (r *Runtime) Marshal(instances ...*Instance) (b []byte, err error) {
ctx := newContext(r.index())

staged := []instanceData{}
done := map[string]int{}

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

for _, imp := range imports {
i := getImportFromPath(ctx.index, imp)
i := getImportFromPath(r.idx, imp)
if i == nil || !strings.Contains(imp, ".") {
continue // a builtin package.
}
Expand Down
10 changes: 5 additions & 5 deletions cue/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ func getScopePrefix(v Value, p Path) *adt.Vertex {
func errFn(pos token.Pos, msg string, args ...interface{}) {}

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

astutil.ResolveExpr(x, errFn)

c, err := compile.Expr(cfg, ctx.opCtx, pkgID(), x)
c, err := compile.Expr(cfg, ctx, pkgID(), x)
if err != nil {
return &adt.Bottom{Err: err}
}
return adt.Resolve(ctx.opCtx, c)
return adt.Resolve(ctx, c)
}

// LookupPath reports the value for path p relative to v.
Expand All @@ -59,7 +59,7 @@ func (v Value) LookupPath(p Path) Value {
return Value{}
}
n := v.v
ctx := v.ctx().opCtx
ctx := v.ctx()

outer:
for _, sel := range p.path {
Expand Down Expand Up @@ -88,7 +88,7 @@ outer:
x = &adt.Bottom{Err: err.Error}
} else {
// TODO: better message.
x = v.idx.mkErr(n, adt.NotExistError, "field %q not found", sel.sel)
x = mkErr(v.idx, n, adt.NotExistError, "field %q not found", sel.sel)
}
v := makeValue(v.idx, n)
return newErrValue(v, x)
Expand Down
Loading

0 comments on commit af509c6

Please sign in to comment.