cuelang.org/go@v0.10.1/internal/core/runtime/go.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  	"reflect"
    19  
    20  	"cuelang.org/go/cue/ast"
    21  	"cuelang.org/go/internal/core/adt"
    22  )
    23  
    24  func (x *Runtime) StoreType(t reflect.Type, src ast.Expr, expr adt.Expr) {
    25  	if expr == nil {
    26  		x.index.StoreType(t, src)
    27  	} else {
    28  		x.index.StoreType(t, expr)
    29  	}
    30  }
    31  
    32  func (x *Runtime) LoadType(t reflect.Type) (src ast.Expr, expr adt.Expr, ok bool) {
    33  	v, ok := x.index.LoadType(t)
    34  	if ok {
    35  		switch x := v.(type) {
    36  		case ast.Expr:
    37  			return x, nil, true
    38  		case adt.Expr:
    39  			src, _ = x.Source().(ast.Expr)
    40  			return src, x, true
    41  		}
    42  	}
    43  	return nil, nil, false
    44  }
    45  
    46  func (x *index) StoreType(t reflect.Type, v interface{}) {
    47  	x.typeCache.Store(t, v)
    48  }
    49  
    50  func (x *index) LoadType(t reflect.Type) (v interface{}, ok bool) {
    51  	v, ok = x.typeCache.Load(t)
    52  	return v, ok
    53  }