cuelang.org/go@v0.10.1/internal/core/runtime/runtime.go (about)

     1  // Copyright 2020 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 runtime
    16  
    17  import (
    18  	"cuelang.org/go/cue/build"
    19  	"cuelang.org/go/internal"
    20  	"cuelang.org/go/internal/cuedebug"
    21  )
    22  
    23  // A Runtime maintains data structures for indexing and reuse for evaluation.
    24  type Runtime struct {
    25  	index *index
    26  
    27  	loaded map[*build.Instance]interface{}
    28  
    29  	// interpreters implement extern functionality. The map key corresponds to
    30  	// the kind in a file-level @extern(kind) attribute.
    31  	interpreters map[string]Interpreter
    32  
    33  	version internal.EvaluatorVersion
    34  
    35  	flags cuedebug.Config
    36  }
    37  
    38  func (r *Runtime) Settings() (internal.EvaluatorVersion, cuedebug.Config) {
    39  	return r.version, r.flags
    40  }
    41  
    42  func (r *Runtime) SetBuildData(b *build.Instance, x interface{}) {
    43  	r.loaded[b] = x
    44  }
    45  
    46  func (r *Runtime) BuildData(b *build.Instance) (x interface{}, ok bool) {
    47  	x, ok = r.loaded[b]
    48  	return x, ok
    49  }
    50  
    51  // New is a wrapper for NewVersioned(internal.DefaultVersion).
    52  func New() *Runtime {
    53  	r := &Runtime{}
    54  	r.Init()
    55  	return r
    56  }
    57  
    58  // NewWithSettings creates a new Runtime using the given runtime version and
    59  // debug flags. The builtins registered with RegisterBuiltin are available for
    60  // evaluation.
    61  func NewWithSettings(v internal.EvaluatorVersion, flags cuedebug.Config) *Runtime {
    62  	r := &Runtime{version: v, flags: flags}
    63  	r.Init()
    64  	return r
    65  }
    66  
    67  // SetVersion sets the version to use for the Runtime. This should only be set
    68  // before first use.
    69  func (r *Runtime) SetVersion(v internal.EvaluatorVersion) {
    70  	r.version = v
    71  }
    72  
    73  // SetDebugOptions sets the debug flags to use for the Runtime. This should only
    74  // be set before first use.
    75  func (r *Runtime) SetDebugOptions(flags *cuedebug.Config) {
    76  	r.flags = *flags
    77  }
    78  
    79  // IsInitialized reports whether the runtime has been initialized.
    80  func (r *Runtime) IsInitialized() bool {
    81  	return r.index != nil
    82  }
    83  
    84  func (r *Runtime) Init() {
    85  	if r.index != nil {
    86  		return
    87  	}
    88  	r.index = newIndex()
    89  
    90  	// TODO: the builtin-specific instances will ultimately also not be
    91  	// shared by indexes.
    92  	r.index.builtinPaths = sharedIndex.builtinPaths
    93  	r.index.builtinShort = sharedIndex.builtinShort
    94  
    95  	r.loaded = map[*build.Instance]interface{}{}
    96  }