@@ -381,8 +381,8 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
381381 if fv_sym.kind == .struct && ! is_option && field_op != '->' {
382382 dec.writeln ('\t if (root->type == cJSON_NULL) { ' )
383383 dec.writeln ('\t\t struct ${first_variant_name} empty = {0};' )
384- dec.writeln ('res = ${variant_typ} _to_sumtype_${ret_styp} (&empty); } \n else ' )
385- // dec.writeln('res = ${variant_typ}_to_sumtype_${sym.cname}(&empty); } \n else ')
384+ dec.writeln ('res = ${variant_typ} _to_sumtype_${ret_styp} (&empty, false ); } \n else ' )
385+ // dec.writeln('res = ${variant_typ}_to_sumtype_${sym.cname}(&empty, false ); } \n else ')
386386 }
387387 //
388388 dec.writeln ('if (cJSON_IsObject(root) || (cJSON_IsArray(root) && cJSON_IsObject(root->child))) {' )
@@ -409,7 +409,7 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
409409
410410 // Helpers for decoding
411411 g.get_sumtype_casting_fn (variant, typ)
412- g.definitions.writeln ('static inline ${sym.cname} ${variant_typ} _to_sumtype_${sym.cname} (${variant_typ} * x);' )
412+ g.definitions.writeln ('static inline ${sym.cname} ${variant_typ} _to_sumtype_${sym.cname} (${variant_typ} * x, bool is_mut );' )
413413
414414 // ENCODING
415415 enc.writeln ('\t if (${var_data}${field_op} _typ == ${int(variant.idx())} ) {' )
@@ -485,9 +485,9 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
485485 dec.writeln ('\t\t ${variant_typ} value = *(${variant_typ} *)(${tmp} .data);' )
486486 }
487487 if is_option {
488- dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${variant_typ} _to_sumtype_${sym.cname} (&value) }, (${option_name} *)&res, sizeof(${sym.cname} ));' )
488+ dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${variant_typ} _to_sumtype_${sym.cname} (&value, false ) }, (${option_name} *)&res, sizeof(${sym.cname} ));' )
489489 } else {
490- dec.writeln ('\t\t res = ${variant_typ} _to_sumtype_${ret_styp} (&value);' )
490+ dec.writeln ('\t\t res = ${variant_typ} _to_sumtype_${ret_styp} (&value, false );' )
491491 }
492492 dec.writeln ('\t }' )
493493 } $else {
@@ -498,10 +498,10 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
498498 if utyp.has_flag (.option) {
499499 dec.writeln ('\t\t\t\t ${prefix} res.state = 0;' )
500500 tmp_time_var := g.new_tmp_var ()
501- dec.writeln ('\t\t\t\t ${g.base_type(utyp)} ${tmp_time_var} = ${variant_typ} _to_sumtype_${sym.cname} (&${tmp} );' )
501+ dec.writeln ('\t\t\t\t ${g.base_type(utyp)} ${tmp_time_var} = ${variant_typ} _to_sumtype_${sym.cname} (&${tmp} , false );' )
502502 dec.writeln ('\t\t\t\t vmemcpy(&${prefix} res.data, ${tmp_time_var} ._time__Time, sizeof(${variant_typ} ));' )
503503 } else {
504- dec.writeln ('\t\t\t\t ${prefix} res = ${variant_typ} _to_sumtype_${sym.cname} (&${tmp} );' )
504+ dec.writeln ('\t\t\t\t ${prefix} res = ${variant_typ} _to_sumtype_${sym.cname} (&${tmp} , false );' )
505505 }
506506 dec.writeln ('\t\t\t }' )
507507 } else if ! is_js_prim (variant_typ) && variant_sym.kind != .enum {
@@ -512,9 +512,9 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
512512 dec.writeln ('\t\t\t\t\t return (${result_name} _${ret_styp} ){ .is_error = true, .err = ${tmp} .err, .data = {0} };' )
513513 dec.writeln ('\t\t\t\t }' )
514514 if is_option {
515- dec.writeln ('\t\t\t\t _option_ok(&(${sym.cname} []){ ${variant_typ} _to_sumtype_${sym.cname} ((${variant_typ} *)${tmp} .data) }, (${option_name} *)&res, sizeof(${sym.cname} ));' )
515+ dec.writeln ('\t\t\t\t _option_ok(&(${sym.cname} []){ ${variant_typ} _to_sumtype_${sym.cname} ((${variant_typ} *)${tmp} .data, false ) }, (${option_name} *)&res, sizeof(${sym.cname} ));' )
516516 } else {
517- dec.writeln ('\t\t\t\t ${prefix} res = ${variant_typ} _to_sumtype_${sym.cname} ((${variant_typ} *)${tmp} .data);' )
517+ dec.writeln ('\t\t\t\t ${prefix} res = ${variant_typ} _to_sumtype_${sym.cname} ((${variant_typ} *)${tmp} .data, false );' )
518518 }
519519 dec.writeln ('\t\t\t }' )
520520 }
@@ -536,7 +536,7 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
536536 var_t := 'bool'
537537 dec.writeln ('\t\t if (cJSON_IsBool(root)) {' )
538538 dec.writeln ('\t\t\t ${var_t} value = ${js_dec_name(var_t)} (root);' )
539- dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} (&value);' )
539+ dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} (&value, false );' )
540540 dec.writeln ('\t\t }' )
541541 }
542542
@@ -552,9 +552,9 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
552552 dec.writeln ('\t\t if (cJSON_IsNumber(root)) {' )
553553 dec.writeln ('\t\t\t ${var_t} value = ${js_dec_name('u64')} (root);' )
554554 if utyp.has_flag (.option) {
555- dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${var_t} _to_sumtype_${sym.cname} (&value) }, (${option_name} *)&${prefix} res, sizeof(${sym.cname} ));' )
555+ dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${var_t} _to_sumtype_${sym.cname} (&value, false ) }, (${option_name} *)&${prefix} res, sizeof(${sym.cname} ));' )
556556 } else {
557- dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} (&value);' )
557+ dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} (&value, false );' )
558558 }
559559 dec.writeln ('\t\t }' )
560560 }
@@ -568,9 +568,9 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
568568 dec.writeln ('\t\t if (cJSON_IsString(root)) {' )
569569 dec.writeln ('\t\t\t ${var_t} value = ${js_dec_name(var_t)} (root);' )
570570 if utyp.has_flag (.option) {
571- dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${var_t} _to_sumtype_${sym.cname} (&value) }, (${option_name} *)&${prefix} res, sizeof(${sym.cname} ));' )
571+ dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${var_t} _to_sumtype_${sym.cname} (&value, false ) }, (${option_name} *)&${prefix} res, sizeof(${sym.cname} ));' )
572572 } else {
573- dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} (&value);' )
573+ dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} (&value, false );' )
574574 }
575575 dec.writeln ('\t\t }' )
576576 }
@@ -592,9 +592,9 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
592592 dec.writeln ('\t\t\t\t return (${result_name} _${ret_styp} ){ .is_error = true, .err = ${tmp} .err, .data = {0} };' )
593593 dec.writeln ('\t\t\t }' )
594594 if utyp.has_flag (.option) {
595- dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${var_t} _to_sumtype_${sym.cname} ((${var_t} *)${tmp} .data) }, (${option_name} *)&${prefix} res, sizeof(${sym.cname} ));' )
595+ dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${var_t} _to_sumtype_${sym.cname} ((${var_t} *)${tmp} .data, false ) }, (${option_name} *)&${prefix} res, sizeof(${sym.cname} ));' )
596596 } else {
597- dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} ((${var_t} *)${tmp} .data);' )
597+ dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} ((${var_t} *)${tmp} .data, false );' )
598598 }
599599 dec.writeln ('\t\t }' )
600600 }
@@ -611,9 +611,9 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
611611 dec.writeln ('\t\t if (cJSON_IsNumber(root)) {' )
612612 dec.writeln ('\t\t\t ${var_t} value = ${js_dec_name(var_t)} (root);' )
613613 if utyp.has_flag (.option) {
614- dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${var_t} _to_sumtype_${sym.cname} (&value) }, (${option_name} *)&${prefix} res, sizeof(${sym.cname} ));' )
614+ dec.writeln ('\t\t\t _option_ok(&(${sym.cname} []){ ${var_t} _to_sumtype_${sym.cname} (&value, false ) }, (${option_name} *)&${prefix} res, sizeof(${sym.cname} ));' )
615615 } else {
616- dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} (&value);' )
616+ dec.writeln ('\t\t\t ${prefix} res = ${var_t} _to_sumtype_${sym.cname} (&value, false );' )
617617 }
618618 dec.writeln ('\t\t }' )
619619 }
0 commit comments