diff --git a/cue/build.go b/cue/build.go index 02ef7a663..61f9f8118 100644 --- a/cue/build.go +++ b/cue/build.go @@ -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() { @@ -59,7 +59,7 @@ 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 } @@ -67,7 +67,7 @@ func init() { 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() } @@ -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) } diff --git a/cue/build_test.go b/cue/build_test.go index ccaeca8f1..85de8c8fb 100644 --- a/cue/build_test.go +++ b/cue/build_test.go @@ -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) diff --git a/cue/context.go b/cue/context.go index df444a16c..6122fc4b0 100644 --- a/cue/context.go +++ b/cue/context.go @@ -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 { diff --git a/cue/errors.go b/cue/errors.go index 94c4b29da..f1a803016 100644 --- a/cue/errors.go +++ b/cue/errors.go @@ -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) { @@ -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: diff --git a/cue/go.go b/cue/go.go index 1d6717c8c..c0d5a6396 100644 --- a/cue/go.go +++ b/cue/go.go @@ -24,7 +24,7 @@ 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} @@ -32,7 +32,7 @@ func init() { 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} diff --git a/cue/instance.go b/cue/instance.go index 907e8f502..cae1f9aee 100644 --- a/cue/instance.go +++ b/cue/instance.go @@ -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 @@ -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 { @@ -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 @@ -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 @@ -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, @@ -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) @@ -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) diff --git a/cue/marshal.go b/cue/marshal.go index 47e319b79..f4f0c6bc5 100644 --- a/cue/marshal.go +++ b/cue/marshal.go @@ -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 { diff --git a/cue/query.go b/cue/query.go index d2829b6e7..a4cec1fd9 100644 --- a/cue/query.go +++ b/cue/query.go @@ -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 diff --git a/cue/types.go b/cue/types.go index e2a5b1845..1d1d84401 100644 --- a/cue/types.go +++ b/cue/types.go @@ -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 @@ -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 @@ -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 } @@ -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. @@ -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) } @@ -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)) @@ -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) } @@ -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) } @@ -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} @@ -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) } @@ -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())) @@ -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 } @@ -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 { @@ -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 diff --git a/internal/core/runtime/runtime.go b/internal/core/runtime/runtime.go index cc67bce75..0f303be72 100644 --- a/internal/core/runtime/runtime.go +++ b/internal/core/runtime/runtime.go @@ -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{} }