...

Source file src/cuelang.org/go/internal/core/runtime/runtime.go

Documentation: cuelang.org/go/internal/core/runtime

     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  )
    21  
    22  // A Runtime maintains data structures for indexing and reuse for evaluation.
    23  type Runtime struct {
    24  	index *index
    25  
    26  	loaded map[*build.Instance]interface{}
    27  
    28  	// interpreters implement extern functionality. The map key corresponds to
    29  	// the kind in a file-level @extern(kind) attribute.
    30  	interpreters map[string]Interpreter
    31  
    32  	version internal.EvaluatorVersion
    33  }
    34  
    35  func (r *Runtime) EvaluatorVersion() internal.EvaluatorVersion {
    36  	return r.version
    37  }
    38  
    39  func (r *Runtime) SetBuildData(b *build.Instance, x interface{}) {
    40  	r.loaded[b] = x
    41  }
    42  
    43  func (r *Runtime) BuildData(b *build.Instance) (x interface{}, ok bool) {
    44  	x, ok = r.loaded[b]
    45  	return x, ok
    46  }
    47  
    48  // New is a wrapper for NewVersioned(internal.DefaultVersion).
    49  func New() *Runtime {
    50  	r := &Runtime{}
    51  	r.Init()
    52  	return r
    53  }
    54  
    55  // NewVersioned creates a new Runtime using the given runtime version.
    56  // The builtins registered with RegisterBuiltin are available for evaluation.
    57  func NewVersioned(v internal.EvaluatorVersion) *Runtime {
    58  	r := &Runtime{version: v}
    59  	r.Init()
    60  	return r
    61  }
    62  
    63  // IsInitialized reports whether the runtime has been initialized.
    64  func (r *Runtime) IsInitialized() bool {
    65  	return r.index != nil
    66  }
    67  
    68  func (r *Runtime) Init() {
    69  	if r.index != nil {
    70  		return
    71  	}
    72  	r.index = newIndex()
    73  
    74  	// TODO: the builtin-specific instances will ultimately also not be
    75  	// shared by indexes.
    76  	r.index.builtinPaths = sharedIndex.builtinPaths
    77  	r.index.builtinShort = sharedIndex.builtinShort
    78  
    79  	r.loaded = map[*build.Instance]interface{}{}
    80  }
    81  

View as plain text