Skip to content

Commit aa3a8c5

Browse files
committed
v.ast: add a customizable ast.Table .panic() method
1 parent 7335258 commit aa3a8c5

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

vlib/v/ast/table.v

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ pub mut:
2222
is_fmt bool
2323
used_fns map[string]bool // filled in by the checker, when pref.skip_unused = true;
2424
used_consts map[string]bool // filled in by the checker, when pref.skip_unused = true;
25+
panic_handler FnPanicHandler = default_table_panic_handler
26+
panic_userdata voidptr = voidptr(0) // can be used to pass arbitrary data to panic_handler;
27+
panic_npanics int
2528
}
2629

2730
[unsafe]
@@ -42,6 +45,18 @@ pub fn (t &Table) free() {
4245
}
4346
}
4447

48+
pub type FnPanicHandler = fn (&Table, string)
49+
50+
fn default_table_panic_handler(t &Table, message string) {
51+
panic(message)
52+
}
53+
54+
pub fn (t &Table) panic(message string) {
55+
mut mt := unsafe { &Table(t) }
56+
mt.panic_npanics++
57+
t.panic_handler(t, message)
58+
}
59+
4560
pub struct Fn {
4661
pub:
4762
params []Param
@@ -209,7 +224,7 @@ pub fn (mut t TypeSymbol) register_method(new_fn Fn) int {
209224

210225
pub fn (t &Table) register_aggregate_method(mut sym TypeSymbol, name string) ?Fn {
211226
if sym.kind != .aggregate {
212-
panic('Unexpected type symbol: $sym.kind')
227+
t.panic('Unexpected type symbol: $sym.kind')
213228
}
214229
agg_info := sym.info as Aggregate
215230
// an aggregate always has at least 2 types
@@ -263,7 +278,7 @@ pub fn (t &Table) type_find_method(s &TypeSymbol, name string) ?Fn {
263278

264279
fn (t &Table) register_aggregate_field(mut sym TypeSymbol, name string) ?StructField {
265280
if sym.kind != .aggregate {
266-
panic('Unexpected type symbol: $sym.kind')
281+
t.panic('Unexpected type symbol: $sym.kind')
267282
}
268283
mut agg_info := sym.info as Aggregate
269284
// an aggregate always has at least 2 types
@@ -424,7 +439,7 @@ pub fn (t &Table) get_type_symbol(typ Type) &TypeSymbol {
424439
return unsafe { &t.type_symbols[idx] }
425440
}
426441
// this should never happen
427-
panic('get_type_symbol: invalid type (typ=$typ idx=$idx). Compiler bug. This should never happen. Please create a GitHub issue.
442+
t.panic('get_type_symbol: invalid type (typ=$typ idx=$idx). Compiler bug. This should never happen. Please create a GitHub issue.
428443
')
429444
}
430445

@@ -441,7 +456,7 @@ pub fn (t &Table) get_final_type_symbol(typ Type) &TypeSymbol {
441456
return unsafe { &t.type_symbols[idx] }
442457
}
443458
// this should never happen
444-
panic('get_final_type_symbol: invalid type (typ=$typ idx=$idx). Compiler bug. This should never happen. Please create a GitHub issue.')
459+
t.panic('get_final_type_symbol: invalid type (typ=$typ idx=$idx). Compiler bug. This should never happen. Please create a GitHub issue.')
445460
}
446461

447462
[inline]
@@ -956,7 +971,7 @@ pub fn (mut t Table) bitsize_to_type(bit_size int) Type {
956971
}
957972
else {
958973
if bit_size % 8 != 0 { // there is no way to do `i2131(32)` so this should never be reached
959-
panic('compiler bug: bitsizes must be multiples of 8')
974+
t.panic('compiler bug: bitsizes must be multiples of 8')
960975
}
961976
return new_type(t.find_or_register_array_fixed(byte_type, bit_size / 8))
962977
}

0 commit comments

Comments
 (0)