github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/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  	"github.com/joomcode/cue/cue/build"
    19  	"github.com/joomcode/cue/cue/registry"
    20  )
    21  
    22  // A Runtime maintains data structures for indexing and resuse for evaluation.
    23  type Runtime struct {
    24  	index *index
    25  
    26  	loaded map[*build.Instance]interface{}
    27  	Registry *registry.Registry
    28  }
    29  
    30  func (r *Runtime) SetBuildData(b *build.Instance, x interface{}) {
    31  	r.loaded[b] = x
    32  }
    33  
    34  func (r *Runtime) BuildData(b *build.Instance) (x interface{}, ok bool) {
    35  	x, ok = r.loaded[b]
    36  	return x, ok
    37  }
    38  
    39  // New creates a new Runtime. The builtins registered with RegisterBuiltin
    40  // are available for
    41  func New() *Runtime {
    42  	r := &Runtime{}
    43  	r.Init()
    44  	return r
    45  }
    46  
    47  func (r *Runtime) Init() {
    48  	if r.index != nil {
    49  		return
    50  	}
    51  	r.index = newIndex()
    52  
    53  	// TODO: the builtin-specific instances will ultimately also not be
    54  	// shared by indexes.
    55  	r.index.builtinPaths = sharedIndex.builtinPaths
    56  	r.index.builtinShort = sharedIndex.builtinShort
    57  
    58  	r.loaded = map[*build.Instance]interface{}{}
    59  }