github.com/solo-io/cue@v0.4.7/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/solo-io/cue/cue/build"
    19  )
    20  
    21  // A Runtime maintains data structures for indexing and resuse for evaluation.
    22  type Runtime struct {
    23  	index *index
    24  
    25  	loaded map[*build.Instance]interface{}
    26  }
    27  
    28  func (r *Runtime) SetBuildData(b *build.Instance, x interface{}) {
    29  	r.loaded[b] = x
    30  }
    31  
    32  func (r *Runtime) BuildData(b *build.Instance) (x interface{}, ok bool) {
    33  	x, ok = r.loaded[b]
    34  	return x, ok
    35  }
    36  
    37  // New creates a new Runtime. The builtins registered with RegisterBuiltin
    38  // are available for
    39  func New() *Runtime {
    40  	r := &Runtime{}
    41  	r.Init()
    42  	return r
    43  }
    44  
    45  func (r *Runtime) Init() {
    46  	if r.index != nil {
    47  		return
    48  	}
    49  	r.index = sharedIndex
    50  	r.loaded = map[*build.Instance]interface{}{}
    51  }