cue: maintain type cache per index (runtime)

Not doing so results in crashes in upcoming
Go codec implementation. In general this
seems like a good idea.

Note that it may be better to do no caching at all,
and leave it up to the go codec to cache. But for
now we leave it until cuego is deprecated.

Also hide Runtime.Context.

Change-Id: I5179bc974503e78c8ae02f083e4b42a5ffb2cd39
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2709
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/build.go b/cue/build.go
index f0fb2a5..4285d63 100644
--- a/cue/build.go
+++ b/cue/build.go
@@ -20,6 +20,7 @@
 	"encoding/gob"
 	"path"
 	"strconv"
+	"sync"
 
 	"cuelang.org/go/cue/ast"
 	"cuelang.org/go/cue/build"
@@ -33,8 +34,8 @@
 // Any operation that involves two Values or Instances should originate from
 // the same Runtime.
 type Runtime struct {
-	Context *build.Context // TODO: remove
-	idx     *index
+	ctx *build.Context // TODO: remove
+	idx *index
 }
 
 func dummyLoad(token.Pos, string) *build.Instance { return nil }
@@ -131,7 +132,7 @@
 // name in position information. The source may import builtin packages. Use
 // Build to allow importing non-builtin packages.
 func (r *Runtime) Compile(filename string, source interface{}) (*Instance, error) {
-	ctx := r.Context
+	ctx := r.ctx
 	if ctx == nil {
 		ctx = build.NewContext()
 	}
@@ -145,7 +146,7 @@
 // CompileFile compiles the given source file into an Instance. The source may
 // import builtin packages. Use Build to allow importing non-builtin packages.
 func (r *Runtime) CompileFile(file *ast.File) (*Instance, error) {
-	ctx := r.Context
+	ctx := r.ctx
 	if ctx == nil {
 		ctx = build.NewContext()
 	}
@@ -164,7 +165,7 @@
 // may import builtin packages. Use Build to allow importing non-builtin
 // packages.
 func (r *Runtime) CompileExpr(expr ast.Expr) (*Instance, error) {
-	ctx := r.Context
+	ctx := r.ctx
 	if ctx == nil {
 		ctx = build.NewContext()
 	}
@@ -250,6 +251,9 @@
 	offset label
 	parent *index
 	freeze bool
+
+	mutex     sync.Mutex
+	typeCache sync.Map // map[reflect.Type]evaluated
 }
 
 const sharedOffset = 0x40000000
diff --git a/cue/go.go b/cue/go.go
index e9435c3..5235f16 100644
--- a/cue/go.go
+++ b/cue/go.go
@@ -22,7 +22,6 @@
 	"reflect"
 	"sort"
 	"strings"
-	"sync"
 
 	"cuelang.org/go/cue/ast"
 	"cuelang.org/go/cue/parser"
@@ -381,16 +380,11 @@
 	return n
 }
 
-var (
-	typeCache sync.Map // map[reflect.Type]evaluated
-	mutex     sync.Mutex
-)
-
 func convertGoType(r *Runtime, t reflect.Type) value {
 	ctx := r.index().newContext()
 	// TODO: this can be much more efficient.
-	mutex.Lock()
-	defer mutex.Unlock()
+	ctx.mutex.Lock()
+	defer ctx.mutex.Unlock()
 	return goTypeToValue(ctx, true, t)
 }
 
@@ -405,7 +399,7 @@
 // TODO: if this value will always be unified with a concrete type in Go, then
 // many of the fields may be omitted.
 func goTypeToValue(ctx *context, allowNullDefault bool, t reflect.Type) (e value) {
-	if e, ok := typeCache.Load(t); ok {
+	if e, ok := ctx.typeCache.Load(t); ok {
 		return e.(value)
 	}
 
@@ -477,7 +471,7 @@
 		// resolve field tags to allow field tags to refer to the struct fields.
 		tags := map[label]string{}
 		obj := newStruct(baseValue{})
-		typeCache.Store(t, obj)
+		ctx.typeCache.Store(t, obj)
 
 		for i := 0; i < t.NumField(); i++ {
 			f := t.Field(i)
@@ -559,7 +553,7 @@
 store:
 	// TODO: store error if not nil?
 	if e != nil {
-		typeCache.Store(t, e)
+		ctx.typeCache.Store(t, e)
 	}
 	return e
 }