1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v: support an optional fn cleanup() { in each module, to complement the existing optional fn init() { (#17119)

This commit is contained in:
MatejMagat305 2023-01-27 10:31:03 +01:00 committed by GitHub
parent a9a04bba55
commit fe157db0ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5330,6 +5330,8 @@ fn (mut g Gen) write_init_function() {
}
}
mut cleaning_up_array := []string{cap: g.table.modules.len}
for mod_name in g.table.modules {
if g.has_reflection && mod_name == 'v.reflection' {
// ignore v.reflection already initialized above
@ -5359,6 +5361,15 @@ fn (mut g Gen) write_init_function() {
g.writeln('\t${init_fn_c_name}();')
}
}
cleanup_fn_name := '${mod_name}.cleanup'
if cleanupfn := g.table.find_fn(cleanup_fn_name) {
if cleanupfn.return_type == ast.void_type && cleanupfn.params.len == 0 {
mod_c_name := util.no_dots(mod_name)
cleanup_fn_c_name := '${mod_c_name}__cleanup'
cleaning_up_array << '\t${cleanup_fn_c_name}();'
cleaning_up_array << '\t// Cleaning up for module ${mod_name}'
}
}
}
g.writeln('}')
@ -5380,6 +5391,9 @@ fn (mut g Gen) write_init_function() {
}
g.writeln('\tarray_free(&as_cast_type_indexes);')
}
for x in cleaning_up_array.reverse() {
g.writeln(x)
}
g.writeln('}')
if g.pref.printfn_list.len > 0 && '_vcleanup' in g.pref.printfn_list {
println(g.out.after(fn_vcleanup_start_pos))