cgen: fix parallel cgen for json encoding of struct fields that have default values

master
Delyan Angelov 2022-05-26 16:55:44 +03:00
parent 8c969efe6b
commit bb6ef8bba8
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 29 additions and 1 deletions

View File

@ -283,6 +283,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string {
global_g.init()
global_g.timers.show('cgen init')
global_g.tests_inited = false
global_g.file = files.last()
if !pref.no_parallel {
mut pp := pool.new_pool_processor(callback: cgen_process_one_file_cb)
pp.set_shared_context(global_g) // TODO: make global_g shared
@ -2886,7 +2887,7 @@ fn (mut g Gen) expr(node_ ast.Expr) {
g.writeln('($shared_styp*)__dup${shared_styp}(&($shared_styp){.mtx = {0}, .val =')
}
}
last_stmt_pos := g.stmt_path_pos.last()
last_stmt_pos := if g.stmt_path_pos.len > 0 { g.stmt_path_pos.last() } else { 0 }
g.call_expr(node)
// if g.fileis('1.strings') {
// println('before:' + node.autofree_pregen)

View File

@ -0,0 +1,27 @@
import json
struct Window {
pub mut:
width f64
height f64
}
fn make_default_window_settings() Window {
return Window{
width: 1280
height: 720
}
}
struct Settings {
pub mut:
window Window = make_default_window_settings()
}
fn test_encoding_works() {
mut settings := Settings{}
dump(settings)
encoded := json.encode(settings)
println(encoded)
assert encoded == '{"window":{"width":1280,"height":720}}'
}