@@ -19,6 +19,7 @@ package jsonnet
19
19
import (
20
20
"errors"
21
21
"fmt"
22
+ "io"
22
23
"os"
23
24
"path/filepath"
24
25
"runtime/debug"
@@ -44,6 +45,7 @@ type VM struct {
44
45
ErrorFormatter ErrorFormatter
45
46
StringOutput bool
46
47
importCache * importCache
48
+ traceOut io.Writer
47
49
}
48
50
49
51
// extKind indicates the kind of external variable that is being initialized for the VM
@@ -78,6 +80,7 @@ func MakeVM() *VM {
78
80
ErrorFormatter : & termErrorFormatter {pretty : false , maxStackTraceSize : 20 },
79
81
importer : & FileImporter {},
80
82
importCache : makeImportCache (defaultImporter ),
83
+ traceOut : os .Stderr ,
81
84
}
82
85
}
83
86
@@ -93,6 +96,11 @@ func (vm *VM) flushValueCache() {
93
96
vm .importCache .flushValueCache ()
94
97
}
95
98
99
+ // SetTraceOut sets the output stream for the builtin function std.trace().
100
+ func (vm * VM ) SetTraceOut (traceOut io.Writer ) {
101
+ vm .traceOut = traceOut
102
+ }
103
+
96
104
// ExtVar binds a Jsonnet external var to the given value.
97
105
func (vm * VM ) ExtVar (key string , val string ) {
98
106
vm .ext [key ] = vmExt {value : val , kind : extKindVar }
@@ -174,7 +182,7 @@ func (vm *VM) Evaluate(node ast.Node) (val string, err error) {
174
182
err = fmt .Errorf ("(CRASH) %v\n %s" , r , debug .Stack ())
175
183
}
176
184
}()
177
- return evaluate (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm .StringOutput )
185
+ return evaluate (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm .traceOut , vm . StringOutput )
178
186
}
179
187
180
188
// EvaluateStream evaluates a Jsonnet program given by an Abstract Syntax Tree
@@ -185,7 +193,7 @@ func (vm *VM) EvaluateStream(node ast.Node) (output []string, err error) {
185
193
err = fmt .Errorf ("(CRASH) %v\n %s" , r , debug .Stack ())
186
194
}
187
195
}()
188
- return evaluateStream (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache )
196
+ return evaluateStream (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm . traceOut )
189
197
}
190
198
191
199
// EvaluateMulti evaluates a Jsonnet program given by an Abstract Syntax Tree
@@ -197,7 +205,7 @@ func (vm *VM) EvaluateMulti(node ast.Node) (output map[string]string, err error)
197
205
err = fmt .Errorf ("(CRASH) %v\n %s" , r , debug .Stack ())
198
206
}
199
207
}()
200
- return evaluateMulti (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm .StringOutput )
208
+ return evaluateMulti (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm .traceOut , vm . StringOutput )
201
209
}
202
210
203
211
func (vm * VM ) evaluateSnippet (diagnosticFileName ast.DiagnosticFileName , filename string , snippet string , kind evalKind ) (output interface {}, err error ) {
@@ -212,11 +220,11 @@ func (vm *VM) evaluateSnippet(diagnosticFileName ast.DiagnosticFileName, filenam
212
220
}
213
221
switch kind {
214
222
case evalKindRegular :
215
- output , err = evaluate (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm .StringOutput )
223
+ output , err = evaluate (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm .traceOut , vm . StringOutput )
216
224
case evalKindMulti :
217
- output , err = evaluateMulti (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm .StringOutput )
225
+ output , err = evaluateMulti (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm .traceOut , vm . StringOutput )
218
226
case evalKindStream :
219
- output , err = evaluateStream (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache )
227
+ output , err = evaluateStream (node , vm .ext , vm .tla , vm .nativeFuncs , vm .MaxStack , vm .importCache , vm . traceOut )
220
228
}
221
229
if err != nil {
222
230
return "" , err
0 commit comments