mirror of
https://github.com/google/go-jsonnet.git
synced 2025-08-07 23:07:14 +02:00
Support for stack trace size limit
This commit is contained in:
parent
02a4eed22d
commit
a4058fc177
@ -85,23 +85,36 @@ func (ef *ErrorFormatter) showCode(buf *bytes.Buffer, loc ast.LocationRange) {
|
|||||||
fmt.Fprintf(buf, "\n")
|
fmt.Fprintf(buf, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ef *ErrorFormatter) frame(frame *TraceFrame, buf *bytes.Buffer) {
|
||||||
|
// TODO(sbarzowski) tabs are probably a bad idea
|
||||||
|
fmt.Fprintf(buf, "\t%v\t%v\n", frame.Loc.String(), frame.Name)
|
||||||
|
if ef.pretty {
|
||||||
|
ef.showCode(buf, frame.Loc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (ef *ErrorFormatter) buildStackTrace(frames []TraceFrame) string {
|
func (ef *ErrorFormatter) buildStackTrace(frames []TraceFrame) string {
|
||||||
// https://github.com/google/jsonnet/blob/master/core/libjsonnet.cpp#L594
|
// https://github.com/google/jsonnet/blob/master/core/libjsonnet.cpp#L594
|
||||||
|
maxAbove := ef.MaxStackTraceSize / 2
|
||||||
|
maxBelow := ef.MaxStackTraceSize - maxAbove
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
for i := len(frames) - 1; i >= 0; i-- {
|
sz := len(frames)
|
||||||
f := frames[i]
|
for i := 0; i < sz; i++ {
|
||||||
// TODO(sbarzowski) make pretty format more readable (it's already useful)
|
// TODO(sbarzowski) make pretty format more readable (it's already useful)
|
||||||
if ef.pretty {
|
if ef.pretty {
|
||||||
fmt.Fprintf(&buf, "-------------------------------------------------\n")
|
fmt.Fprintf(&buf, "-------------------------------------------------\n")
|
||||||
}
|
}
|
||||||
// TODO(sbarzowski) tabs are probably a bad idea
|
if i >= maxAbove && i < sz-maxBelow {
|
||||||
fmt.Fprintf(&buf, "\t%v\t%v\n", &f.Loc, f.Name)
|
if ef.pretty {
|
||||||
if ef.pretty {
|
fmt.Fprintf(&buf, "\t... (skipped %v frames)\n", sz-maxAbove-maxBelow)
|
||||||
ef.showCode(&buf, f.Loc)
|
} else {
|
||||||
}
|
buf.WriteString("\t...\n")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO(sbarzowski) handle max stack trace size
|
i = sz - maxBelow - 1
|
||||||
// TODO(sbarzowski) I think the order of frames is reversed
|
} else {
|
||||||
|
ef.frame(&frames[sz-i-1], &buf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ func TestMain(t *testing.T) {
|
|||||||
}
|
}
|
||||||
mainTests = append(mainTests, mainTest{name: name, input: input, golden: golden})
|
mainTests = append(mainTests, mainTest{name: name, input: input, golden: golden})
|
||||||
}
|
}
|
||||||
errFormatter := ErrorFormatter{pretty: true}
|
errFormatter := ErrorFormatter{pretty: true, MaxStackTraceSize: 9}
|
||||||
for _, test := range mainTests {
|
for _, test := range mainTests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
vm := MakeVM()
|
vm := MakeVM()
|
||||||
@ -207,7 +207,7 @@ var minimalErrorTests = []errorFormattingTest{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMinimalError(t *testing.T) {
|
func TestMinimalError(t *testing.T) {
|
||||||
formatter := ErrorFormatter{}
|
formatter := ErrorFormatter{MaxStackTraceSize: 20}
|
||||||
genericTestErrorMessage(t, minimalErrorTests, func(r RuntimeError) string {
|
genericTestErrorMessage(t, minimalErrorTests, func(r RuntimeError) string {
|
||||||
return formatter.format(r)
|
return formatter.format(r)
|
||||||
})
|
})
|
||||||
|
103
testdata/percent_format_str5.golden
vendored
103
testdata/percent_format_str5.golden
vendored
@ -18,108 +18,7 @@ RUNTIME ERROR: Not enough values to format, got 1
|
|||||||
<builtin> builtin function <toString>
|
<builtin> builtin function <toString>
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
<std>:557:29-74 thunk <s> from <function <format_codes_arr>>
|
... (skipped 21 frames)
|
||||||
|
|
||||||
format_code(val, code, tmp.fw, tmp2.prec, j2);
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:562:38-39 thunk from <thunk <s_padded> from <function <format_codes_arr>>>
|
|
||||||
|
|
||||||
pad_left(s, tmp.fw, " ");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:36-39 thunk from <thunk from <function <pad_left>>>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<builtin> builtin function <length>
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:21-40 thunk from <function <pad_left>>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:365:17-18 thunk from <function <padding>>
|
|
||||||
|
|
||||||
aux(w, "");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:361:20-21 function <aux>
|
|
||||||
|
|
||||||
if w <= 0 then
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:361:20-26 function <aux>
|
|
||||||
|
|
||||||
if w <= 0 then
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:365:13-23 function <padding>
|
|
||||||
|
|
||||||
aux(w, "");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:13-44 function <pad_left>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:13-50 function <pad_left>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:562:29-53 thunk <s_padded> from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
pad_left(s, tmp.fw, " ");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:65-73 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:61-73 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:60-61 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:60-68 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:521:21-22 function <format_codes_arr>
|
|
||||||
|
|
||||||
v
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:21-69 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:21-74 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:21-69 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:21-74 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
<std>:525:21-69 function <format_codes_arr>
|
<std>:525:21-69 function <format_codes_arr>
|
||||||
|
|
||||||
|
103
testdata/percent_format_str6.golden
vendored
103
testdata/percent_format_str6.golden
vendored
@ -18,108 +18,7 @@ RUNTIME ERROR: Not enough values to format, got 1
|
|||||||
<builtin> builtin function <toString>
|
<builtin> builtin function <toString>
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
<std>:557:29-74 thunk <s> from <function <format_codes_arr>>
|
... (skipped 21 frames)
|
||||||
|
|
||||||
format_code(val, code, tmp.fw, tmp2.prec, j2);
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:562:38-39 thunk from <thunk <s_padded> from <function <format_codes_arr>>>
|
|
||||||
|
|
||||||
pad_left(s, tmp.fw, " ");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:36-39 thunk from <thunk from <function <pad_left>>>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<builtin> builtin function <length>
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:21-40 thunk from <function <pad_left>>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:365:17-18 thunk from <function <padding>>
|
|
||||||
|
|
||||||
aux(w, "");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:361:20-21 function <aux>
|
|
||||||
|
|
||||||
if w <= 0 then
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:361:20-26 function <aux>
|
|
||||||
|
|
||||||
if w <= 0 then
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:365:13-23 function <padding>
|
|
||||||
|
|
||||||
aux(w, "");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:13-44 function <pad_left>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:13-50 function <pad_left>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:562:29-53 thunk <s_padded> from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
pad_left(s, tmp.fw, " ");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:65-73 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:61-73 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:60-61 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:60-68 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:521:21-22 function <format_codes_arr>
|
|
||||||
|
|
||||||
v
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:21-69 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:21-74 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:21-69 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:21-74 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
<std>:525:21-69 function <format_codes_arr>
|
<std>:525:21-69 function <format_codes_arr>
|
||||||
|
|
||||||
|
78
testdata/percent_format_str7.golden
vendored
78
testdata/percent_format_str7.golden
vendored
@ -21,83 +21,7 @@ RUNTIME ERROR: Format required number at 0, got string
|
|||||||
padding(w - std.length(str), s) + str;
|
padding(w - std.length(str), s) + str;
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
<builtin> builtin function <length>
|
... (skipped 16 frames)
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:21-40 thunk from <function <pad_left>>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:365:17-18 thunk from <function <padding>>
|
|
||||||
|
|
||||||
aux(w, "");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:361:20-21 function <aux>
|
|
||||||
|
|
||||||
if w <= 0 then
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:361:20-26 function <aux>
|
|
||||||
|
|
||||||
if w <= 0 then
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:365:13-23 function <padding>
|
|
||||||
|
|
||||||
aux(w, "");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:13-44 function <pad_left>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:369:13-50 function <pad_left>
|
|
||||||
|
|
||||||
padding(w - std.length(str), s) + str;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:562:29-53 thunk <s_padded> from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
pad_left(s, tmp.fw, " ");
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:65-73 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:61-73 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:60-61 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:60-68 thunk from <function <format_codes_arr>>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:521:21-22 function <format_codes_arr>
|
|
||||||
|
|
||||||
v
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:525:21-69 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
<std>:568:21-74 function <format_codes_arr>
|
|
||||||
|
|
||||||
format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
|
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
<std>:525:21-69 function <format_codes_arr>
|
<std>:525:21-69 function <format_codes_arr>
|
||||||
|
|
||||||
|
5
testdata/recursive_thunk.golden
vendored
5
testdata/recursive_thunk.golden
vendored
@ -20,10 +20,7 @@ local foo(x) = bar(foo(x - 1), x - 1);
|
|||||||
local bar(th, x) = if x == 0 then error "xxx" else th;
|
local bar(th, x) = if x == 0 then error "xxx" else th;
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
testdata/recursive_thunk:2:16-38 function <foo>
|
... (skipped 1 frames)
|
||||||
|
|
||||||
local foo(x) = bar(foo(x - 1), x - 1);
|
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
testdata/recursive_thunk:2:20-30 thunk from <function <foo>>
|
testdata/recursive_thunk:2:20-30 thunk from <function <foo>>
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
3
vm.go
3
vm.go
@ -53,9 +53,8 @@ type vmExtMap map[string]vmExt
|
|||||||
func MakeVM() *VM {
|
func MakeVM() *VM {
|
||||||
return &VM{
|
return &VM{
|
||||||
MaxStack: 500,
|
MaxStack: 500,
|
||||||
MaxTrace: 20,
|
|
||||||
ext: make(vmExtMap),
|
ext: make(vmExtMap),
|
||||||
ef: ErrorFormatter{pretty: true, colorful: true},
|
ef: ErrorFormatter{pretty: true, colorful: true, MaxStackTraceSize: 20},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user