Skip to content

Commit

Permalink
cmd/cue/cmd: expose evalv3 experiment
Browse files Browse the repository at this point in the history
Right now this is only done through a global variable
for the cue command line. It is not possible to set this
through the API.

Note: do not close this issue until we allow exposing the
experiment everywhere in the API.

Issue #2886

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I445485d905a21831ae1395a9d3dc78c19365a11f
  • Loading branch information
mpvl committed Apr 10, 2024
1 parent 6339558 commit 36cf31d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/cue/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import (
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/stats"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
cueruntime "cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/cuedebug"
"cuelang.org/go/internal/cueexperiment"
"cuelang.org/go/internal/encoding"
Expand Down Expand Up @@ -96,6 +98,13 @@ func mkRunE(c *Command, f runFunction) func(*cobra.Command, []string) error {
// We don't want that work to count towards $CUE_STATS.
adt.ResetStats()

// TODO: do not rely on a global variable here, as this API is also used
// in a non-tooling context.
if cueexperiment.Flags.EvalV3 {
const dev = internal.DevVersion
(*cueruntime.Runtime)(c.ctx).SetVersion(internal.EvaluatorVersion(dev))
}

err := f(c, args)

if statsEnc != nil {
Expand Down
7 changes: 7 additions & 0 deletions cmd/cue/cmd/testdata/script/cmd_textproto.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ cmp stdout out/foo.textproto
! exec cue eval -d '#X' schema.cue foo.textproto -l c
cmp stderr out/stderr3

# This asserts the compatibility of Vertex.MatchAndInsert, which is only used
# outside of the core evaluator.
env CUE_EXPERIMENT=evalv3

exec cue eval topschema.cue foo.textproto
cmp stdout out/topfoo.textproto

-- topschema.cue --
a: int
b: [...int]
Expand Down
13 changes: 13 additions & 0 deletions cmd/cue/cmd/testdata/script/dev.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
env CUE_EXPERIMENT=evalv3
exec cue eval
cmp stdout out

-- test.cue --
package foo

// This only works in the new evaluator as of this writing.
y: 1 | {y: 1}
y

-- out --
y: 1
6 changes: 6 additions & 0 deletions cue/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ func (c *Context) CompileBytes(b []byte, options ...BuildOption) Value {

func (c *Context) make(v *adt.Vertex) Value {
opCtx := newContext(c.runtime())
// TODO: this is currently needed to ensure that node is properly recognized
// as evaluated. Not dereferencing nodes, however, will have the benefit of
// retaining more information. Remove the indirection when the code will be
// able to properly handle this.
v.Finalize(opCtx)
v = v.Indirect()
x := newValueRoot(c.runtime(), opCtx, v)
adt.AddStats(opCtx)
return x
Expand Down
6 changes: 6 additions & 0 deletions internal/core/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func NewVersioned(v internal.EvaluatorVersion) *Runtime {
return r
}

// SetVersion sets the version to use for the Runtime. This should only be set
// before first use.
func (r *Runtime) SetVersion(v internal.EvaluatorVersion) {
r.version = v
}

// IsInitialized reports whether the runtime has been initialized.
func (r *Runtime) IsInitialized() bool {
return r.index != nil
Expand Down
1 change: 1 addition & 0 deletions internal/cueexperiment/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
// by Init.
var Flags struct {
Modules bool
EvalV3 bool
}

// Init initializes Flags. Note: this isn't named "init" because we
Expand Down

0 comments on commit 36cf31d

Please sign in to comment.