diff --git a/cue/cuecontext/cuecontext_test.go b/cue/cuecontext/cuecontext_test.go index 144e34d3e5d..6fd85a70409 100644 --- a/cue/cuecontext/cuecontext_test.go +++ b/cue/cuecontext/cuecontext_test.go @@ -21,6 +21,7 @@ import ( "cuelang.org/go/cue" "cuelang.org/go/cue/ast" "cuelang.org/go/internal" + "cuelang.org/go/internal/core/adt" "cuelang.org/go/internal/core/runtime" "cuelang.org/go/internal/cueexperiment" ) @@ -82,7 +83,8 @@ func TestEvalVersion(t *testing.T) { defer func() { cueexperiment.Flags.EvalV3 = saved }() test := func(c *cue.Context, want internal.EvaluatorVersion) { - got, _ := (*runtime.Runtime)(c).Settings() + opCtx := adt.NewContext((*runtime.Runtime)(c), nil) + got := opCtx.Version if got != want { t.Errorf("got %v; want %v", got, want) } diff --git a/internal/core/adt/context.go b/internal/core/adt/context.go index f9e1e132f4e..9be16e766df 100644 --- a/internal/core/adt/context.go +++ b/internal/core/adt/context.go @@ -167,7 +167,9 @@ type Runtime interface { // type if available. LoadType(t reflect.Type) (src ast.Expr, expr Expr, ok bool) - Settings() (internal.EvaluatorVersion, cuedebug.Config) + // ConfigureOpCtx configures the [*OpContext] with details such as + // evaluator version, debug options etc. + ConfigureOpCtx(ctx *OpContext) } type Config struct { @@ -180,16 +182,15 @@ func New(v *Vertex, cfg *Config) *OpContext { if cfg.Runtime == nil { panic("nil Runtime") } - version, flags := cfg.Runtime.Settings() + ctx := &OpContext{ Runtime: cfg.Runtime, Format: cfg.Format, vertex: v, - Version: version, - stats: stats.Counts{EvalVersion: version}, - Config: flags, taskContext: schedConfig, } + cfg.Runtime.ConfigureOpCtx(ctx) + ctx.stats.EvalVersion = ctx.Version if v != nil { ctx.e = &Environment{Up: nil, Vertex: v} } @@ -213,7 +214,8 @@ type OpContext struct { Format func(Runtime, Node) string cuedebug.Config - Version internal.EvaluatorVersion // Copied from Runtime + Version internal.EvaluatorVersion // Copied from Runtime + TopoSort bool // Copied from Runtime taskContext diff --git a/internal/core/eval/eval.go b/internal/core/eval/eval.go index dc0f9bf4004..6c486d125d0 100644 --- a/internal/core/eval/eval.go +++ b/internal/core/eval/eval.go @@ -30,8 +30,6 @@ func Evaluate(r adt.Runtime, v *adt.Vertex) { func New(r adt.Runtime) *Unifier { ctx := NewContext(r, nil) - // TODO: we could access these directly if we can use runtime.Runtime directly. - ctx.Version, ctx.Config = r.Settings() return &Unifier{r: r, e: ctx} } diff --git a/internal/core/export/toposort.go b/internal/core/export/toposort.go index 78a66b33ed3..9550a946f9f 100644 --- a/internal/core/export/toposort.go +++ b/internal/core/export/toposort.go @@ -20,7 +20,6 @@ import ( "cuelang.org/go/internal/core/adt" "cuelang.org/go/internal/core/toposort" - "cuelang.org/go/internal/cueexperiment" ) // TODO: topological sort should go arguably in a more fundamental place as it @@ -30,7 +29,7 @@ import ( // features than for which there are arcs and also includes features for // optional fields. It assumes the Structs fields are initialized and evaluated. func VertexFeatures(c *adt.OpContext, v *adt.Vertex) []adt.Feature { - if cueexperiment.Flags.TopoSort { + if c.TopoSort { return toposort.VertexFeatures(c, v) } else { return vertexFeatures(c, v) diff --git a/internal/core/runtime/runtime.go b/internal/core/runtime/runtime.go index 43dcbefbae4..3724a027ef4 100644 --- a/internal/core/runtime/runtime.go +++ b/internal/core/runtime/runtime.go @@ -17,6 +17,7 @@ package runtime import ( "cuelang.org/go/cue/build" "cuelang.org/go/internal" + "cuelang.org/go/internal/core/adt" "cuelang.org/go/internal/cuedebug" "cuelang.org/go/internal/cueexperiment" ) @@ -31,7 +32,8 @@ type Runtime struct { // the kind in a file-level @extern(kind) attribute. interpreters map[string]Interpreter - version internal.EvaluatorVersion + version internal.EvaluatorVersion + topoSort bool flags cuedebug.Config } @@ -40,6 +42,12 @@ func (r *Runtime) Settings() (internal.EvaluatorVersion, cuedebug.Config) { return r.version, r.flags } +func (r *Runtime) ConfigureOpCtx(ctx *adt.OpContext) { + ctx.Version = r.version + ctx.TopoSort = r.topoSort + ctx.Config = r.flags +} + func (r *Runtime) SetBuildData(b *build.Instance, x interface{}) { r.loaded[b] = x } @@ -73,6 +81,12 @@ func (r *Runtime) SetVersion(v internal.EvaluatorVersion) { r.version = v } +// SetTopologicalSort sets whether or not to use topological sorting +// for the Runtime. +func (r *Runtime) SetTopologicalSort(b bool) { + r.topoSort = b +} + // SetDebugOptions sets the debug flags to use for the Runtime. This should only // be set before first use. func (r *Runtime) SetDebugOptions(flags *cuedebug.Config) { @@ -103,4 +117,5 @@ func (r *Runtime) Init() { } else { r.version = internal.DefaultVersion } + r.topoSort = cueexperiment.Flags.TopoSort } diff --git a/internal/core/toposort/sort_test.go b/internal/core/toposort/sort_test.go index 2bbf7c9690c..c82c25696a7 100644 --- a/internal/core/toposort/sort_test.go +++ b/internal/core/toposort/sort_test.go @@ -21,18 +21,11 @@ import ( "cuelang.org/go/cue/format" "cuelang.org/go/internal/core/eval" "cuelang.org/go/internal/core/export" - "cuelang.org/go/internal/cueexperiment" "cuelang.org/go/internal/cuetdtest" "cuelang.org/go/internal/cuetxtar" ) func TestTopologicalSort(t *testing.T) { - cueexperiment.Init() - saved := cueexperiment.Flags.TopoSort - defer func() { cueexperiment.Flags.TopoSort = saved }() - - cueexperiment.Flags.TopoSort = true - test := cuetxtar.TxTarTest{ Root: "testdata", Name: "toposort", @@ -41,6 +34,7 @@ func TestTopologicalSort(t *testing.T) { test.Run(t, func(t *cuetxtar.Test) { run := t.Runtime() + run.SetTopologicalSort(true) inst := t.Instance() v, err := run.Build(nil, inst)