cuelang.org/go@v0.10.1/cue/cuecontext/cuecontext.go (about) 1 // Copyright 2021 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cuecontext 16 17 import ( 18 "fmt" 19 20 "cuelang.org/go/cue" 21 "cuelang.org/go/internal" 22 "cuelang.org/go/internal/core/runtime" 23 "cuelang.org/go/internal/cuedebug" 24 "cuelang.org/go/internal/envflag" 25 26 _ "cuelang.org/go/pkg" 27 ) 28 29 // Option controls a build context. 30 type Option struct { 31 apply func(r *runtime.Runtime) 32 } 33 34 // defaultFlags defines the debug flags that are set by default. 35 var defaultFlags cuedebug.Config 36 37 func init() { 38 if err := envflag.Parse(&defaultFlags, ""); err != nil { 39 panic(err) 40 } 41 } 42 43 // New creates a new Context. 44 func New(options ...Option) *cue.Context { 45 r := runtime.New() 46 // Ensure default behavior if the flags are not set explicitly. 47 r.SetDebugOptions(&defaultFlags) 48 for _, o := range options { 49 o.apply(r) 50 } 51 return (*cue.Context)(r) 52 } 53 54 // An ExternInterpreter creates a compiler that can produce implementations of 55 // functions written in a language other than CUE. It is currently for internal 56 // use only. 57 type ExternInterpreter = runtime.Interpreter 58 59 // Interpreter associates an interpreter for external code with this context. 60 func Interpreter(i ExternInterpreter) Option { 61 return Option{func(r *runtime.Runtime) { 62 r.SetInterpreter(i) 63 }} 64 } 65 66 type EvalVersion = internal.EvaluatorVersion 67 68 const ( 69 // EvalDefault is the latest stable version of the evaluator. 70 EvalDefault EvalVersion = EvalV2 71 72 // EvalExperiment refers to the latest unstable version of the evaluator. 73 // Note that this version may change without notice. 74 EvalExperiment EvalVersion = EvalV3 75 76 // EvalV2 is the currently latest stable version of the evaluator. 77 // It was introduced in CUE version 0.3 and is being maintained until 2024. 78 EvalV2 EvalVersion = internal.DefaultVersion 79 80 // EvalV3 is the currently experimental version of the evaluator. 81 // It was introduced in 2024 and brought a new disjunction algorithm, 82 // a new closedness algorithm, a new core scheduler, and adds performance 83 // enhancements like structure sharing. 84 EvalV3 EvalVersion = internal.DevVersion 85 ) 86 87 // EvaluatorVersion indicates which version of the evaluator to use. Currently 88 // only experimental versions can be selected as an alternative. 89 func EvaluatorVersion(v EvalVersion) Option { 90 return Option{func(r *runtime.Runtime) { 91 r.SetVersion(v) 92 }} 93 } 94 95 // CUE_DEBUG takes a string with the same contents as CUE_DEBUG and configures 96 // the context with the relevant debug options. It panics for unknown or 97 // malformed options. 98 func CUE_DEBUG(s string) Option { 99 var c cuedebug.Config 100 if err := envflag.Parse(&c, s); err != nil { 101 panic(fmt.Errorf("cuecontext.CUE_DEBUG: %v", err)) 102 } 103 104 return Option{func(r *runtime.Runtime) { 105 r.SetDebugOptions(&c) 106 }} 107 }