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

Delyan Angelov 2022-05-26 16:55:44 +03:00 committed by Chewing_Bever
parent 7541d84038
commit c4783628e6
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
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.init()
global_g.timers.show('cgen init') global_g.timers.show('cgen init')
global_g.tests_inited = false global_g.tests_inited = false
global_g.file = files.last()
if !pref.no_parallel { if !pref.no_parallel {
mut pp := pool.new_pool_processor(callback: cgen_process_one_file_cb) mut pp := pool.new_pool_processor(callback: cgen_process_one_file_cb)
pp.set_shared_context(global_g) // TODO: make global_g shared 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 =') 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) g.call_expr(node)
// if g.fileis('1.strings') { // if g.fileis('1.strings') {
// println('before:' + node.autofree_pregen) // 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}}'
}