Skip to content

Commit 52db572

Browse files
committed
cue: get rid of internal index type
Change-Id: I0ce4bb9ab8263a1a8a2774ce3cd1ff9de3bbca46 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9365 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent af509c6 commit 52db572

File tree

10 files changed

+44
-59
lines changed

10 files changed

+44
-59
lines changed

cue/build.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
//
3232
// The zero value of a Runtime is ready to use.
3333
type Runtime struct {
34-
idx *index
34+
idx *runtime.Runtime
3535
}
3636

3737
func init() {
@@ -59,15 +59,15 @@ func init() {
5959

6060
internal.CoreValue = func(value interface{}) (runtime, vertex interface{}) {
6161
if v, ok := value.(Value); ok && v.v != nil {
62-
return v.idx.Runtime, v.v
62+
return v.idx, v.v
6363
}
6464
return nil, nil
6565
}
6666
}
6767

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

70-
func (r *Runtime) index() *index {
70+
func (r *Runtime) index() *runtime.Runtime {
7171
if r.idx == nil {
7272
r.idx = newIndex()
7373
}
@@ -184,35 +184,22 @@ func (r *Runtime) FromExpr(expr ast.Expr) (*Instance, error) {
184184
})
185185
}
186186

187-
// index maps conversions from label names to internal codes.
188-
//
189-
// All instances belonging to the same package should share this index.
190-
type index struct {
191-
*runtime.Runtime
192-
}
193-
194187
// NewRuntime creates a *runtime.Runtime with builtins preloaded.
195188
func NewRuntime() *runtime.Runtime {
196189
i := newIndex()
197-
i.Runtime.Data = i
198-
return i.Runtime
190+
return i
199191
}
200192

201193
// newIndex creates a new index.
202-
func newIndex() *index {
203-
r := runtime.New()
204-
i := &index{
205-
Runtime: r,
206-
}
207-
r.Data = i
208-
return i
194+
func newIndex() *runtime.Runtime {
195+
return runtime.New()
209196
}
210197

211198
func isBuiltin(s string) bool {
212199
return runtime.SharedRuntime.IsBuiltinPackage(s)
213200
}
214201

215-
func loadInstance(idx *index, p *build.Instance) *Instance {
216-
v, _ := idx.Runtime.Build(p)
202+
func loadInstance(idx *runtime.Runtime, p *build.Instance) *Instance {
203+
v, _ := idx.Build(p)
217204
return getImportFromBuild(idx, p, v)
218205
}

cue/build_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func TestBuild(t *testing.T) {
200200
got = err.Error()
201201
} else {
202202
cfg := &debug.Config{Compact: true}
203-
got = debug.NodeString(insts[0].index.Runtime, insts[0].Value().v, cfg)
203+
got = debug.NodeString(insts[0].index, insts[0].Value().v, cfg)
204204
}
205205
if got != tc.emit {
206206
t.Errorf("\n got: %s\nwant: %s", got, tc.emit)

cue/context.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import (
1818
"cuelang.org/go/internal/core/adt"
1919
"cuelang.org/go/internal/core/debug"
2020
"cuelang.org/go/internal/core/eval"
21+
"cuelang.org/go/internal/core/runtime"
2122
)
2223

2324
// newContext returns a new evaluation context.
24-
func newContext(idx *index) *adt.OpContext {
25+
func newContext(idx *runtime.Runtime) *adt.OpContext {
2526
if idx == nil {
2627
return nil
2728
}
28-
return eval.NewContext(idx.Runtime, nil)
29+
return eval.NewContext(idx, nil)
2930
}
3031

3132
func debugStr(ctx *adt.OpContext, v adt.Node) string {

cue/errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"cuelang.org/go/cue/errors"
1919
"cuelang.org/go/cue/token"
2020
"cuelang.org/go/internal/core/adt"
21+
"cuelang.org/go/internal/core/runtime"
2122
)
2223

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

97-
func mkErr(idx *index, src adt.Node, args ...interface{}) *adt.Bottom {
98+
func mkErr(idx *runtime.Runtime, src adt.Node, args ...interface{}) *adt.Bottom {
9899
var e *adt.Bottom
99100
var code adt.ErrorCode = -1
100101
outer:

cue/go.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ import (
2424
func init() {
2525
internal.FromGoValue = func(runtime, x interface{}, nilIsTop bool) interface{} {
2626
r := runtime.(*Runtime)
27-
ctx := eval.NewContext(r.index().Runtime, nil)
27+
ctx := eval.NewContext(r.index(), nil)
2828
v := convert.GoValueToValue(ctx, x, nilIsTop)
2929
n := adt.ToVertex(v)
3030
return Value{r.idx, n}
3131
}
3232

3333
internal.FromGoType = func(runtime, x interface{}) interface{} {
3434
r := runtime.(*Runtime)
35-
ctx := eval.NewContext(r.index().Runtime, nil)
35+
ctx := eval.NewContext(r.index(), nil)
3636
expr, err := convert.GoTypeToExpr(ctx, x)
3737
if err != nil {
3838
expr = &adt.Bottom{Err: err}

cue/instance.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ import (
2323
"cuelang.org/go/internal/core/compile"
2424
"cuelang.org/go/internal/core/convert"
2525
"cuelang.org/go/internal/core/eval"
26+
"cuelang.org/go/internal/core/runtime"
2627
)
2728

2829
// An Instance defines a single configuration based on a collection of
2930
// underlying CUE files.
3031
type Instance struct {
31-
index *index
32+
index *runtime.Runtime
3233

3334
root *adt.Vertex
3435

@@ -45,28 +46,27 @@ type Instance struct {
4546
// complete bool // for cycle detection
4647
}
4748

48-
func addInst(x *index, p *Instance) *Instance {
49+
func addInst(x *runtime.Runtime, p *Instance) *Instance {
4950
if p.inst == nil {
5051
p.inst = &build.Instance{
5152
ImportPath: p.ImportPath,
5253
PkgName: p.PkgName,
5354
}
5455
}
55-
// fmt.Println(p.ImportPath, "XXX")
5656
x.AddInst(p.ImportPath, p.root, p.inst)
5757
x.SetBuildData(p.inst, p)
5858
p.index = x
5959
return p
6060
}
6161

62-
func lookupInstance(x *index, p *build.Instance) *Instance {
62+
func lookupInstance(x *runtime.Runtime, p *build.Instance) *Instance {
6363
if x, ok := x.BuildData(p); ok {
6464
return x.(*Instance)
6565
}
6666
return nil
6767
}
6868

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

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

94-
func getImportFromNode(x *index, v *adt.Vertex) *Instance {
94+
func getImportFromNode(x *runtime.Runtime, v *adt.Vertex) *Instance {
9595
p := x.GetInstanceFromNode(v)
9696
if p == nil {
9797
return nil
@@ -100,7 +100,7 @@ func getImportFromNode(x *index, v *adt.Vertex) *Instance {
100100
return getImportFromBuild(x, p, v)
101101
}
102102

103-
func getImportFromPath(x *index, id string) *Instance {
103+
func getImportFromPath(x *runtime.Runtime, id string) *Instance {
104104
node, _ := x.LoadImport(id)
105105
if node == nil {
106106
return nil
@@ -135,7 +135,7 @@ func init() {
135135
}
136136

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

318318
idx := inst.index
319-
r := inst.index.Runtime
319+
r := inst.index
320320

321321
rErr := r.ResolveFiles(p)
322322

@@ -411,7 +411,7 @@ func (inst *Instance) Fill(x interface{}, path ...string) (*Instance, error) {
411411
u.AddConjunct(c)
412412
}
413413
} else {
414-
ctx := eval.NewContext(inst.index.Runtime, nil)
414+
ctx := eval.NewContext(inst.index, nil)
415415
expr := convert.GoValueToExpr(ctx, true, x)
416416
u.AddConjunct(adt.MakeRootConjunct(nil, expr))
417417
u.Finalize(ctx)

cue/marshal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (r *Runtime) Marshal(instances ...*Instance) (b []byte, err error) {
140140
return p
141141
}
142142
// TODO: support exporting instance
143-
file, _ := export.Def(r.idx.Runtime, i.inst.ID(), i.root)
143+
file, _ := export.Def(r.idx, i.inst.ID(), i.root)
144144
imports := []string{}
145145
file.VisitImports(func(i *ast.ImportDecl) {
146146
for _, spec := range i.Specs {

cue/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (v Value) LookupPath(p Path) Value {
6363

6464
outer:
6565
for _, sel := range p.path {
66-
f := sel.sel.feature(v.idx.Runtime)
66+
f := sel.sel.feature(v.idx)
6767
for _, a := range n.Arcs {
6868
if a.Label == f {
6969
n = a

cue/types.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func unwrapJSONError(err error) errors.Error {
213213
//
214214
type Iterator struct {
215215
val Value
216-
idx *index
216+
idx *runtime.Runtime
217217
ctx *adt.OpContext
218218
arcs []field
219219
p int
@@ -551,7 +551,7 @@ func (v Value) appendPath(a []string) []string {
551551
}
552552
a = append(a, label)
553553
default:
554-
a = append(a, f.SelectorString(v.idx.Runtime))
554+
a = append(a, f.SelectorString(v.idx))
555555
}
556556
}
557557
return a
@@ -560,7 +560,7 @@ func (v Value) appendPath(a []string) []string {
560560
// Value holds any value, which may be a Boolean, Error, List, Null, Number,
561561
// Struct, or String.
562562
type Value struct {
563-
idx *index
563+
idx *runtime.Runtime
564564
v *adt.Vertex
565565
}
566566

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

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

589-
func newValueRoot(idx *index, ctx *adt.OpContext, x adt.Expr) Value {
589+
func newValueRoot(idx *runtime.Runtime, ctx *adt.OpContext, x adt.Expr) Value {
590590
if n, ok := x.(*adt.Vertex); ok {
591591
return newVertexRoot(idx, ctx, n)
592592
}
@@ -629,13 +629,12 @@ func Dereference(v Value) Value {
629629
//
630630
// For internal use only.
631631
func MakeValue(ctx *adt.OpContext, v adt.Value) Value {
632-
runtime := ctx.Impl().(*runtime.Runtime)
633-
index := runtime.Data.(*index)
632+
index := ctx.Impl().(*runtime.Runtime)
634633

635634
return newValueRoot(index, newContext(index), v)
636635
}
637636

638-
func makeValue(idx *index, v *adt.Vertex) Value {
637+
func makeValue(idx *runtime.Runtime, v *adt.Vertex) Value {
639638
if v.Status() == 0 || v.BaseValue == nil {
640639
panic(fmt.Sprintf("not properly initialized (state: %v, value: %T)",
641640
v.Status(), v.BaseValue))
@@ -969,7 +968,7 @@ You could file a bug with the above information at:
969968
if o.concrete || o.final {
970969
// inst = v.instance()
971970
var expr ast.Expr
972-
expr, err = p.Value(v.idx.Runtime, pkgID, v.v)
971+
expr, err = p.Value(v.idx, pkgID, v.v)
973972
if err != nil {
974973
return bad(`"cuelang.org/go/internal/core/export".Value`, err)
975974
}
@@ -981,7 +980,7 @@ You could file a bug with the above information at:
981980
}
982981
// return expr
983982
} else {
984-
f, err = p.Def(v.idx.Runtime, pkgID, v.v)
983+
f, err = p.Def(v.idx, pkgID, v.v)
985984
if err != nil {
986985
return bad(`"cuelang.org/go/internal/core/export".Def`, err)
987986
}
@@ -1491,10 +1490,10 @@ func (v Value) Path() Path {
14911490
a[i] = Selector{indexSelector(f)}
14921491

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

14961495
case adt.StringLabel:
1497-
a[i] = Selector{stringSelector(f.StringValue(v.idx.Runtime))}
1496+
a[i] = Selector{stringSelector(f.StringValue(v.idx))}
14981497
}
14991498
}
15001499
return Path{path: a}
@@ -1824,7 +1823,7 @@ func (v Value) Format(state fmt.State, verb rune) {
18241823
case state.Flag('+'):
18251824
_, _ = io.WriteString(state, ctx.Str(v.v))
18261825
default:
1827-
n, _ := export.Raw.Expr(v.idx.Runtime, v.instance().ID(), v.v)
1826+
n, _ := export.Raw.Expr(v.idx, v.instance().ID(), v.v)
18281827
b, _ := format.Node(n)
18291828
_, _ = state.Write(b)
18301829
}
@@ -1852,7 +1851,7 @@ func (v Value) Reference() (inst *Instance, path []string) {
18521851
return reference(v.idx, ctx, c.Env, c.Expr())
18531852
}
18541853

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

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

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

2393-
ctx := eval.NewContext(v.idx.Runtime, nil)
2392+
ctx := eval.NewContext(v.idx, nil)
23942393
ctx.Unify(&a, adt.Finalized)
23952394
ctx.Unify(&b, adt.Finalized)
23962395
if allowed(ctx, v.v, &b) != nil {
@@ -2434,7 +2433,7 @@ func (v Value) Expr() (Op, []Value) {
24342433
a = append(a, remakeValue(v, env, x.X))
24352434
// A string selector is quoted.
24362435
a = append(a, remakeValue(v, env, &adt.String{
2437-
Str: x.Sel.SelectorString(v.idx.Runtime),
2436+
Str: x.Sel.SelectorString(v.idx),
24382437
}))
24392438
op = SelectorOp
24402439

internal/core/runtime/runtime.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ import (
2222
type Runtime struct {
2323
index *index
2424

25-
// Data holds the legacy index strut. It is for transitional purposes only.
26-
Data interface{}
27-
2825
loaded map[*build.Instance]interface{}
2926
}
3027

0 commit comments

Comments
 (0)