1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package cue
16
17 import (
18 "cuelang.org/go/cue/ast"
19 "cuelang.org/go/cue/ast/astutil"
20 "cuelang.org/go/cue/build"
21 "cuelang.org/go/cue/errors"
22 "cuelang.org/go/internal/core/adt"
23 "cuelang.org/go/internal/core/runtime"
24 )
25
26
27
28
29
30
31
32
33
34
35 type Runtime runtime.Runtime
36
37 func (r *Runtime) runtime() *runtime.Runtime {
38 rt := (*runtime.Runtime)(r)
39 rt.Init()
40 return rt
41 }
42
43 type hiddenRuntime = Runtime
44
45 func (r *Runtime) complete(p *build.Instance, v *adt.Vertex) (*Instance, error) {
46 idx := r.runtime()
47 inst := getImportFromBuild(idx, p, v)
48 inst.ImportPath = p.ImportPath
49 if inst.Err != nil {
50 return nil, inst.Err
51 }
52 return inst, nil
53 }
54
55
56
57
58
59
60
61 func (r *hiddenRuntime) Compile(filename string, source interface{}) (*Instance, error) {
62 cfg := &runtime.Config{Filename: filename}
63 v, p := r.runtime().Compile(cfg, source)
64 return r.complete(p, v)
65 }
66
67
68
69
70
71 func (r *hiddenRuntime) CompileFile(file *ast.File) (*Instance, error) {
72 v, p := r.runtime().CompileFile(nil, file)
73 return r.complete(p, v)
74 }
75
76
77
78
79
80
81 func (r *hiddenRuntime) CompileExpr(expr ast.Expr) (*Instance, error) {
82 f, err := astutil.ToFile(expr)
83 if err != nil {
84 return nil, err
85 }
86 runtime := r.runtime()
87 v := (*Context)(runtime).BuildExpr(expr)
88 err = v.Err()
89 inst := &Instance{
90 index: runtime,
91 root: v.v,
92 inst: &build.Instance{
93 Files: []*ast.File{f},
94 },
95 Err: errors.Promote(err, ""),
96 Incomplete: err != nil,
97 }
98 return inst, err
99 }
100
101
102
103
104
105
106
107 func (r *hiddenRuntime) Parse(name string, source interface{}) (*Instance, error) {
108 return r.Compile(name, source)
109 }
110
111
112
113
114
115 func (r *hiddenRuntime) Build(p *build.Instance) (*Instance, error) {
116 v, _ := r.runtime().Build(nil, p)
117 return r.complete(p, v)
118 }
119
120
121 func Build(instances []*build.Instance) []*Instance {
122 if len(instances) == 0 {
123 panic("cue: list of instances must not be empty")
124 }
125 var r Runtime
126 a, _ := r.build(instances)
127 return a
128 }
129
130 func (r *hiddenRuntime) build(instances []*build.Instance) ([]*Instance, error) {
131 index := r.runtime()
132
133 loaded := []*Instance{}
134
135 var errs errors.Error
136
137 for _, p := range instances {
138 v, _ := index.Build(nil, p)
139 i := getImportFromBuild(index, p, v)
140 errs = errors.Append(errs, i.Err)
141 loaded = append(loaded, i)
142 }
143
144
145 return loaded, errs
146 }
147
148
149
150
151
152 func (r *hiddenRuntime) FromExpr(expr ast.Expr) (*Instance, error) {
153 return r.CompileFile(&ast.File{
154 Decls: []ast.Decl{&ast.EmbedDecl{Expr: expr}},
155 })
156 }
157
View as plain text