Skip to content

Commit

Permalink
cue: get rid of internal index type
Browse files Browse the repository at this point in the history
Change-Id: I0ce4bb9ab8263a1a8a2774ce3cd1ff9de3bbca46
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9365
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 af509c6 commit 52db572
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 59 deletions.
29 changes: 8 additions & 21 deletions cue/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
//
// The zero value of a Runtime is ready to use.
type Runtime struct {
idx *index
idx *runtime.Runtime
}

func init() {
Expand Down Expand Up @@ -59,15 +59,15 @@ func init() {

internal.CoreValue = func(value interface{}) (runtime, vertex interface{}) {
if v, ok := value.(Value); ok && v.v != nil {
return v.idx.Runtime, v.v
return v.idx, v.v
}
return nil, nil
}
}

func dummyLoad(token.Pos, string) *build.Instance { return nil }

func (r *Runtime) index() *index {
func (r *Runtime) index() *runtime.Runtime {
if r.idx == nil {
r.idx = newIndex()
}
Expand Down Expand Up @@ -184,35 +184,22 @@ func (r *Runtime) FromExpr(expr ast.Expr) (*Instance, error) {
})
}

// index maps conversions from label names to internal codes.
//
// All instances belonging to the same package should share this index.
type index struct {
*runtime.Runtime
}

// NewRuntime creates a *runtime.Runtime with builtins preloaded.
func NewRuntime() *runtime.Runtime {
i := newIndex()
i.Runtime.Data = i
return i.Runtime
return i
}

// newIndex creates a new index.
func newIndex() *index {
r := runtime.New()
i := &index{
Runtime: r,
}
r.Data = i
return i
func newIndex() *runtime.Runtime {
return runtime.New()
}

func isBuiltin(s string) bool {
return runtime.SharedRuntime.IsBuiltinPackage(s)
}

func loadInstance(idx *index, p *build.Instance) *Instance {
v, _ := idx.Runtime.Build(p)
func loadInstance(idx *runtime.Runtime, p *build.Instance) *Instance {
v, _ := idx.Build(p)
return getImportFromBuild(idx, p, v)
}
2 changes: 1 addition & 1 deletion cue/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestBuild(t *testing.T) {
got = err.Error()
} else {
cfg := &debug.Config{Compact: true}
got = debug.NodeString(insts[0].index.Runtime, insts[0].Value().v, cfg)
got = debug.NodeString(insts[0].index, insts[0].Value().v, cfg)
}
if got != tc.emit {
t.Errorf("\n got: %s\nwant: %s", got, tc.emit)
Expand Down
5 changes: 3 additions & 2 deletions cue/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/debug"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/runtime"
)

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

func debugStr(ctx *adt.OpContext, v adt.Node) string {
Expand Down
3 changes: 2 additions & 1 deletion cue/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/runtime"
)

func (v Value) toErr(b *adt.Bottom) (err errors.Error) {
Expand Down Expand Up @@ -94,7 +95,7 @@ var errNotExists = &adt.Bottom{
Err: errors.Newf(token.NoPos, "undefined value"),
}

func mkErr(idx *index, src adt.Node, args ...interface{}) *adt.Bottom {
func mkErr(idx *runtime.Runtime, src adt.Node, args ...interface{}) *adt.Bottom {
var e *adt.Bottom
var code adt.ErrorCode = -1
outer:
Expand Down
4 changes: 2 additions & 2 deletions cue/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import (
func init() {
internal.FromGoValue = func(runtime, x interface{}, nilIsTop bool) interface{} {
r := runtime.(*Runtime)
ctx := eval.NewContext(r.index().Runtime, nil)
ctx := eval.NewContext(r.index(), nil)
v := convert.GoValueToValue(ctx, x, nilIsTop)
n := adt.ToVertex(v)
return Value{r.idx, n}
}

internal.FromGoType = func(runtime, x interface{}) interface{} {
r := runtime.(*Runtime)
ctx := eval.NewContext(r.index().Runtime, nil)
ctx := eval.NewContext(r.index(), nil)
expr, err := convert.GoTypeToExpr(ctx, x)
if err != nil {
expr = &adt.Bottom{Err: err}
Expand Down
20 changes: 10 additions & 10 deletions cue/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import (
"cuelang.org/go/internal/core/compile"
"cuelang.org/go/internal/core/convert"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/runtime"
)

// An Instance defines a single configuration based on a collection of
// underlying CUE files.
type Instance struct {
index *index
index *runtime.Runtime

root *adt.Vertex

Expand All @@ -45,28 +46,27 @@ type Instance struct {
// complete bool // for cycle detection
}

func addInst(x *index, p *Instance) *Instance {
func addInst(x *runtime.Runtime, p *Instance) *Instance {
if p.inst == nil {
p.inst = &build.Instance{
ImportPath: p.ImportPath,
PkgName: p.PkgName,
}
}
// fmt.Println(p.ImportPath, "XXX")
x.AddInst(p.ImportPath, p.root, p.inst)
x.SetBuildData(p.inst, p)
p.index = x
return p
}

func lookupInstance(x *index, p *build.Instance) *Instance {
func lookupInstance(x *runtime.Runtime, p *build.Instance) *Instance {
if x, ok := x.BuildData(p); ok {
return x.(*Instance)
}
return nil
}

func getImportFromBuild(x *index, p *build.Instance, v *adt.Vertex) *Instance {
func getImportFromBuild(x *runtime.Runtime, p *build.Instance, v *adt.Vertex) *Instance {
inst := lookupInstance(x, p)

if inst != nil {
Expand All @@ -91,7 +91,7 @@ func getImportFromBuild(x *index, p *build.Instance, v *adt.Vertex) *Instance {
return inst
}

func getImportFromNode(x *index, v *adt.Vertex) *Instance {
func getImportFromNode(x *runtime.Runtime, v *adt.Vertex) *Instance {
p := x.GetInstanceFromNode(v)
if p == nil {
return nil
Expand All @@ -100,7 +100,7 @@ func getImportFromNode(x *index, v *adt.Vertex) *Instance {
return getImportFromBuild(x, p, v)
}

func getImportFromPath(x *index, id string) *Instance {
func getImportFromPath(x *runtime.Runtime, id string) *Instance {
node, _ := x.LoadImport(id)
if node == nil {
return nil
Expand Down Expand Up @@ -135,7 +135,7 @@ func init() {
}

// newInstance creates a new instance. Use Insert to populate the instance.
func newInstance(x *index, p *build.Instance, v *adt.Vertex) *Instance {
func newInstance(x *runtime.Runtime, p *build.Instance, v *adt.Vertex) *Instance {
// TODO: associate root source with structLit.
inst := &Instance{
root: v,
Expand Down Expand Up @@ -316,7 +316,7 @@ func (inst *Instance) Build(p *build.Instance) *Instance {
p.Complete()

idx := inst.index
r := inst.index.Runtime
r := inst.index

rErr := r.ResolveFiles(p)

Expand Down Expand Up @@ -411,7 +411,7 @@ func (inst *Instance) Fill(x interface{}, path ...string) (*Instance, error) {
u.AddConjunct(c)
}
} else {
ctx := eval.NewContext(inst.index.Runtime, nil)
ctx := eval.NewContext(inst.index, nil)
expr := convert.GoValueToExpr(ctx, true, x)
u.AddConjunct(adt.MakeRootConjunct(nil, expr))
u.Finalize(ctx)
Expand Down
2 changes: 1 addition & 1 deletion cue/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (r *Runtime) Marshal(instances ...*Instance) (b []byte, err error) {
return p
}
// TODO: support exporting instance
file, _ := export.Def(r.idx.Runtime, i.inst.ID(), i.root)
file, _ := export.Def(r.idx, i.inst.ID(), i.root)
imports := []string{}
file.VisitImports(func(i *ast.ImportDecl) {
for _, spec := range i.Specs {
Expand Down
2 changes: 1 addition & 1 deletion cue/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (v Value) LookupPath(p Path) Value {

outer:
for _, sel := range p.path {
f := sel.sel.feature(v.idx.Runtime)
f := sel.sel.feature(v.idx)
for _, a := range n.Arcs {
if a.Label == f {
n = a
Expand Down
33 changes: 16 additions & 17 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func unwrapJSONError(err error) errors.Error {
//
type Iterator struct {
val Value
idx *index
idx *runtime.Runtime
ctx *adt.OpContext
arcs []field
p int
Expand Down Expand Up @@ -551,7 +551,7 @@ func (v Value) appendPath(a []string) []string {
}
a = append(a, label)
default:
a = append(a, f.SelectorString(v.idx.Runtime))
a = append(a, f.SelectorString(v.idx))
}
}
return a
Expand All @@ -560,7 +560,7 @@ func (v Value) appendPath(a []string) []string {
// Value holds any value, which may be a Boolean, Error, List, Null, Number,
// Struct, or String.
type Value struct {
idx *index
idx *runtime.Runtime
v *adt.Vertex
}

Expand All @@ -575,7 +575,7 @@ func newErrValue(v Value, b *adt.Bottom) Value {
return makeValue(v.idx, node)
}

func newVertexRoot(idx *index, ctx *adt.OpContext, x *adt.Vertex) Value {
func newVertexRoot(idx *runtime.Runtime, ctx *adt.OpContext, x *adt.Vertex) Value {
if ctx != nil {
// This is indicative of an zero Value. In some cases this is called
// with an error value.
Expand All @@ -586,7 +586,7 @@ func newVertexRoot(idx *index, ctx *adt.OpContext, x *adt.Vertex) Value {
return makeValue(idx, x)
}

func newValueRoot(idx *index, ctx *adt.OpContext, x adt.Expr) Value {
func newValueRoot(idx *runtime.Runtime, ctx *adt.OpContext, x adt.Expr) Value {
if n, ok := x.(*adt.Vertex); ok {
return newVertexRoot(idx, ctx, n)
}
Expand Down Expand Up @@ -629,13 +629,12 @@ func Dereference(v Value) Value {
//
// For internal use only.
func MakeValue(ctx *adt.OpContext, v adt.Value) Value {
runtime := ctx.Impl().(*runtime.Runtime)
index := runtime.Data.(*index)
index := ctx.Impl().(*runtime.Runtime)

return newValueRoot(index, newContext(index), v)
}

func makeValue(idx *index, v *adt.Vertex) Value {
func makeValue(idx *runtime.Runtime, v *adt.Vertex) Value {
if v.Status() == 0 || v.BaseValue == nil {
panic(fmt.Sprintf("not properly initialized (state: %v, value: %T)",
v.Status(), v.BaseValue))
Expand Down Expand Up @@ -969,7 +968,7 @@ You could file a bug with the above information at:
if o.concrete || o.final {
// inst = v.instance()
var expr ast.Expr
expr, err = p.Value(v.idx.Runtime, pkgID, v.v)
expr, err = p.Value(v.idx, pkgID, v.v)
if err != nil {
return bad(`"cuelang.org/go/internal/core/export".Value`, err)
}
Expand All @@ -981,7 +980,7 @@ You could file a bug with the above information at:
}
// return expr
} else {
f, err = p.Def(v.idx.Runtime, pkgID, v.v)
f, err = p.Def(v.idx, pkgID, v.v)
if err != nil {
return bad(`"cuelang.org/go/internal/core/export".Def`, err)
}
Expand Down Expand Up @@ -1491,10 +1490,10 @@ func (v Value) Path() Path {
a[i] = Selector{indexSelector(f)}

case adt.DefinitionLabel, adt.HiddenDefinitionLabel, adt.HiddenLabel:
a[i] = Selector{definitionSelector(f.SelectorString(v.idx.Runtime))}
a[i] = Selector{definitionSelector(f.SelectorString(v.idx))}

case adt.StringLabel:
a[i] = Selector{stringSelector(f.StringValue(v.idx.Runtime))}
a[i] = Selector{stringSelector(f.StringValue(v.idx))}
}
}
return Path{path: a}
Expand Down Expand Up @@ -1824,7 +1823,7 @@ func (v Value) Format(state fmt.State, verb rune) {
case state.Flag('+'):
_, _ = io.WriteString(state, ctx.Str(v.v))
default:
n, _ := export.Raw.Expr(v.idx.Runtime, v.instance().ID(), v.v)
n, _ := export.Raw.Expr(v.idx, v.instance().ID(), v.v)
b, _ := format.Node(n)
_, _ = state.Write(b)
}
Expand Down Expand Up @@ -1852,7 +1851,7 @@ func (v Value) Reference() (inst *Instance, path []string) {
return reference(v.idx, ctx, c.Env, c.Expr())
}

func reference(rt *index, c *adt.OpContext, env *adt.Environment, r adt.Expr) (inst *Instance, path []string) {
func reference(rt *runtime.Runtime, c *adt.OpContext, env *adt.Environment, r adt.Expr) (inst *Instance, path []string) {
ctx := c
defer ctx.PopState(ctx.PushState(env, r.Source()))

Expand Down Expand Up @@ -1900,7 +1899,7 @@ func reference(rt *index, c *adt.OpContext, env *adt.Environment, r adt.Expr) (i
return inst, path
}

func mkPath(ctx *index, a []string, v *adt.Vertex) (inst *Instance, path []string) {
func mkPath(ctx *runtime.Runtime, a []string, v *adt.Vertex) (inst *Instance, path []string) {
if v.Parent == nil {
return getImportFromNode(ctx, v), a
}
Expand Down Expand Up @@ -2390,7 +2389,7 @@ func (v Value) Expr() (Op, []Value) {
a.AddConjunct(adt.MakeRootConjunct(env, n.Val))
b.AddConjunct(adt.MakeRootConjunct(env, disjunct.Val))

ctx := eval.NewContext(v.idx.Runtime, nil)
ctx := eval.NewContext(v.idx, nil)
ctx.Unify(&a, adt.Finalized)
ctx.Unify(&b, adt.Finalized)
if allowed(ctx, v.v, &b) != nil {
Expand Down Expand Up @@ -2434,7 +2433,7 @@ func (v Value) Expr() (Op, []Value) {
a = append(a, remakeValue(v, env, x.X))
// A string selector is quoted.
a = append(a, remakeValue(v, env, &adt.String{
Str: x.Sel.SelectorString(v.idx.Runtime),
Str: x.Sel.SelectorString(v.idx),
}))
op = SelectorOp

Expand Down
3 changes: 0 additions & 3 deletions internal/core/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
type Runtime struct {
index *index

// Data holds the legacy index strut. It is for transitional purposes only.
Data interface{}

loaded map[*build.Instance]interface{}
}

Expand Down

0 comments on commit 52db572

Please sign in to comment.